cancel
Showing results for 
Search instead for 
Did you mean: 

Flexible UI Command with Onestep Screenflow - How to read user inputs

Former Member
0 Kudos

HI All,

I tried to implement the one step screenflow as given in SDN example but with user inputs (like say inputfield).

	public IRenderingEvent execute(IScreenflowData sfd) throws WcmException {		
		
			
		InputField inputField = new InputField("if");
		inputField.setLabel(new Label("Enter:"));
		
		ConfirmComponent cc = new ConfirmComponent(
			ConfirmComponent.OK_CANCEL, 
			this.context.getLocale(),
			inputField
		);
		
		String sRid = (String)this.values.get(0);
		RID rid = RID.getRID(sRid, null);
		
		OneStepScreenflow oscf = new OneStepScreenflow(
			sfd,
			this.getAlias(),
			rid,
			cc
		);
		
		return oscf.execute();
	}

But I was unsuccessful in retrieving user entered values back in "execute(IResource res, Event event)" method.

	public IRenderingEvent execute(IResource res, Event event) throws WcmException {		
				
		EventModifierData data;	
		
		String componentName = event.getComponentName();
		Component component;
		InputField inputField;
		
		if (event instanceof ConfirmEvent) {
			ConfirmEvent cce = (ConfirmEvent)event;
			
			if (ConfirmEvent.CHOICE_YES.equals(cce.getChoice())) {
				// do the action here ...
				inputField = (InputField)this.proxy.getControlContext().getDynPage().getComponentByName("if");
				//dynPage.
				//getComponentByName("if");
				
				if(inputField != null){
					//inputField = (InputField)component;
					String value = inputField.getValue().toString();//(String)inputField.getValue();
					return new InfoEvent(Status.OK, "Hurray..."+value);
					
				} else {
					return new InfoEvent(Status.OK, "Whatever you want to retrieve is null....");	
				}
				
				
			}	else if (ConfirmEvent.CHOICE_NO.equals(cce.getChoice())) {
				return ConfirmComponent.onNo(event, res.getContext().getLocale());
			} else if (ConfirmEvent.CHOICE_CANCEL.equals(cce.getChoice())) {
				return ConfirmComponent.onCancel(event, res.getContext().getLocale());
			}	
		}
		return new InfoEvent(Status.ABORT, "Aborted.");
	} 

I am getting NullPointer exception as the it's not able to get the dynpage from this code.

My questions is: How to read user entered input values in one step screenflow like this. Any help on this would be much appreciated. Thanks a lot for your help.

Pavan Keely

Accepted Solutions (1)

Accepted Solutions (1)

former_member388485
Contributor
0 Kudos

Hi Pavan,

Create a class that extends OneStepComponent:



public final class yourcompanyConfirmInputComponent extends OneStepComponent
{

	public yourcompanyConfirmInputComponent()
	{
		mode = 0;
		locale = null;
		rendererID = null;
		actiontarget = null;
		comp = null;
		title = null;
		design = GroupDesign.SECONDARYBOXCOLOR;
	}

	public yourcompanyConfirmInputComponent(int mode, Locale locale, Component title, Component body, GroupDesign design)
	{
		this.mode = 0;
		this.locale = null;
		rendererID = null;
		actiontarget = null;
		comp = null;
		this.title = null;
		this.design = GroupDesign.SECONDARYBOXCOLOR;
		if(locale == null || body == null)
			throw new NullPointerException("The parameter locale <" + locale + "> and body <" + body + "> must not be <null>");
		this.title = title;
		this.mode = mode;
		this.locale = locale;
		setId(IDCreator.currentID());
		comp = body;
		if(design != null)
			this.design = design;
	}

