View Javadoc

1   /* ===================================================================
2    * Copyright 2002-05 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.wizard.completion.management;
11  
12  import com.jcorporate.expresso.core.controller.*;
13  import com.jcorporate.expresso.core.db.DBException;
14  import com.jcorporate.expresso.core.dbobj.ValidValue;
15  import com.sri.common.controller.StateHandler;
16  import com.sri.emo.dbobj.Node;
17  import com.sri.emo.dbobj.Part;
18  import com.sri.emo.wizard.completion.model.CompletionBean;
19  import com.sri.emo.wizard.completion.model.CompletionPartsBean;
20  import com.sri.emo.wizard.completion.model.FieldCompletion;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Set;
26  
27  
28  /***
29   * @author Michael Rimov
30   */
31  public class PromptChooseParts implements StateHandler {
32  
33  
34      /***
35       * The name of this state as referred to by the Expresso Controller
36       */
37      public static final String STATE_NAME = "promptChooseParts";
38  
39      /***
40       * The description of this state as referred to by the Expresso Controller.
41       */
42      public static final String STATE_DESCRIPTION = "Choose Parts For Wizard";
43  
44  
45      /***
46       * Parent controller backreference.
47       */
48      private final Controller parent;
49  
50      private CompletionBeanManager beanManager;
51  
52      /***
53       * Constructor that takes the owner controller as a paramter.
54       *
55       * @param owner Controller the owning controller.
56       */
57      public PromptChooseParts(final Controller owner) {
58          parent = owner;
59          beanManager = new CompletionBeanManager();
60      }
61  
62      /***
63       * Called to handle the request.
64       *
65       * @param request  ControllerRequest The Function's ControllerRequest
66       *                 object.
67       * @param response ControllerResponse The Function's ControllerResponse
68       *                 object.
69       * @throws DBException         upon underlying database exception error.
70       * @throws ControllerException upon underlying ControllerException error.
71       */
72      public void handleRequest(final ExpressoRequest request, final ExpressoResponse response) throws DBException,
73              ControllerException {
74  
75          response.setTitle("Completion Wiz Creator, Part 2");
76  
77          List allValidValues = new ArrayList(2);
78          allValidValues.add(new ValidValue(FieldCompletion.FIXED.toString(), "Fixed"));
79          allValidValues.add(new ValidValue(FieldCompletion.WIZARD.toString(), "Wizard"));
80  
81          if (beanManager.handleSessionTimeout(request, response)) {
82              return;
83          }
84  
85          CompletionBean cb = beanManager.getActionForm(request);
86          Set allParts = cb.getCompletionParts();
87          Node node = new Node();
88          node.setNodeId(cb.getTargetId().toString());
89          node.retrieve();
90  
91          boolean includedDisabledRadios = false;
92          assert allParts != null;
93          for (Iterator i = allParts.iterator(); i.hasNext();) {
94              CompletionPartsBean onePart = (CompletionPartsBean) i.next();
95              Part thePart = onePart.getPart();
96  
97              Input oneInput = new Input();
98              oneInput.setName(thePart.getPartNum());
99              oneInput.setLabel(thePart.getPartLabel());
100             oneInput.setValidValues(allValidValues);
101 
102             // if field completion is set, use it; otherwise, see if there is data in target object
103             if (onePart.getFieldCompletion() == null) {
104                 // see if there is data in target object
105                 if (thePart.isOwnedAttribute() && node.getAttributes(thePart.getPartType()).length > 0) {
106                     // have attributes
107                     oneInput.setDefaultValue(FieldCompletion.FIXED.toString());
108                 } else if (thePart.isSharedNodeAttrib() && node.getRelatedNodes(thePart.getNodeRelation(),
109                         thePart.getPartType()).length > 0) {
110                     // have relations
111                     oneInput.setDefaultValue(FieldCompletion.FIXED.toString());
112                 } else {
113                     oneInput.setDefaultValue(FieldCompletion.WIZARD.toString());
114                 }
115             } else if (onePart.getFieldCompletion() == FieldCompletion.WIZARD) {
116                 oneInput.setDefaultValue(FieldCompletion.WIZARD.toString());
117             } else {
118                 oneInput.setDefaultValue(FieldCompletion.FIXED.toString());
119             }
120 
121             //Disable any non-related nodes without data.
122             //
123             if (thePart.isSharedNodeAttrib() && node.getRelatedNodes(thePart.getNodeRelation(),
124                     thePart.getPartType()).length == 0) {
125                 oneInput.setAttribute(Input.ATTRIBUTE_DISABLED, "true");
126                 oneInput.setDefaultValue(FieldCompletion.FIXED.toString());
127                 includedDisabledRadios = true;
128             }
129 
130             response.add(oneInput);
131 
132         }
133 
134         //Create navigation transitions
135         Transition back = new Transition(PromptChooseNode.STATE_NAME, parent);
136         back.setName("back");
137         back.setLabel("Back");
138         response.add(back);
139 
140         Transition next = new Transition(DoChooseParts.STATE_NAME, parent);
141         next.setName("next");
142         next.setLabel("Next");
143         response.add(next);
144 
145         // add msg explaining disabled radios
146         if (includedDisabledRadios) {
147             response.add(new Output("includedDisabledRadios", node.getNodeTitle()));
148         }
149     }
150 }