package com.example.customcontrol;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.oracle.determinations.engine.Uncertain;
import com.oracle.determinations.interview.engine.screens.InputInterviewControl;
import com.oracle.determinations.interview.engine.screens.InterviewScreen;
import com.oracle.determinations.interview.engine.screens.ListOption;
import com.oracle.determinations.web.platform.datamodel.screen.NativeScreen;
import com.oracle.determinations.web.platform.datamodel.screen.controls.ControlContainer;
import com.oracle.determinations.web.platform.datamodel.screen.controls.CustomInputControl;
import com.oracle.determinations.web.platform.datamodel.screen.controls.ListItem;
import com.oracle.determinations.web.platform.exceptions.FormatValueException;
import com.oracle.determinations.web.platform.exceptions.WebDeterminationsException;
import com.oracle.determinations.web.platform.servlet.WebDeterminationsServletContext;
public abstract class InputInterviewControlWrapper extends InterviewControlWrapper implements CustomInputControl {
protected final InputInterviewControl iiCtrl; //This is the same object as super.iCtrl, with more specific type
InputInterviewControlWrapper(InputInterviewControl iiCtrl, ControlContainer parent, NativeScreen screen) {
super(iiCtrl, parent, screen);
this.iiCtrl = iiCtrl;
}
//Methods from InputControl
/**
* @return the current value to be displayed in the control. If the current value is not null then this.getValue() will be returned
* otherwise this.getDefaultValue() will be return. The value returned from this method will sutibly be encoded to be displayed on a HTML form.
*/
public String getDisplayValue() {
return WebDeterminationsServletContext.encodeHTML(formatValue(getRawDisplayValue()));
}
/**
* @return the raw, unencoded display value of this control. If the current value not null then return this.getRawSetValue(), otherwise
* return this.getRawDefaultValue().
*/
public Object getRawDisplayValue() {
return (this.getRawValue() == null) ? this.getRawDefaultValue() : this.getRawValue();
}
/**
* @return the HTML encoded default value associated with this control.
*/
public String getDefaultValue() {
return WebDeterminationsServletContext.encodeHTML(formatValue(getRawDefaultValue()));
}
/**
* @return the raw, unencoded default value associated with this control.
*/
public Object getRawDefaultValue() {
return iiCtrl.getDefaultValue();
}
/**
* @return the HTML encoded current value set for this control
*/
public String getValue() {
return WebDeterminationsServletContext.encodeHTML(formatValue(getRawValue()));
}
/**
* @return the raw, unencoded current value set for this control
*/
public abstract Object getRawValue();
/**
* @return true if this control is read only, otherwise false
*/
public boolean isReadOnly() {
return iiCtrl.isReadOnly();
}
/**
* @return true if it is mandatory to supply a value for this control, otherwise false.
*/
public boolean isMandatory() {
return iiCtrl.isMandatory();
}
/**
* Populate the value of this control based on the specified input params. Is used when redisplaying the same
* screen due to some error condition
*
* @param inputParams the list of input parameters from which the value is derived
*/
public abstract void populateValue(Map inputParams);
/**
* Parses the parameters passed in the HTTP post and set the values in the screen instance.
*
* @param inputParams - the parameters to parse the control's value from
* @return the value for this control acording to the parameters passed in.
*/
public void mapValues(InterviewScreen iScreen, Map inputParams) {
if (isVisible() && !isReadOnly()) {
InputInterviewControl ctrlInst = (InputInterviewControl) iScreen.findControl(getRawId());
if (ctrlInst == null) {
throw new WebDeterminationsException("Could not find matching control instance for control: " + this.getRawId());
}
try {
Object value = this.extractValue(inputParams);
ctrlInst.setValue(value);
} catch (RuntimeException ex) {
if (ex instanceof FormatValueException) {
throw ex;
} else {
throw new FormatValueException("Could not format value", "", ex);
}
}
}
}
/**
* Parses the parameters passed in the HTTP post and returns the value for this control.
*
* @param inputParams HTTP post parameters
* @return parsed value
* @throws com.oracle.determinations.web.platform.exceptions.FormatValueException if the submitted value cannot be parsed.
*/
public abstract Object extractValue(Map inputParams) throws FormatValueException;
//Methods from AttributeInputControl
/**
* @return the id of the entity this input control is bound to
*/
public String getRawEntityId() {
return iiCtrl.getParent().getContext().getEntityId();
}
/**
* @return the name of the entity instance this input control is bound to
*/
public String getRawEntityInstanceName() {
return iiCtrl.getParent().getContext().getInstanceName();
}
/**
* @return the id of the attribute this input control is bound to
*/
public String getRawAttributeId() {
return iiCtrl.getAttribute().getName();
}
/**
* @return the encoded text values to be displayed as a list (if any). If this is not a list selection control,
* this method returns <code>null</code>.
*/
public List getListOptions() {
return iiCtrl.getListOptions();
}
/**
* @return the selection style that this input control is to be rendered with.
*/
public String getSelectionStyle() {
return iiCtrl.getSelectionStyle();
}
/**
* Format an attribute value according to the default rules of the attribute's data type
* This version of the function is suitable only for simple values that convert back and forth
* directly to a single string
* @param value the object value
* @return the formatted string
*/
protected String formatValue(Object value) {
return screen.getSessionContext().getFormatter().getFormattedValue(iiCtrl.getValueType(), value, iiCtrl.getAttribute());
}
@SuppressWarnings("unchecked")
public String getListDisplayForValue(){
ArrayList listOptions = null;
HashMap valToListItem = null;
List wrappedListOptions = getListOptions();
/* If list options were given for this control, we initialise the presentation of these options. */
if (wrappedListOptions != null) {
listOptions = new ArrayList(wrappedListOptions.size());
valToListItem = new HashMap(listOptions.size());
for (Object optionObject : listOptions) {
/* Make sure that the list option is visible before inspecting its fields */
if (!(optionObject instanceof ListOption)) {
throw new IllegalArgumentException("Expected list options parameter to be a list of " + ListOption.class.toString() +
" objects.");
}
ListOption option = (ListOption) optionObject;
String formattedValue = formatValue(option.getValue());
//boolean isVisible = option.isVisible(new
InterviewEntityInstance(iCtrl.getParent().getContext()),screen.getSessionContext().getInterviewSession());
boolean isVisible = option.isVisible();
ListItem item = new ListItem(option.getValue(), formattedValue, option.getDisplayText(), isVisible);
listOptions.add(item);
valToListItem.put(formattedValue, item);
}
}
Object val = getRawValue();
String formattedVal = formatValue(val);
if(isSelectionInput() &&
val != null && val != Uncertain.INSTANCE &&
valToListItem != null && valToListItem.containsKey(formattedVal)){
return ((ListItem)valToListItem.get(formattedVal)).getDisplayText();
}
return this.getValue();
}
protected boolean isSelectionInput() {
return getSelectionStyle() != null && (getSelectionStyle().equals("Dropdown") || getSelectionStyle().equals("Listbox")
|| getSelectionStyle().equals("Radiobutton"));
}
}