	public yourcompanyConfirmInputComponent(int mode, Locale locale, Component comp)
	{
		this.mode = 0;
		this.locale = null;
		rendererID = null;
		actiontarget = null;
		this.comp = null;
		title = null;
		design = GroupDesign.SECONDARYBOXCOLOR;
		if(locale == null || comp == null)
		{
			throw new NullPointerException("The parameter locale <" + locale + "> and comp <" + comp + "> must not be <null>");
		} else
		{
			this.mode = mode;
			this.locale = locale;
			setId(IDCreator.currentID());
			this.comp = comp;
			return;
		}
	}

	protected Component buildContent()
	{
		getCompositeModel().setProperty("rID", rendererID);
		getCompositeModel().setProperty("acttrg", actiontarget);
		GridLayout result = new GridLayout();
		result.setCellPadding(4);
		int row = 1;
		if(comp != null)
			result.addComponent(row, 1, comp);
		input = new InputField("inf");
		result.addComponent(row++, 2, input);
		GridLayout buttons = new GridLayout();
		buttons.setCellPadding(4);
		if(mode == 1)
		{
			buttons.addComponent(row, 1, renderButton("xbut_Yes", "2"));
			buttons.addComponent(row, 2, renderButton("xbut_No", "1"));
		} else
		if(mode == 0)
		{
			buttons.addComponent(row, 1, renderButton("xbut_Yes", "2"));
			buttons.addComponent(row, 2, renderButton("xbut_No", "1"));
			buttons.addComponent(row, 2, renderButton("xbut_Cancel", "0"));
		} else
		if(mode == 2)
		{
			buttons.addComponent(row, 1, renderButton("xbut_OK", "2"));
			buttons.addComponent(row, 2, renderButton("xbut_Cancel", "0"));
		}
		result.addComponent(row++, 1, buttons);
		if(title != null)
			return renderGroupBox(result);
		else
			return result;
	}

	private Component renderGroupBox(Component comp)
	{
		Group result = new Group();
		result.setDesign(design);
		result.setHeaderComponent(title);
		result.addComponent(comp);
		result.setTooltip(getTooltip());
		return result;
	}

	private String getTooltip()
	{
		return staticBundle.getString("BoxInfo", locale);
	}

	public Event onClick(Event event)
	{
		String choice = decodeEvent(event.getComponentName());
		InputField inputField = (InputField) this.getComponentForId("inf");
		String inputString = inputField.getValue().toString();
		yourcompanyConfirmInputEvent result = new yourcompanyConfirmInputEvent(getCompositeModel().getProperty("rID"), choice, inputString);
		result.setAction(getCompositeModel().getProperty("acttrg"));
		return result;
	}

	private String decodeEvent(String value)
	{
		value = value.substring(value.lastIndexOf('>') + 1, value.length());
		return value;
	}

	private Component renderButton(String key, String choice)
	{
		Button result = null;
		result = new Button(createCompositeComponentId(choice));
		result.setOnClick("onClick");
		result.setText(staticBundle.getString(key, locale));
		if(choice.equals("2"))
			result.setDesign(ButtonDesign.EMPHASIZED);
		return result;
	}

	protected void initFromPageContextImpl(IPageContext ipagecontext, String s)
	{
	}

	public void setActiontarget(String string)
	{
		actiontarget = string;
	}

	public void setRendererID(String string)
	{
		rendererID = string;
	}

	public static IRenderingEvent onNo(Event event, Locale locale)
	{
		return new InfoEvent(Status.OK, staticBundle.getString("xmsg_On_No", locale));
	}

	public static IRenderingEvent onCancel(Event event, Locale locale)
	{
		return new InfoEvent(Status.OK, staticBundle.getString("ymsg_On_Cancel", locale));
	}

