KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > acl > db > ObjectDBData


1 package de.webman.acl.db;
2
3 import java.sql.ResultSet JavaDoc;
4 import java.sql.SQLException JavaDoc;
5 import com.teamkonzept.db.TKDBVectorData;
6 import com.teamkonzept.db.TKDBTableData;
7 import com.teamkonzept.db.TKQuery;
8 import com.teamkonzept.lib.TKVector;
9 import de.webman.acl.WMObject;
10
11 /**
12  * $Header: /cvsroot/webman-cms/source/webman/de/webman/acl/db/ObjectDBData.java,v 1.1 2001/08/20 08:25:08 mischa Exp $
13  *
14  * Base class of data containers for persistent objects. Maps a primary key to
15  * an object identifier, provides object data transfer, and handles collections.
16  *
17  * @version 0.10
18  * @since 0.10
19  * @author © 2000 Team-Konzept
20  */

21 public abstract class ObjectDBData
22     extends TKDBVectorData
23 {
24     // Attributes
25

26     /**
27      * Specifies wether the data container may be considered as empty.
28      */

29     private boolean empty = true;
30
31     /**
32      * Specifies wether the data container may be considered as empty.
33      */

34     private boolean ignore = false;
35
36     /**
37      * The additional query class.
38      */

39     private Class JavaDoc query = null;
40
41     /**
42      * The ID field of the data container.
43      */

44     private Integer JavaDoc id = null;
45
46     /**
47      * The vector containing the selected or inserted collection.
48      */

49     private TKVector collection = null;
50
51     /**
52      * The prototype used for the selection of multiple object IDs.
53      */

54     private ObjectCollectionDBData prototype = null;
55
56
57     // Constructors
58

59     /**
60      * Creates a data container for persistent objects.
61      *
62      * @param id the ID of the persistent object.
63      */

64     public ObjectDBData (Integer JavaDoc id)
65     {
66         this.id = id;
67     }
68
69     /**
70      * Creates a data container for persistent objects.
71      *
72      * @param object the persistent object.
73      */

74     public ObjectDBData (WMObject object)
75     {
76         this(object.getID());
77     }
78
79
80     // Method signatures
81

82     /**
83      * Returns the database interface.
84      *
85      * @return the database interface.
86      */

87     public abstract ObjectDBInterface getDBInterface ();
88
89
90     // Method implementations
91

92     /**
93      * Inserts primary key data into the given query.
94      * <P>
95      * This method is used for <CODE>SELECT</CODE> and
96      * <CODE>DELETE</CODE> statements.
97      *
98      * @param query the query to be executed.
99      * @exception java.sql.SQLException if an database error occured.
100      */

101     public void insertPrimaryIntoQuery (TKQuery query)
102         throws SQLException JavaDoc
103     {
104         query.setQueryParams(getDBInterface().getPrimaryKeyName(), this.id);
105     }
106
107     /**
108      * Inserts all data into the given query.
109      * <P>
110      * This method is used for <CODE>UPDATE</CODE> statements.
111      *
112      * @param query the query to be executed.
113      * @exception java.sql.SQLException if an database error occured.
114      */

115     public void insertIntoQuery (TKQuery query)
116         throws SQLException JavaDoc
117     {
118         insertPrimaryIntoQuery(query);
119         insertInitialIntoQuery(query);
120
121         if (this.prototype != null)
122         {
123             this.prototype.insertIntoQuery(query);
124         }
125     }
126
127     /**
128      * Reads all data from the given result set.
129      * <P>
130      * This method is used for <CODE>SELECT</CODE> and
131      * <CODE>INSERT</CODE> statements.
132      *
133      * @param result the result set to be read.
134      * @exception java.sql.SQLException if an database error occured.
135      */

136     public void fill (ResultSet JavaDoc result)
137         throws SQLException JavaDoc
138     {
139         this.id = new Integer JavaDoc(result.getInt(getDBInterface().getPrimaryKeyName()));
140         this.empty = false;
141     }
142
143     /**
144      * Returns the data vector for the given dependent table.
145      *
146      * @param table the name of the dependent table.
147      * @return the data vector for the given dependent table.
148      */

149     public final TKVector getVector (String JavaDoc table)
150     {
151         if (this.collection == null)
152         {
153             this.collection = new TKVector();
154         }
155
156         return this.collection;
157     }
158
159     /**
160      * Returns the data prototype for the given dependent table.
161      *
162      * @param table the name of the dependent table.
163      * @return the data prototype for the given dependent table.
164      */

165     public final TKDBTableData getProtoType (String JavaDoc table)
166     {
167         return getPrototype();
168     }
169
170     /**
171      * Checks wether the given dependent table should be ignored
172      * during the current query.
173      *
174      * @param table the name of the dependent table.
175      * @return <CODE>true</CODE> if the given dependent table should
176      * be ignored during the current query, otherwise <CODE>false</CODE>.
177      */

178     public final boolean isIgnoreTable (String JavaDoc table)
179     {
180         return this.ignore;
181     }
182
183
184     // Convenience methods
185

186     /**
187      * Checks wether the data container may be considered as empty.
188      *
189      * @return <CODE>true</CODE> if the data container may be considered
190      * as empty, otherwise <CODE>false</CODE>.
191      */

192     public final boolean isEmpty ()
193     {
194         return this.empty;
195     }
196
197     /**
198      * Returns the ID field of the data container.
199      *
200      * @return the ID field of the data container.
201      */

202     public final Integer JavaDoc getID ()
203     {
204         return this.id;
205     }
206
207     /**
208      * Determines wether the dependent table should be ignored
209      * during the current query.
210      *
211      * @param ignore the determinator.
212      */

213     public final void setIgnore (boolean ignore)
214     {
215         this.ignore = ignore;
216     }
217
218     /**
219      * Returns the query for the dependent table.
220      *
221      * @return the query for the dependent table.
222      */

223     public final Class JavaDoc getQuery ()
224     {
225         return this.query;
226     }
227
228     /**
229      * Assigns the query for the dependent table.
230      *
231      * @param query the query for the dependent table.
232      */

233     public final void setQuery (Class JavaDoc query)
234     {
235         this.query = query;
236     }
237
238     /**
239      * Returns the vector containing the IDs of the dependent objects.
240      *
241      * @return the vector containing the IDs of the dependent objects.
242      */

243     public final TKVector getCollection ()
244     {
245         return data2id(this.collection);
246     }
247
248     /**
249      * Assigns the vector containing the IDs of the dependent objects.
250      *
251      * @param collection the vector containing the IDs of the dependent objects.
252      */

253     public final void setCollection (TKVector collection)
254     {
255         this.collection = id2data(collection);
256     }
257
258     /**
259      * Returns the data prototype for the dependent table.
260      *
261      * @return the data prototype for the dependent table.
262      */

263     public final ObjectCollectionDBData getPrototype ()
264     {
265         return this.prototype;
266     }
267
268     /**
269      * Assigns the data prototype for the dependent table.
270      *
271      * @param prototype the data prototype for the dependent table.
272      */

273     public final void setPrototype (ObjectCollectionDBData prototype)
274     {
275         this.prototype = prototype;
276     }
277
278     /**
279      * Converts a vector of object IDs to a vector of data prototypes.
280      *
281      * @param ids the vector of object IDs.
282      */

283     private TKVector id2data (TKVector ids)
284     {
285         TKVector data = null;
286
287         if (ids != null)
288         {
289             int index = 0;
290             int size = ids.size();
291
292             data = new TKVector(size);
293
294             while (index < size)
295             {
296                 data.addElement(this.prototype.newFromValue((Integer JavaDoc) ids.elementAt(index++)));
297             }
298         }
299
300         return data;
301     }
302
303     /**
304      * Converts a vector of data prototypes to a vector of object IDs.
305      *
306      * @param data the vector of data prototypes.
307      */

308     private TKVector data2id (TKVector data)
309     {
310         TKVector ids = null;
311
312         if (data != null &&
313             data.size() > 0)
314         {
315             int index = 0;
316
317             ids = new TKVector(data.size());
318
319             while (index < data.size())
320             {
321                 ids.addElement(((ObjectCollectionDBData) data.elementAt(index++)).getValue());
322             }
323         }
324
325         return ids;
326     }
327
328 }
329
Popular Tags