You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by ra...@apache.org on 2005/08/10 23:11:40 UTC
svn commit: r231329 [6/13] - in /jakarta/taglibs/proper/rdc/trunk/src:
META-INF/tags/rdc/ org/apache/taglibs/rdc/ org/apache/taglibs/rdc/core/
org/apache/taglibs/rdc/dm/ org/apache/taglibs/rdc/resources/
org/apache/taglibs/rdc/sampleapps/mortgage/ org/...
Modified: jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/DMUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/DMUtils.java?rev=231329&r1=231328&r2=231329&view=diff
==============================================================================
--- jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/DMUtils.java (original)
+++ jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/DMUtils.java Wed Aug 10 14:10:59 2005
@@ -44,242 +44,242 @@
*/
public class DMUtils {
- // ******************
- // PRIVATE CONSTANTS
- // ******************
- private static final String NESTED_DATAMODEL_SEPARATOR = "|";
-
- private static final String FILLER = ".*";
- private static final String ID_TOK = "\\$((\\w|_|\\" +
- NESTED_DATAMODEL_SEPARATOR + ")+)";
- private static final String START_EXPR = "\\#\\{";
- private static final String FILLER_NO_END_EXPR = "[^\\}]*";
-
- private static final Pattern PATTERN_ID =
- Pattern.compile(FILLER + ID_TOK + FILLER);
- private static final int ID_GROUP = 1;
- private static final Pattern PATTERN_OPEN_EXPR =
- Pattern.compile(FILLER + START_EXPR + FILLER_NO_END_EXPR);
-
- // Error messages (to be i18n'zed)
- private static final String ERR_NO_SUCH_MODEL = "No model for " +
- "\"{0}\" found as child of \"{1}\"";
- private static final String ERR_NULL_EXPR = "EL error, \"{0}\"" +
- "; JspContext or expression or return type is null";
- private static final String ERR_BAD_EXPR = "EL error while " +
- "evaluating expression: \"{0}\"";
-
- // Logging
- private static Log log = LogFactory.getLog(DMUtils.class);
-
- // ******************
- // PUBLIC METHODS
- // ******************
- /**
- * Return true if the given component or container is done.
- *
- * @param BaseModel
- */
- public static boolean isChildDone(BaseModel model) {
- if (model instanceof GroupModel) {
- GroupModel grpModel = (GroupModel) model;
- if (grpModel.getGroupConfirm() != null) {
- if (grpModel.getState() == Constants.GRP_STATE_DONE) {
- return true;
- }
- } else {
- if (grpModel.getActiveChildren().size() == 0) {
- grpModel.setState(Constants.GRP_STATE_DONE);
- return true;
- }
- }
- return false;
- } else if (model instanceof ComponentModel &&
- model.getState() == Constants.FSM_DONE) {
- return true;
- } else if (model instanceof BaseModel &&
- model.getState() == Constants.FSM_DONE) {
- return true;
- }
- return false;
- }
-
- // ******************
- // PACKAGE METHODS
- // ******************
- /**
- * Activate child so it takes over the dialog from the next turn
- */
- static int invokeDormantChild(Map children, List activeChildren,
- String id) {
- if (id == null) {
- log.info("Null ID of identified target");
- return Constants.GRP_ALL_CHILDREN_DONE;
- }
- BaseModel model = (BaseModel) children.get(id);
- if (model != null) {
- if (model.getState() == Constants.FSM_DORMANT) {
- activeChildren.add(id);
- if (model instanceof BaseModel) {
- model.setState(Constants.FSM_INPUT);
- } else if (model instanceof GroupModel) {
- model.setState(Constants.GRP_STATE_RUNNING);
- } else if (model instanceof ComponentModel) {
- model.setState(Constants.FSM_INPUT);
- }
- }
- return Constants.GRP_SOME_CHILD_RUNNING;
- } else {
- log.info("ID \"" + id + "\" of target in group dialog " +
- "configuration is invalid");
- return Constants.GRP_ALL_CHILDREN_DONE;
- }
- }
-
- /**
- * Two pass evaluation:
- * 1) Substitute value-of RDCs
- * 2) Evaluate using the EL evaluator
- *
- * @param String to be evaluated
- */
- static Object proprietaryEval(GroupTag groupTag, GroupModel groupModel,
- String expr, Class retClass, LinkedHashMap lruCache,
- List tempVars) {
-
- Matcher matcher = PATTERN_ID.matcher(expr);
- while (matcher.matches()) {
- String matched = matcher.group(ID_GROUP);
- int start = expr.lastIndexOf(matched) - 1; // -1 for $
- int end = start + 1 + matched.length();
- String startStr = "", endStr = "";
- if (start > 0) {
- startStr = expr.substring(0, start);
- }
- if (end < expr.length()) {
- endStr = expr.substring(end);
- }
- Object[] exprData = null;
- Object rdcValue = null;
- String pageVar = null;
- if (lruCache != null && lruCache.containsKey(matched)) {
- exprData = (Object[])lruCache.get(matched);
- rdcValue = exprData[0];
- pageVar = (String)exprData[1];
- }
- if (exprData == null) {
- rdcValue = valueOfIDExpr(groupModel, matched);
- pageVar = RDCUtils.getNextRDCVarName();
- tempVars.add(pageVar);
- addToCache(lruCache, matched, rdcValue, pageVar);
- }
- groupTag.getJspContext().setAttribute(pageVar, rdcValue);
- expr = wrapAsNeeded(startStr, pageVar, endStr);
- matcher = PATTERN_ID.matcher(expr);
- }
- return valueOfELExpr(groupTag.getJspContext(), expr, retClass);
- }
-
-
- // ******************
- // PRIVATE METHODS
- // ******************
- /**
- * LRU Cache implementation using LinkedHashMap
- *
- */
- private static void addToCache(LinkedHashMap cache,
- String key, Object value, String var) {
-
- final int MAX_CACHE_SIZE = 50;
- if (cache.size() == MAX_CACHE_SIZE) {
- cache.remove(cache.keySet().iterator().next());
- }
- cache.put(key, new Object[] { value, var });
- }
-
- /**
- * Make sure the temporary page context variable gets evaluated in an
- * EL expression
- *
- */
- private static String wrapAsNeeded(String startStr, String pageVar,
- String endStr) {
- Matcher openExpr = PATTERN_OPEN_EXPR.matcher(startStr);
- if (openExpr.matches()) {
- return startStr + pageVar + endStr;
- }
- return startStr + "#{" + pageVar + "}" + endStr;
- }
-
- /**
- * Obtain the value of the given ID with respect to the given GroupModel.
- *
- * @param GroupModel The group model which holds a child of the given ID
- * @param String The ID whose value is required
- */
- private static Object valueOfIDExpr(GroupModel groupModel, String id) {
- Object value = null;
- BaseModel iteratorModel = groupModel;
- if (id == null) {
- return null;
- }
- StringTokenizer idTokenizer = new StringTokenizer(id,
- NESTED_DATAMODEL_SEPARATOR);
- BaseModel model = null;
- while (idTokenizer.hasMoreTokens()) {
- String idTok = idTokenizer.nextToken();
- if (iteratorModel instanceof GroupModel) {
- model = (BaseModel) ((GroupModel)iteratorModel).
- getLocalMap().get(idTok);
- } else if (iteratorModel instanceof ComponentModel) {
- model = (BaseModel) ((ComponentModel)iteratorModel).
- getLocalMap().get(idTok);
- }
- if (model == null) {
- // Invalid child ID specified in XML navigation rules
- MessageFormat msgFormat = new MessageFormat(ERR_NO_SUCH_MODEL);
- log.warn(msgFormat.format(new Object[] {idTok,
- iteratorModel.getId()}));
- return "$" + id;
- }
- iteratorModel = model;
- }
- value = model.getValue();
- return value;
- }
-
- /**
- * Obtain the value of the given JSP 2.0 EL expression in the group
- * tag's PageContext.
- *
- * @param JspContext The context in which the expression should be
- * evaluated
- * @param String The JSP 2.0 EL expression
- * @param Class retType The expected return type
- */
- private static Object valueOfELExpr(JspContext ctx, String expr_,
- Class retType) {
-
- if (ctx == null || expr_ == null || retType == null) {
- MessageFormat msgFormat = new MessageFormat(ERR_NULL_EXPR);
- log.warn(msgFormat.format(new Object[] {expr_}));
- return null;
- }
- String expr = expr_.replaceAll("\\#\\{", "\\$\\{");
- Object value = null;
- if (retType == null) {
- retType = java.lang.String.class;
- }
- ExpressionEvaluatorImpl exprEvaluator = new ExpressionEvaluatorImpl();
- VariableResolver varResolver = ctx.getVariableResolver();
- try {
- value = exprEvaluator.evaluate(expr, retType, varResolver, null);
- } catch (javax.servlet.jsp.el.ELException e) {
- MessageFormat msgFormat = new MessageFormat(ERR_BAD_EXPR);
- log.warn(msgFormat.format(new Object[] {e.getMessage()}), e);
- return null;
- } // end of try-catch
- return value;
- }
+ // ******************
+ // PRIVATE CONSTANTS
+ // ******************
+ private static final String NESTED_DATAMODEL_SEPARATOR = "|";
+
+ private static final String FILLER = ".*";
+ private static final String ID_TOK = "\\$((\\w|_|\\" +
+ NESTED_DATAMODEL_SEPARATOR + ")+)";
+ private static final String START_EXPR = "\\#\\{";
+ private static final String FILLER_NO_END_EXPR = "[^\\}]*";
+
+ private static final Pattern PATTERN_ID =
+ Pattern.compile(FILLER + ID_TOK + FILLER);
+ private static final int ID_GROUP = 1;
+ private static final Pattern PATTERN_OPEN_EXPR =
+ Pattern.compile(FILLER + START_EXPR + FILLER_NO_END_EXPR);
+
+ // Error messages (to be i18n'zed)
+ private static final String ERR_NO_SUCH_MODEL = "No model for " +
+ "\"{0}\" found as child of \"{1}\"";
+ private static final String ERR_NULL_EXPR = "EL error, \"{0}\"" +
+ "; JspContext or expression or return type is null";
+ private static final String ERR_BAD_EXPR = "EL error while " +
+ "evaluating expression: \"{0}\"";
+
+ // Logging
+ private static Log log = LogFactory.getLog(DMUtils.class);
+
+ // ******************
+ // PUBLIC METHODS
+ // ******************
+ /**
+ * Return true if the given component or container is done.
+ *
+ * @param BaseModel
+ */
+ public static boolean isChildDone(BaseModel model) {
+ if (model instanceof GroupModel) {
+ GroupModel grpModel = (GroupModel) model;
+ if (grpModel.getGroupConfirm() != null) {
+ if (grpModel.getState() == Constants.GRP_STATE_DONE) {
+ return true;
+ }
+ } else {
+ if (grpModel.getActiveChildren().size() == 0) {
+ grpModel.setState(Constants.GRP_STATE_DONE);
+ return true;
+ }
+ }
+ return false;
+ } else if (model instanceof ComponentModel &&
+ model.getState() == Constants.FSM_DONE) {
+ return true;
+ } else if (model instanceof BaseModel &&
+ model.getState() == Constants.FSM_DONE) {
+ return true;
+ }
+ return false;
+ }
+
+ // ******************
+ // PACKAGE METHODS
+ // ******************
+ /**
+ * Activate child so it takes over the dialog from the next turn
+ */
+ static int invokeDormantChild(Map children, List activeChildren,
+ String id) {
+ if (id == null) {
+ log.info("Null ID of identified target");
+ return Constants.GRP_ALL_CHILDREN_DONE;
+ }
+ BaseModel model = (BaseModel) children.get(id);
+ if (model != null) {
+ if (model.getState() == Constants.FSM_DORMANT) {
+ activeChildren.add(id);
+ if (model instanceof BaseModel) {
+ model.setState(Constants.FSM_INPUT);
+ } else if (model instanceof GroupModel) {
+ model.setState(Constants.GRP_STATE_RUNNING);
+ } else if (model instanceof ComponentModel) {
+ model.setState(Constants.FSM_INPUT);
+ }
+ }
+ return Constants.GRP_SOME_CHILD_RUNNING;
+ } else {
+ log.info("ID \"" + id + "\" of target in group dialog " +
+ "configuration is invalid");
+ return Constants.GRP_ALL_CHILDREN_DONE;
+ }
+ }
+
+ /**
+ * Two pass evaluation:
+ * 1) Substitute value-of RDCs
+ * 2) Evaluate using the EL evaluator
+ *
+ * @param String to be evaluated
+ */
+ static Object proprietaryEval(GroupTag groupTag, GroupModel groupModel,
+ String expr, Class retClass, LinkedHashMap lruCache,
+ List tempVars) {
+
+ Matcher matcher = PATTERN_ID.matcher(expr);
+ while (matcher.matches()) {
+ String matched = matcher.group(ID_GROUP);
+ int start = expr.lastIndexOf(matched) - 1; // -1 for $
+ int end = start + 1 + matched.length();
+ String startStr = "", endStr = "";
+ if (start > 0) {
+ startStr = expr.substring(0, start);
+ }
+ if (end < expr.length()) {
+ endStr = expr.substring(end);
+ }
+ Object[] exprData = null;
+ Object rdcValue = null;
+ String pageVar = null;
+ if (lruCache != null && lruCache.containsKey(matched)) {
+ exprData = (Object[])lruCache.get(matched);
+ rdcValue = exprData[0];
+ pageVar = (String)exprData[1];
+ }
+ if (exprData == null) {
+ rdcValue = valueOfIDExpr(groupModel, matched);
+ pageVar = RDCUtils.getNextRDCVarName();
+ tempVars.add(pageVar);
+ addToCache(lruCache, matched, rdcValue, pageVar);
+ }
+ groupTag.getJspContext().setAttribute(pageVar, rdcValue);
+ expr = wrapAsNeeded(startStr, pageVar, endStr);
+ matcher = PATTERN_ID.matcher(expr);
+ }
+ return valueOfELExpr(groupTag.getJspContext(), expr, retClass);
+ }
+
+
+ // ******************
+ // PRIVATE METHODS
+ // ******************
+ /**
+ * LRU Cache implementation using LinkedHashMap
+ *
+ */
+ private static void addToCache(LinkedHashMap cache,
+ String key, Object value, String var) {
+
+ final int MAX_CACHE_SIZE = 50;
+ if (cache.size() == MAX_CACHE_SIZE) {
+ cache.remove(cache.keySet().iterator().next());
+ }
+ cache.put(key, new Object[] { value, var });
+ }
+
+ /**
+ * Make sure the temporary page context variable gets evaluated in an
+ * EL expression
+ *
+ */
+ private static String wrapAsNeeded(String startStr, String pageVar,
+ String endStr) {
+ Matcher openExpr = PATTERN_OPEN_EXPR.matcher(startStr);
+ if (openExpr.matches()) {
+ return startStr + pageVar + endStr;
+ }
+ return startStr + "#{" + pageVar + "}" + endStr;
+ }
+
+ /**
+ * Obtain the value of the given ID with respect to the given GroupModel.
+ *
+ * @param GroupModel The group model which holds a child of the given ID
+ * @param String The ID whose value is required
+ */
+ private static Object valueOfIDExpr(GroupModel groupModel, String id) {
+ Object value = null;
+ BaseModel iteratorModel = groupModel;
+ if (id == null) {
+ return null;
+ }
+ StringTokenizer idTokenizer = new StringTokenizer(id,
+ NESTED_DATAMODEL_SEPARATOR);
+ BaseModel model = null;
+ while (idTokenizer.hasMoreTokens()) {
+ String idTok = idTokenizer.nextToken();
+ if (iteratorModel instanceof GroupModel) {
+ model = (BaseModel) ((GroupModel)iteratorModel).
+ getLocalMap().get(idTok);
+ } else if (iteratorModel instanceof ComponentModel) {
+ model = (BaseModel) ((ComponentModel)iteratorModel).
+ getLocalMap().get(idTok);
+ }
+ if (model == null) {
+ // Invalid child ID specified in XML navigation rules
+ MessageFormat msgFormat = new MessageFormat(ERR_NO_SUCH_MODEL);
+ log.warn(msgFormat.format(new Object[] {idTok,
+ iteratorModel.getId()}));
+ return "$" + id;
+ }
+ iteratorModel = model;
+ }
+ value = model.getValue();
+ return value;
+ }
+
+ /**
+ * Obtain the value of the given JSP 2.0 EL expression in the group
+ * tag's PageContext.
+ *
+ * @param JspContext The context in which the expression should be
+ * evaluated
+ * @param String The JSP 2.0 EL expression
+ * @param Class retType The expected return type
+ */
+ private static Object valueOfELExpr(JspContext ctx, String expr_,
+ Class retType) {
+
+ if (ctx == null || expr_ == null || retType == null) {
+ MessageFormat msgFormat = new MessageFormat(ERR_NULL_EXPR);
+ log.warn(msgFormat.format(new Object[] {expr_}));
+ return null;
+ }
+ String expr = expr_.replaceAll("\\#\\{", "\\$\\{");
+ Object value = null;
+ if (retType == null) {
+ retType = java.lang.String.class;
+ }
+ ExpressionEvaluatorImpl exprEvaluator = new ExpressionEvaluatorImpl();
+ VariableResolver varResolver = ctx.getVariableResolver();
+ try {
+ value = exprEvaluator.evaluate(expr, retType, varResolver, null);
+ } catch (javax.servlet.jsp.el.ELException e) {
+ MessageFormat msgFormat = new MessageFormat(ERR_BAD_EXPR);
+ log.warn(msgFormat.format(new Object[] {e.getMessage()}), e);
+ return null;
+ } // end of try-catch
+ return value;
+ }
}
Modified: jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/DialogManagerImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/DialogManagerImpl.java?rev=231329&r1=231328&r2=231329&view=diff
==============================================================================
--- jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/DialogManagerImpl.java (original)
+++ jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/dm/DialogManagerImpl.java Wed Aug 10 14:10:59 2005
@@ -47,258 +47,258 @@
*/
public abstract class DialogManagerImpl implements DialogManager {
- // Error messages (to be i18n'zed)
- private static final String ERR_SET_PROPERTY = "Error while setting " +
- "\"{0}\" property on model with ID \"{1}\"; received error message: " +
- "\"{2}\"\n";
-
- // Logging
- private static Log log = LogFactory.getLog(DialogManagerImpl.class);
-
- // The RDC stack
- protected Stack stack;
- // The private data Model of Group
- protected LinkedHashMap stateMap;
- // The GroupTag object whose dialog we are managing
- protected GroupTag groupTag;
- // The state of the group implementation
- protected int groupState;
-
- /* Constructor */
- public DialogManagerImpl() {
- }
-
- public void setGroupTag(GroupTag groupTag) {
- this.groupTag = groupTag;
- }
-
- /**
- * Initialiaze this doTag() invocation
- *
- *
- */
- public boolean initialize(JspContext ctx, JspFragment bodyFragment)
- throws JspException, IOException {
-
- //Get the stateMap from the rdcStack to store the GroupModel
- stack = (Stack) ctx.getAttribute(Constants.STR_RDC_STACK,
- PageContext.REQUEST_SCOPE);
- if (stack != null) {
- stateMap = (LinkedHashMap) stack.peek();
- } else {
- throw new JspException(Constants.ERR_NO_RDC_STACK);
- }
- if (stateMap == null) {
- throw new JspException(Constants.ERR_EMPTY_RDC_STACK);
- }
-
- // Get the GroupModel from the stateMap or create one if it does not
- // exist and put it in stateMap
- GroupModel groupModel = (GroupModel) stateMap.get(groupTag.getId());
- if (groupModel == null) {
- groupModel = new GroupModel();
- groupModel.setId(groupTag.getId());
- groupModel.setSubmit(groupTag.getSubmit());
- if (groupModel.getLocalMap() == null) {
- groupModel.setLocalMap(new LinkedHashMap());
- }
- if (groupModel.getGroupConfirm() == null &&
- groupTag.getConfirm().booleanValue()) {
- groupModel.setGroupConfirm(new GroupConfirm((PageContext)
- ctx, groupModel, groupTag.getId()));
- }
- groupModel.getLocalMap().put(Constants.STR_INIT_ONLY_FLAG, Boolean.TRUE);
- stateMap.put(groupTag.getId(), groupModel);
- }
-
- /*
- * Return now, if:
- * 1) Caller is in initOnly mode, just return as we already registered
- * or
- * 2) In dormant state then turn for execution has not yet come.
- * Check for null value as this may be called directly from JSP page
- */
- if (((Boolean) stateMap.get(Constants.STR_INIT_ONLY_FLAG) == Boolean.TRUE)
- || (groupModel.getState() == Constants.FSM_DORMANT)) {
- return false;
- }
-
- Map modelMap = groupModel.getLocalMap();
- stack.push(modelMap);
-
- if (modelMap.get(Constants.STR_INIT_ONLY_FLAG) == Boolean.TRUE) {
- // Ask children to register if in registration phase
- if (bodyFragment != null) {
- bodyFragment.invoke(null);
- }
- // Push the submit down to the children
- if (groupTag.getSubmit() != null) {
- setPropertyChildren(modelMap, "submit", groupTag.getSubmit());
- } else {
- throw new NullPointerException("Group " + groupTag.getId() +
- " has no specified submit URI");
- }
- }
- return true;
- }
-
- /**
- * Collect the required information from the user
- *
- *
- */
- public abstract void collect(JspContext ctx, JspFragment bodyFragment)
- throws JspException, IOException;
-
- /**
- * Confirm the collected information from the user
- *
- *
- */
- public void confirm() {
-
- GroupModel groupModel = (GroupModel) stateMap.get(groupTag.getId());
- Map modelMap = groupModel.getLocalMap();
-
- if (groupModel.getState() == Constants.GRP_STATE_RUNNING) {
- if (groupState == Constants.GRP_ALL_CHILDREN_DONE) {
- if (groupTag.getConfirm().booleanValue()) {
- if (groupModel.getGroupConfirm().doGroupConfirmation() ==
- Constants.CONF_STATE_DONE) {
- groupModel.setState(Constants.GRP_STATE_DONE);
- }
- } else {
- groupModel.setState(Constants.GRP_STATE_DONE);
- }
- }
- }
- }
-
- /**
- * Finish up this doTag() invocation
- *
- *
- */
- public void finish(JspContext ctx) throws JspException, IOException {
-
- GroupModel groupModel = (GroupModel) stateMap.get(groupTag.getId());
- Map modelMap = groupModel.getLocalMap();
-
- // Populate the return value HashMap and provide the JSP scripting var
- if (groupModel.getState() == Constants.GRP_STATE_DONE) {
- Map retValMap = createReturnValueMap(modelMap);
- groupModel.setValue(retValMap);
- ctx.setAttribute(groupTag.getId(), retValMap);
- }
-
- stack.pop();
- }
-
- /**
- * Creates the Map for the retVal of the group tag where the keys
- * are the id's of the children and the value is the BaseModel.value
- * object of each child
- *
- * @param children Map that holds the id's and child data models
- */
- private Map createReturnValueMap(Map children) {
-
- if (children == null) {
- return null;
- }
-
- Map retValMap = new HashMap();
- Iterator iter = children.keySet().iterator();
- String current = null;
-
- // Loop for all the children in the list, get their values
- // fill the valuemap with {Id, Value} pair for all children RDCs
- while (iter.hasNext()) {
- current = (String) iter.next();
- if (current.equals(Constants.STR_INIT_ONLY_FLAG)) {
- continue;
- }
- retValMap.put(current,
- ((BaseModel) children.get(current)).getValue());
- } // end while() loop over all the children
-
- return retValMap;
- } // end createReturnValueMap()
-
-
- /**
- * Set the state of the children
- *
- * @param children Map
- * @param state the new state for the children
- *
- */
- protected void setStateChildren(Map children, int state) {
- if (children == null) {
- return;
- }
-
- Iterator iter = children.keySet().iterator();
- String current;
- BaseModel model = null;
-
- //Loop while there are elements in the list
- while (iter.hasNext()) {
- current = (String) iter.next();
- if (current.equals(Constants.STR_INIT_ONLY_FLAG)) {
- continue;
- }
-
- model = (BaseModel) children.get(current);
- if (model != null) {
- model.setState(state);
- }
- }
- }
-
- /**
- * Sets property for all children to value
- *
- *
- */
- protected void setPropertyChildren(Map children, String propertyName,
- Object value) {
-
- if (children == null || propertyName == null ||
- propertyName.length() == 0) {
- return;
- }
-
- Iterator iter = children.keySet().iterator();
- String current;
- BaseModel model = null;
- String setterName = "set" + propertyName.substring(0,1).toUpperCase() +
- ((propertyName.length() > 1) ? propertyName.substring(1) :
- Constants.STR_EMPTY);
- Class[] argClasses = { value.getClass() };
- Object[] args = { value };
-
- //Loop while there are elements in the list
- while (iter.hasNext()) {
- current = (String) iter.next();
- if (current.equals(Constants.STR_INIT_ONLY_FLAG)) {
- continue;
- }
-
- model = (BaseModel) children.get(current);
- if (model != null) {
- Class modelClass = model.getClass();
- try {
- Method setter = modelClass.getMethod(setterName,
- argClasses);
- setter.invoke((Object)model, args);
- } catch (Exception e) {
- MessageFormat msgFormat =
- new MessageFormat(ERR_SET_PROPERTY);
- log.error(msgFormat.format(new Object[] {propertyName,
- model.getId(), e.getMessage()}), e);
- }
- }
- }
- }
+ // Error messages (to be i18n'zed)
+ private static final String ERR_SET_PROPERTY = "Error while setting " +
+ "\"{0}\" property on model with ID \"{1}\"; received error message: " +
+ "\"{2}\"\n";
+
+ // Logging
+ private static Log log = LogFactory.getLog(DialogManagerImpl.class);
+
+ // The RDC stack
+ protected Stack stack;
+ // The private data Model of Group
+ protected LinkedHashMap stateMap;
+ // The GroupTag object whose dialog we are managing
+ protected GroupTag groupTag;
+ // The state of the group implementation
+ protected int groupState;
+
+ /* Constructor */
+ public DialogManagerImpl() {
+ }
+
+ public void setGroupTag(GroupTag groupTag) {
+ this.groupTag = groupTag;
+ }
+
+ /**
+ * Initialiaze this doTag() invocation
+ *
+ *
+ */
+ public boolean initialize(JspContext ctx, JspFragment bodyFragment)
+ throws JspException, IOException {
+
+ //Get the stateMap from the rdcStack to store the GroupModel
+ stack = (Stack) ctx.getAttribute(Constants.STR_RDC_STACK,
+ PageContext.REQUEST_SCOPE);
+ if (stack != null) {
+ stateMap = (LinkedHashMap) stack.peek();
+ } else {
+ throw new JspException(Constants.ERR_NO_RDC_STACK);
+ }
+ if (stateMap == null) {
+ throw new JspException(Constants.ERR_EMPTY_RDC_STACK);
+ }
+
+ // Get the GroupModel from the stateMap or create one if it does not
+ // exist and put it in stateMap
+ GroupModel groupModel = (GroupModel) stateMap.get(groupTag.getId());
+ if (groupModel == null) {
+ groupModel = new GroupModel();
+ groupModel.setId(groupTag.getId());
+ groupModel.setSubmit(groupTag.getSubmit());
+ if (groupModel.getLocalMap() == null) {
+ groupModel.setLocalMap(new LinkedHashMap());
+ }
+ if (groupModel.getGroupConfirm() == null &&
+ groupTag.getConfirm().booleanValue()) {
+ groupModel.setGroupConfirm(new GroupConfirm((PageContext)
+ ctx, groupModel, groupTag.getId()));
+ }
+ groupModel.getLocalMap().put(Constants.STR_INIT_ONLY_FLAG, Boolean.TRUE);
+ stateMap.put(groupTag.getId(), groupModel);
+ }
+
+ /*
+ * Return now, if:
+ * 1) Caller is in initOnly mode, just return as we already registered
+ * or
+ * 2) In dormant state then turn for execution has not yet come.
+ * Check for null value as this may be called directly from JSP page
+ */
+ if (((Boolean) stateMap.get(Constants.STR_INIT_ONLY_FLAG) == Boolean.TRUE)
+ || (groupModel.getState() == Constants.FSM_DORMANT)) {
+ return false;
+ }
+
+ Map modelMap = groupModel.getLocalMap();
+ stack.push(modelMap);
+
+ if (modelMap.get(Constants.STR_INIT_ONLY_FLAG) == Boolean.TRUE) {
+ // Ask children to register if in registration phase
+ if (bodyFragment != null) {
+ bodyFragment.invoke(null);
+ }
+ // Push the submit down to the children
+ if (groupTag.getSubmit() != null) {
+ setPropertyChildren(modelMap, "submit", groupTag.getSubmit());
+ } else {
+ throw new NullPointerException("Group " + groupTag.getId() +
+ " has no specified submit URI");
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Collect the required information from the user
+ *
+ *
+ */
+ public abstract void collect(JspContext ctx, JspFragment bodyFragment)
+ throws JspException, IOException;
+
+ /**
+ * Confirm the collected information from the user
+ *
+ *
+ */
+ public void confirm() {
+
+ GroupModel groupModel = (GroupModel) stateMap.get(groupTag.getId());
+ Map modelMap = groupModel.getLocalMap();
+
+ if (groupModel.getState() == Constants.GRP_STATE_RUNNING) {
+ if (groupState == Constants.GRP_ALL_CHILDREN_DONE) {
+ if (groupTag.getConfirm().booleanValue()) {
+ if (groupModel.getGroupConfirm().doGroupConfirmation() ==
+ Constants.CONF_STATE_DONE) {
+ groupModel.setState(Constants.GRP_STATE_DONE);
+ }
+ } else {
+ groupModel.setState(Constants.GRP_STATE_DONE);
+ }
+ }
+ }
+ }
+
+ /**
+ * Finish up this doTag() invocation
+ *
+ *
+ */
+ public void finish(JspContext ctx) throws JspException, IOException {
+
+ GroupModel groupModel = (GroupModel) stateMap.get(groupTag.getId());
+ Map modelMap = groupModel.getLocalMap();
+
+ // Populate the return value HashMap and provide the JSP scripting var
+ if (groupModel.getState() == Constants.GRP_STATE_DONE) {
+ Map retValMap = createReturnValueMap(modelMap);
+ groupModel.setValue(retValMap);
+ ctx.setAttribute(groupTag.getId(), retValMap);
+ }
+
+ stack.pop();
+ }
+
+ /**
+ * Creates the Map for the retVal of the group tag where the keys
+ * are the id's of the children and the value is the BaseModel.value
+ * object of each child
+ *
+ * @param children Map that holds the id's and child data models
+ */
+ private Map createReturnValueMap(Map children) {
+
+ if (children == null) {
+ return null;
+ }
+
+ Map retValMap = new HashMap();
+ Iterator iter = children.keySet().iterator();
+ String current = null;
+
+ // Loop for all the children in the list, get their values
+ // fill the valuemap with {Id, Value} pair for all children RDCs
+ while (iter.hasNext()) {
+ current = (String) iter.next();
+ if (current.equals(Constants.STR_INIT_ONLY_FLAG)) {
+ continue;
+ }
+ retValMap.put(current,
+ ((BaseModel) children.get(current)).getValue());
+ } // end while() loop over all the children
+
+ return retValMap;
+ } // end createReturnValueMap()
+
+
+ /**
+ * Set the state of the children
+ *
+ * @param children Map
+ * @param state the new state for the children
+ *
+ */
+ protected void setStateChildren(Map children, int state) {
+ if (children == null) {
+ return;
+ }
+
+ Iterator iter = children.keySet().iterator();
+ String current;
+ BaseModel model = null;
+
+ //Loop while there are elements in the list
+ while (iter.hasNext()) {
+ current = (String) iter.next();
+ if (current.equals(Constants.STR_INIT_ONLY_FLAG)) {
+ continue;
+ }
+
+ model = (BaseModel) children.get(current);
+ if (model != null) {
+ model.setState(state);
+ }
+ }
+ }
+
+ /**
+ * Sets property for all children to value
+ *
+ *
+ */
+ protected void setPropertyChildren(Map children, String propertyName,
+ Object value) {
+
+ if (children == null || propertyName == null ||
+ propertyName.length() == 0) {
+ return;
+ }
+
+ Iterator iter = children.keySet().iterator();
+ String current;
+ BaseModel model = null;
+ String setterName = "set" + propertyName.substring(0,1).toUpperCase() +
+ ((propertyName.length() > 1) ? propertyName.substring(1) :
+ Constants.STR_EMPTY);
+ Class[] argClasses = { value.getClass() };
+ Object[] args = { value };
+
+ //Loop while there are elements in the list
+ while (iter.hasNext()) {
+ current = (String) iter.next();
+ if (current.equals(Constants.STR_INIT_ONLY_FLAG)) {
+ continue;
+ }
+
+ model = (BaseModel) children.get(current);
+ if (model != null) {
+ Class modelClass = model.getClass();
+ try {
+ Method setter = modelClass.getMethod(setterName,
+ argClasses);
+ setter.invoke((Object)model, args);
+ } catch (Exception e) {
+ MessageFormat msgFormat =
+ new MessageFormat(ERR_SET_PROPERTY);
+ log.error(msgFormat.format(new Object[] {propertyName,
+ model.getId(), e.getMessage()}), e);
+ }
+ }
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org