1
2
3
4
5
6
7
8
9
10
11 package com.sri.emo.wizard.creation.persistence;
12
13 import com.jcorporate.expresso.core.controller.ErrorCollection;
14 import com.jcorporate.expresso.core.db.DBException;
15 import com.sri.common.dbobj.RepositoryConversionException;
16 import com.sri.emo.controller.CreationWizardAction;
17 import com.sri.emo.dbobj.WizDefinition;
18 import com.sri.emo.wizard.creation.EmoCreationWizardFactory;
19 import com.sri.emo.wizard.creation.management.CreationEditor;
20 import com.sri.emo.wizard.creation.model.CreationBeans;
21 import com.sri.emo.wizard.creation.model.CreationPartsBean;
22 import com.sri.emo.wizard.creation.model.FieldCompletion;
23
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.*;
27 import com.sri.emo.wizard.creation.model.*;
28
29 /***
30 * @author Michael Rimov
31 */
32 public class CreationDBObjConverterImpl implements CreationDBObjConverter {
33
34
35 /***
36 * Default constructor.
37 */
38 public CreationDBObjConverterImpl() {
39 }
40
41 /***
42 * Converts a wizard definition to a completion bean.
43 *
44 * @param source WizDefinition
45 * @return CompletionBean
46 * @throws RepositoryConversionException upon generic conversion error.
47 */
48 public CreationBeans convertToBean(final com.sri.emo.dbobj.WizDefinition source) throws
49 RepositoryConversionException {
50 try {
51 System.out.println("CreationDBObjConverterImpl.convertToBean("+source+")");
52 CreationDefinition associatedData = (CreationDefinition) source.getAdditionalInfo();
53
54
55 CreationBeans cb = new CreationBeans();
56 if (source.getId() != null && source.getId().length() > 0) {
57 cb.setWizardId(new Integer(source.getId()));
58 }
59
60 cb.setSummary(source.getSummaryRaw());
61 cb.setSummaryNonRaw(source.getSummary());
62 cb.setWizardClass(source.getWizardClass());
63 cb.setWizardTitle(source.getWizName());
64
65 ErrorCollection ec = new ErrorCollection();
66 System.out.println("associatedData.getTargetNodeId() = " + associatedData.getTargetNodeId());
67
68 StringTokenizer tokenizer = new StringTokenizer(associatedData.getTargetNodeId(), ",");
69 int numBeans = tokenizer.countTokens()/2;
70 for(int b = 0; b < numBeans; b++){
71 CreationBean bean = new CreationBean();
72 String partIdStr = tokenizer.nextToken();
73 if(!partIdStr.equals("null")){
74 int partId = Integer.parseInt(partIdStr);
75 bean.setPartId(partId);
76 }
77 int targetId = Integer.parseInt(tokenizer.nextToken());
78 bean.setTargetId(targetId);
79 bean.initializeFromNodeId(ec);
80 cb.add(bean);
81
82 if (ec.size() > 0) {
83 String errorStrings[] = ec.getErrorStrings();
84 boolean needComma = false;
85 StringBuffer buffer = new StringBuffer();
86 for (int i = 0; i < errorStrings.length; i++) {
87 if (needComma) {
88 buffer.append(", ");
89 } else {
90 needComma = true;
91 }
92
93 buffer.append(errorStrings[i]);
94 }
95
96 throw new RepositoryConversionException(
97 "There were errors initializing completion wizard data: "
98 + buffer.toString());
99 }
100
101
102
103
104
105
106
107 List definitionFields = associatedData.getCompletionDetails();
108 for (Iterator i = definitionFields.iterator(); i.hasNext(); ) {
109 CreationDetails eachDetail = (CreationDetails) i.next();
110 if(!bean.hasPartsBeanFromPart(eachDetail.getPart())){
111
112
113 continue;
114 }
115 CreationPartsBean onePartBean = bean.
116 getPartsBeanFromPart(
117 eachDetail.getPart());
118
119 onePartBean.setDirective(eachDetail.getField(
120 CreationDetails.FLD_DIRECTIVE));
121 onePartBean.setHelpText(eachDetail.getField(CreationDetails.
122 FLD_HELPTEXT));
123
124
125 onePartBean.setCreateText(eachDetail.getField(
126 CreationDetails.FLD_CREATE_TXT));
127 onePartBean.setBrowseText(eachDetail.getField(
128 CreationDetails.FLD_BROWSE_TXT));
129 onePartBean.setSearchText(eachDetail.getField(
130 CreationDetails.FLD_SEARCH_TXT));
131
132 if ("Y".equals(eachDetail.getField(CreationDetails.
133 FLD_CREATE))) {
134 onePartBean.setCreate(true);
135 }
136 if ("Y".equals(eachDetail.getField(CreationDetails.
137 FLD_BROWSE))) {
138 onePartBean.setBrowse(true);
139 }
140 if ("Y".equals(eachDetail.getField(CreationDetails.
141 FLD_SEARCH))) {
142 onePartBean.setSearch(true);
143 }
144 if ("Y".equals(eachDetail.getField(CreationDetails.
145 FLD_REQUIRED))) {
146 onePartBean.setRequired(true);
147 }
148 String completionType = eachDetail.getField(CreationDetails.
149 FLD_COMPLETION);
150
151 if (completionType.equals(FieldCompletion.WIZARD.toString())) {
152 onePartBean.setFieldCompletion(FieldCompletion.WIZARD);
153 } else if (completionType.equals(FieldCompletion.
154 NOT_INCLUDED.toString())) {
155 onePartBean.setFieldCompletion(FieldCompletion.
156 NOT_INCLUDED);
157 } else {
158 throw new RepositoryConversionException(
159 "Unable to determine field completion type for value: "
160 + completionType +
161 getLocationForErrorMessage(bean, cb, onePartBean));
162 }
163
164
165
166
167
168
169 onePartBean.setMaxEntries(eachDetail.getFieldInt(
170 CreationDetails.FLD_MAX_ENTRIES));
171 onePartBean.setMinEntries(eachDetail.getFieldInt(
172 CreationDetails.FLD_MIN_ENTRIES));
173
174 if ((!eachDetail.getDataField(CreationDetails.
175 FLD_SINGLE_ENTRY).isNull())) {
176 onePartBean.setSingleEntry(convertFieldToBoolean(bean, cb,
177 onePartBean, eachDetail,
178 CreationDetails.FLD_SINGLE_ENTRY));
179 }
180 }
181 }
182 return cb;
183 } catch (DBException ex) {
184 throw new RepositoryConversionException("Unable to convert wizard definition: " + source.toString()
185 + " to creation bean", ex);
186 } catch (ClassNotFoundException ex) {
187 throw new RepositoryConversionException("Unable to locate class specified in wizard definition: "
188 + ex.getMessage(), ex);
189 }
190 }
191
192 /***
193 * Converts a text y/n field to boolean.
194 *
195 * @param cb CompletionBean the parent completion bean.
196 * @param partsBean CompletionPartsBean the parts bean we're populating.
197 * @param details CompletionDetails the details data object.
198 * @param fieldName String the field name we're converting.
199 * @return boolean true or false.
200 * @throws DBException upon error.
201 * @throws RepositoryConversionException if the data was not y/n
202 */
203 private boolean convertFieldToBoolean(final CreationBean cb,
204 final CreationBeans beans,
205 final CreationPartsBean partsBean,
206 final CreationDetails details, final String fieldName) throws DBException,
207 RepositoryConversionException {
208 String freeText = details.getField(fieldName);
209 if (freeText.equalsIgnoreCase("Y")) {
210 return true;
211 } else if (freeText.equalsIgnoreCase("N")) {
212 return false;
213 } else {
214 throw new RepositoryConversionException("Invalid freetext value of: " + freeText
215 + " it should be 'y' or 'n' (case insensitive) " + getLocationForErrorMessage(cb, beans, partsBean));
216 }
217
218 }
219
220 /***
221 * Creates detailed error messages for throwing conversion errors.
222 *
223 * @param cb CompletionBean the completion bean we're populating.
224 * @param partsBean CompletionPartsBean the current part we're dealing with.
225 * @return String the error message including detailed location information.
226 * @throws DBException upon error querying the part.
227 */
228 private String getLocationForErrorMessage(final CreationBean cb,
229 final CreationBeans beans,
230 final CreationPartsBean partsBean) throws DBException {
231 StringBuffer returnValue = new StringBuffer("[ For Part Id: ");
232 returnValue.append(partsBean.getPart().getId());
233 returnValue.append("in completion Wizard: ");
234 returnValue.append(beans.getWizardTitle());
235 returnValue.append("(");
236 returnValue.append(beans.getWizardId());
237 returnValue.append(") ]");
238
239 return returnValue.toString();
240 }
241
242 /***
243 * Converts a Completion Bean to a Wiz Definition bean.
244 *
245 * @param source CompletionBean the completion bean.
246 * @return WizDefinition constructed wizard definition.
247 * @throws RepositoryConversionException upon generic conversion error.
248 */
249 public com.sri.emo.dbobj.WizDefinition convertToDBObject(final CreationBeans sourceBeans) throws
250 RepositoryConversionException {
251
252 boolean adding = (sourceBeans.getWizardId() == null);
253
254 try {
255 WizDefinition wizdef = new WizDefinition();
256
257 if (!adding) {
258 wizdef.setId(sourceBeans.getWizardId().toString());
259 wizdef.retrieve();
260 }
261
262
263 wizdef.setField(WizDefinition.FLD_ADDITIONAL_INFO, CreationDefinition.class.getName());
264 wizdef.setField(WizDefinition.FLD_SUMMARY, sourceBeans.getSummary());
265 wizdef.setField(WizDefinition.FLD_WIZARD, sourceBeans.getWizardClass().getName());
266 wizdef.setField(WizDefinition.FLD_NAME, sourceBeans.getWizardTitle());
267
268 if (adding) {
269 wizdef.set(WizDefinition.FLD_EDITOR_CONTROLLER, CreationEditor.class.getName());
270 wizdef.set(WizDefinition.FLD_FACTORY, EmoCreationWizardFactory.class.getName());
271 wizdef.set(WizDefinition.FLD_CONTROLLER, CreationWizardAction.class.getName());
272 }
273
274
275 CreationDefinition completionDefinition = (CreationDefinition) wizdef.getAdditionalInfo();
276
277 if (sourceBeans.getCreationBean(0).getTargetId() == null) {
278 throw new RepositoryConversionException(sourceBeans.toString() + " must have a target id set");
279 }
280
281 StringBuffer targetNodes = new StringBuffer();
282 Iterator iter = sourceBeans.iterator();
283 while (iter.hasNext()) {
284 CreationBean item = (CreationBean) iter.next();
285 targetNodes.append(item.getPartId());
286 targetNodes.append(",");
287 targetNodes.append(item.getTargetId());
288 if(iter.hasNext()){
289 targetNodes.append(",");
290 }
291 }
292
293 completionDefinition.setField(CreationDefinition.FLD_TARGET_NODE, targetNodes.toString());
294
295 int order = 0;
296 iter = sourceBeans.iterator();
297 while (iter.hasNext()) {
298 CreationBean source = (CreationBean) iter.next();
299 if(source.getCompletionParts() == null){
300 continue;
301 }
302 for (Iterator i = source.getCompletionParts().iterator();
303 i.hasNext(); ) {
304
305 CreationPartsBean onePart = (CreationPartsBean) i.next();
306 CreationDetails details = new CreationDetails();
307 details.clear();
308 details.set(CreationDetails.FLD_PART_ID,
309 onePart.getPart().getId());
310
311 if (!adding) {
312 details.set(CreationDetails.FLD_WIZARD_ID, wizdef.getId());
313 if (!details.find()) {
314 throw new IncompleteDetailsException(
315 "Unable to locate completion details of with wizard id of: "
316 + wizdef.getId() + " and part id of: " +
317 onePart.getPart().getId(), details);
318 }
319 }
320
321
322
323
324 if (onePart.getFieldCompletion() == null) {
325 details.set(CreationDetails.FLD_COMPLETION,
326 FieldCompletion.NOT_INCLUDED.toString());
327 } else {
328 details.set(CreationDetails.FLD_COMPLETION,
329 onePart.getFieldCompletion().toString());
330
331 }
332
333 if (onePart.getDirective() != null) {
334 details.set(CreationDetails.FLD_DIRECTIVE,
335 onePart.getDirective().toString());
336 }
337
338 if (onePart.isFreeTextAllowed()) {
339 details.set(CreationDetails.FLD_FREETEXT, "Y");
340 } else {
341 details.set(CreationDetails.FLD_FREETEXT, "N");
342 }
343
344
345 if (onePart.getCreateText() != null) {
346 details.set(CreationDetails.FLD_CREATE_TXT,
347 onePart.getCreateText().toString());
348 }
349 if (onePart.getBrowseText() != null) {
350 details.set(CreationDetails.FLD_BROWSE_TXT,
351 onePart.getBrowseText().toString());
352 }
353 if (onePart.getSearchText() != null) {
354 details.set(CreationDetails.FLD_SEARCH_TXT,
355 onePart.getSearchText().toString());
356 }
357
358 if (onePart.isCreate()) {
359 details.set(CreationDetails.FLD_CREATE, "Y");
360 } else {
361 details.set(CreationDetails.FLD_CREATE, "N");
362 }
363 if (onePart.isBrowse()) {
364 details.set(CreationDetails.FLD_BROWSE, "Y");
365 } else {
366 details.set(CreationDetails.FLD_BROWSE, "N");
367 }
368 if (onePart.isSearch()) {
369 details.set(CreationDetails.FLD_SEARCH, "Y");
370 } else {
371 details.set(CreationDetails.FLD_SEARCH, "N");
372 }
373 if (onePart.isRequired()) {
374 details.set(CreationDetails.FLD_REQUIRED, "Y");
375 } else {
376 details.set(CreationDetails.FLD_REQUIRED, "N");
377 }
378
379 details.set(CreationDetails.FLD_HELPTEXT,
380 onePart.getHelpText());
381 if (onePart.getMaxEntries() == null) {
382 details.setField(CreationDetails.FLD_MAX_ENTRIES,
383 (String)null);
384 } else {
385 details.setField(CreationDetails.FLD_MAX_ENTRIES,
386 onePart.getMaxEntries().intValue());
387 }
388
389 if (onePart.getMinEntries() != null) {
390 details.setField(CreationDetails.FLD_MIN_ENTRIES,
391 onePart.getMinEntries().intValue());
392 } else {
393 details.setField(CreationDetails.FLD_MIN_ENTRIES,
394 (String)null);
395 }
396
397 details.setField(CreationDetails.FLD_PART_ORDER, order);
398
399 if (onePart.isSingleEntry()) {
400 details.set(CreationDetails.FLD_SINGLE_ENTRY, "Y");
401 } else {
402 details.set(CreationDetails.FLD_SINGLE_ENTRY, "N");
403 }
404
405 completionDefinition.addCompletionDetail(details);
406 order++;
407 }
408 }
409
410 return wizdef;
411 } catch (DBException ex) {
412 throw new RepositoryConversionException("Error converting to a database object from: " + sourceBeans.toString(),
413 ex);
414 }
415
416 }
417 }