	public static final int YES_NO_CANCEL = 0;
	public static final int YES_NO = 1;
	public static final int OK_CANCEL = 2;
	public static final String RES_BTN_CANCEL = "xbut_Cancel";
	public static final String RES_BTN_NO = "xbut_No";
	public static final String RES_BTN_YES = "xbut_Yes";
	public static final String RES_BTN_OK = "xbut_OK";
	private static final String RES_ON_NO = "xmsg_On_No";
	private static final String RES_ON_CANCEL = "ymsg_On_Cancel";
	private static final String TOOLTIP = "BoxInfo";
	private static final String RENDERERID = "rID";
	private static final String ACTIONTARGET = "acttrg";
	private static final String ON_CLICK = "onClick";
	private int mode;
	private Locale locale;
	private String rendererID;
	private String actiontarget;
	private Component comp;
	private Component title;
	private GroupDesign design;
	private static ResourceBundles staticBundle = ResourceBundles.getBundle("com.sapportals.wcm.rendering.screenflow.os.ConfirmComponent");
	private InputField input;

}

and a class that extends Event:



public final class yourcompanyConfirmInputEvent extends Event
{

	public yourcompanyConfirmInputEvent(String id, String choice, String input)
	{
		super(id);
		this.choice = null;
		this.input = null;
		if(id == null || choice == null)
		{
			throw new NullPointerException("The parameters id <" + id + "> and choice <" + choice + "> must not be <null>");
		} else
		{
			this.choice = choice;
			this.input = input;
			return;
		}
	}

	public String getChoice()
	{
		return choice;
	}
	
	public String getStringInput()
	{
		return input == null ? "" : input;
	}
	public int getIntInput() 
	{
		try {
			return Integer.parseInt(input);
		} catch (NumberFormatException e) {
			return Integer.MAX_VALUE;
		}
	}

	protected void setParams(String as[])
	{
	}

	private String choice;
	private String input;
	public static final String CHOICE_CANCEL = "0";
	public static final String CHOICE_NO = "1";
	public static final String CHOICE_YES = "2";
}

and then in your command class in the <b>getScreenflow</b> method create your component:

...
TextView tv =
			new TextView("Some Text");
		TextView header = new TextView("Some Text2");
		header.setTooltip("Some ToolTip");
		header.setDesign(TextViewDesign.EMPHASIZED);
		yourcompanyConfirmInputComponent cc =
			new yourcompanyConfirmInputComponent(
				2,
				getProxy().getResourceContext().getLocale(),
				header,
				tv,
				null);
		OneStepScreenflow result =
			new OneStepScreenflow(sd, getAlias(), rid, cc);
...

and in your execute method:

public IRenderingEvent execute(IResource res, Event event)
		throws WcmException {
		if (event instanceof yourcompanyConfirmInputEvent) {
			yourcompanyConfirmInputEvent cce = (yourcompanyConfirmInputEvent) event;
			if ("2".equals(cce.getChoice())) {
				int input = cce.getIntInput();
				return execute(res, input);
			}
			if ("1".equals(cce.getChoice()))
				return yourcompanyConfirmInputComponent.onNo(
					event,
					res.getContext().getLocale());
			if ("0".equals(cce.getChoice()))
				return yourcompanyConfirmInputComponent.onCancel(
					event,
					res.getContext().getLocale());
		}
		return execute(res, 0);
	}


	private IRenderingEvent execute(IResource res, int input) throws WcmException {
		//Do something with it
}

Best Regards,

Avishai Zamir

Answers (2)

Answers (2)

Former Member
0 Kudos

Avishai,

Thank you so much for taking time in providing the code. Thank you so much...Appreciate the help you provided.

Thanks,

Pavan

former_member388485
Contributor
0 Kudos

Hi,

You cannot use <i>ConfirmComponent</i> and <i>ConfirmEvent</i> for that.

What you should do is to implement two new classes one that will extend <i>OneStepComponent</i> (similar to <i>ConfirmComponent</i> but with an extra InputField) and the other that will extend <i>Event</i> (similar to <i>ConfirmEvent</i> but with a method to get the InputField Value).

If you need some code samples, let me know and i'll work on something.

Best Regards,

Avishai Zamir

Former Member
0 Kudos

Zamir,

Thanks for your reply. Can you please provide me code samples for the same. I appreciate any help regarding this.

Thanks,

Pavan Keely