1
2
3
4
5
6
7
8
9
10
11 package com.sri.emo.wizard.completion.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.CompletionWizardAction;
17 import com.sri.emo.dbobj.WizDefinition;
18 import com.sri.emo.wizard.completion.EmoCompletionFactory;
19 import com.sri.emo.wizard.completion.management.CompletionEditor;
20 import com.sri.emo.wizard.completion.model.CompletionBean;
21 import com.sri.emo.wizard.completion.model.CompletionPartsBean;
22 import com.sri.emo.wizard.completion.model.FieldCompletion;
23
24 import java.util.Iterator;
25 import java.util.List;
26
27 /***
28 * @author Michael Rimov
29 */
30 public class CompletionDBObjConverterImpl implements CompletionDBObjConverter {
31
32
33 /***
34 * Default constructor.
35 */
36 public CompletionDBObjConverterImpl() {
37 }
38
39 /***
40 * Converts a wizard definition to a completion bean.
41 *
42 * @param source WizDefinition
43 * @return CompletionBean
44 * @throws RepositoryConversionException upon generic conversion error.
45 */
46 public CompletionBean convertToBean(final com.sri.emo.dbobj.WizDefinition source) throws
47 RepositoryConversionException {
48 try {
49 CompletionDefinition associatedData = (CompletionDefinition) source.getAdditionalInfo();
50
51
52 CompletionBean cb = new CompletionBean();
53 if (source.getId() != null && source.getId().length() > 0) {
54 cb.setWizardId(new Integer(source.getId()));
55 }
56
57 cb.setSummary(source.getSummaryRaw());
58 cb.setTargetId(associatedData.getTargetNodeId());
59 cb.setWizardClass(source.getWizardClass());
60 cb.setWizardTitle(source.getWizName());
61
62 ErrorCollection ec = new ErrorCollection();
63 cb.initializeFromNodeId(ec);
64 if (ec.size() > 0) {
65 String errorStrings[] = ec.getErrorStrings();
66 boolean needComma = false;
67 StringBuffer buffer = new StringBuffer();
68 for (int i = 0; i < errorStrings.length; i++) {
69 if (needComma) {
70 buffer.append(", ");
71 } else {
72 needComma = true;
73 }
74
75 buffer.append(errorStrings[i]);
76 }
77
78 throw new RepositoryConversionException("There were errors initializing completion wizard data: "
79 + buffer.toString());
80 }
81
82 List definitionFields = associatedData.getCompletionDetails();
83 for (Iterator i = definitionFields.iterator(); i.hasNext();) {
84 CompletionDetails eachDetail = (CompletionDetails) i.next();
85 CompletionPartsBean onePartBean = cb.getPartsBeanFromPart(eachDetail.getPart());
86
87 onePartBean.setDirective(eachDetail.getField(CompletionDetails.FLD_DIRECTIVE));
88 onePartBean.setHelpText(eachDetail.getField(CompletionDetails.FLD_HELPTEXT));
89
90 String completionType = eachDetail.getField(CompletionDetails.FLD_COMPLETION);
91
92 if (completionType.equals(FieldCompletion.WIZARD.toString())) {
93 onePartBean.setFieldCompletion(FieldCompletion.WIZARD);
94 } else if (completionType.equals(FieldCompletion.FIXED.toString())) {
95 onePartBean.setFieldCompletion(FieldCompletion.FIXED);
96 } else {
97 throw new RepositoryConversionException("Unable to determine field completion type for value: "
98 + completionType + getLocationForErrorMessage(cb, onePartBean));
99 }
100
101
102
103
104
105
106 onePartBean.setMaxEntries(eachDetail.getFieldInt(CompletionDetails.FLD_MAX_ENTRIES));
107 onePartBean.setMinEntries(eachDetail.getFieldInt(CompletionDetails.FLD_MIN_ENTRIES));
108
109 if ((!eachDetail.getDataField(CompletionDetails.FLD_SINGLE_ENTRY).isNull())) {
110 onePartBean.setSingleEntry(convertFieldToBoolean(cb, onePartBean, eachDetail,
111 CompletionDetails.FLD_SINGLE_ENTRY));
112 }
113 }
114
115 return cb;
116 } catch (DBException ex) {
117 throw new RepositoryConversionException("Unable to convert wizard definition: " + source.toString()
118 + " to completion bean", ex);
119 } catch (ClassNotFoundException ex) {
120 throw new RepositoryConversionException("Unable to locate class specified in wizard definition: "
121 + ex.getMessage(), ex);
122 }
123 }
124
125 /***
126 * Converts a text y/n field to boolean.
127 *
128 * @param cb CompletionBean the parent completion bean.
129 * @param partsBean CompletionPartsBean the parts bean we're populating.
130 * @param details CompletionDetails the details data object.
131 * @param fieldName String the field name we're converting.
132 * @return boolean true or false.
133 * @throws DBException upon error.
134 * @throws RepositoryConversionException if the data was not y/n
135 */
136 private boolean convertFieldToBoolean(final CompletionBean cb, final CompletionPartsBean partsBean,
137 final CompletionDetails details, final String fieldName) throws DBException,
138 RepositoryConversionException {
139 String freeText = details.getField(fieldName);
140 if (freeText.equalsIgnoreCase("Y")) {
141 return true;
142 } else if (freeText.equalsIgnoreCase("N")) {
143 return false;
144 } else {
145 throw new RepositoryConversionException("Invalid freetext value of: " + freeText
146 + " it should be 'y' or 'n' (case insensitive) " + getLocationForErrorMessage(cb, partsBean));
147 }
148
149 }
150
151 /***
152 * Creates detailed error messages for throwing conversion errors.
153 *
154 * @param cb CompletionBean the completion bean we're populating.
155 * @param partsBean CompletionPartsBean the current part we're dealing with.
156 * @return String the error message including detailed location information.
157 * @throws DBException upon error querying the part.
158 */
159 private String getLocationForErrorMessage(final CompletionBean cb,
160 final CompletionPartsBean partsBean) throws DBException {
161 StringBuffer returnValue = new StringBuffer("[ For Part Id: ");
162 returnValue.append(partsBean.getPart().getId());
163 returnValue.append("in completion Wizard: ");
164 returnValue.append(cb.getWizardTitle());
165 returnValue.append("(");
166 returnValue.append(cb.getWizardId());
167 returnValue.append(") ]");
168
169 return returnValue.toString();
170 }
171
172 /***
173 * Converts a Completion Bean to a Wiz Definition bean.
174 *
175 * @param source CompletionBean the completion bean.
176 * @return WizDefinition constructed wizard definition.
177 * @throws RepositoryConversionException upon generic conversion error.
178 */
179 public com.sri.emo.dbobj.WizDefinition convertToDBObject(final CompletionBean source) throws
180 RepositoryConversionException {
181
182 boolean adding = (source.getWizardId() == null);
183
184 try {
185 WizDefinition wizdef = new WizDefinition();
186
187 if (!adding) {
188 wizdef.setId(source.getWizardId().toString());
189 wizdef.retrieve();
190 }
191
192
193 wizdef.setField(WizDefinition.FLD_ADDITIONAL_INFO, CompletionDefinition.class.getName());
194 wizdef.setField(WizDefinition.FLD_SUMMARY, source.getSummary());
195 wizdef.setField(WizDefinition.FLD_WIZARD, source.getWizardClass().getName());
196 wizdef.setField(WizDefinition.FLD_NAME, source.getWizardTitle());
197
198 if (adding) {
199 wizdef.set(WizDefinition.FLD_EDITOR_CONTROLLER, CompletionEditor.class.getName());
200 wizdef.set(WizDefinition.FLD_FACTORY, EmoCompletionFactory.class.getName());
201 wizdef.set(WizDefinition.FLD_CONTROLLER, CompletionWizardAction.class.getName());
202 }
203
204
205 CompletionDefinition completionDefinition = (CompletionDefinition) wizdef.getAdditionalInfo();
206
207 if (source.getTargetId() == null) {
208 throw new RepositoryConversionException(source.toString() + " must have a target id set");
209 }
210
211 completionDefinition.setField(CompletionDefinition.FLD_TARGET_NODE, source.getTargetId().intValue());
212
213
214 int order = 0;
215 for (Iterator i = source.getCompletionParts().iterator(); i.hasNext();) {
216
217 CompletionPartsBean onePart = (CompletionPartsBean) i.next();
218 CompletionDetails details = new CompletionDetails();
219 details.clear();
220 details.set(CompletionDetails.FLD_PART_ID, onePart.getPart().getId());
221
222 if (!adding) {
223 details.set(CompletionDetails.FLD_WIZARD_ID, wizdef.getId());
224 if (!details.find()) {
225 throw new IncompleteDetailsException(
226 "Unable to locate completion details of with wizard id of: "
227 + wizdef.getId() + " and part id of: " + onePart.getPart().getId(), details);
228 }
229 }
230
231
232
233
234 if (onePart.getFieldCompletion() == null) {
235 details.set(CompletionDetails.FLD_COMPLETION, FieldCompletion.FIXED.toString());
236 } else {
237 details.set(CompletionDetails.FLD_COMPLETION, onePart.getFieldCompletion().toString());
238
239 }
240
241 if (onePart.getDirective() != null) {
242 details.set(CompletionDetails.FLD_DIRECTIVE, onePart.getDirective().toString());
243 }
244
245 if (onePart.isFreeTextAllowed()) {
246 details.set(CompletionDetails.FLD_FREETEXT, "Y");
247 } else {
248 details.set(CompletionDetails.FLD_FREETEXT, "N");
249 }
250
251
252 details.set(CompletionDetails.FLD_HELPTEXT, onePart.getHelpText());
253 if (onePart.getMaxEntries() == null) {
254 details.setField(CompletionDetails.FLD_MAX_ENTRIES, (String) null);
255 } else {
256 details.setField(CompletionDetails.FLD_MAX_ENTRIES, onePart.getMaxEntries().intValue());
257 }
258
259 if (onePart.getMinEntries() != null) {
260 details.setField(CompletionDetails.FLD_MIN_ENTRIES, onePart.getMinEntries().intValue());
261 } else {
262 details.setField(CompletionDetails.FLD_MIN_ENTRIES, (String) null);
263 }
264
265 details.setField(CompletionDetails.FLD_PART_ORDER, order);
266
267 if (onePart.isSingleEntry()) {
268 details.set(CompletionDetails.FLD_SINGLE_ENTRY, "Y");
269 } else {
270 details.set(CompletionDetails.FLD_SINGLE_ENTRY, "N");
271 }
272
273 completionDefinition.addCompletionDetail(details);
274 order++;
275 }
276
277
278 return wizdef;
279 } catch (DBException ex) {
280 throw new RepositoryConversionException("Error converting to a database object from: " + source.toString(),
281 ex);
282 }
283 }
284 }