1 package com.sri.emo.wizard.creation.persistence;
2
3 import com.jcorporate.expresso.core.db.DBConnection;
4 import com.jcorporate.expresso.core.db.DBException;
5 import com.jcorporate.expresso.core.dbobj.DBField;
6 import com.jcorporate.expresso.core.dbobj.SecurDBObject;
7 import com.jcorporate.expresso.core.security.ReadOnlyUser;
8 import com.sri.emo.dbobj.WizDefinition;
9 import com.sri.emo.wizard.persistence.AdditionalInfo;
10
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14
15 /***
16 * Gateway class to the completion definition. Provides a slightly different
17 * definition that wizard
18 *
19 * @author Michael Rimov
20 * @version 1.0
21 */
22 public class CreationDefinition extends SecurDBObject implements AdditionalInfo {
23
24
25 public static final String FLD_ID = "Id";
26
27 public static final String FLD_TARGET_NODE = "TargetNode";
28
29 public List completionDetails = new ArrayList();
30
31 public static final String TABLE_NAME = "EMOCREATIONDEF";
32
33 /***
34 * Retrieve the target node id.
35 *
36 * @return int
37 * @throws DBException
38 */
39 public String getTargetNodeId() throws DBException {
40 return this.getField(FLD_TARGET_NODE);
41 }
42
43
44 /***
45 * Sets the wizard definition.
46 *
47 * @param definition WizDefinition
48 * @throws DBException
49 */
50 public void setWizardDefinition(WizDefinition definition) throws DBException {
51 this.setField(FLD_ID, definition.getId());
52 }
53
54 /***
55 * Retrieve the wizard definition.
56 *
57 * @return WizDefinition
58 * @throws DBException
59 */
60 public WizDefinition getWizardDefinition() throws DBException {
61 WizDefinition wizdef = new WizDefinition();
62 wizdef.setId(this.getField(FLD_ID));
63 wizdef.retrieve();
64 return wizdef;
65 }
66
67 /***
68 * Default constructor to create a new object.
69 *
70 * @throws DBException upon construction error.
71 */
72 public CreationDefinition() throws DBException {
73 super();
74 }
75
76 /***
77 * Constructor that takes a <tt>DBConnection</tt> for use inside a transaction.
78 *
79 * @param newConnection DBConnection the transaction connection.
80 * @throws DBException upon construction error.
81 */
82 public CreationDefinition(DBConnection newConnection) throws DBException {
83 super(newConnection);
84 }
85
86 /***
87 * Constructor taht takes a <tt>ReadOnlyUser</tt> security context. This
88 * is most often set by the default constructor, so you only want to do this
89 * if you want to use special security permissions (such as Admin or SuperUser)
90 *
91 * @param readOnlyUser ReadOnlyUser the ReadOnlyUser instance.
92 * @throws DBException upon construction error.
93 */
94 public CreationDefinition(ReadOnlyUser readOnlyUser) throws DBException {
95 super(readOnlyUser);
96 }
97
98
99 /***
100 * Retrieve a list of all completion details.
101 *
102 * @return List java.util.List of <tt>CompletionDetail</tt> objects.
103 * @throws DBException upon query error.
104 */
105 public List getCompletionDetails() throws DBException {
106 if (getStatus().equals(STATUS_NEW) && this.get(FLD_ID) == null) {
107 return completionDetails;
108 } else {
109 CreationDetails details = new CreationDetails();
110 details.set(CreationDetails.FLD_WIZARD_ID, this.get(FLD_ID));
111 return (List) details.searchAndRetrieveList(CreationDetails.FLD_PART_ID);
112 }
113 }
114
115 /***
116 * Adds a completion detail to the definition.
117 *
118 * @param oneDetail CompletionDetails
119 * @throws DBException
120 */
121 public void addCompletionDetail(CreationDetails oneDetail) throws DBException {
122 if (this.getStatus().equals(STATUS_NEW) && this.get(FLD_ID) == null) {
123 completionDetails.add(oneDetail);
124 } else {
125 oneDetail.set(CreationDetails.FLD_WIZARD_ID, this.get(FLD_ID));
126 oneDetail.update();
127 }
128 }
129
130 /***
131 * Override of add that also adds the completion details as well.
132 *
133 * @throws DBException
134 */
135 public void add() throws DBException {
136 super.add();
137
138 if (completionDetails == null || completionDetails.size() == 0) {
139 return;
140 }
141
142 DBConnection connection = this.getLocalConnection();
143 boolean setConnection = false;
144 if (connection != null) {
145 setConnection = true;
146 }
147
148 for (Iterator i = completionDetails.iterator(); i.hasNext();) {
149 CreationDetails oneDetail = (CreationDetails) i.next();
150 if (setConnection) {
151 oneDetail.setConnection(connection);
152 }
153 oneDetail.set(CreationDetails.FLD_WIZARD_ID, this.get(FLD_ID));
154 oneDetail.add();
155 }
156
157 completionDetails = null;
158 }
159
160
161 /***
162 * Method to set up the fields for this database object.
163 *
164 * @throws DBException If there is an error setting up the fields as
165 * requested. For example, if a field allowing null is requested as
166 * part of the key
167 */
168 protected void setupFields() throws DBException {
169 this.setTargetTable(TABLE_NAME);
170 setDescription("Creation Wizard Additional Info");
171 setCharset("ISO-8859-1");
172
173 this.addField(FLD_ID, DBField.INT_TYPE, 0, false, "Wizard Id");
174 this.addField(FLD_TARGET_NODE, DBField.TEXT_TYPE, 0, false, "Target Node Id");
175
176 this.addDetail(CreationDetails.class.getName(), FLD_ID, CreationDetails.FLD_WIZARD_ID);
177 this.addKey(FLD_ID);
178 }
179
180 /***
181 * Attribute to know what the id field of the additional information
182 * database object is.
183 *
184 * @return String
185 * @todo Implement this com.sri.emo.dbobj.wizards.AdditionalInfo method
186 */
187 public String getIdField() {
188 return FLD_ID;
189 }
190 }