View Javadoc

1   package com.sri.emo.controller;
2   
3   import java.io.*;
4   
5   import com.jcorporate.expresso.core.controller.*;
6   import com.jcorporate.expresso.core.db.*;
7   import com.sri.emo.dbobj.*;
8   import com.sri.emo.wizard.*;
9   import com.sri.emo.wizard.creation.*;
10  import com.sri.emo.wizard.expressoimpl.*;
11  import com.sri.emo.wizard.wizardgateway.*;
12  import org.apache.log4j.*;
13  import java.util.Map;
14  import com.sri.emo.wizard.branch.*;
15  import com.sri.emo.controller.*;
16  
17  public class CreationWizardAction extends WizardController {
18  
19      public static final String PARAM_TARGET_ID = "targetId";
20  //    public static final String SUMMARY_STATE = "Summary";
21  
22      /***
23       * Default constructor -- no added state.s
24       */
25      public CreationWizardAction() {
26          super();
27  //        State s = new State(SUMMARY_STATE, "Summary Page");
28  //        s.addParameter(WIZ_PARAMETER_ID, DBObject.INT_MASK);
29  //        addState(s);
30      }
31  
32      /***
33       * Runs the next state.  This fucntion checks for input and transitions
34       * to the display state after invoking the wizard's 'next' function if there
35       * were not errors.
36       *
37       * @param request  the <code>ExpressoRequest</code> object
38       * @param response the <code>ExpressoResponse</code> object.
39       * @throws ControllerException    upon error
40       * @throws NonHandleableException upon fatal error
41       */
42      protected void runNextState(final ExpressoRequest request,
43                                  final ExpressoResponse response) throws ControllerException, NonHandleableException {
44          try {
45              Wizard wiz = getCurrentWizard(request);
46              if (wiz == null) {
47                  response.addError("We're sorry, but your session has expired.  Please re-run the wizard");
48                  Transition cancel = getCancelTransition();
49                  cancel.executeTransition(request, response);
50                  return;
51              }
52  
53              //Verify that data has been entered.
54              WizardPage currentPage = getCurrentPage(request, wiz);
55  
56              //At the time of the writing of this comment, data can be
57              //either a list or Strings -- of course, other possibilities
58              //exist.
59              Serializable data = extractPostedWizardData(request);
60              Serializable branchingData = extractBranchingData(request);
61              System.out.println("branching data = " + branchingData);
62  
63              boolean errorInPage = false;
64              errorInPage = hasNoDataEntryAndShould(data, currentPage);
65  
66              if (errorInPage) {
67                  handleNoEntryErrorInPage(request, response);
68                  return;
69              }
70              System.out.println("!!!!!!!wiz is " + wiz.getClass());
71              if(wiz instanceof BranchingWizard ||
72                      wiz instanceof EmoCreationWizard){
73                  System.out.println("going to expanded next");
74                  ((BranchingWizard)wiz).next(currentPage, data, branchingData);
75              }else{
76                  System.out.println("going to short next");
77                  wiz.next(currentPage, data);
78              }
79              //Check for errors on the page.
80              transferPageErrorsToRequest(currentPage, response);
81              transition(DISPLAY_STATE, request, response);
82          } catch (WizardException ex) {
83              throw new ControllerException("Error going to previous page", ex);
84          }
85      }
86  
87      /***
88       * extractBranchingData
89       *
90       * @param request ExpressoRequest
91       * @return Serializable
92       */
93      private Serializable extractBranchingData(ExpressoRequest request) {
94          return (Serializable)request.getAllParameters();
95      }
96  
97      //    protected void runSummaryState(final ExpressoRequest request,
98  //                                   final ExpressoResponse response) throws ControllerException, NonHandleableException {
99  //        System.out.println("*******************************************");
100 //        System.out.println("*******************************************");
101 //        System.out.println("*******************************************");
102 //        System.out.println("*******************************************");
103 //        System.out.println("summary state ran");
104 //        System.out.println("*******************************************");
105 //        System.out.println("*******************************************");
106 //        System.out.println("*******************************************");
107 //        System.out.println("*******************************************");
108 //    }
109 
110 
111     /***
112      * Override to provide backlink to list wizards page in EMO.
113      *
114      * @return Transition.
115      */
116     protected Transition getCancelTransition() {
117         return new Transition("cancel", "cancel", WizardGatewayController.class, ListWizards.STATE_NAME);
118     }
119 
120 
121     private void transferPageErrorsToRequest(WizardPage currentPage, final ExpressoResponse response) throws
122         ControllerException {
123     ErrorCollection ec = currentPage.getPageErrors();
124     if (ec != null && ec.size() > 0) {
125         response.saveErrors(ec);
126         //Clear errors associated with the page
127         //since we've handled them now.
128         currentPage.removeErrors();
129     }
130 }
131 
132     /***
133      * Override of WizardController to provide for the unique capabilities
134      * and requirements of an ExpressoAwareWizardRepository as defined in
135      * the Schema.
136      *
137      * @param request The <code>ExpressoRequest</code>Object
138      * @return Wizard instance
139      * @throws com.sri.emo.wizard.WizardException
140      *          upon
141      *          wizard related construction error
142      * @throws com.jcorporate.expresso.core.controller.ControllerException
143      *          upon
144      *          parameter retrieval error.
145      */
146     protected Wizard newWizard(final ExpressoRequest request) throws WizardException, ControllerException {
147         EmoCreationWizard theNewWizard = (EmoCreationWizard) super.newWizard(request);
148         //commented out by rich
149 //        if (request.getParameter(PARAM_TARGET_ID) != null && request.getParameter(PARAM_TARGET_ID).length() > 0) {
150 //            theNewWizard.setTargetNodeId(request.getParameter(PARAM_TARGET_ID));
151 //        }
152 
153         return theNewWizard;
154     }
155 
156 
157     /***
158      * <tt>Template Method</tt>.  Retrieves/Constructs the WizardMementoConverter
159      * appropriate for the current run.
160      *
161      * @return WizardMementoConverter or null if none is defined.
162      */
163     protected WizardMementoConverter getMementoConverter() {
164         return (WizardMementoConverter) this.locate("CreationMementoConverter");
165     }
166 
167     /***
168      * For relation pages, this version extracts the entire request paramter
169      * map because the relation wizard needs all that nasty hard-to-read checkbox
170      * data.  Otherwise, we hand it up to the default behavior.
171      *
172      * @param request ExpressoRequest
173      * @return Serializable may be null if the data is not found or
174      *         incomplete.
175      */
176     protected Serializable extractPostedWizardData(ExpressoRequest request) {
177         try {
178 
179             WizardPage currentPage = this.getCurrentPage(request, this.getCurrentWizard(request));
180             assert currentPage != null;
181 
182 
183 //            return (Serializable)request.getAllParameters();
184             if (currentPage instanceof RelationWizardPage) {
185                 return (Serializable) request.getAllParameters();
186             } else if (currentPage instanceof SearchListPage){
187                 return (Serializable) request.getAllParameters();
188             }else if (currentPage instanceof CustomPartHandlerPage) {
189                 return (Serializable) request.getAllParameters();
190             } else {
191                 return super.extractPostedWizardData(request);
192             }
193         } catch (ControllerException ex) {
194             Logger.getLogger(CreationWizardAction.class).error(
195                     "Error extracting wizard and page from requested session.", ex);
196             throw new IllegalStateException(
197                     "Error extracting wizard and page from requested session. (Details have been logged) Message: " + ex.getMessage());
198         }
199 
200     }
201 
202     /***
203      * Template method.
204      *
205      * @param request  ExpressoRequest Expresso Request object
206      * @param response ExpressoResponse ExpressoResponse object
207      * @throws ControllerException upon errors.
208      * @todo Implement this com.sri.emo.wizard.expressoimpl.WizardController
209      * method
210      */
211     protected void afterFinishState(ExpressoRequest request, ExpressoResponse response) throws ControllerException {
212         try {
213             Node finalNode = (Node) request.getSession().getAttribute(WIZ_RESULT_ID);
214             Transition editTransition = new Transition();
215             editTransition.setControllerObject(AddNodeAction.class);
216             editTransition.setState(AddNodeAction.VIEW_NODE);
217             editTransition.addParam(Node.NODE_ID, finalNode.getNodeId());
218             editTransition.redirectTransition(request, response);
219         } catch (DBException ex) {
220             throw new ControllerException(ex);
221         }
222 
223     }
224 
225 
226 }