001: /*
002: * Created on 16 Jul 2007
003: */
004: package uk.org.ponder.rsf.swf.support;
005:
006: import java.util.Iterator;
007: import java.util.Map;
008:
009: import org.springframework.validation.Errors;
010: import org.springframework.webflow.action.FormObjectAccessor;
011: import org.springframework.webflow.execution.FlowExecution;
012: import org.springframework.webflow.executor.ResponseInstruction;
013:
014: import uk.org.ponder.beanutil.WriteableBeanLocator;
015: import uk.org.ponder.messageutil.TargettedMessageList;
016: import uk.org.ponder.rsf.flow.ARIResult;
017: import uk.org.ponder.rsf.request.EarlyRequestParser;
018: import uk.org.ponder.rsf.swf.util.RSFSWFUtil;
019: import uk.org.ponder.rsf.swf.viewparams.SWFViewParams;
020: import uk.org.ponder.rsf.viewstate.ViewParameters;
021: import uk.org.ponder.springutil.errors.SpringErrorConverter;
022: import uk.org.ponder.util.Logger;
023: import uk.org.ponder.util.RunnableInvoker;
024:
025: public class SWFAlterationWrapper implements RunnableInvoker {
026: protected SWFExecutor executor;
027: private WriteableBeanLocator wbl;
028: private TargettedMessageList messages;
029: private String requestType;
030: private ViewParameters incoming;
031: private ResponseInstructionGetter responseInstructionGetter;
032:
033: public void setExecutor(SWFExecutor executor) {
034: this .executor = executor;
035: }
036:
037: public void setRequestBeanLocator(WriteableBeanLocator wbl) {
038: this .wbl = wbl;
039: }
040:
041: public void setIncomingViewParameters(ViewParameters incoming) {
042: this .incoming = incoming;
043: }
044:
045: public void setTargettedMessageList(TargettedMessageList tml) {
046: this .messages = tml;
047: }
048:
049: public void setRequestType(String requestType) {
050: this .requestType = requestType;
051: }
052:
053: public ARIResult handleEvent(String eventId) {
054: ResponseInstruction ri = responseInstructionGetter
055: .getResponse(eventId);
056: executor.interpretResponseInstruction(ri,
057: (SWFViewParams) incoming);
058: return executor.getARIResult();
059: }
060:
061: private void chuckIn(Map inchuck) {
062: if (inchuck == null)
063: return;
064: for (Iterator keyit = inchuck.keySet().iterator(); keyit
065: .hasNext();) {
066: String key = (String) keyit.next();
067: Object value = inchuck.get(key);
068: wbl.set(key, value);
069: if (key.equals(FormObjectAccessor
070: .getCurrentFormErrorsName())) {
071: // Errors errors = (Errors) value;
072:
073: }
074: Logger.log.info("Chucked in model bean " + key + " as "
075: + inchuck.get(key));
076: }
077: }
078:
079: public void invokeRunnable(final Runnable torun) {
080: final boolean ispost = requestType
081: .equals(EarlyRequestParser.ACTION_REQUEST);
082: if (!ispost) {
083: // Get request are ballistic
084: chuckIn(executor.getInChuckMap());
085: }
086: if (incoming instanceof SWFViewParams && ispost) {
087: SWFViewParams incomings = (SWFViewParams) incoming;
088: if (incomings.flowExecutionKey != null) {
089: executor.execute(incomings, new SWFExecutionPayload() {
090: public void executeInRequest(
091: FlowExecution execution,
092: ResponseInstructionGetter rig) {
093: Map inchuck = RSFSWFUtil
094: .getModelForcibly(execution);
095: chuckIn(inchuck);
096:
097: responseInstructionGetter = rig;
098: // when we run this runnable, the request will operate, up to the point
099: // where we hit the SWFEventBean or not. in general, BINDINGS will occur.
100: // when the SWFEventBean receives a signal, it could potentially call
101: // UPWARDS to here.
102: torun.run();
103: if (ispost && execution.isActive()) {
104: inchuck = RSFSWFUtil
105: .getModelForcibly(execution);
106: Errors errors = (Errors) inchuck
107: .get(FormObjectAccessor
108: .getCurrentFormErrorsName());
109: if (errors != null) {
110: SpringErrorConverter.appendErrors(
111: "SWFBindingBean", messages,
112: errors);
113: }
114: }
115: }
116: });
117: }
118: return;
119: }
120: torun.run();
121:
122: }
123:
124: }
|