1
2
3
4
5
6
7
8
9
10
11 package com.sri.emo.wizard.creation.model;
12
13 import java.io.*;
14 import java.util.*;
15
16 import com.jcorporate.expresso.core.controller.*;
17 import com.jcorporate.expresso.core.db.*;
18 import com.jcorporate.expresso.core.registry.*;
19 import com.sri.emo.dbobj.*;
20 import com.sri.emo.wizard.*;
21 import com.sri.emo.wizard.creation.*;
22 import org.apache.commons.lang.builder.*;
23 import org.apache.log4j.*;
24 import org.apache.struts.action.*;
25 import com.sri.emo.wizard.creation.StringUtil;
26
27 /***
28 * Bean for handling action form.
29 *
30 * @author Michael Rimov
31 * @version 1.0
32 * @todo Change Constructor to use NodeId for constructor to allow automatic
33 * initialization.
34 */
35 public class CreationBean implements Serializable {
36
37 /***
38 * The target node type id.
39 */
40 private Integer targetId = null;
41
42 /***
43 * The part type id, null if this is the main bean
44 */
45 private Integer partId = null;
46
47 /***
48 * A set of all completion parts, ordered by part-defined ordering.
49 */
50 private Set completionParts = null;
51
52 private SearchListPage searchPage=null;
53
54 /***
55 * The current node.
56 */
57 private Node currentNode = null;
58
59 /***
60 * The current node type;
61 */
62 private NodeType currentNodeType = null;
63
64
65
66
67
68
69 /***
70 * marker for whether we got a 'dynamic' indication of id for target node. if so,
71 * we permit saving; otherwise, not.
72 */
73 private boolean isDynamicTarget = false;
74
75 /***
76 * Default constructor.
77 */
78 public CreationBean() {
79
80
81
82 }
83
84 public void setSearchPage(SearchListPage page){
85 this.searchPage = page;
86 }
87
88 public SearchListPage getSearchPage(){
89 return this.searchPage;
90 }
91
92
93
94
95
96
97
98
99
100 public boolean isDynamicTarget() {
101 return isDynamicTarget;
102 }
103
104 /***
105 * @todo should this just be set whenever setTargetId() is called??
106 */
107 public void setDynamicTarget(boolean dynamicTarget) {
108 isDynamicTarget = dynamicTarget;
109 }
110
111 /***
112 * Sets the target id (as integer)
113 *
114 * @param nodeId int
115 */
116 public void setTargetId(int nodeId) {
117 Integer newTargetId = new Integer(nodeId);
118
119
120 if (targetId == null || this.targetId.intValue() != newTargetId.intValue()) {
121
122 targetId = newTargetId;
123
124
125
126 currentNode = null;
127 completionParts = null;
128 }
129 }
130
131 public void setPartId(int partId){
132 this.partId = new Integer(partId);
133 }
134
135 /***
136 * Sets the target id.
137 *
138 * @param newTargetId String
139 */
140 public void setTargetId(String newTargetId) {
141 if (newTargetId != null) {
142 setTargetId(Integer.parseInt(newTargetId));
143 } else {
144 targetId = null;
145 currentNode = null;
146 completionParts = null;
147 isDynamicTarget = false;
148 }
149 }
150
151 /***
152 * Retrieve the target node id.
153 *
154 * @return Integer
155 */
156 public Integer getTargetId() {
157 return targetId;
158 }
159
160 public Integer getPartId(){
161 return partId;
162 }
163
164
165 /***
166 * Retrieve all completion parts.
167 *
168 * @return Set a set of all completion parts.
169 */
170 public Set getCompletionParts() {
171 return completionParts;
172 }
173
174
175 /***
176 * Retrieve a specific parts bean given the specified part.
177 *
178 * @param key Part
179 * @return CompletionPartsBean
180 */
181 public CreationPartsBean getPartsBeanFromPart(final Part key) {
182 for (Iterator i = getCompletionParts().iterator(); i.hasNext();) {
183 CreationPartsBean oneBean = (CreationPartsBean) i.next();
184 if (oneBean.getPart().equals(key)) {
185 return oneBean;
186 }
187 }
188 throw new RuntimeException("Unable to locate creation bean by part: " + key.toString());
189 }
190
191 public boolean hasPartsBeanFromPart(final Part key){
192 for (Iterator i = getCompletionParts().iterator(); i.hasNext();) {
193 CreationPartsBean oneBean = (CreationPartsBean) i.next();
194 if (oneBean.getPart().equals(key)) {
195 return true;
196 }
197 }
198 return false;
199 }
200
201 /***
202 * Retrieve the set of all completion parts that have the field completion
203 * value set to 'wizard'.
204 *
205 * @return Set of <tt>CompletionPartsBean</tt>s
206 */
207 public Set getWizardCompletionParts() {
208 Set returnValue = new TreeSet();
209 for (Iterator i = getCompletionParts().iterator(); i.hasNext();) {
210 CreationPartsBean oneCompletionPart = (CreationPartsBean) i.next();
211 if (oneCompletionPart.getFieldCompletion() == FieldCompletion.WIZARD) {
212 returnValue.add(oneCompletionPart);
213 }
214 }
215
216 return returnValue;
217 }
218
219 /***
220 * Retrieve the currently set node.
221 *
222 * @return Node
223 */
224 public Node getCurrentNode() {
225
226 return currentNode;
227 }
228
229 public NodeType getCurrentNodeType(){
230 return currentNodeType;
231 }
232
233
234
235
236
237
238
239
240
241
242 /***
243 * <p>Validate the properties that have been set for this HTTP request,
244 * and return an <code>ActionErrors</code> object that encapsulates any
245 * validation errors that have been found.
246 *
247 * @param returnValue the constructed ErrorCollection to populate into.
248 * @return ActionErrors
249 */
250 public ErrorCollection validate(final ErrorCollection returnValue) {
251
252 if (targetId == null) {
253 returnValue.add("targetId", new ActionMessage("You must enter a valid node id (integer)"));
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 /***
271 * Don't continue validation if we can't even get this far.
272 */
273 if (returnValue.size() > 0) {
274 return returnValue;
275 }
276
277 if (!isInitialized()) {
278 initializeFromNodeId(returnValue);
279 } else {
280
281 for (Iterator i = completionParts.iterator(); i.hasNext();) {
282 CreationPartsBean oneBean = (CreationPartsBean) i.next();
283 oneBean.validateAndAdjust(returnValue);
284 }
285 }
286
287 return returnValue;
288
289 }
290
291 /***
292 * Checks to make sure the bean is initialize.d
293 *
294 * @return boolean
295 */
296 public boolean isInitialized() {
297 return currentNode != null;
298 }
299
300
301 /***
302 * Initialize from the node id. Populate ErrorCollection if there is problems
303 * initializing.
304 *
305 * @param errorMessages ErrorCollection may be null, in which case the method
306 * throws IllegalArgumentExceptions if validation/construction fails.
307 */
308 public void initializeFromNodeId(final ErrorCollection errorMessages) {
309 try {
310 System.out.println("CreationBean.initializeFromNodeId()"+super.hashCode());
311
312 NodeType n = new NodeType();
313 n.setId(getTargetId().toString());
314 if (!n.find()) {
315 if (errorMessages != null) {
316 errorMessages.addError("Unable to find node type with id of : " + getTargetId());
317 } else {
318 throw new IllegalArgumentException("Unable to find node type with id of : " + getTargetId());
319 }
320 }
321
322 currentNodeType = n;
323 currentNode = new Node(RequestRegistry.getUser());
324 currentNode.setNodeId("-1");
325 currentNode.setNodeOwner(RequestRegistry.getUser().getLoginName());
326 currentNode.setNodeType(n.getEntityName());
327
328
329 completionParts = new TreeSet();
330
331
332 Part[] allParts = PartsFactory.getParts(n.getField(NodeType.NODE_TYPE_NAME));
333 for (int i = 0; i < allParts.length; i++) {
334 Part onePart = allParts[i];
335 completionParts.add(new CreationPartsBean(this, onePart));
336 }
337
338 } catch (DBException ex) {
339 ex.printStackTrace();
340 if (errorMessages != null) {
341 errorMessages.add("targetId",
342 new ActionMessage("Error initializing node id with id of " + getTargetId()));
343 }
344 Logger.getLogger(CreationBean.class).error("Error initializing node id with id of " + getTargetId(), ex);
345 }
346
347 }
348
349
350
351 /***
352 * Indicates whether some other object is "equal to" this one.
353 *
354 * @param obj the reference object with which to compare.
355 * @return <code>true</code> if this object is the same as the obj
356 * argument; <code>false</code> otherwise.
357 */
358 public boolean equals(Object obj) {
359 if (!(obj instanceof CreationBean)) {
360 return false;
361 }
362
363
364 CreationBean other = (CreationBean) obj;
365
366 EqualsBuilder equals = new EqualsBuilder().append(completionParts, other.completionParts)
367
368 .append(partId, other.partId)
369 .append(targetId, other.targetId);
370
371
372
373
374 return equals.isEquals();
375 }
376
377 public int getMemLocation(){
378 return super.hashCode();
379 }
380
381 /***
382 * Returns a hash code value for the object.
383 *
384 * @return a hash code value for this object.
385 */
386 public int hashCode() {
387
388 HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(131, 91)
389 .append(completionParts)
390
391 .append(partId)
392 .append(targetId);
393
394
395
396
397 return hashCodeBuilder.toHashCode();
398 }
399
400 /***
401 * Returns a string representation of the object.
402 *
403 * @return a string representation of the object.
404 */
405 public String toString() {
406 return new ToStringBuilder(this)
407 .append(completionParts)
408
409 .append(partId)
410 .append(targetId)
411
412
413
414 .toString();
415 }
416 }