View Javadoc

1   package org.apache.struts.flow.portlet;
2   
3   import java.io.PrintWriter;
4   import java.io.IOException;
5   
6   import javax.portlet.GenericPortlet;
7   import javax.portlet.RenderRequest;
8   import javax.portlet.*;
9   import javax.portlet.PortletException;
10  
11  import org.apache.struts.flow.*;
12  import org.apache.struts.flow.core.*;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.apache.struts.flow.core.Factory;
17  import org.apache.struts.flow.core.javascript.fom.FOM_JavaScriptInterpreter;
18  import org.apache.struts.flow.core.Interpreter;
19  import org.apache.struts.flow.core.javascript.ConversionHelper;
20  import org.apache.struts.flow.core.DefaultFlowVariableFactory;
21  import org.apache.struts.flow.sugar.SugarWrapFactory;
22  import org.apache.commons.chain.*;
23  import org.apache.commons.chain.web.portlet.*;
24  import org.apache.struts.flow.core.source.impl.ChainSourceResolver;
25  import java.util.*;
26  import java.io.*;
27  import org.mozilla.javascript.Scriptable;
28  
29  /***  Description of the Class */
30  public class FlowPortlet extends GenericPortlet {
31      
32      private CompilingInterpreter interp;
33      
34      public void init() {
35          
36          Factory.setLogger(new CommonsLogger());
37          Factory.getContinuationsManager().setDefaultTimeToLive(10 * 60 * 1000);
38          interp = createInterpreter();
39          interp.register(getInitParameter("scriptPath"));
40      }
41      
42      private CompilingInterpreter createInterpreter() {
43          FOM_JavaScriptInterpreter interp = new FOM_JavaScriptInterpreter();
44          interp.setSourceResolver(new ChainSourceResolver(new PortletWebContext(getPortletContext(), null, null)));
45          interp.setDebugger(false);
46          interp.setCheckTime(0);
47          interp.setReloadScripts(true);
48          interp.setWrapFactory(new SugarWrapFactory());
49          interp.initialize();
50          interp.register("/org/apache/struts/flow/core/javascript/fom/fom_system.js");
51          interp.addFlowVariable("portlet", new DefaultFlowVariableFactory(Portlet.class));
52          //interp.addVariableRegistrar(new DefaultCallVariableRegistrar(SqlMap.class, "sqlMap"));
53          return interp;
54      }
55      
56      /***
57       *  The portlet's main view prints "Hello, World"
58       *
59       *@param  request               Description of the Parameter
60       *@param  response              Description of the Parameter
61       *@exception  PortletException  If anything goes wrong
62       *@exception  IOException       If anything goes wrong
63       */
64      public void doView(RenderRequest request, RenderResponse response)
65               throws PortletException, IOException {
66          
67          // Create and populate a Context for this request
68          PortletWebContext context = new PortletWebContext();
69          context.initialize(getPortletContext(), request, response);
70                   
71          String view = request.getParameter("view");
72          String contid = request.getParameter("contid");
73          
74          String func = "doView";
75          if (view != null) {
76              func = view + "View";
77          }
78          
79          if (contid == null || contid.length() == 0) {
80  
81              // --- start a new flow
82  
83              List args = new LinkedList();
84              
85              try {
86                  // call control script function
87                  interp.callFunction(func, args, context);
88              } catch (Exception ex) {
89                  throw new PortletException("Unable to execute flow script", ex);
90              }
91  
92              // retrieve page, continuation ID, and attributes from chain context
93              String page = (String) ConversionHelper.jsobjectToObject(context.get(Constants.FORWARD_NAME_KEY));
94              contid = (String) context.get(Constants.CONTINUATION_ID_KEY);
95              Scriptable bizdata = (Scriptable) context.get(Constants.BIZ_DATA_KEY);
96              Map atts = null;
97              if (bizdata != null) {
98                  atts = ConversionHelper.jsobjectToMap(bizdata);
99              }    
100             dispatchToPage(request, response, page, contid, atts);
101         } else {
102             // --- continue an existing flow
103 
104             // kick off continuation
105             context.put("id", "5");
106             
107             try {
108                 interp.handleContinuation(contid, new LinkedList(), context);
109             } catch (Exception ex) {
110                 throw new PortletException("Unable to execute flow script", ex);
111             }
112 
113             // retrieve page, continuation ID, and attributes from chain context
114             String page = (String) context.get(Constants.FORWARD_NAME_KEY);
115             contid = (String) context.get(Constants.CONTINUATION_ID_KEY);
116             Scriptable bizdata = (Scriptable) context.get(Constants.BIZ_DATA_KEY);
117             Map atts = null;
118             if (bizdata != null) {
119                 atts = ConversionHelper.jsobjectToMap(bizdata);
120             }    
121            
122             dispatchToPage(request, response, page, contid, atts);
123         }
124     }
125     
126     /***
127      *  Add continuation ID and attributes to request scope, dispatch to page.
128      *
129      *@param  request            The request
130      *@param  response           The response
131      *@param  page               The action forward name
132      *@param  contid             Continuation ID to be set in request.
133      *@param  atts               Attributes to be set in request.
134      *@param  mapping            The action mapping
135      *@return
136      *@throws  ServletException
137      *@throws  IOException
138      */
139     private void dispatchToPage(RenderRequest request, RenderResponse response, 
140             String page, String contid, Map atts) 
141              throws PortletException, IOException {
142 
143         // Probably only need to process if the response hasn't already been committed.  This
144         // should let flow code be able to completely handle a request if desired.
145         if (!response.isCommitted()) {
146             request.setAttribute("contid", contid);
147 
148             if (atts != null) {
149                 Iterator attkeys = atts.keySet().iterator();
150                 while (attkeys.hasNext()) {
151                     String attkey = (String) attkeys.next();
152                     request.setAttribute(attkey, ConversionHelper.jsobjectToObject(atts.get(attkey)));
153                 }
154             }    
155 
156             PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(page);
157             rd.include(request,response);
158         }   
159     }
160 }
161