KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > campware > cream > om > BaseUomPeer


1 package org.campware.cream.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.campware.cream.om.map.*;
32
33
34 /**
35  * This class was autogenerated by Torque on:
36  *
37  * [Wed May 04 09:10:56 CEST 2005]
38  *
39  */

40 public abstract class BaseUomPeer
41     extends BasePeer
42 {
43
44     /** the default database name for this class */
45     public static final String JavaDoc DATABASE_NAME = "cream";
46
47      /** the table name for this class */
48     public static final String JavaDoc TABLE_NAME = "UOM";
49
50     /**
51      * @return the map builder for this peer
52      * @throws TorqueException Any exceptions caught during processing will be
53      * rethrown wrapped into a TorqueException.
54      */

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

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

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

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

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

221     public static void addSelectColumns(Criteria criteria)
222             throws TorqueException
223     {
224           criteria.addSelectColumn(UOM_ID);
225           criteria.addSelectColumn(UOM_NAME);
226           criteria.addSelectColumn(UOM_CODE);
227       }
228
229     /**
230      * Create a new object of type cls from a resultset row starting
231      * from a specified offset. This is done so that you can select
232      * other rows than just those needed for this object. You may
233      * for example want to create two objects from the same row.
234      *
235      * @throws TorqueException Any exceptions caught during processing will be
236      * rethrown wrapped into a TorqueException.
237      */

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

271     public static void populateObject(Record row,
272                                       int offset,
273                                       Uom obj)
274         throws TorqueException
275     {
276         try
277         {
278                 obj.setUomId(row.getValue(offset + 0).asInt());
279                   obj.setUomName(row.getValue(offset + 1).asString());
280                   obj.setUomCode(row.getValue(offset + 2).asString());
281               }
282         catch (DataSetException e)
283         {
284             throw new TorqueException(e);
285         }
286     }
287
288     /**
289      * Method to do selects.
290      *
291      * @param criteria object used to create the SELECT statement.
292      * @return List of selected Objects
293      * @throws TorqueException Any exceptions caught during processing will be
294      * rethrown wrapped into a TorqueException.
295      */

296     public static List JavaDoc doSelect(Criteria criteria) throws TorqueException
297     {
298         return populateObjects(doSelectVillageRecords(criteria));
299     }
300
301     /**
302      * Method to do selects within a transaction.
303      *
304      * @param criteria object used to create the SELECT statement.
305      * @param con the connection to use
306      * @return List of selected Objects
307      * @throws TorqueException Any exceptions caught during processing will be
308      * rethrown wrapped into a TorqueException.
309      */

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

326     public static List JavaDoc doSelectVillageRecords(Criteria criteria)
327         throws TorqueException
328     {
329         return BaseUomPeer
330             .doSelectVillageRecords(criteria, (Connection JavaDoc) null);
331     }
332
333     /**
334      * Grabs the raw Village records to be formed into objects.
335      * This method should be used for transactions
336      *
337      * @param criteria object used to create the SELECT statement.
338      * @param con the connection to use
339      * @throws TorqueException Any exceptions caught during processing will be
340      * rethrown wrapped into a TorqueException.
341      */

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

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

396     public static Class JavaDoc getOMClass()
397         throws TorqueException
398     {
399         return CLASS_DEFAULT;
400     }
401
402     /**
403      * Method to do updates.
404      *
405      * @param criteria object containing data that is used to create the UPDATE
406      * statement.
407      * @throws TorqueException Any exceptions caught during processing will be
408      * rethrown wrapped into a TorqueException.
409      */

410     public static void doUpdate(Criteria criteria) throws TorqueException
411     {
412          BaseUomPeer
413             .doUpdate(criteria, (Connection JavaDoc) null);
414     }
415
416     /**
417      * Method to do updates. This method is to be used during a transaction,
418      * otherwise use the doUpdate(Criteria) method. It will take care of
419      * the connection details internally.
420      *
421      * @param criteria object containing data that is used to create the UPDATE
422      * statement.
423      * @param con the connection to use
424      * @throws TorqueException Any exceptions caught during processing will be
425      * rethrown wrapped into a TorqueException.
426      */

427     public static void doUpdate(Criteria criteria, Connection JavaDoc con)
428         throws TorqueException
429     {
430         Criteria selectCriteria = new Criteria(DATABASE_NAME, 2);
431                    selectCriteria.put(UOM_ID, criteria.remove(UOM_ID));
432                           
433         setDbName(criteria);
434
435         if (con == null)
436         {
437             BasePeer.doUpdate(selectCriteria, criteria);
438         }
439         else
440         {
441             BasePeer.doUpdate(selectCriteria, criteria, con);
442         }
443     }
444
445     /**
446      * Method to do deletes.
447      *
448      * @param criteria object containing data that is used DELETE from database.
449      * @throws TorqueException Any exceptions caught during processing will be
450      * rethrown wrapped into a TorqueException.
451      */

452      public static void doDelete(Criteria criteria) throws TorqueException
453      {
454          UomPeer
455             .doDelete(criteria, (Connection JavaDoc) null);
456      }
457
458     /**
459      * Method to do deletes. This method is to be used during a transaction,
460      * otherwise use the doDelete(Criteria) method. It will take care of
461      * the connection details internally.
462      *
463      * @param criteria object containing data that is used DELETE from database.
464      * @param con the connection to use
465      * @throws TorqueException Any exceptions caught during processing will be
466      * rethrown wrapped into a TorqueException.
467      */

468      public static void doDelete(Criteria criteria, Connection JavaDoc con)
469         throws TorqueException
470      {
471                     
472         setDbName(criteria);
473
474         if (con == null)
475         {
476             BasePeer.doDelete(criteria);
477         }
478         else
479         {
480             BasePeer.doDelete(criteria, con);
481         }
482      }
483
484     /**
485      * Method to do selects
486      *
487      * @throws TorqueException Any exceptions caught during processing will be
488      * rethrown wrapped into a TorqueException.
489      */

490     public static List JavaDoc doSelect(Uom obj) throws TorqueException
491     {
492         return doSelect(buildSelectCriteria(obj));
493     }
494
495     /**
496      * Method to do inserts
497      *
498      * @throws TorqueException Any exceptions caught during processing will be
499      * rethrown wrapped into a TorqueException.
500      */

501     public static void doInsert(Uom obj) throws TorqueException
502     {
503           obj.setPrimaryKey(doInsert(buildCriteria(obj)));
504           obj.setNew(false);
505         obj.setModified(false);
506     }
507
508     /**
509      * @param obj the data object to update in the database.
510      * @throws TorqueException Any exceptions caught during processing will be
511      * rethrown wrapped into a TorqueException.
512      */

513     public static void doUpdate(Uom obj) throws TorqueException
514     {
515         doUpdate(buildCriteria(obj));
516         obj.setModified(false);
517     }
518
519     /**
520      * @param obj the data object to delete in the database.
521      * @throws TorqueException Any exceptions caught during processing will be
522      * rethrown wrapped into a TorqueException.
523      */

524     public static void doDelete(Uom obj) throws TorqueException
525     {
526         doDelete(buildSelectCriteria(obj));
527     }
528
529     /**
530      * Method to do inserts. This method is to be used during a transaction,
531      * otherwise use the doInsert(Uom) method. It will take
532      * care of the connection details internally.
533      *
534      * @param obj the data object to insert into the database.
535      * @param con the connection to use
536      * @throws TorqueException Any exceptions caught during processing will be
537      * rethrown wrapped into a TorqueException.
538      */

539     public static void doInsert(Uom obj, Connection JavaDoc con)
540         throws TorqueException
541     {
542           obj.setPrimaryKey(doInsert(buildCriteria(obj), con));
543           obj.setNew(false);
544         obj.setModified(false);
545     }
546
547     /**
548      * Method to do update. This method is to be used during a transaction,
549      * otherwise use the doUpdate(Uom) method. It will take
550      * care of the connection details internally.
551      *
552      * @param obj the data object to update in the database.
553      * @param con the connection to use
554      * @throws TorqueException Any exceptions caught during processing will be
555      * rethrown wrapped into a TorqueException.
556      */

557     public static void doUpdate(Uom obj, Connection JavaDoc con)
558         throws TorqueException
559     {
560         doUpdate(buildCriteria(obj), con);
561         obj.setModified(false);
562     }
563
564     /**
565      * Method to delete. This method is to be used during a transaction,
566      * otherwise use the doDelete(Uom) method. It will take
567      * care of the connection details internally.
568      *
569      * @param obj the data object to delete in the database.
570      * @param con the connection to use
571      * @throws TorqueException Any exceptions caught during processing will be
572      * rethrown wrapped into a TorqueException.
573      */

574     public static void doDelete(Uom obj, Connection JavaDoc con)
575         throws TorqueException
576     {
577         doDelete(buildSelectCriteria(obj), con);
578     }
579
580     /**
581      * Method to do deletes.
582      *
583      * @param pk ObjectKey that is used DELETE from database.
584      * @throws TorqueException Any exceptions caught during processing will be
585      * rethrown wrapped into a TorqueException.
586      */

587     public static void doDelete(ObjectKey pk) throws TorqueException
588     {
589         BaseUomPeer
590            .doDelete(pk, (Connection JavaDoc) null);
591     }
592
593     /**
594      * Method to delete. This method is to be used during a transaction,
595      * otherwise use the doDelete(ObjectKey) method. It will take
596      * care of the connection details internally.
597      *
598      * @param pk the primary key for the object to delete in the database.
599      * @param con the connection to use
600      * @throws TorqueException Any exceptions caught during processing will be
601      * rethrown wrapped into a TorqueException.
602      */

603     public static void doDelete(ObjectKey pk, Connection JavaDoc con)
604         throws TorqueException
605     {
606         doDelete(buildCriteria(pk), con);
607     }
608
609     /** Build a Criteria object from an ObjectKey */
610     public static Criteria buildCriteria( ObjectKey pk )
611     {
612         Criteria criteria = new Criteria();
613               criteria.add(UOM_ID, pk);
614           return criteria;
615      }
616
617     /** Build a Criteria object from the data object for this peer */
618     public static Criteria buildCriteria( Uom obj )
619     {
620         Criteria criteria = new Criteria(DATABASE_NAME);
621               if (!obj.isNew())
622             criteria.add(UOM_ID, obj.getUomId());
623               criteria.add(UOM_NAME, obj.getUomName());
624               criteria.add(UOM_CODE, obj.getUomCode());
625           return criteria;
626     }
627
628     /** Build a Criteria object from the data object for this peer, skipping all binary columns */
629     public static Criteria buildSelectCriteria( Uom obj )
630     {
631         Criteria criteria = new Criteria(DATABASE_NAME);
632               if (!obj.isNew())
633                     criteria.add(UOM_ID, obj.getUomId());
634                           criteria.add(UOM_NAME, obj.getUomName());
635                           criteria.add(UOM_CODE, obj.getUomCode());
636               return criteria;
637     }
638  
639     
640         /**
641      * Retrieve a single object by pk
642      *
643      * @param pk the primary key
644      * @throws TorqueException Any exceptions caught during processing will be
645      * rethrown wrapped into a TorqueException.
646      * @throws NoRowsException Primary key was not found in database.
647      * @throws TooManyRowsException Primary key was not found in database.
648      */

649     public static Uom retrieveByPK(int pk)
650         throws TorqueException, NoRowsException, TooManyRowsException
651     {
652         return retrieveByPK(SimpleKey.keyFor(pk));
653     }
654
655     /**
656      * Retrieve a single object by pk
657      *
658      * @param pk the primary key
659      * @param con the connection to use
660      * @throws TorqueException Any exceptions caught during processing will be
661      * rethrown wrapped into a TorqueException.
662      * @throws NoRowsException Primary key was not found in database.
663      * @throws TooManyRowsException Primary key was not found in database.
664      */

665     public static Uom retrieveByPK(int pk, Connection JavaDoc con)
666         throws TorqueException, NoRowsException, TooManyRowsException
667     {
668         return retrieveByPK(SimpleKey.keyFor(pk), con);
669     }
670   
671     /**
672      * Retrieve a single object by pk
673      *
674      * @param pk the primary key
675      * @throws TorqueException Any exceptions caught during processing will be
676      * rethrown wrapped into a TorqueException.
677      * @throws NoRowsException Primary key was not found in database.
678      * @throws TooManyRowsException Primary key was not found in database.
679      */

680     public static Uom retrieveByPK(ObjectKey pk)
681         throws TorqueException, NoRowsException, TooManyRowsException
682     {
683         Connection JavaDoc db = null;
684         Uom retVal = null;
685         try
686         {
687             db = Torque.getConnection(DATABASE_NAME);
688             retVal = retrieveByPK(pk, db);
689         }
690         finally
691         {
692             Torque.closeConnection(db);
693         }
694         return(retVal);
695     }
696
697     /**
698      * Retrieve a single object by pk
699      *
700      * @param pk the primary key
701      * @param con the connection to use
702      * @throws TorqueException Any exceptions caught during processing will be
703      * rethrown wrapped into a TorqueException.
704      * @throws NoRowsException Primary key was not found in database.
705      * @throws TooManyRowsException Primary key was not found in database.
706      */

707     public static Uom retrieveByPK(ObjectKey pk, Connection JavaDoc con)
708         throws TorqueException, NoRowsException, TooManyRowsException
709     {
710         Criteria criteria = buildCriteria(pk);
711         List JavaDoc v = doSelect(criteria, con);
712         if (v.size() == 0)
713         {
714             throw new NoRowsException("Failed to select a row.");
715         }
716         else if (v.size() > 1)
717         {
718             throw new TooManyRowsException("Failed to select only one row.");
719         }
720         else
721         {
722             return (Uom)v.get(0);
723         }
724     }
725
726     /**
727      * Retrieve a multiple objects by pk
728      *
729      * @param pks List of primary keys
730      * @throws TorqueException Any exceptions caught during processing will be
731      * rethrown wrapped into a TorqueException.
732      */

733     public static List JavaDoc retrieveByPKs(List JavaDoc pks)
734         throws TorqueException
735     {
736         Connection JavaDoc db = null;
737         List JavaDoc retVal = null;
738         try
739         {
740            db = Torque.getConnection(DATABASE_NAME);
741            retVal = retrieveByPKs(pks, db);
742         }
743         finally
744         {
745             Torque.closeConnection(db);
746         }
747         return(retVal);
748     }
749
750     /**
751      * Retrieve a multiple objects by pk
752      *
753      * @param pks List of primary keys
754      * @param dbcon the connection to use
755      * @throws TorqueException Any exceptions caught during processing will be
756      * rethrown wrapped into a TorqueException.
757      */

758     public static List JavaDoc retrieveByPKs( List JavaDoc pks, Connection JavaDoc dbcon )
759         throws TorqueException
760     {
761         List JavaDoc objs = null;
762         if (pks == null || pks.size() == 0)
763         {
764             objs = new LinkedList JavaDoc();
765         }
766         else
767         {
768             Criteria criteria = new Criteria();
769               criteria.addIn( UOM_ID, pks );
770           objs = doSelect(criteria, dbcon);
771         }
772         return objs;
773     }
774
775  
776
777
778
779         
780   
781   
782     
783   
784       /**
785      * Returns the TableMap related to this peer. This method is not
786      * needed for general use but a specific application could have a need.
787      *
788      * @throws TorqueException Any exceptions caught during processing will be
789      * rethrown wrapped into a TorqueException.
790      */

791     protected static TableMap getTableMap()
792         throws TorqueException
793     {
794         return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME);
795     }
796    
797     private static void setDbName(Criteria crit)
798     {
799         // Set the correct dbName if it has not been overridden
800
// crit.getDbName will return the same object if not set to
801
// another value so == check is okay and faster
802
if (crit.getDbName() == Torque.getDefaultDB())
803         {
804             crit.setDbName(DATABASE_NAME);
805         }
806     }
807 }
808
Popular Tags