1
2
3
4 package com.sri.emo.wizard.creation.model;
5
6 import java.io.*;
7 import java.util.*;
8
9 import com.jcorporate.expresso.core.controller.*;
10 import com.sri.emo.wizard.*;
11 import com.sri.emo.wizard.creation.*;
12 import org.apache.commons.lang.builder.*;
13 import org.apache.struts.action.*;
14
15 /***
16 * Bean for holding multiple CreationBean objects
17 */
18 public class CreationBeans implements Serializable {
19 private int currentCreationBean = 0;
20 private List beans = new Vector(10);
21 /***
22 * Wizard id that only is set if we're editing the wizard.
23 */
24 private Integer wizardId;
25 /***
26 * The wizard title.
27 */
28 private String wizardTitle = "";
29
30 /***
31 * The wizard directive.
32 */
33 private String summary = "";
34 private String summaryNonRaw = "";
35
36
37
38 /***
39 * The Implementation Wizard Class.
40 */
41 private Class wizardClass = EmoCreationWizard.class;
42
43 public CreationBeans() {
44 System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
45 System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
46 System.out.println("new CreationBeans() created");
47 System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
48 System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
49
50 }
51
52 public boolean incrementCurrentCreationBean(){
53 if(currentCreationBean >= size() - 1){
54 return false;
55 }
56 currentCreationBean++;
57 return true;
58 }
59
60 public CreationBean getCreationBean(int index){
61 return (CreationBean)beans.get(index);
62 }
63
64 public void setCurrentBean(CreationBean bean){
65 int index = beans.indexOf(bean);
66 if(index >= 0){
67 currentCreationBean = index;
68 }else{
69 throw new IllegalArgumentException("Unable to set the current bean to be " + bean);
70 }
71 }
72
73 public CreationBean getCurrentBean(){
74 if(currentCreationBean >= beans.size()){
75 return null;
76 }
77 return (CreationBean)beans.get(currentCreationBean);
78 }
79
80
81 /***
82 * Sets the wizard id.
83 *
84 * @param wizardId Integer.
85 */
86 public void setWizardId(Integer wizardId) {
87 this.wizardId = wizardId;
88 }
89
90 public Integer getWizardId() {
91 return wizardId;
92 }
93
94 /***
95 * Get the wizard title.
96 *
97 * @return String
98 */
99 public String getWizardTitle() {
100 System.out.println("getting wizard title as " + wizardTitle);
101 return wizardTitle;
102 }
103
104 /***
105 * Get the wizard directive.
106 *
107 * @return String
108 */
109 public String getSummary() {
110 return summary;
111 }
112
113
114 public String getSummaryNonRaw() {
115 return summaryNonRaw;
116 }
117
118
119 /***
120 * Sets the wizard title.
121 *
122 * @param wizardTitle String
123 */
124 public void setWizardTitle(String wizardTitle) {
125 this.wizardTitle = wizardTitle;
126 }
127
128 /***
129 * Sets the wizard directive.
130 *
131 * @param wizardDirective String
132 */
133 public void setSummary(String wizardDirective) {
134 this.summary = wizardDirective;
135 }
136
137
138 /***
139 * Sets the wizard class to use.
140 *
141 * @param wizardClass Class
142 */
143 public void setWizardClass(Class wizardClass) {
144 this.wizardClass = wizardClass;
145 }
146
147
148
149 public void setSummaryNonRaw(String summaryNonRaw) {
150 this.summaryNonRaw = summaryNonRaw;
151 }
152 /***
153 * Sets the wizard class to use
154 *
155 * @param wizardClass String
156 * @throws java.lang.ClassNotFoundException
157 * if unable to load the specified
158 * class with the Thread's current context class loader.
159 */
160 public void setWizardClass(final String wizardClass) throws ClassNotFoundException {
161 assert wizardClass != null;
162 setWizardClass(Thread.currentThread().getContextClassLoader().loadClass(wizardClass.trim()));
163 }
164 /***
165 * Retrieve the wizard class.
166 *
167 * @return Class
168 */
169 public Class getWizardClass() {
170 return wizardClass;
171 }
172
173 public boolean equals(Object obj) {
174 if (!(obj instanceof CreationBean)) {
175 return false;
176 }
177
178
179 CreationBeans other = (CreationBeans) obj;
180
181 if(this.beans.size() != other.beans.size()){
182 return false;
183 }
184
185 EqualsBuilder equals = new EqualsBuilder();
186 equals.append(wizardId, other.wizardId);
187 equals.append(wizardTitle, other.wizardTitle);
188 equals.append(summary, other.summary);
189 equals.append(wizardClass, other.wizardClass);
190
191 Iterator iter = this.beans.iterator();
192 Iterator otherIter = other.beans.iterator();
193
194 while (iter.hasNext()) {
195 CreationBean item = (CreationBean) iter.next();
196 CreationBean otherItem = (CreationBean)otherIter.next();
197 equals.append(item.getCompletionParts(), otherItem.getCompletionParts())
198 .append(item.getTargetId(), otherItem.getTargetId());
199 }
200
201 return equals.isEquals();
202 }
203
204 public int hashCode() {
205 HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(131, 91);
206 Iterator iter = this.beans.iterator();
207 hashCodeBuilder.append(summary);
208 hashCodeBuilder.append(wizardClass);
209 hashCodeBuilder.append(wizardId);
210 hashCodeBuilder.append(wizardTitle);
211 while (iter.hasNext()) {
212 CreationBean item = (CreationBean) iter.next();
213 hashCodeBuilder.append(item.getCompletionParts())
214 .append(item.getTargetId());
215 }
216
217 return hashCodeBuilder.toHashCode();
218 }
219
220 public String toShortString(){
221 ToStringBuilder builder = new ToStringBuilder(this);
222 Iterator iter = this.beans.iterator();
223 builder.append(wizardClass);
224 builder.append(wizardId);
225 builder.append(wizardTitle);
226 while (iter.hasNext()) {
227 CreationBean item = (CreationBean) iter.next();
228 builder.append("["+item.getPartId());
229 builder.append(item.getTargetId());
230 builder.append("parts?"+(item.getCompletionParts()!=null)+"]");
231 }
232 return builder.toString();
233 }
234
235 public String toString() {
236
237 ToStringBuilder builder = new ToStringBuilder(this);
238 Iterator iter = this.beans.iterator();
239 builder.append(summary);
240 builder.append(wizardClass);
241 builder.append(wizardId);
242 builder.append(wizardTitle);
243 while (iter.hasNext()) {
244 CreationBean item = (CreationBean) iter.next();
245 builder.append(item.toString());
246 }
247 return builder.toString();
248 }
249
250 public void add(CreationBean bean){
251 beans.add(bean);
252 }
253 public Iterator iterator(){
254 return beans.iterator();
255 }
256 public CreationBean get(int index){
257 return (CreationBean)beans.get(index);
258 }
259 public int size(){
260 return beans.size();
261 }
262
263 /***
264 * <p>Validate the properties that have been set for this HTTP request,
265 * and return an <code>ActionErrors</code> object that encapsulates any
266 * validation errors that have been found.
267 *
268 * @param returnValue the constructed ErrorCollection to populate into.
269 * @return ActionErrors
270 */
271 public ErrorCollection validate(final ErrorCollection returnValue) {
272 if (getWizardTitle() == null) {
273 returnValue.add("wizardTitle",
274 new ActionMessage("You must enter a wizard title"));
275 }
276
277 if (getWizardClass() == null) {
278 returnValue.add("wizardClass",
279 new ActionMessage(
280 "You need to specify a valid wizard class"));
281 }
282
283 if (!Wizard.class.isAssignableFrom(getWizardClass())) {
284 returnValue.add("wizardClass",
285 new ActionMessage(
286 "The wizard class specified does not implement the: " +
287 Wizard.class.getName()
288 + " interface"));
289 }
290 Iterator iter = this.beans.iterator();
291 while (iter.hasNext()) {
292 CreationBean item = (CreationBean) iter.next();
293 item.validate(returnValue);
294 }
295 return returnValue;
296 }
297
298 /***
299 * getCreationBeanByPartId
300 *
301 * @param string String
302 * @return boolean
303 */
304 public CreationBean getCreationBeanByPartId(String string) {
305 Iterator iter = iterator();
306 if(string == null){
307 return (CreationBean)iter.next();
308 }
309 Integer partId = new Integer(string);
310 iter.next();
311 while (iter.hasNext()) {
312 CreationBean item = (CreationBean) iter.next();
313 if(item.getPartId().equals(partId)){
314 return item;
315 }
316 }
317 return null;
318 }
319
320 public boolean hasCreationBeanByPartId(String string){
321 return getCreationBeanByPartId(string) != null;
322 }
323
324 /***
325 * removeBeanByPart
326 *
327 * @param string String
328 */
329 public void removeBeanByPart(String string) {
330 Integer partId = new Integer(string);
331 Iterator iter = iterator();
332 iter.next();
333 while (iter.hasNext()) {
334 CreationBean item = (CreationBean) iter.next();
335 if(item.getPartId().equals(partId)){
336 iter.remove();
337 return;
338 }
339 }
340 }
341
342
343 }