1
2
3
4
5
6
7
8
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.sri.common.controller.StateHandler;
15 import com.sri.common.dbobj.ObjectNotFoundException;
16 import com.sri.emo.dbobj.Node;
17 import com.sri.emo.wizard.completion.CompletionRepository;
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 import com.sri.emo.wizard.wizardgateway.ListWizards;
22 import com.sri.emo.wizard.wizardgateway.WizardGatewayController;
23 import org.apache.log4j.Logger;
24
25 import java.util.Iterator;
26
27 /***
28 * @author Michael Rimov
29 */
30 public class DoChooseCriteria implements StateHandler {
31
32
33 public static final String STATE_NAME = "doChooseCriteria";
34
35 public static final String STATE_DESCRIPTION = "Save Criteria Choices";
36
37 /***
38 * Parent controller backreference.
39 */
40 private final Controller parent;
41
42 /***
43 * The completion session manager.
44 */
45 private final ICompletionBeanManager beanManager;
46
47
48
49 private final String MINALLOWED_SUFFIX = "_minEntries";
50
51 private final String MAXALLOWED_SUFFIX = "_maxEntries";
52
53 private final String DIRECTIVE_SUFFIX = "_directive";
54
55 private final String HELPTEXT_SUFFIX = "_helpText";
56
57
58 /***
59 * Interface to the internal repository.
60 */
61 private final CompletionRepository completionRepository;
62
63 /***
64 * Constructor that takes the owner controller as a paramter.
65 *
66 * @param owner Controller the owning controller.
67 * @param respository The Completion Repository bean.
68 * @param stateBeanManager The CompletionBean Manager.
69 */
70 public DoChooseCriteria(final Controller owner,
71 final CompletionRepository respository,
72 final ICompletionBeanManager stateBeanManager) {
73 parent = owner;
74 beanManager = stateBeanManager;
75 completionRepository = respository;
76 }
77
78
79 /***
80 * Called to handle the request.
81 *
82 * @param request ControllerRequest The Function's ControllerRequest
83 * object.
84 * @param response ControllerResponse The Function's ControllerResponse
85 * object.
86 * @throws DBException upon underlying database exception error.
87 * @throws ControllerException upon underlying ControllerException error.
88 * @todo Clean up validation logic to clear out
89 */
90 public void handleRequest(final ExpressoRequest request, final ExpressoResponse response) throws DBException,
91 ControllerException {
92
93 if (beanManager.handleSessionTimeout(request, response)) {
94 return;
95 }
96
97
98 ErrorCollection ec = new ErrorCollection();
99
100 CompletionBean wizardBean = beanManager.getActionForm(request);
101 for (Iterator i = wizardBean.getCompletionParts().iterator(); i.hasNext();) {
102
103 CompletionPartsBean partsBean = (CompletionPartsBean) i.next();
104
105
106
107
108 if (partsBean.getFieldCompletion() == FieldCompletion.FIXED) {
109 continue;
110 }
111
112 String partNum = partsBean.getPart().getPartNum();
113
114 String minEntries = request.getParameter(partNum + MINALLOWED_SUFFIX);
115 String maxEntries = request.getParameter(partNum + MAXALLOWED_SUFFIX);
116
117 String directive = request.getParameter(partNum + DIRECTIVE_SUFFIX);
118 String helptext = request.getParameter(partNum + HELPTEXT_SUFFIX);
119
120
121 if (partsBean.isSingleEntry()) {
122 if (minEntries == null || minEntries.length() == 0) {
123 partsBean.setMinEntries(1);
124 partsBean.setMaxEntries(1);
125 } else {
126 boolean error = false;
127 try {
128 Integer minEntriesAsNumber = new Integer(minEntries);
129 if (minEntriesAsNumber.intValue() > 1) {
130 error = true;
131 }
132 } catch (NumberFormatException ex1) {
133 error = true;
134 }
135
136 if (error) {
137 ec.addError(
138 "Min Entries for: " + partsBean.getPart().getPartLabel() + " must be a valid whole number and either 0 or 1");
139 } else {
140 partsBean.setMinEntries(Integer.parseInt(minEntries));
141 partsBean.setMaxEntries(1);
142 }
143
144
145 error = false;
146
147 if (maxEntries != null) {
148 try {
149 Integer maxEntriesAsNumber = new Integer(maxEntries);
150 if (maxEntriesAsNumber.intValue() > 1) {
151 error = true;
152 }
153 } catch (NumberFormatException ex1) {
154 error = true;
155 }
156 }
157
158 if (error) {
159 ec.addError(
160 "Max Entries for: " + partsBean.getPart().getPartLabel() + " must be a valid whole number and either 0 or 1");
161 } else {
162 partsBean.setMinEntries(Integer.parseInt(minEntries));
163 partsBean.setMaxEntries(1);
164 }
165
166
167 }
168
169 } else if (partsBean.isMinMaxAllowed()) {
170 if (minEntries == null || minEntries.length() == 0) {
171 partsBean.setMinEntries(1);
172 } else {
173 try {
174 Integer minEntriesAsNumber = new Integer(minEntries);
175 if (minEntriesAsNumber.intValue() < 0) {
176 ec.addError(
177 "Min Entries for: " + partsBean.getPart().getPartLabel() + " must be a valid number >= 0");
178 }
179 } catch (NumberFormatException ex1) {
180 ec.addError(
181 "Min Entries for: " + partsBean.getPart().getPartLabel() + " must be a valid number >= 0");
182 }
183 partsBean.setMinEntries(Integer.parseInt(minEntries));
184 }
185
186 if (maxEntries == null || maxEntries.length() == 0) {
187 partsBean.setMaxEntries(1);
188 } else {
189 Integer maxEntriesAsNumber = partsBean.getMaxEntries();
190 try {
191 maxEntriesAsNumber = new Integer(maxEntries);
192 if (maxEntriesAsNumber.intValue() < 0) {
193 ec.addError("The Maximum for '" + partsBean.getPart().getPartLabel()
194 + "' must be a valid number >= 0");
195 } else if (partsBean.getMinEntries().intValue() > maxEntriesAsNumber.intValue()) {
196 ec.addError("The Maximum for '" + partsBean.getPart().getPartLabel()
197 + "' must be greater than or equal to the Minimum value ("
198 + partsBean.getMinEntries().intValue() + ")");
199 }
200
201 } catch (NumberFormatException ex1) {
202 ec.addError("Max Entries for: " + partsBean.getPart().getPartLabel()
203 + " must be a valid number >= 0)");
204 }
205 partsBean.setMaxEntries(maxEntriesAsNumber.intValue());
206 }
207 }
208
209
210 if (partsBean.getPart().isSharedNodeAttrib()) {
211 Node targetNode = wizardBean.getCurrentNode();
212 final Node[] relatedNodes = targetNode.getRelatedNodes(partsBean.getPart().getNodeRelation(),
213 partsBean.getPart().getPartType());
214 if (maxEntries == null || minEntries == null) {
215 ec.addError(
216 "Please enter a value for minimum and maximum entries for : " + partsBean.getPart().getPartLabel());
217 } else {
218
219 final int maxEntriesAsInt = Integer.parseInt(maxEntries);
220 final int minEntriesAsInt = Integer.parseInt(minEntries);
221
222 if (minEntriesAsInt > relatedNodes.length) {
223 final String errorMessage = "The Minimum for '" + partsBean.getPart().getPartLabel()
224 + "' cannot be greater than the number already found in '"
225 + wizardBean.getCurrentNode().getNodeTitle()
226 + "' (" + relatedNodes.length + ")";
227 ec.addError(errorMessage);
228 }
229
230 if (maxEntriesAsInt > relatedNodes.length) {
231 final String errorMessage = "The Maximum for '" + partsBean.getPart().getPartLabel()
232 + "' cannot be greater than the number already found in '"
233 + wizardBean.getCurrentNode().getNodeTitle()
234 + "' (" + relatedNodes.length + ")";
235
236 ec.addError(errorMessage);
237 }
238 }
239 }
240
241 partsBean.setDirective(directive);
242 partsBean.setHelpText(helptext);
243 }
244
245
246 if (ec.size() == 0) {
247 wizardBean.validate(ec);
248 }
249
250
251 if (ec.size() > 0) {
252 response.saveErrors(ec);
253 Transition toTransitionTo = new Transition(PromptChooseCriteria.STATE_NAME, parent);
254 toTransitionTo.executeTransition(request, response);
255 return;
256 }
257
258
259
260
261 if (wizardBean.getWizardId() == null) {
262 completionRepository.add(wizardBean);
263 } else {
264 try {
265 completionRepository.update(wizardBean);
266 } catch (ObjectNotFoundException ex) {
267 Logger.getLogger(DoChooseCriteria.class).warn(
268 "Error updating wizard bean: Bean not found. Adding instead", ex);
269 completionRepository.add(wizardBean);
270 }
271 }
272
273
274 beanManager.destroyActionForm(request);
275
276
277 new Transition("aftersave", "after save", WizardGatewayController.class,
278 ListWizards.STATE_NAME).redirectTransition(
279 request, response);
280 }
281 }