KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sql > CoreDO


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: CoreDO.java,v 1.3 2005/05/26 08:08:10 predrag Exp $
22  */

23 package com.lutris.appserver.server.sql;
24
25 import java.io.Serializable JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import com.lutris.dods.builder.generator.query.DataObjectException;
31
32 /**
33  * Base class from which the data objects are derived.
34  *
35  * @version $Revision: 1.3 $
36  * @author Kyle Clark
37  */

38 abstract public class CoreDO implements Transaction, Serializable JavaDoc {
39     private static boolean transactionCheck = false;
40     private static boolean deleteCheckVersion = false;
41     private static boolean autoSave = false;
42     public static boolean versioning = true;
43
44     /**
45      * Disable use of the version column.
46      *
47      * author Jay Gunter
48      */

49     public static void disableVersioning() {
50         versioning = false;
51     }
52     // True if this object exists in the database.
53
protected boolean persistent = false;
54     protected CoreDataStruct originalData = null;
55
56     /**
57      * Public constructor.
58      */

59     public CoreDO() {
60         persistent = false;
61     }
62
63     /**
64      * Public constructor.
65      * @param rs a result set.
66      * @exception SQLException if an error occurs while instantiating this
67      * object from the result set.
68      * @exception ObjectIdException if the object id for this object is
69      * invalid.
70      */

71     public CoreDO(ResultSet JavaDoc rs) throws SQLException JavaDoc, ObjectIdException {
72         persistent = true;
73         set_OId(new ObjectId(rs.getBigDecimal(get_OIdColumnName())));
74         if (versioning) {
75             set_Version(rs.getInt(get_versionColumnName()));
76         }
77     }
78
79     /**
80      * Returns the object identifier column name.
81      * @return the object identifier column name.
82      */

83     public static String JavaDoc get_OIdColumnName() {
84         return CoreDataStruct.get_OIdColumnName();
85     }
86
87     /**
88      * Returns the object identifier column name.
89      * @return the object identifier column name.
90      * @deprecated use get_OIdColumnName()
91      */

92     public static String JavaDoc getOIdColumnName() {
93         return get_OIdColumnName();
94     }
95
96     /**
97      * Sets the object identifier column name.
98      * @param _oidColumnName Name of object identifier column.
99      */

100     public static void set_OIdColumnName(String JavaDoc _oidColumnName) {
101         CoreDataStruct.set_OIdColumnName(_oidColumnName);
102     }
103
104     /**
105      * Sets the object identifier column name.
106      * @param _oidColumnName Name of object identifier column.
107      * @deprecated use get_versionColumnName()
108      */

109     public static void setOIdColumnName(String JavaDoc _oidColumnName) {
110         set_OIdColumnName(_oidColumnName);
111     }
112
113     /**
114      * Returns the version column name.
115      * @return the version column name.
116      */

117     public static String JavaDoc get_versionColumnName() {
118         return CoreDataStruct.get_versionColumnName();
119     }
120
121     /**
122      * Returns the version column name.
123      * @return the version column name.
124      * @deprecated use get_versionColumnName()
125      */

126     public static String JavaDoc getVersionColumnName() {
127         return get_versionColumnName();
128     }
129
130     /**
131      * Sets the version column name.
132      * @param _versionColumnName Name of version column.
133      */

134     public static void set_versionColumnName(String JavaDoc _versionColumnName) {
135         CoreDataStruct.set_versionColumnName(_versionColumnName);
136     }
137
138     /**
139      * Sets the version column name.
140      * @param _versionColumnName Name of version column.
141      * @deprecated use set_versionColumnName()
142      */

143     public static void setVersionColumnName(String JavaDoc _versionColumnName) {
144         set_versionColumnName(_versionColumnName);
145     }
146
147     /**
148      * @deprecated Use get_OId()
149      * @return this object's identifier.
150      */

151     public ObjectId getOId() {
152         return get_OId();
153     }
154
155     /**
156      * Returns this object's identifier.
157      * @return this object's identifier.
158      */

159     public ObjectId get_OId() {
160         if (originalData != null) {
161             return originalData.get_OId();
162         } else {
163             return null;
164         }
165     }
166
167     /**
168      * Sets this object's identifier.
169      * @deprecated Use set_OId()
170      * @param oId this object's identifier.
171      */

172     protected void setOId(ObjectId oId) {
173         set_OId(oId);
174     }
175
176     /**
177      * Sets this object's identifier.
178      * @param oId this object's identifier.
179      */

180     protected void set_OId(ObjectId oId) {
181         if (originalData != null) {
182             originalData.set_OId(oId);
183         }
184     }
185
186     /**
187      * @deprecated Use set_Version()
188      * @param version the object's version.
189      */

190     protected void setVersion(int version) {
191         set_Version(version);
192     }
193
194     /**
195      * Sets this object's version.
196      * @param version the object's version.
197      */

198     protected void set_Version(int version) {
199         if (originalData != null) {
200             originalData.set_Version(version);
201         }
202     }
203
204     /**
205      * @deprecated Use get_Version()
206      * @return this object's version.
207      */

208     protected int getVersion() {
209         return get_Version();
210     }
211
212     /**
213      * Returns this object's version.
214      * @return this object's version.
215      */

216     protected int get_Version() {
217         if (originalData != null) {
218             return originalData.get_Version();
219         } else {
220             return 0;
221         }
222     }
223
224     /**
225      * Sets this object's new version number.
226      * @param newVersion this object's next version.
227      * @deprecated use set_NewVersion instead
228      */

229     protected void setNewVersion(int newVersion) {
230         if (originalData != null) {
231             originalData.newVersion = newVersion;
232         }
233     }
234
235     /**
236      * Sets this object's new version number.
237      * @param newVersion this object's next version.
238      */

239     protected void set_NewVersion(int newVersion) {
240         if (originalData != null) {
241             originalData.newVersion = newVersion;
242         }
243     }
244
245     /**
246      * Returns this object's new version.
247      * @return this object's new version.
248      * @deprecated use get_NewVersion instead
249      */

250     protected int getNewVersion() {
251         if (originalData != null) {
252             return originalData.newVersion;
253         } else {
254             return 0;
255         }
256     }
257
258     /**
259      * Returns this object's new version.
260      * @return this object's new version.
261      */

262     protected int get_NewVersion() {
263         if (originalData != null) {
264             return originalData.newVersion;
265         } else {
266             return 0;
267         }
268     }
269
270     /**
271      *
272      * @return true if this data object has been inserted into
273      * the database.
274      *
275      * Returns true if this object is persistent in the database
276      * (i.e. it has been inserted and not deleted)
277      *
278      */

279     public boolean isPersistent() {
280         return this.persistent;
281     }
282
283     /**
284      * Sets the persistent state for this object.
285      * @param persistent true if this object is present in
286      * persistent store.
287      */

288     public void setPersistent(boolean persistent) {
289         this.persistent = persistent;
290     }
291
292     /**
293      * Returns the statement that can be used to insert this
294      * object into the database.
295      *
296      * @param conn the database connection.
297      * @return the statement used to insert this object into the
298      * database. null if the object cannot be inserted.
299      * @exception java.sql.SQLException If an error occurs.
300      */

301     public abstract PreparedStatement JavaDoc getInsertStatement(DBConnection conn)
302         throws SQLException JavaDoc;
303
304     /**
305      * Returns the statement that can be used to update this
306      * object in the database.
307      *
308      * @param conn the database connection.
309      * @return the statement used to update this object in the
310      * database. null if the object cannot be updated.
311      * @exception java.sql.SQLException If an error occurs.
312      */

313     public abstract PreparedStatement JavaDoc getUpdateStatement(DBConnection conn)
314         throws SQLException JavaDoc;
315
316     /**
317      * Returns the statement that can be used to delete this
318      * object from the database.
319      *
320      * @param conn the database connection.
321      * @return the statement used to delete this object from the
322      * database. null if the object cannot be deleted.
323      * @exception java.sql.SQLException If an error occurs.
324      */

325     public abstract PreparedStatement JavaDoc getDeleteStatement(DBConnection conn)
326         throws SQLException JavaDoc;
327
328     /**
329      * Inserts this object into the database.
330      *
331      * @param conn the database connection.
332      * @exception java.sql.SQLException if a database access error occurs.
333      * @exception DBRowUpdateException If a version error occurs.
334      */

335     public synchronized void executeInsert(DBConnection conn)
336         throws SQLException JavaDoc, DBRowUpdateException {
337         PreparedStatement JavaDoc stmt;
338
339         if (persistent) {
340             executeUpdate(conn);
341         } else {
342             stmt = getInsertStatement(conn);
343             if (stmt != null) {
344                 try {
345                     conn.executeUpdate(stmt, "executeInsert");
346                 } catch (SQLException JavaDoc e) {
347                     throw e;
348                 } finally{
349                     if(((ExtendedDBConnection)conn).getMaxPreparedStmts()==0){
350                       stmt.close();
351                     }
352                     //stmt.close();
353
}
354             }
355             persistent = true;
356         }
357     }
358
359     /**
360      * Updates the persistent state.
361      *
362      * @param success true if the transaction succeeded
363      * and this object was successfully inserted into the database.
364      */

365     public void finalizeInsert(boolean success) {
366         persistent = success;
367         /*
368          if (versioning) {
369          if (success) {
370          set_Version(get_NewVersion());
371          } else {
372          set_NewVersion(get_Version());
373          }
374          }
375          */

376     }
377
378     /**
379      * Updates the contents of this object in the database.
380      *
381      * @param conn the database connection.
382      * @exception java.sql.SQLException If a database access error occurs.
383      * @exception DBRowUpdateException If a version error occurs.
384      */

385     public synchronized void executeUpdate(DBConnection conn)
386         throws SQLException JavaDoc, DBRowUpdateException {
387         boolean mustUpdate = (get_NewVersion() == get_Version());
388         /*
389          if (mustUpdate && versioning) {
390          set_NewVersion(get_Version() + 1);
391          }
392          */

393         PreparedStatement JavaDoc stmt = getUpdateStatement(conn);
394
395         if (stmt != null) {
396             try{
397                 if ((conn.executeUpdate(stmt, "execute update") == 0) && mustUpdate) {
398                     // Zero rows were affected by the update.
399
// This usually occurs when the version number does not match
400
// the version number for this oId in the database table.
401
// We verify that this is the case, and throw an appropriate
402
// exception.
403
stmt.close();
404                     stmt=null;
405                     seeWhatHappened(conn,"Update");
406                 }
407             } catch (SQLException JavaDoc e) {
408                 throw e;
409             } finally{
410                 if(((ExtendedDBConnection)conn).getMaxPreparedStmts()==0 && (stmt!= null)){
411                     stmt.close();
412                   }
413                   //stmt.close();
414
}
415         }
416     }
417
418     private void seeWhatHappened(DBConnection conn,
419                                  String JavaDoc action)
420         throws SQLException JavaDoc, DBRowUpdateException {
421         ResultSet JavaDoc rs=null;
422         PreparedStatement JavaDoc stmt=null;
423         try {
424             String JavaDoc table = this.getTableName();
425
426             stmt = conn.prepareStatement("select " + get_versionColumnName()
427                                              + " from " + table + " where " + get_OIdColumnName() + " = "
428                                              + get_OId());
429             rs = stmt.executeQuery();
430
431             if (false == rs.next()) {
432                 throw new DBRowUpdateException(action + " failed: Table "
433                                                    + table + " contains no row with id=" + get_OId());
434             }
435             int v = rs.getInt(1);
436
437             if (get_Version() != v) {
438                 throw new DBRowUpdateException(action + " failed: Table "
439                                                    + table + " id=" + get_OId() + " has version " + v
440                                                    + " where object has version " + get_Version()
441                                                    + (versioning ? "." : ", and versioning is disabled!"));
442             }
443             throw new SQLException JavaDoc(action + " failed, but Table " + table
444                                        + " id=" + get_OId() + " does exist with version=" + v);
445         } catch (DBRowUpdateException re) {
446             throw re;
447         } catch (SQLException JavaDoc se) {
448             throw se;
449         } catch (Exception JavaDoc e) {
450             throw new SQLException JavaDoc("Error determining cause of update failure.",
451                                    e.getMessage());
452         }finally{
453             if (rs!=null){
454                 rs.close();
455             }
456             if(stmt!=null){
457                 stmt.close();
458             }
459         }
460     }
461
462     /**
463      * Return the name of the table whose rows represent these objects.
464      * This method should be overridden by derived classes.
465      *
466      * @return the name of the table.
467      * @see #executeUpdate
468      */

469     protected String JavaDoc getTableName()throws SQLException JavaDoc {
470         // FIXME: really should be a specific error
471
throw new SQLException JavaDoc("getTableName() not implemented. DO classes need to be regenerated");
472     }
473
474     /**
475      * The version number of this object is set to the next
476      * version for this object if
477      * this object was successfully updated in the database.
478      *
479      * @param success true if the transaction succeeded
480      * and this object was successfully updated in the database.
481      * @see #get_NewVersion
482      */

483     public void finalizeUpdate(boolean success) {
484         /*
485          if (versioning) {
486          if (success) {
487          set_Version(get_NewVersion());
488          } else {
489          set_NewVersion(get_Version());
490          }
491          }
492          */

493     }
494
495     /**
496      * Deletes this object from the database.
497      *
498      * @param conn Database connection.
499      * @exception java.sql.SQLException If a database access error
500      * occurs.
501      */

502     public void executeDelete(DBConnection conn)
503         throws SQLException JavaDoc {
504         PreparedStatement JavaDoc stmt = getDeleteStatement(conn);
505
506         if (stmt != null) {
507             try{
508                 if (conn.executeUpdate(stmt, "execute delete") == 0) {
509                     try {
510                         stmt.close();
511                         stmt=null;
512                         seeWhatHappened(conn, "Delete");
513                     } catch (DBRowUpdateException de) {
514                         SQLException JavaDoc sqle = new SQLException JavaDoc(de.toString());
515                         throw sqle;
516                     }
517                 }
518             }catch( SQLException JavaDoc e){
519                 throw e;
520             }finally{
521                 if(((ExtendedDBConnection)conn).getMaxPreparedStmts()==0 && (stmt!=null)){
522                     stmt.close();
523                   }
524                   //stmt.close();
525
}
526         }
527     }
528
529     /**
530      * Currently does nothing.
531      *
532      * @param success true if the transaction succeeded
533      * and this object was successfully deleted from the
534      * database.
535      */

536     public void finalizeDelete(boolean success) {
537         if (persistent) {
538             persistent = !success;
539         }
540     }
541
542     /**
543      * This method is invoked whenever an object original data is needed. This
544      * method returns null and should be overwritten by subclasses.
545      */

546     public Object JavaDoc originalData_get() {
547         return null;
548     }
549
550     /**
551      * This method is invoked whenever an object is inserted or updated in the
552      * cache. This is empty method and should be overwritten by subclasses.
553      */

554     public void updateCache() {}
555
556     /**
557      * This method is invoked whenever an object is deleted from the cache.
558      * This is empty method and should be overwritten by subclasses.
559      */

560     public void deleteFromCache() {}
561
562     /**
563      * This method is invoked whenever an object is added to the cache.
564      * This is empty method and should be overwritten by subclasses.
565      */

566     public void addToCache() {}
567
568     /**
569      * This method is invoked whenever an object is removed from the cache.
570      * This is empty method and should be overwritten by subclasses.
571      */

572     public void evict() {}
573
574     /**
575      * This method is invoked whenever object's data needs to be loaded.
576      * This is empty method and should be overwritten by subclasses.
577      *
578      * @exception DataObjectException If a data access error occurs.
579      */

580     public void refresh() throws DataObjectException {}
581
582     /**
583      *
584      */

585     public int getOriginalVersion() {
586         if (originalData != null) {
587             return originalData.get_Version();
588         } else {
589             return 0;
590         }
591     }
592
593     /**
594      *
595      */

596     public void makeVisible() {}
597
598     /**
599      *
600      */

601     public void makeInvisible() {}
602
603     /**
604      *
605      */

606     public void executeLockingStatement(DBConnection conn) throws SQLException JavaDoc {}
607
608     /**
609      *
610      */

611     abstract public void dumpData(boolean incrementVersion);
612 }
613
614
Popular Tags