1   
2   
3   
4   
5   
6   
7   
8   
9   
10  package com.sri.emo.wizard.creation.management;
11  
12  import com.jcorporate.expresso.core.controller.Controller;
13  import com.jcorporate.expresso.core.controller.ControllerException;
14  import com.jcorporate.expresso.core.controller.ErrorCollection;
15  import com.jcorporate.expresso.core.controller.ExpressoRequest;
16  import com.jcorporate.expresso.core.controller.ExpressoResponse;
17  import com.jcorporate.expresso.core.controller.Transition;
18  import com.jcorporate.expresso.core.db.DBException;
19  import com.sri.common.controller.StateHandler;
20  import com.sri.common.dbobj.ObjectNotFoundException;
21  import com.sri.emo.dbobj.Node;
22  import com.sri.emo.wizard.creation.CreationRepository;
23  import com.sri.emo.wizard.creation.model.CreationBean;
24  import com.sri.emo.wizard.creation.model.CreationPartsBean;
25  import com.sri.emo.wizard.creation.model.FieldCompletion;
26  import com.sri.emo.wizard.wizardgateway.ListWizards;
27  import com.sri.emo.wizard.wizardgateway.WizardGatewayController;
28  import org.apache.log4j.Logger;
29  
30  import java.util.Iterator;
31  import com.sri.emo.wizard.creation.model.*;
32  import com.sri.emo.dbobj.*;
33  
34  /***
35   * @author Michael Rimov
36   */
37  public class DoChooseCriteria implements StateHandler {
38  
39  
40      public static final String STATE_NAME = "doChooseCriteria";
41  
42      public static final String STATE_DESCRIPTION = "Save Criteria Choices";
43  
44      /***
45       * Parent controller backreference.
46       */
47      private final Controller parent;
48  
49      /***
50       * The completion session manager.
51       */
52      private final ICreationBeanManager beanManager;
53  
54  
55      /***
56       * added by rich
57       */
58      private final String CREATE_SUFFIX = "_create";
59      private final String BROWSE_SUFFIX = "_browse";
60      private final String SEARCH_SUFFIX = "_search";
61      private final String REQUIRED_SUFFIX = "_required";
62  
63      private final String CREATE_TEXT_SUFFIX = "_createText";
64      private final String BROWSE_TEXT_SUFFIX = "_browseText";
65      private final String SEARCH_TEXT_SUFFIX = "_searchText";
66  
67  
68      private final String MINALLOWED_SUFFIX = "_minEntries";
69  
70      private final String MAXALLOWED_SUFFIX = "_maxEntries";
71  
72      private final String DIRECTIVE_SUFFIX = "_directive";
73  
74      private final String HELPTEXT_SUFFIX = "_helpText";
75  
76  
77      /***
78       * Interface to the internal repository.
79       */
80      private final CreationRepository completionRepository;
81  
82      /***
83       * Constructor that takes the owner controller as a paramter.
84       *
85       * @param owner            Controller the owning controller.
86       * @param respository      The Completion Repository bean.
87       * @param stateBeanManager The CompletionBean Manager.
88       */
89      public DoChooseCriteria(final Controller owner,
90                              final CreationRepository respository,
91                              final ICreationBeanManager stateBeanManager) {
92          parent = owner;
93          beanManager = stateBeanManager;
94          completionRepository = respository;
95      }
96  
97  
98      /***
99       * Called to handle the request.
100      *
101      * @param request  ControllerRequest The Function's ControllerRequest
102      *                 object.
103      * @param response ControllerResponse The Function's ControllerResponse
104      *                 object.
105      * @throws DBException         upon underlying database exception error.
106      * @throws ControllerException upon underlying ControllerException error.
107      * @todo Clean up validation logic to clear out
108      */
109     public void handleRequest(final ExpressoRequest request, final ExpressoResponse response) throws DBException,
110             ControllerException {
111         
112         if (beanManager.handleSessionTimeout(request, response)) {
113             return;
114         }
115 
116 
117         ErrorCollection ec = new ErrorCollection();
118 
119         CreationBeans wizardBeans = beanManager.getActionForm(request);
120         CreationBean wizardBean = wizardBeans.getCurrentBean();
121         for (Iterator i = wizardBean.getCompletionParts().iterator(); i.hasNext();) {
122 
123             CreationPartsBean partsBean = (CreationPartsBean) i.next();
124 
125             
126             
127             
128             if (partsBean.getFieldCompletion() == FieldCompletion.NOT_INCLUDED) {
129                 continue;
130             }
131 
132             String partNum = partsBean.getPart().getPartNum();
133 
134             String minEntries = request.getParameter(partNum + MINALLOWED_SUFFIX);
135             String maxEntries = request.getParameter(partNum + MAXALLOWED_SUFFIX);
136 
137             String directive = request.getParameter(partNum + DIRECTIVE_SUFFIX);
138             String helptext = request.getParameter(partNum + HELPTEXT_SUFFIX);
139 
140             String create = request.getParameter(partNum+CREATE_SUFFIX);
141             String browse = request.getParameter(partNum+BROWSE_SUFFIX);
142             String search = request.getParameter(partNum+SEARCH_SUFFIX);
143             String required = request.getParameter(partNum+REQUIRED_SUFFIX);
144 
145             String createText = request.getParameter(partNum+CREATE_TEXT_SUFFIX);
146             String browseText = request.getParameter(partNum+BROWSE_TEXT_SUFFIX);
147             String searchText = request.getParameter(partNum+SEARCH_TEXT_SUFFIX);
148 
149             if (partsBean.isSingleEntry()) {
150                 if (minEntries == null || minEntries.length() == 0) {
151                     partsBean.setMinEntries(1);
152                     partsBean.setMaxEntries(1);
153                 } else {
154                     boolean error = false;
155                     try {
156                         Integer minEntriesAsNumber = new Integer(minEntries);
157                         if (minEntriesAsNumber.intValue() > 1) {
158                             error = true;
159                         }
160                     } catch (NumberFormatException ex1) {
161                         error = true;
162                     }
163 
164                     if (error) {
165                         ec.addError(
166                                 "Min Entries for: " + partsBean.getPart().getPartLabel() + " must be a valid whole number and either 0 or 1");
167                     } else {
168                         partsBean.setMinEntries(Integer.parseInt(minEntries));
169                         partsBean.setMaxEntries(1);
170                     }
171 
172                     
173                     error = false;
174 
175                     if (maxEntries != null) {
176                         try {
177                             Integer maxEntriesAsNumber = new Integer(maxEntries);
178                             if (maxEntriesAsNumber.intValue() > 1) {
179                                 error = true;
180                             }
181                         } catch (NumberFormatException ex1) {
182                             error = true;
183                         }
184                     }
185 
186                     if (error) {
187                         ec.addError(
188                                 "Max Entries for: " + partsBean.getPart().getPartLabel() + " must be a valid whole number and either 0 or 1");
189                     } else {
190                         partsBean.setMinEntries(Integer.parseInt(minEntries));
191                         partsBean.setMaxEntries(1);
192                     }
193 
194 
195                 }
196 
197             } else if (partsBean.isMinMaxAllowed()) {
198                 if (minEntries == null || minEntries.length() == 0) {
199                     partsBean.setMinEntries(1);
200                 } else {
201                     try {
202                         Integer minEntriesAsNumber = new Integer(minEntries);
203                         if (minEntriesAsNumber.intValue() < 0) {
204                             ec.addError(
205                                     "Min Entries for: " + partsBean.getPart().getPartLabel() + " must be a valid number >= 0");
206                         }
207                     } catch (NumberFormatException ex1) {
208                         ec.addError(
209                                 "Min Entries for: " + partsBean.getPart().getPartLabel() + " must be a valid number >= 0");
210                     }
211                     partsBean.setMinEntries(Integer.parseInt(minEntries));
212                 }
213 
214                 if (maxEntries == null || maxEntries.length() == 0) {
215                     partsBean.setMaxEntries(1);
216                 } else {
217                     Integer maxEntriesAsNumber = partsBean.getMaxEntries();
218                     try {
219                         maxEntriesAsNumber = new Integer(maxEntries);
220                         if (maxEntriesAsNumber.intValue() < 0) {
221                             ec.addError("The Maximum for '" + partsBean.getPart().getPartLabel()
222                                     + "' must be a valid number >= 0");
223                         } else if (partsBean.getMinEntries().intValue() > maxEntriesAsNumber.intValue()) {
224                             ec.addError("The Maximum for '" + partsBean.getPart().getPartLabel()
225                                     + "' must be greater than or equal to the Minimum value ("
226                                     + partsBean.getMinEntries().intValue() + ")");
227                         }
228 
229                     } catch (NumberFormatException ex1) {
230                         ec.addError("Max Entries for: " + partsBean.getPart().getPartLabel()
231                                 + " must be a valid number >= 0)");
232                     }
233                     partsBean.setMaxEntries(maxEntriesAsNumber.intValue());
234                 }
235             }
236 
237             
238             
239             /***
240              * @todo RICHARD: validate that we still need to check for
241              * shared node attributes
242              */
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274             
275 
276 
277 
278 
279 
280 
281             partsBean.setCreate(create != null);
282             partsBean.setBrowse(browse != null);
283             partsBean.setSearch(search != null);
284             partsBean.setRequired(required != null);
285 
286             partsBean.setCreateText(createText);
287             partsBean.setBrowseText(browseText);
288             partsBean.setSearchText(searchText);
289 
290             partsBean.setDirective(directive);
291             partsBean.setHelpText(helptext);
292             
293             
294             if(!partsBean.isCreate() && wizardBeans.hasCreationBeanByPartId(partsBean.getPart().getId())){
295                 wizardBeans.removeBeanByPart(partsBean.getPart().getId());
296             }
297 
298             if(partsBean.isCreate() && !wizardBeans.hasCreationBeanByPartId(partsBean.getPart().getId())){
299                 CreationBean newCreation = new CreationBean();
300                 wizardBeans.add(newCreation);
301                 newCreation.setPartId(new Integer(partsBean.getPart().getId()).intValue());
302                 if(partsBean.getPart().isSharedNodeAttrib()){
303                     NodeType n = new NodeType();
304 
305                     n.setEntityName(partsBean.getPart().getPartType());
306 
307                     System.out.println("parts type is " +  partsBean.getPart().getPartType());
308                     if (!n.find()) {
309 
310 
311 
312                         throw new IllegalArgumentException("Unable to find node type with id of : " + partsBean.getPart().getPartType());
313 
314                     }else{
315                         n.retrieve();
316                         System.out.println("target id retrieved as " + n.getId());
317                         System.out.println("display title retr as " + n.getDisplayName());
318                         newCreation.setTargetId(n.getId());
319                     }
320                 }
321                 System.out.println("created new creation bean with partId: " + newCreation.getPartId() + " and targetId: " + newCreation.getTargetId());
322                 newCreation.initializeFromNodeId(ec);
323                 if(ec.size() > 0){
324                     break;
325                 }
326             }
327         }
328 
329         if(wizardBeans.incrementCurrentCreationBean()){
330             Transition redirect = new Transition(PromptChooseParts.STATE_NAME, parent);
331             redirect.redirectTransition(request, response);
332             return;
333         }
334 
335         
336 
337 
338 
339 
340 
341 
342 
343 
344         
345         if (ec.size() == 0) {
346             wizardBean.validate(ec);
347         }
348 
349         
350         if (ec.size() > 0) {
351             response.saveErrors(ec);
352             Transition toTransitionTo = new Transition(PromptChooseCriteria.STATE_NAME, parent);
353             toTransitionTo.executeTransition(request, response);
354             return;
355         }
356 
357         
358 
359 
360         if (wizardBeans.getWizardId() == null) {
361             completionRepository.add(wizardBeans);
362         } else {
363             try {
364                 completionRepository.update(wizardBeans);
365             } catch (ObjectNotFoundException ex) {
366                 Logger.getLogger(DoChooseCriteria.class).warn(
367                         "Error updating wizard bean: Bean not found.  Adding instead", ex);
368                 completionRepository.add(wizardBeans);
369             }
370         }
371 
372         
373         beanManager.destroyActionForm(request);
374 
375         
376         new Transition("aftersave", "after save", WizardGatewayController.class,
377                 ListWizards.STATE_NAME).redirectTransition(
378                 request, response);
379     }
380 }