1
2
3
4
5
6
7
8
9
10
11
12 package com.sri.emo.dbobj;
13
14 import java.util.List;
15
16 import com.jcorporate.expresso.core.db.DBConnection;
17 import com.jcorporate.expresso.core.db.DBException;
18 import com.jcorporate.expresso.core.dbobj.DBField;
19 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
20 import com.jcorporate.expresso.core.security.ReadOnlyUser;
21
22 /***
23 * WizDecisionSet represents a set of responses that make up a "row"
24 * in the decision matrix.
25 *
26 * @author Michael Rimov
27 */
28
29 public class WizDecisionSet extends SecuredDBObject {
30
31 /***
32 *
33 */
34 private static final long serialVersionUID = 1L;
35
36 /***
37 * Public constant for access to field
38 * "Item ID"
39 */
40 public static final String FLD_ID = "Id";
41
42 /***
43 * Public constant for access to field
44 * "Wizard Id"
45 */
46 public static final String FLD_WIZID = "WizId";
47
48 /***
49 * Public constant for access to field
50 * "Decision Result"
51 */
52 public static final String FLD_DECISIONRESULT = "DecisionResult";
53
54 /***
55 * Public constant for access to field
56 * "Example Title"
57 */
58 public static final String FLD_DECISIONTITLE = "DecisionTitle";
59
60
61 /***
62 * Creates an instance of WizDecisionSet. Call setRequestingUid()
63 * and setDataContext() before using.
64 *
65 * @throws DBException upon initialization exception.
66 * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#SecuredDBObject
67 */
68 public WizDecisionSet() throws DBException {
69 }
70
71 /***
72 * Retrieves the node associated with this decision set.
73 *
74 * @return Node
75 * @throws DBException
76 */
77 public Node getDecisionNode() throws DBException {
78 String decisionResult = getField(FLD_DECISIONRESULT);
79 if (decisionResult == null || decisionResult.length() == 0) {
80 return null;
81 }
82
83 Node n = new Node();
84 n.setField(Node.NODE_ID, getField(FLD_DECISIONRESULT));
85 n.retrieve();
86 return n;
87 }
88
89 /***
90 * <p/>
91 * Constructor that sets a connection as the object is created - typically
92 * this is used when a particular DBConnection is required for
93 * the purposes of
94 * maintaining a database transaction. If a specific connection
95 * is not used,
96 * there is no way to use commit() and rollback() in the event of
97 * failure, as a
98 * different DBConnection might be used for each phase of the transaction.
99 * Critial sections should therefore explicity request a DBConnection
100 * from the
101 * connection pool and pass it to each of the DB objects in that section.
102 * </p>
103 * <p>This constructor is neceesary to work with otherDBMap and transaction
104 * capabilities</p>
105 *
106 * @param dbConnection The DBConnection to utilize
107 * @param uid User Uid.
108 * @throws DBException upon initialization error
109 */
110 public WizDecisionSet(DBConnection dbConnection, final int uid) throws
111 DBException {
112 super(dbConnection, uid);
113 }
114
115
116
117 /***
118 * Creates an instance of WizDecisionSet automatically setting the
119 * object's requesting user.
120 *
121 * @param user The user's security context.
122 * @throws DBException upon construction error
123 */
124 public WizDecisionSet(final ReadOnlyUser user) throws DBException {
125 super(user);
126 }
127
128 /***
129 * One time intiialization of all the field types. Set all specifications
130 * here such as field names, table name, friendly name of the data object,
131 * characterset, etc.
132 *
133 * @throws DBException upon error
134 */
135 protected void setupFields() throws DBException {
136 super.setupFields();
137 setTargetTable("WIZDECISIONSET");
138
139 setDescription("Wizard Decision Set");
140 setCharset("ISO-8859-1");
141
142 addField(FLD_ID, DBField.AUTOINC_TYPE, 0, false, "Item ID");
143
144 addKey(FLD_ID);
145
146 addField(FLD_WIZID, DBField.INTEGER_TYPE, 0, false, "Wizard Id");
147
148 setLookupObject(FLD_WIZID, WizDefinition.FLD_ID);
149
150 addField(FLD_DECISIONRESULT, DBField.INTEGER_TYPE, 0, true, "Result Node ID");
151
152 setLookupObject(FLD_DECISIONRESULT, Node.NODE_ID);
153
154 addVirtualField(FLD_DECISIONTITLE, DBField.VARCHAR_TYPE, 254, "Example Title");
155
156 addIndex("WIZDECISION_MAP", FLD_WIZID, false);
157
158 addDetail(WizDecisionEntities.class.getName(), FLD_ID, WizDecisionEntities.FLD_SETID);
159 }
160
161 /***
162 * Retrieve all details for this object.
163 * @return
164 * @throws DBException
165 */
166 public List getDecisionEntities() throws DBException {
167 WizDecisionEntities queryObject = new WizDecisionEntities();
168 queryObject.setField(WizDecisionEntities.FLD_SETID, getField(FLD_ID));
169
170 return queryObject.searchAndRetrieveList();
171 }
172
173 /***
174 * Adds a decision entry into this 'set' of decisions.
175 * <p>Assertions:</p>
176 * <p>wizStepIds[].length == pickListIds[].length</p>
177 * <p>wizStepsIds[i] maps to pickListIds[i]</p>
178 * <p>This object's local connection MUST be already set: ie this occurs
179 * within a transaction</p>
180 *
181 * @param wizStepIds int[] the step ids corresponding to the picklist ids.
182 * @param pickListIds String[] the picklist ids corresponding to
183 * the step ids.
184 * @throws DBException upon add() error.
185 */
186 public void addDecisionEntity(final int wizStepIds[],
187 final String pickListIds[])
188 throws DBException {
189 assert wizStepIds.length == pickListIds.length;
190 DBConnection connection = getLocalConnection();
191 if (connection == null) {
192 throw new IllegalStateException("You must set the DecisionSet's"
193 +
194 " connection first before adding decision entities");
195 }
196
197 for (int i = 0; i < wizStepIds.length; i++) {
198 addSingleDecisionEntity(connection, wizStepIds[i], pickListIds[i]);
199 }
200 }
201
202 /***
203 * Adds a single decision entity to the set.
204 *
205 * @param connection DBConnection the database connection
206 * @param wizStepId int the step id.
207 * @param pickListId String the corresponding picklist string
208 * @throws DBException upon addOrUpdate() error.
209 */
210 public void addSingleDecisionEntity(DBConnection connection, int wizStepId,
211 String pickListId) throws DBException {
212 assert wizStepId > 0;
213 assert pickListId != null && pickListId.length() > 0;
214
215 WizDecisionEntities decisions = new WizDecisionEntities(connection);
216 decisions.setField(WizDecisionEntities.FLD_SETID, getField(WizDecisionSet.FLD_ID));
217 decisions.setField(WizDecisionEntities.FLD_STEPID, wizStepId);
218 decisions.setField(WizDecisionEntities.FLD_STEPVALUE, pickListId);
219 decisions.add();
220 }
221
222
223 /***
224 * Set the wizard id for this decision set.
225 *
226 * @param newValue int the new wizard id (integer > 0)
227 * @throws DBException upon setField() error
228 */
229 public void setWizardId(int newValue) throws DBException {
230 setField(FLD_WIZID, newValue);
231 }
232
233 /***
234 * Set the wizard id for this decision set.
235 *
236 * @param newValue int the new wizard id (integer > 0)
237 * @throws DBException upon setField() error
238 */
239 public void setWizardId(String newValue) throws DBException {
240 setField(FLD_WIZID, newValue);
241 }
242
243 /***
244 * Retrieve the wizard id for this decision set.
245 *
246 * @return Integer or null if it is not set.
247 * @throws DBException upon getDataField() generated error.
248 */
249 public Integer getWizardId() throws DBException {
250 if (getDataField(FLD_WIZID).isNull()) {
251 return null;
252 } else {
253 return new Integer(getFieldInt(FLD_WIZID));
254 }
255 }
256
257 /***
258 * Returns a hash code value for the object.
259 *
260 * @return a hash code value for this object.
261 */
262 public int hashCode() {
263 try {
264 return getField(FLD_ID).hashCode();
265 } catch (DBException ex) {
266 return 0;
267 }
268 }
269
270 /***
271 * Indicates whether some other object is "equal to" this one.
272 * This particular implementation delegates equality to the base
273 * DBObject class which has per-field comparison.
274 *
275 * @param obj the reference object with which to compare.
276 * @return <code>true</code> if this object is the same as the obj
277 * argument; <code>false</code> otherwise.
278 */
279 public boolean equals(final Object obj) {
280 return super.equals(obj);
281 }
282
283 /***
284 * Override of getField to provide for virtual field 'decision title'
285 *
286 * @param fieldName the name of the field to retrieve
287 * @return Object or null if the field was null
288 * @throws DBException upon error
289 */
290 public String getField(String fieldName) throws DBException {
291 if (FLD_DECISIONTITLE.equals(fieldName)) {
292 Node n = getDecisionNode();
293 if (n == null) {
294 return "";
295 }
296
297 return n.getNodeTitle();
298
299 } else {
300 return super.getField(fieldName);
301 }
302 }
303
304 public void setDecisionResult(int parameterValue) throws DBException {
305 setField(FLD_DECISIONRESULT, parameterValue);
306 }
307
308 public String getDecisionResult() throws DBException {
309 return getField(FLD_DECISIONRESULT);
310 }
311 }