KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > om > BaseDependTypePeer


1 package org.tigris.scarab.om;
2
3 import java.math.BigDecimal JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.LinkedList JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.apache.torque.NoRowsException;
13 import org.apache.torque.TooManyRowsException;
14 import org.apache.torque.Torque;
15 import org.apache.torque.TorqueException;
16 import org.apache.torque.map.MapBuilder;
17 import org.apache.torque.map.TableMap;
18 import org.apache.torque.om.DateKey;
19 import org.apache.torque.om.NumberKey;
20 import org.apache.torque.om.StringKey;
21 import org.apache.torque.om.ObjectKey;
22 import org.apache.torque.om.SimpleKey;
23 import org.apache.torque.util.BasePeer;
24 import org.apache.torque.util.Criteria;
25
26 import com.workingdogs.village.DataSetException;
27 import com.workingdogs.village.QueryDataSet;
28 import com.workingdogs.village.Record;
29
30 // Local classes
31
import org.tigris.scarab.om.map.*;
32
33
34 /**
35  */

36 public abstract class BaseDependTypePeer
37     extends BasePeer
38 {
39
40     /** the default database name for this class */
41     public static final String JavaDoc DATABASE_NAME = "scarab";
42
43      /** the table name for this class */
44     public static final String JavaDoc TABLE_NAME = "SCARAB_DEPEND_TYPE";
45
46     /**
47      * @return the map builder for this peer
48      * @throws TorqueException Any exceptions caught during processing will be
49      * rethrown wrapped into a TorqueException.
50      */

51     public static MapBuilder getMapBuilder()
52         throws TorqueException
53     {
54         return getMapBuilder(DependTypeMapBuilder.CLASS_NAME);
55     }
56
57       /** the column name for the DEPEND_TYPE_ID field */
58     public static final String JavaDoc DEPEND_TYPE_ID;
59       /** the column name for the DEPEND_TYPE_NAME field */
60     public static final String JavaDoc DEPEND_TYPE_NAME;
61   
62     static
63     {
64           DEPEND_TYPE_ID = "SCARAB_DEPEND_TYPE.DEPEND_TYPE_ID";
65           DEPEND_TYPE_NAME = "SCARAB_DEPEND_TYPE.DEPEND_TYPE_NAME";
66           if (Torque.isInit())
67         {
68             try
69             {
70                 getMapBuilder(DependTypeMapBuilder.CLASS_NAME);
71             }
72             catch (Exception JavaDoc e)
73             {
74                 log.error("Could not initialize Peer", e);
75             }
76         }
77         else
78         {
79             Torque.registerMapBuilder(DependTypeMapBuilder.CLASS_NAME);
80         }
81     }
82  
83     /** number of columns for this peer */
84     public static final int numColumns = 2;
85
86     /** A class that can be returned by this peer. */
87     protected static final String JavaDoc CLASSNAME_DEFAULT =
88         "org.tigris.scarab.om.DependType";
89
90     /** A class that can be returned by this peer. */
91     protected static final Class JavaDoc CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
92
93     /**
94      * Class object initialization method.
95      *
96      * @param className name of the class to initialize
97      * @return the initialized class
98      */

99     private static Class JavaDoc initClass(String JavaDoc className)
100     {
101         Class JavaDoc c = null;
102         try
103         {
104             c = Class.forName(className);
105         }
106         catch (Throwable JavaDoc t)
107         {
108             log.error("A FATAL ERROR has occurred which should not "
109                 + "have happened under any circumstance. Please notify "
110                 + "the Torque developers <torque-dev@db.apache.org> "
111                 + "and give as many details as possible (including the error "
112                 + "stack trace).", t);
113
114             // Error objects should always be propogated.
115
if (t instanceof Error JavaDoc)
116             {
117                 throw (Error JavaDoc) t.fillInStackTrace();
118             }
119         }
120         return c;
121     }
122
123     /**
124      * Get the list of objects for a ResultSet. Please not that your
125      * resultset MUST return columns in the right order. You can use
126      * getFieldNames() in BaseObject to get the correct sequence.
127      *
128      * @param results the ResultSet
129      * @return the list of objects
130      * @throws TorqueException Any exceptions caught during processing will be
131      * rethrown wrapped into a TorqueException.
132      */

133     public static List JavaDoc resultSet2Objects(java.sql.ResultSet JavaDoc results)
134             throws TorqueException
135     {
136         try
137         {
138             QueryDataSet qds = null;
139             List JavaDoc rows = null;
140             try
141             {
142                 qds = new QueryDataSet(results);
143                 rows = getSelectResults(qds);
144             }
145             finally
146             {
147                 if (qds != null)
148                 {
149                     qds.close();
150                 }
151             }
152
153             return populateObjects(rows);
154         }
155         catch (SQLException JavaDoc e)
156         {
157             throw new TorqueException(e);
158         }
159         catch (DataSetException e)
160         {
161             throw new TorqueException(e);
162         }
163     }
164
165
166   
167     /**
168      * Method to do inserts.
169      *
170      * @param criteria object used to create the INSERT statement.
171      * @throws TorqueException Any exceptions caught during processing will be
172      * rethrown wrapped into a TorqueException.
173      */

174     public static ObjectKey doInsert(Criteria criteria)
175         throws TorqueException
176     {
177         return BaseDependTypePeer
178             .doInsert(criteria, (Connection JavaDoc) null);
179     }
180
181     /**
182      * Method to do inserts. This method is to be used during a transaction,
183      * otherwise use the doInsert(Criteria) method. It will take care of
184      * the connection details internally.
185      *
186      * @param criteria object used to create the INSERT statement.
187      * @param con the connection to use
188      * @throws TorqueException Any exceptions caught during processing will be
189      * rethrown wrapped into a TorqueException.
190      */

191     public static ObjectKey doInsert(Criteria criteria, Connection JavaDoc con)
192         throws TorqueException
193     {
194               
195         setDbName(criteria);
196
197         if (con == null)
198         {
199             return BasePeer.doInsert(criteria);
200         }
201         else
202         {
203             return BasePeer.doInsert(criteria, con);
204         }
205     }
206
207     /**
208      * Add all the columns needed to create a new object.
209      *
210      * @param criteria object containing the columns to add.
211      * @throws TorqueException Any exceptions caught during processing will be
212      * rethrown wrapped into a TorqueException.
213      */

214     public static void addSelectColumns(Criteria criteria)
215             throws TorqueException
216     {
217           criteria.addSelectColumn(DEPEND_TYPE_ID);
218           criteria.addSelectColumn(DEPEND_TYPE_NAME);
219       }
220
221     /**
222      * Create a new object of type cls from a resultset row starting
223      * from a specified offset. This is done so that you can select
224      * other rows than just those needed for this object. You may
225      * for example want to create two objects from the same row.
226      *
227      * @throws TorqueException Any exceptions caught during processing will be
228      * rethrown wrapped into a TorqueException.
229      */

230     public static DependType row2Object(Record row,
231                                              int offset,
232                                              Class JavaDoc cls)
233         throws TorqueException
234     {
235         try
236         {
237             DependType obj = (DependType) cls.newInstance();
238             DependTypePeer.populateObject(row, offset, obj);
239                   obj.setModified(false);
240               obj.setNew(false);
241
242             return obj;
243         }
244         catch (InstantiationException JavaDoc e)
245         {
246             throw new TorqueException(e);
247         }
248         catch (IllegalAccessException JavaDoc e)
249         {
250             throw new TorqueException(e);
251         }
252     }
253
254     /**
255      * Populates an object from a resultset row starting
256      * from a specified offset. This is done so that you can select
257      * other rows than just those needed for this object. You may
258      * for example want to create two objects from the same row.
259      *
260      * @throws TorqueException Any exceptions caught during processing will be
261      * rethrown wrapped into a TorqueException.
262      */

263     public static void populateObject(Record row,
264                                       int offset,
265                                       DependType obj)
266         throws TorqueException
267     {
268         try
269         {
270                 obj.setDependTypeId(row.getValue(offset + 0).asIntegerObj());
271                   obj.setName(row.getValue(offset + 1).asString());
272               }
273         catch (DataSetException e)
274         {
275             throw new TorqueException(e);
276         }
277     }
278
279     /**
280      * Method to do selects.
281      *
282      * @param criteria object used to create the SELECT statement.
283      * @return List of selected Objects
284      * @throws TorqueException Any exceptions caught during processing will be
285      * rethrown wrapped into a TorqueException.
286      */

287     public static List JavaDoc doSelect(Criteria criteria) throws TorqueException
288     {
289         return populateObjects(doSelectVillageRecords(criteria));
290     }
291
292     /**
293      * Method to do selects within a transaction.
294      *
295      * @param criteria object used to create the SELECT statement.
296      * @param con the connection to use
297      * @return List of selected Objects
298      * @throws TorqueException Any exceptions caught during processing will be
299      * rethrown wrapped into a TorqueException.
300      */

301     public static List JavaDoc doSelect(Criteria criteria, Connection JavaDoc con)
302         throws TorqueException
303     {
304         return populateObjects(doSelectVillageRecords(criteria, con));
305     }
306
307     /**
308      * Grabs the raw Village records to be formed into objects.
309      * This method handles connections internally. The Record objects
310      * returned by this method should be considered readonly. Do not
311      * alter the data and call save(), your results may vary, but are
312      * certainly likely to result in hard to track MT bugs.
313      *
314      * @throws TorqueException Any exceptions caught during processing will be
315      * rethrown wrapped into a TorqueException.
316      */

317     public static List JavaDoc doSelectVillageRecords(Criteria criteria)
318         throws TorqueException
319     {
320         return BaseDependTypePeer
321             .doSelectVillageRecords(criteria, (Connection JavaDoc) null);
322     }
323
324     /**
325      * Grabs the raw Village records to be formed into objects.
326      * This method should be used for transactions
327      *
328      * @param criteria object used to create the SELECT statement.
329      * @param con the connection to use
330      * @throws TorqueException Any exceptions caught during processing will be
331      * rethrown wrapped into a TorqueException.
332      */

333     public static List JavaDoc doSelectVillageRecords(Criteria criteria, Connection JavaDoc con)
334         throws TorqueException
335     {
336         if (criteria.getSelectColumns().size() == 0)
337         {
338             addSelectColumns(criteria);
339         }
340
341               
342         setDbName(criteria);
343
344         // BasePeer returns a List of Value (Village) arrays. The array
345
// order follows the order columns were placed in the Select clause.
346
if (con == null)
347         {
348             return BasePeer.doSelect(criteria);
349         }
350         else
351         {
352             return BasePeer.doSelect(criteria, con);
353         }
354     }
355
356     /**
357      * The returned List will contain objects of the default type or
358      * objects that inherit from the default.
359      *
360      * @throws TorqueException Any exceptions caught during processing will be
361      * rethrown wrapped into a TorqueException.
362      */

363     public static List JavaDoc populateObjects(List JavaDoc records)
364         throws TorqueException
365     {
366         List JavaDoc results = new ArrayList JavaDoc(records.size());
367
368         // populate the object(s)
369
for (int i = 0; i < records.size(); i++)
370         {
371             Record row = (Record) records.get(i);
372               results.add(DependTypePeer.row2Object(row, 1,
373                 DependTypePeer.getOMClass()));
374           }
375         return results;
376     }
377  
378
379     /**
380      * The class that the Peer will make instances of.
381      * If the BO is abstract then you must implement this method
382      * in the BO.
383      *
384      * @throws TorqueException Any exceptions caught during processing will be
385      * rethrown wrapped into a TorqueException.
386      */

387     public static Class JavaDoc getOMClass()
388         throws TorqueException
389     {
390         return CLASS_DEFAULT;
391     }
392
393     /**
394      * Method to do updates.
395      *
396      * @param criteria object containing data that is used to create the UPDATE
397      * statement.
398      * @throws TorqueException Any exceptions caught during processing will be
399      * rethrown wrapped into a TorqueException.
400      */

401     public static void doUpdate(Criteria criteria) throws TorqueException
402     {
403          BaseDependTypePeer
404             .doUpdate(criteria, (Connection JavaDoc) null);
405     }
406
407     /**
408      * Method to do updates. This method is to be used during a transaction,
409      * otherwise use the doUpdate(Criteria) method. It will take care of
410      * the connection details internally.
411      *
412      * @param criteria object containing data that is used to create the UPDATE
413      * statement.
414      * @param con the connection to use
415      * @throws TorqueException Any exceptions caught during processing will be
416      * rethrown wrapped into a TorqueException.
417      */

418     public static void doUpdate(Criteria criteria, Connection JavaDoc con)
419         throws TorqueException
420     {
421         Criteria selectCriteria = new Criteria(DATABASE_NAME, 2);
422                    selectCriteria.put(DEPEND_TYPE_ID, criteria.remove(DEPEND_TYPE_ID));
423                 
424         setDbName(criteria);
425
426         if (con == null)
427         {
428             BasePeer.doUpdate(selectCriteria, criteria);
429         }
430         else
431         {
432             BasePeer.doUpdate(selectCriteria, criteria, con);
433         }
434     }
435
436     /**
437      * Method to do deletes.
438      *
439      * @param criteria object containing data that is used DELETE from database.
440      * @throws TorqueException Any exceptions caught during processing will be
441      * rethrown wrapped into a TorqueException.
442      */

443      public static void doDelete(Criteria criteria) throws TorqueException
444      {
445          DependTypePeer
446             .doDelete(criteria, (Connection JavaDoc) null);
447      }
448
449     /**
450      * Method to do deletes. This method is to be used during a transaction,
451      * otherwise use the doDelete(Criteria) method. It will take care of
452      * the connection details internally.
453      *
454      * @param criteria object containing data that is used DELETE from database.
455      * @param con the connection to use
456      * @throws TorqueException Any exceptions caught during processing will be
457      * rethrown wrapped into a TorqueException.
458      */

459      public static void doDelete(Criteria criteria, Connection JavaDoc con)
460         throws TorqueException
461      {
462               
463         setDbName(criteria);
464
465         if (con == null)
466         {
467             BasePeer.doDelete(criteria);
468         }
469         else
470         {
471             BasePeer.doDelete(criteria, con);
472         }
473      }
474
475     /**
476      * Method to do selects
477      *
478      * @throws TorqueException Any exceptions caught during processing will be
479      * rethrown wrapped into a TorqueException.
480      */

481     public static List JavaDoc doSelect(DependType obj) throws TorqueException
482     {
483         return doSelect(buildSelectCriteria(obj));
484     }
485
486     /**
487      * Method to do inserts
488      *
489      * @throws TorqueException Any exceptions caught during processing will be
490      * rethrown wrapped into a TorqueException.
491      */

492     public static void doInsert(DependType obj) throws TorqueException
493     {
494           obj.setPrimaryKey(doInsert(buildCriteria(obj)));
495           obj.setNew(false);
496         obj.setModified(false);
497     }
498
499     /**
500      * @param obj the data object to update in the database.
501      * @throws TorqueException Any exceptions caught during processing will be
502      * rethrown wrapped into a TorqueException.
503      */

504     public static void doUpdate(DependType obj) throws TorqueException
505     {
506         doUpdate(buildCriteria(obj));
507         obj.setModified(false);
508     }
509
510     /**
511      * @param obj the data object to delete in the database.
512      * @throws TorqueException Any exceptions caught during processing will be
513      * rethrown wrapped into a TorqueException.
514      */

515     public static void doDelete(DependType obj) throws TorqueException
516     {
517         doDelete(buildSelectCriteria(obj));
518     }
519
520     /**
521      * Method to do inserts. This method is to be used during a transaction,
522      * otherwise use the doInsert(DependType) method. It will take
523      * care of the connection details internally.
524      *
525      * @param obj the data object to insert into the database.
526      * @param con the connection to use
527      * @throws TorqueException Any exceptions caught during processing will be
528      * rethrown wrapped into a TorqueException.
529      */

530     public static void doInsert(DependType obj, Connection JavaDoc con)
531         throws TorqueException
532     {
533           obj.setPrimaryKey(doInsert(buildCriteria(obj), con));
534           obj.setNew(false);
535         obj.setModified(false);
536     }
537
538     /**
539      * Method to do update. This method is to be used during a transaction,
540      * otherwise use the doUpdate(DependType) method. It will take
541      * care of the connection details internally.
542      *
543      * @param obj the data object to update in the database.
544      * @param con the connection to use
545      * @throws TorqueException Any exceptions caught during processing will be
546      * rethrown wrapped into a TorqueException.
547      */

548     public static void doUpdate(DependType obj, Connection JavaDoc con)
549         throws TorqueException
550     {
551         doUpdate(buildCriteria(obj), con);
552         obj.setModified(false);
553     }
554
555     /**
556      * Method to delete. This method is to be used during a transaction,
557      * otherwise use the doDelete(DependType) method. It will take
558      * care of the connection details internally.
559      *
560      * @param obj the data object to delete in the database.
561      * @param con the connection to use
562      * @throws TorqueException Any exceptions caught during processing will be
563      * rethrown wrapped into a TorqueException.
564      */

565     public static void doDelete(DependType obj, Connection JavaDoc con)
566         throws TorqueException
567     {
568         doDelete(buildSelectCriteria(obj), con);
569     }
570
571     /**
572      * Method to do deletes.
573      *
574      * @param pk ObjectKey that is used DELETE from database.
575      * @throws TorqueException Any exceptions caught during processing will be
576      * rethrown wrapped into a TorqueException.
577      */

578     public static void doDelete(ObjectKey pk) throws TorqueException
579     {
580         BaseDependTypePeer
581            .doDelete(pk, (Connection JavaDoc) null);
582     }
583
584     /**
585      * Method to delete. This method is to be used during a transaction,
586      * otherwise use the doDelete(ObjectKey) method. It will take
587      * care of the connection details internally.
588      *
589      * @param pk the primary key for the object to delete in the database.
590      * @param con the connection to use
591      * @throws TorqueException Any exceptions caught during processing will be
592      * rethrown wrapped into a TorqueException.
593      */

594     public static void doDelete(ObjectKey pk, Connection JavaDoc con)
595         throws TorqueException
596     {
597         doDelete(buildCriteria(pk), con);
598     }
599
600     /** Build a Criteria object from an ObjectKey */
601     public static Criteria buildCriteria( ObjectKey pk )
602     {
603         Criteria criteria = new Criteria();
604               criteria.add(DEPEND_TYPE_ID, pk);
605           return criteria;
606      }
607
608     /** Build a Criteria object from the data object for this peer */
609     public static Criteria buildCriteria( DependType obj )
610     {
611         Criteria criteria = new Criteria(DATABASE_NAME);
612               if (!obj.isNew())
613             criteria.add(DEPEND_TYPE_ID, obj.getDependTypeId());
614               criteria.add(DEPEND_TYPE_NAME, obj.getName());
615           return criteria;
616     }
617
618     /** Build a Criteria object from the data object for this peer, skipping all binary columns */
619     public static Criteria buildSelectCriteria( DependType obj )
620     {
621         Criteria criteria = new Criteria(DATABASE_NAME);
622               if (!obj.isNew())
623                     criteria.add(DEPEND_TYPE_ID, obj.getDependTypeId());
624                           criteria.add(DEPEND_TYPE_NAME, obj.getName());
625               return criteria;
626     }
627  
628     
629         /**
630      * Retrieve a single object by pk
631      *
632      * @param pk the primary key
633      * @throws TorqueException Any exceptions caught during processing will be
634      * rethrown wrapped into a TorqueException.
635      * @throws NoRowsException Primary key was not found in database.
636      * @throws TooManyRowsException Primary key was not found in database.
637      */

638     public static DependType retrieveByPK(Integer JavaDoc pk)
639         throws TorqueException, NoRowsException, TooManyRowsException
640     {
641         return retrieveByPK(SimpleKey.keyFor(pk));
642     }
643
644     /**
645      * Retrieve a single object by pk
646      *
647      * @param pk the primary key
648      * @param con the connection to use
649      * @throws TorqueException Any exceptions caught during processing will be
650      * rethrown wrapped into a TorqueException.
651      * @throws NoRowsException Primary key was not found in database.
652      * @throws TooManyRowsException Primary key was not found in database.
653      */

654     public static DependType retrieveByPK(Integer JavaDoc pk, Connection JavaDoc con)
655         throws TorqueException, NoRowsException, TooManyRowsException
656     {
657         return retrieveByPK(SimpleKey.keyFor(pk), con);
658     }
659   
660     /**
661      * Retrieve a single object by pk
662      *
663      * @param pk the primary key
664      * @throws TorqueException Any exceptions caught during processing will be
665      * rethrown wrapped into a TorqueException.
666      * @throws NoRowsException Primary key was not found in database.
667      * @throws TooManyRowsException Primary key was not found in database.
668      */

669     public static DependType retrieveByPK(ObjectKey pk)
670         throws TorqueException, NoRowsException, TooManyRowsException
671     {
672         Connection JavaDoc db = null;
673         DependType retVal = null;
674         try
675         {
676             db = Torque.getConnection(DATABASE_NAME);
677             retVal = retrieveByPK(pk, db);
678         }
679         finally
680         {
681             Torque.closeConnection(db);
682         }
683         return(retVal);
684     }
685
686     /**
687      * Retrieve a single object by pk
688      *
689      * @param pk the primary key
690      * @param con the connection to use
691      * @throws TorqueException Any exceptions caught during processing will be
692      * rethrown wrapped into a TorqueException.
693      * @throws NoRowsException Primary key was not found in database.
694      * @throws TooManyRowsException Primary key was not found in database.
695      */

696     public static DependType retrieveByPK(ObjectKey pk, Connection JavaDoc con)
697         throws TorqueException, NoRowsException, TooManyRowsException
698     {
699         Criteria criteria = buildCriteria(pk);
700         List JavaDoc v = doSelect(criteria, con);
701         if (v.size() == 0)
702         {
703             throw new NoRowsException("Failed to select a row.");
704         }
705         else if (v.size() > 1)
706         {
707             throw new TooManyRowsException("Failed to select only one row.");
708         }
709         else
710         {
711             return (DependType)v.get(0);
712         }
713     }
714
715     /**
716      * Retrieve a multiple objects by pk
717      *
718      * @param pks List of primary keys
719      * @throws TorqueException Any exceptions caught during processing will be
720      * rethrown wrapped into a TorqueException.
721      */

722     public static List JavaDoc retrieveByPKs(List JavaDoc pks)
723         throws TorqueException
724     {
725         Connection JavaDoc db = null;
726         List JavaDoc retVal = null;
727         try
728         {
729            db = Torque.getConnection(DATABASE_NAME);
730            retVal = retrieveByPKs(pks, db);
731         }
732         finally
733         {
734             Torque.closeConnection(db);
735         }
736         return(retVal);
737     }
738
739     /**
740      * Retrieve a multiple objects by pk
741      *
742      * @param pks List of primary keys
743      * @param dbcon the connection to use
744      * @throws TorqueException Any exceptions caught during processing will be
745      * rethrown wrapped into a TorqueException.
746      */

747     public static List JavaDoc retrieveByPKs( List JavaDoc pks, Connection JavaDoc dbcon )
748         throws TorqueException
749     {
750         List JavaDoc objs = null;
751         if (pks == null || pks.size() == 0)
752         {
753             objs = new LinkedList JavaDoc();
754         }
755         else
756         {
757             Criteria criteria = new Criteria();
758               criteria.addIn( DEPEND_TYPE_ID, pks );
759           objs = doSelect(criteria, dbcon);
760         }
761         return objs;
762     }
763
764  
765
766
767
768         
769   
770   
771     
772   
773       /**
774      * Returns the TableMap related to this peer. This method is not
775      * needed for general use but a specific application could have a need.
776      *
777      * @throws TorqueException Any exceptions caught during processing will be
778      * rethrown wrapped into a TorqueException.
779      */

780     protected static TableMap getTableMap()
781         throws TorqueException
782     {
783         return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME);
784     }
785    
786     private static void setDbName(Criteria crit)
787     {
788         // Set the correct dbName if it has not been overridden
789
// crit.getDbName will return the same object if not set to
790
// another value so == check is okay and faster
791
if (crit.getDbName() == Torque.getDefaultDB())
792         {
793             crit.setDbName(DATABASE_NAME);
794         }
795     }
796 }
797
Popular Tags