001: /*
002: * Created on 16 Jul 2007
003: */
004: package uk.org.ponder.rsf.swf.util;
005:
006: import java.lang.reflect.Method;
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: import org.springframework.web.bind.WebDataBinder;
011: import org.springframework.webflow.action.FormAction;
012: import org.springframework.webflow.core.collection.AttributeMap;
013: import org.springframework.webflow.engine.ActionList;
014: import org.springframework.webflow.engine.AnnotatedAction;
015: import org.springframework.webflow.engine.ViewState;
016: import org.springframework.webflow.execution.Action;
017: import org.springframework.webflow.execution.FlowExecutionContext;
018: import org.springframework.webflow.execution.FlowSession;
019:
020: import uk.org.ponder.beanutil.BeanModelAlterer;
021: import uk.org.ponder.mapping.DARList;
022: import uk.org.ponder.mapping.DataAlterationRequest;
023: import uk.org.ponder.reflect.ReflectUtils;
024: import uk.org.ponder.saxalizer.SAXAccessMethod;
025: import uk.org.ponder.util.Logger;
026:
027: public class RSFSWFUtil {
028: /** Returns a request map with the equivalent effect to the DAR list, as if
029: * encoded by the supplied WebDataBinder. If the <code>wdb</code> is null,
030: * a default-constructed instance will be assumed.
031: */
032: public static Map getEquivalentBinderMap(DARList darlist,
033: WebDataBinder wdb, BeanModelAlterer bma) {
034: if (wdb == null) {
035: wdb = new WebDataBinder(darlist);
036: }
037: String prefix = wdb.getFieldMarkerPrefix();
038: Map togo = new HashMap();
039: for (int i = 0; i < darlist.size(); ++i) {
040: DataAlterationRequest dar = darlist.DARAt(i);
041: if (dar.type.equals(DataAlterationRequest.DELETE)) {
042: Logger.log
043: .warn("Unsupported DataAlterationRequest of type DELETE for path "
044: + dar.path
045: + " in Spring Web Flow environment");
046: }
047: togo.put(dar.path, bma.getFlattenedValue("", dar.data,
048: String.class, null));
049: if (prefix != null) {
050: togo.put(prefix + dar.path, "");
051: }
052: }
053: return togo;
054: }
055:
056: public static ActionList findAllCurrentActions(
057: FlowExecutionContext context) {
058: FlowSession session = context.getActiveSession();
059: ViewState state = (ViewState) session.getState();
060: ActionList togo = new ActionList();
061: togo.addAll(state.getEntryActionList().toArray());
062: togo.addAll(state.getRenderActionList().toArray());
063: return togo;
064: }
065:
066: public static Map getModelForcibly(FlowExecutionContext context) {
067: FlowSession session = context.getActiveSession();
068: AttributeMap model = context.getConversationScope().union(
069: session.getScope());
070: // The "getFlashMap" method has moved in SWF 2.0 M1
071: try {
072: if (ReflectUtils.hasMethod(session, "getFlashMap")) {
073:
074: Method method = FlowSession.class.getMethod(
075: "getFlashMap", SAXAccessMethod.emptyclazz);
076: AttributeMap flashmap = (AttributeMap) method.invoke(
077: session, SAXAccessMethod.emptyobj);
078: model = model.union(flashmap);
079:
080: } else if (ReflectUtils.hasMethod(context, "getFlashScope")) {
081: Method method = FlowExecutionContext.class.getMethod(
082: "getFlashScope", SAXAccessMethod.emptyclazz);
083: AttributeMap flashmap = (AttributeMap) method.invoke(
084: context, SAXAccessMethod.emptyobj);
085: model = model.union(flashmap);
086:
087: }
088: } catch (Exception e) {
089: Logger.log
090: .warn("Failed to acquire FlashScope from either FlowSession or FlowExecutionContext");
091: // Throw this away
092: }
093: return model.asMap();
094: // Note that we cannot apparently recover the request map - but this data is
095: // to be destroyed anyway
096: }
097:
098: public static FormAction findCurrentFormAction(
099: FlowExecutionContext context) {
100: ActionList list = findAllCurrentActions(context);
101: for (int i = 0; i < list.size(); ++i) {
102: FormAction action = findFormAction(list.get(i));
103: if (action != null)
104: return action;
105: }
106: return null;
107: }
108:
109: public static FormAction findFormAction(Action action) {
110: if (action instanceof FormAction) {
111: return (FormAction) action;
112: } else if (action instanceof AnnotatedAction) {
113: return findFormAction(((AnnotatedAction) action)
114: .getTargetAction());
115: }
116: return null;
117: }
118: }
|