View Javadoc

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