KickJava   Java API By Example, From Geeks To Geeks.

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


1 package de.webman.acl.db;
2
3 import java.sql.SQLException JavaDoc;
4 import com.teamkonzept.db.TKDatabaseException;
5 import com.teamkonzept.db.TKDBManager;
6 import com.teamkonzept.db.TKDBVectorData;
7 import com.teamkonzept.db.TKDBVectorInterface;
8 import com.teamkonzept.db.TKSQLError;
9 import com.teamkonzept.lib.DatabaseErrorCodes;
10 import com.teamkonzept.lib.ErrorCodes;
11 import com.teamkonzept.lib.TKException;
12 import com.teamkonzept.webman.mainint.WebmanExceptionHandler;
13
14 /**
15  * $Header: /cvsroot/webman-cms/source/webman/de/webman/acl/db/ObjectDBInterface.java,v 1.1 2001/08/20 08:25:08 mischa Exp $
16  *
17  * Base class of database interfaces for persistent objects. Analogous to object
18  * factories, the should be a singleton instance per persistent object class.
19  *
20  * @version 0.10
21  * @since 0.10
22  * @author © 2000 Team-Konzept
23  */

24 public abstract class ObjectDBInterface
25     extends TKDBVectorInterface
26 {
27
28     // Constants
29

30     /**
31      * Default table names.
32      */

33     private static final String JavaDoc[] TABLES = {""};
34
35
36     // Attributes
37

38     /**
39      * Default insert queries.
40      */

41     private Class JavaDoc[] inserts = null;
42
43     /**
44      * Default select queries.
45      */

46     private Class JavaDoc[] selects = null;
47
48
49     // Constructors
50

51     /**
52      * Creates a database interface for persistent objects.
53      *
54      * @param primaryInsert the class object containing the primary <CODE>INSERT</CODE> statement.
55      * @param primaryUpdate the class object containing the primary <CODE>UPDATE</CODE> statement.
56      * @param primarySelect the class object containing the primary <CODE>SELECT</CODE> statement.
57      * @param primaryDelete the class object containing the primary <CODE>DELETE</CODE> statement.
58      * @param dependentInserts the class objects containing the dependent <CODE>INSERT</CODE> statements.
59      * @param dependentSelects the class objects containing the dependent <CODE>SELECT</CODE> statements.
60      * @param dependentDelete the class object containing the dependent <CODE>DELETE</CODE> statement.
61      */

62     public ObjectDBInterface (Class JavaDoc primaryInsert,
63                                 Class JavaDoc primaryUpdate,
64                                 Class JavaDoc primarySelect,
65                                 Class JavaDoc primaryDelete,
66                                 Class JavaDoc[] dependentInserts,
67                                 Class JavaDoc[] dependentSelects,
68                                 Class JavaDoc dependentDelete)
69     {
70         super(primaryInsert,
71               primaryUpdate,
72               primarySelect,
73               primaryDelete,
74               TABLES,
75               dependentInserts,
76               dependentSelects,
77               dependentDelete);
78
79         // Keep references to enable dynamic setting of queries.
80
this.inserts = dependentInserts;
81         this.selects = dependentSelects;
82     }
83
84
85     // Method signatures
86

87     /**
88      * Returns the name of the database table.
89      *
90      * @return the name of the database table.
91      */

92     public abstract String JavaDoc getTableName ();
93
94     /**
95      * Returns the name of the primary key.
96      *
97      * @return the name of the primary key.
98      */

99     public abstract String JavaDoc getPrimaryKeyName ();
100
101     /**
102      * Returns the name of the dependent key.
103      *
104      * @return the name of the dependent key.
105      */

106     public abstract String JavaDoc getDependentKeyName ();
107
108     /**
109      * Returns the query for selection of all objects.
110      *
111      * @return the query for selection of all objects.
112      */

113     public abstract Class JavaDoc getSelectAllQuery ();
114
115     /**
116      * Returns the query for selection of all dependent objects.
117      *
118      * @return the query for selection of all dependent objects.
119      */

120     public abstract Class JavaDoc getSelectDependentQuery ();
121
122     /**
123      * Returns the query for insertion of all dependent objects.
124      *
125      * @return the query for insertion of all dependent objects.
126      */

127     public abstract Class JavaDoc getInsertDependentQuery ();
128
129
130     // Method implementations
131

132     /**
133      * Selects the specified primary data set(s).
134      *
135      * @param data the data set.
136      * @exception com.teamkonzept.lib.TKException if an database error occured.
137      */

138     public final void selectPrimary (ObjectDBData data)
139         throws TKException
140     {
141         try
142         {
143             // Avoid dependent select.
144
data.setIgnore(true);
145
146             // Select specified object data.
147
getEntry(data);
148         }
149         catch (Throwable JavaDoc throwable)
150         {
151             throw WebmanExceptionHandler.getException(throwable);
152         }
153     }
154
155     /**
156      * Selects the specified dependent data set(s).
157      *
158      * @param data the data set.
159      * @exception com.teamkonzept.lib.TKException if an database error occured.
160      */

161     public final void selectDependent (ObjectDBData data)
162         throws TKException
163     {
164         try
165         {
166             // Force dependent select.
167
data.setIgnore(false);
168
169             // Set dependent query.
170
this.selects[0] = data.getQuery();
171
172             // Select specified dependent data.
173
getEntryTables(data);
174         }
175         catch (Throwable JavaDoc throwable)
176         {
177             throw WebmanExceptionHandler.getException(throwable);
178         }
179     }
180
181     /**
182      * Selects the specified primary and dependent data set(s).
183      *
184      * @param data the data set.
185      * @exception com.teamkonzept.lib.TKException if an database error occured.
186      */

187     public final void selectFull (ObjectDBData data)
188         throws TKException
189     {
190         try
191         {
192             // Set dependent query.
193
this.selects[0] = data.getQuery();
194
195             // Select specified object and optional dependent data.
196
getEntry(data);
197         }
198         catch (Throwable JavaDoc throwable)
199         {
200             throw WebmanExceptionHandler.getException(throwable);
201         }
202     }
203
204     /**
205      * Inserts the specified data set(s).
206      *
207      * @param data the data set.
208      * @exception com.teamkonzept.lib.TKException if an database error occured.
209      */

210     public final void insert (ObjectDBData data)
211         throws TKException
212     {
213         try
214         {
215             // Insert specified object data.
216
newEntry(data);
217         }
218         catch (Throwable JavaDoc throwable)
219         {
220             throw WebmanExceptionHandler.getException(throwable);
221         }
222     }
223
224     /**
225      * Updates the specified primary data set(s).
226      *
227      * @param data the data set.
228      * @exception com.teamkonzept.lib.TKException if an database error occured.
229      */

230     public final void updatePrimary (ObjectDBData data)
231         throws TKException
232     {
233         try
234         {
235             // Start own transaction, since all other methods out here are
236
// guarded by transactions in super-class 'TKDBVectorInterface'.
237
// The method 'updateEntry' is inherited from 'TKDBInterface'
238
// w/o any transaction management.
239
TKDBManager.beginTransaction();
240
241             // Update specified object data.
242
updateEntry(data);
243
244             // Transaction overhead is an inherently part of the measurement.
245
TKDBManager.commitTransaction();
246         }
247         catch (Throwable JavaDoc throwable)
248         {
249             try
250             {
251                 // Can anybody tell me why this method is called 'safe' ???
252
TKDBManager.safeRollbackTransaction(throwable);
253             }
254             catch (TKSQLError error)
255             {
256                 throw WebmanExceptionHandler.getException(error);
257             }
258         }
259     }
260
261     /**
262      * Updates the specified dependent data set(s).
263      *
264      * @param data the data set.
265      * @exception com.teamkonzept.lib.TKException if an database error occured.
266      */

267     public final void updateDependent (ObjectDBData data)
268         throws TKException
269     {
270         try
271         {
272             // Set dependent query.
273
this.inserts[0] = data.getQuery();
274
275             // Update specified dependent data.
276
putEntryTables(data);
277         }
278         catch (Throwable JavaDoc throwable)
279         {
280             throw WebmanExceptionHandler.getException(throwable);
281         }
282     }
283
284     /**
285      * Updates the specified primary and dependent data set(s).
286      *
287      * @param data the data set.
288      * @exception com.teamkonzept.lib.TKException if an database error occured.
289      */

290     public final void updateFull (ObjectDBData data)
291         throws TKException
292     {
293         try
294         {
295             // Set dependent query.
296
this.inserts[0] = data.getQuery();
297
298             // Update specified object and dependent data.
299
putEntry(data);
300         }
301         catch (Throwable JavaDoc throwable)
302         {
303             throw WebmanExceptionHandler.getException(throwable);
304         }
305     }
306
307     /**
308      * Deletes the specified data set(s).
309      *
310      * @param data the data set.
311      * @exception com.teamkonzept.lib.TKException if an database error occured.
312      */

313     public final void delete (ObjectDBData data)
314         throws TKException
315     {
316         try
317         {
318             // Delete specified object and dependent data.
319
super.delEntry(data);
320         }
321         catch (Throwable JavaDoc throwable)
322         {
323             TKException exception = WebmanExceptionHandler.getException(throwable);
324
325             if (exception.getErrorCode() == DatabaseErrorCodes.FK_CONSTRAINT_VIOLATION)
326             {
327                 throw new TKDatabaseException(getDescription(data),
328                                               DatabaseErrorCodes.OBJECT_IN_USE,
329                                               ErrorCodes.USER_SEVERITY,
330                                               true,
331                                               throwable);
332             }
333
334             throw exception;
335         }
336     }
337
338
339     // Overridden methods
340

341     /**
342      * Overridden with empty implementation.
343      *
344      * @param data the data set.
345      * @exception java.sql.SQLException for declarative purposes only.
346      */

347     public final void doNewEntryTables (TKDBVectorData data)
348         throws SQLException JavaDoc
349     {
350         // NOP.
351
}
352
353
354     // Convenience methods
355

356     /**
357      * Returns a description of the data set.
358      *
359      * @param data the data set.
360      * @return a description of the data set.
361      */

362     private static final String JavaDoc getDescription (ObjectDBData data)
363     {
364         String JavaDoc name = data.getClass().getName();
365
366         return name.substring(name.indexOf(".WM") + 3, name.indexOf("DBData"));
367     }
368
369 }
370
Popular Tags