KickJava   Java API By Example, From Geeks To Geeks.

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


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  */

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

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

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

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

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

201     public static ObjectKey doInsert(Criteria criteria, Connection JavaDoc con)
202         throws TorqueException
203     {
204                           // check for conversion from boolean to int
205
if (criteria.containsKey(ACTIVE))
206         {
207             Object JavaDoc possibleBoolean = criteria.get(ACTIVE);
208             if (possibleBoolean instanceof Boolean JavaDoc)
209             {
210                 criteria.add(ACTIVE, ((Boolean JavaDoc) possibleBoolean).booleanValue() ? 1 : 0);
211             }
212          }
213                   // check for conversion from boolean to int
214
if (criteria.containsKey(MODIFIABLE))
215         {
216             Object JavaDoc possibleBoolean = criteria.get(MODIFIABLE);
217             if (possibleBoolean instanceof Boolean JavaDoc)
218             {
219                 criteria.add(MODIFIABLE, ((Boolean JavaDoc) possibleBoolean).booleanValue() ? 1 : 0);
220             }
221          }
222             
223         setDbName(criteria);
224
225         if (con == null)
226         {
227             return BasePeer.doInsert(criteria);
228         }
229         else
230         {
231             return BasePeer.doInsert(criteria, con);
232         }
233     }
234
235     /**
236      * Add all the columns needed to create a new object.
237      *
238      * @param criteria object containing the columns to add.
239      * @throws TorqueException Any exceptions caught during processing will be
240      * rethrown wrapped into a TorqueException.
241      */

242     public static void addSelectColumns(Criteria criteria)
243             throws TorqueException
244     {
245           criteria.addSelectColumn(LIST_ID);
246           criteria.addSelectColumn(NAME);
247           criteria.addSelectColumn(ACTIVE);
248           criteria.addSelectColumn(MODIFIABLE);
249           criteria.addSelectColumn(USER_ID);
250       }
251
252     /**
253      * Create a new object of type cls from a resultset row starting
254      * from a specified offset. This is done so that you can select
255      * other rows than just those needed for this object. You may
256      * for example want to create two objects from the same row.
257      *
258      * @throws TorqueException Any exceptions caught during processing will be
259      * rethrown wrapped into a TorqueException.
260      */

261     public static MITList row2Object(Record row,
262                                              int offset,
263                                              Class JavaDoc cls)
264         throws TorqueException
265     {
266         try
267         {
268             MITList obj = (MITList) cls.newInstance();
269             MITListPeer.populateObject(row, offset, obj);
270                   obj.setModified(false);
271               obj.setNew(false);
272
273             return obj;
274         }
275         catch (InstantiationException JavaDoc e)
276         {
277             throw new TorqueException(e);
278         }
279         catch (IllegalAccessException JavaDoc e)
280         {
281             throw new TorqueException(e);
282         }
283     }
284
285     /**
286      * Populates an object from a resultset row starting
287      * from a specified offset. This is done so that you can select
288      * other rows than just those needed for this object. You may
289      * for example want to create two objects from the same row.
290      *
291      * @throws TorqueException Any exceptions caught during processing will be
292      * rethrown wrapped into a TorqueException.
293      */

294     public static void populateObject(Record row,
295                                       int offset,
296                                       MITList obj)
297         throws TorqueException
298     {
299         try
300         {
301                 obj.setListId(row.getValue(offset + 0).asLongObj());
302                   obj.setName(row.getValue(offset + 1).asString());
303                   obj.setActive(row.getValue(offset + 2).asBoolean());
304                   obj.setModifiable(row.getValue(offset + 3).asBoolean());
305                   obj.setUserId(row.getValue(offset + 4).asIntegerObj());
306               }
307         catch (DataSetException e)
308         {
309             throw new TorqueException(e);
310         }
311     }
312
313     /**
314      * Method to do selects.
315      *
316      * @param criteria object used to create the SELECT statement.
317      * @return List of selected Objects
318      * @throws TorqueException Any exceptions caught during processing will be
319      * rethrown wrapped into a TorqueException.
320      */

321     public static List JavaDoc doSelect(Criteria criteria) throws TorqueException
322     {
323         return populateObjects(doSelectVillageRecords(criteria));
324     }
325
326     /**
327      * Method to do selects within a transaction.
328      *
329      * @param criteria object used to create the SELECT statement.
330      * @param con the connection to use
331      * @return List of selected Objects
332      * @throws TorqueException Any exceptions caught during processing will be
333      * rethrown wrapped into a TorqueException.
334      */

335     public static List JavaDoc doSelect(Criteria criteria, Connection JavaDoc con)
336         throws TorqueException
337     {
338         return populateObjects(doSelectVillageRecords(criteria, con));
339     }
340
341     /**
342      * Grabs the raw Village records to be formed into objects.
343      * This method handles connections internally. The Record objects
344      * returned by this method should be considered readonly. Do not
345      * alter the data and call save(), your results may vary, but are
346      * certainly likely to result in hard to track MT bugs.
347      *
348      * @throws TorqueException Any exceptions caught during processing will be
349      * rethrown wrapped into a TorqueException.
350      */

351     public static List JavaDoc doSelectVillageRecords(Criteria criteria)
352         throws TorqueException
353     {
354         return BaseMITListPeer
355             .doSelectVillageRecords(criteria, (Connection JavaDoc) null);
356     }
357
358     /**
359      * Grabs the raw Village records to be formed into objects.
360      * This method should be used for transactions
361      *
362      * @param criteria object used to create the SELECT statement.
363      * @param con the connection to use
364      * @throws TorqueException Any exceptions caught during processing will be
365      * rethrown wrapped into a TorqueException.
366      */

367     public static List JavaDoc doSelectVillageRecords(Criteria criteria, Connection JavaDoc con)
368         throws TorqueException
369     {
370         if (criteria.getSelectColumns().size() == 0)
371         {
372             addSelectColumns(criteria);
373         }
374
375                           // check for conversion from boolean to int
376
if (criteria.containsKey(ACTIVE))
377         {
378             Object JavaDoc possibleBoolean = criteria.get(ACTIVE);
379             if (possibleBoolean instanceof Boolean JavaDoc)
380             {
381                 criteria.add(ACTIVE, ((Boolean JavaDoc) possibleBoolean).booleanValue() ? 1 : 0);
382             }
383          }
384                   // check for conversion from boolean to int
385
if (criteria.containsKey(MODIFIABLE))
386         {
387             Object JavaDoc possibleBoolean = criteria.get(MODIFIABLE);
388             if (possibleBoolean instanceof Boolean JavaDoc)
389             {
390                 criteria.add(MODIFIABLE, ((Boolean JavaDoc) possibleBoolean).booleanValue() ? 1 : 0);
391             }
392          }
393             
394         setDbName(criteria);
395
396         // BasePeer returns a List of Value (Village) arrays. The array
397
// order follows the order columns were placed in the Select clause.
398
if (con == null)
399         {
400             return BasePeer.doSelect(criteria);
401         }
402         else
403         {
404             return BasePeer.doSelect(criteria, con);
405         }
406     }
407
408     /**
409      * The returned List will contain objects of the default type or
410      * objects that inherit from the default.
411      *
412      * @throws TorqueException Any exceptions caught during processing will be
413      * rethrown wrapped into a TorqueException.
414      */

415     public static List JavaDoc populateObjects(List JavaDoc records)
416         throws TorqueException
417     {
418         List JavaDoc results = new ArrayList JavaDoc(records.size());
419
420         // populate the object(s)
421
for (int i = 0; i < records.size(); i++)
422         {
423             Record row = (Record) records.get(i);
424               results.add(MITListPeer.row2Object(row, 1,
425                 MITListPeer.getOMClass()));
426           }
427         return results;
428     }
429  
430
431     /**
432      * The class that the Peer will make instances of.
433      * If the BO is abstract then you must implement this method
434      * in the BO.
435      *
436      * @throws TorqueException Any exceptions caught during processing will be
437      * rethrown wrapped into a TorqueException.
438      */

439     public static Class JavaDoc getOMClass()
440         throws TorqueException
441     {
442         return CLASS_DEFAULT;
443     }
444
445     /**
446      * Method to do updates.
447      *
448      * @param criteria object containing data that is used to create the UPDATE
449      * statement.
450      * @throws TorqueException Any exceptions caught during processing will be
451      * rethrown wrapped into a TorqueException.
452      */

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

470     public static void doUpdate(Criteria criteria, Connection JavaDoc con)
471         throws TorqueException
472     {
473         Criteria selectCriteria = new Criteria(DATABASE_NAME, 2);
474                    selectCriteria.put(LIST_ID, criteria.remove(LIST_ID));
475                             // check for conversion from boolean to int
476
if (criteria.containsKey(ACTIVE))
477         {
478             Object JavaDoc possibleBoolean = criteria.get(ACTIVE);
479             if (possibleBoolean instanceof Boolean JavaDoc)
480             {
481                 criteria.add(ACTIVE, ((Boolean JavaDoc) possibleBoolean).booleanValue() ? 1 : 0);
482             }
483          }
484                       // check for conversion from boolean to int
485
if (criteria.containsKey(MODIFIABLE))
486         {
487             Object JavaDoc possibleBoolean = criteria.get(MODIFIABLE);
488             if (possibleBoolean instanceof Boolean JavaDoc)
489             {
490                 criteria.add(MODIFIABLE, ((Boolean JavaDoc) possibleBoolean).booleanValue() ? 1 : 0);
491             }
492          }
493                     
494         setDbName(criteria);
495
496         if (con == null)
497         {
498             BasePeer.doUpdate(selectCriteria, criteria);
499         }
500         else
501         {
502             BasePeer.doUpdate(selectCriteria, criteria, con);
503         }
504     }
505
506     /**
507      * Method to do deletes.
508      *
509      * @param criteria object containing data that is used DELETE from database.
510      * @throws TorqueException Any exceptions caught during processing will be
511      * rethrown wrapped into a TorqueException.
512      */

513      public static void doDelete(Criteria criteria) throws TorqueException
514      {
515          MITListPeer
516             .doDelete(criteria, (Connection JavaDoc) null);
517      }
518
519     /**
520      * Method to do deletes. This method is to be used during a transaction,
521      * otherwise use the doDelete(Criteria) method. It will take care of
522      * the connection details internally.
523      *
524      * @param criteria object containing data that is used DELETE from database.
525      * @param con the connection to use
526      * @throws TorqueException Any exceptions caught during processing will be
527      * rethrown wrapped into a TorqueException.
528      */

529      public static void doDelete(Criteria criteria, Connection JavaDoc con)
530         throws TorqueException
531      {
532                           // check for conversion from boolean to int
533
if (criteria.containsKey(ACTIVE))
534         {
535             Object JavaDoc possibleBoolean = criteria.get(ACTIVE);
536             if (possibleBoolean instanceof Boolean JavaDoc)
537             {
538                 criteria.add(ACTIVE, ((Boolean JavaDoc) possibleBoolean).booleanValue() ? 1 : 0);
539             }
540          }
541                   // check for conversion from boolean to int
542
if (criteria.containsKey(MODIFIABLE))
543         {
544             Object JavaDoc possibleBoolean = criteria.get(MODIFIABLE);
545             if (possibleBoolean instanceof Boolean JavaDoc)
546             {
547                 criteria.add(MODIFIABLE, ((Boolean JavaDoc) possibleBoolean).booleanValue() ? 1 : 0);
548             }
549          }
550             
551         setDbName(criteria);
552
553         if (con == null)
554         {
555             BasePeer.doDelete(criteria);
556         }
557         else
558         {
559             BasePeer.doDelete(criteria, con);
560         }
561      }
562
563     /**
564      * Method to do selects
565      *
566      * @throws TorqueException Any exceptions caught during processing will be
567      * rethrown wrapped into a TorqueException.
568      */

569     public static List JavaDoc doSelect(MITList obj) throws TorqueException
570     {
571         return doSelect(buildSelectCriteria(obj));
572     }
573
574     /**
575      * Method to do inserts
576      *
577      * @throws TorqueException Any exceptions caught during processing will be
578      * rethrown wrapped into a TorqueException.
579      */

580     public static void doInsert(MITList obj) throws TorqueException
581     {
582           obj.setPrimaryKey(doInsert(buildCriteria(obj)));
583           obj.setNew(false);
584         obj.setModified(false);
585     }
586
587     /**
588      * @param obj the data object to update in the database.
589      * @throws TorqueException Any exceptions caught during processing will be
590      * rethrown wrapped into a TorqueException.
591      */

592     public static void doUpdate(MITList obj) throws TorqueException
593     {
594         doUpdate(buildCriteria(obj));
595         obj.setModified(false);
596     }
597
598     /**
599      * @param obj the data object to delete in the database.
600      * @throws TorqueException Any exceptions caught during processing will be
601      * rethrown wrapped into a TorqueException.
602      */

603     public static void doDelete(MITList obj) throws TorqueException
604     {
605         doDelete(buildSelectCriteria(obj));
606     }
607
608     /**
609      * Method to do inserts. This method is to be used during a transaction,
610      * otherwise use the doInsert(MITList) method. It will take
611      * care of the connection details internally.
612      *
613      * @param obj the data object to insert into the database.
614      * @param con the connection to use
615      * @throws TorqueException Any exceptions caught during processing will be
616      * rethrown wrapped into a TorqueException.
617      */

618     public static void doInsert(MITList obj, Connection JavaDoc con)
619         throws TorqueException
620     {
621           obj.setPrimaryKey(doInsert(buildCriteria(obj), con));
622           obj.setNew(false);
623         obj.setModified(false);
624     }
625
626     /**
627      * Method to do update. This method is to be used during a transaction,
628      * otherwise use the doUpdate(MITList) method. It will take
629      * care of the connection details internally.
630      *
631      * @param obj the data object to update in the database.
632      * @param con the connection to use
633      * @throws TorqueException Any exceptions caught during processing will be
634      * rethrown wrapped into a TorqueException.
635      */

636     public static void doUpdate(MITList obj, Connection JavaDoc con)
637         throws TorqueException
638     {
639         doUpdate(buildCriteria(obj), con);
640         obj.setModified(false);
641     }
642
643     /**
644      * Method to delete. This method is to be used during a transaction,
645      * otherwise use the doDelete(MITList) method. It will take
646      * care of the connection details internally.
647      *
648      * @param obj the data object to delete in the database.
649      * @param con the connection to use
650      * @throws TorqueException Any exceptions caught during processing will be
651      * rethrown wrapped into a TorqueException.
652      */

653     public static void doDelete(MITList obj, Connection JavaDoc con)
654         throws TorqueException
655     {
656         doDelete(buildSelectCriteria(obj), con);
657     }
658
659     /**
660      * Method to do deletes.
661      *
662      * @param pk ObjectKey that is used DELETE from database.
663      * @throws TorqueException Any exceptions caught during processing will be
664      * rethrown wrapped into a TorqueException.
665      */

666     public static void doDelete(ObjectKey pk) throws TorqueException
667     {
668         BaseMITListPeer
669            .doDelete(pk, (Connection JavaDoc) null);
670     }
671
672     /**
673      * Method to delete. This method is to be used during a transaction,
674      * otherwise use the doDelete(ObjectKey) method. It will take
675      * care of the connection details internally.
676      *
677      * @param pk the primary key for the object to delete in the database.
678      * @param con the connection to use
679      * @throws TorqueException Any exceptions caught during processing will be
680      * rethrown wrapped into a TorqueException.
681      */

682     public static void doDelete(ObjectKey pk, Connection JavaDoc con)
683         throws TorqueException
684     {
685         doDelete(buildCriteria(pk), con);
686     }
687
688     /** Build a Criteria object from an ObjectKey */
689     public static Criteria buildCriteria( ObjectKey pk )
690     {
691         Criteria criteria = new Criteria();
692               criteria.add(LIST_ID, pk);
693           return criteria;
694      }
695
696     /** Build a Criteria object from the data object for this peer */
697     public static Criteria buildCriteria( MITList obj )
698     {
699         Criteria criteria = new Criteria(DATABASE_NAME);
700               if (!obj.isNew())
701             criteria.add(LIST_ID, obj.getListId());
702               criteria.add(NAME, obj.getName());
703               criteria.add(ACTIVE, obj.getActive());
704               criteria.add(MODIFIABLE, obj.getModifiable());
705               criteria.add(USER_ID, obj.getUserId());
706           return criteria;
707     }
708
709     /** Build a Criteria object from the data object for this peer, skipping all binary columns */
710     public static Criteria buildSelectCriteria( MITList obj )
711     {
712         Criteria criteria = new Criteria(DATABASE_NAME);
713               if (!obj.isNew())
714                     criteria.add(LIST_ID, obj.getListId());
715                           criteria.add(NAME, obj.getName());
716                           criteria.add(ACTIVE, obj.getActive());
717                           criteria.add(MODIFIABLE, obj.getModifiable());
718                           criteria.add(USER_ID, obj.getUserId());
719               return criteria;
720     }
721  
722     
723         /**
724      * Retrieve a single object by pk
725      *
726      * @param pk the primary key
727      * @throws TorqueException Any exceptions caught during processing will be
728      * rethrown wrapped into a TorqueException.
729      * @throws NoRowsException Primary key was not found in database.
730      * @throws TooManyRowsException Primary key was not found in database.
731      */

732     public static MITList retrieveByPK(Long JavaDoc pk)
733         throws TorqueException, NoRowsException, TooManyRowsException
734     {
735         return retrieveByPK(SimpleKey.keyFor(pk));
736     }
737
738     /**
739      * Retrieve a single object by pk
740      *
741      * @param pk the primary key
742      * @param con the connection to use
743      * @throws TorqueException Any exceptions caught during processing will be
744      * rethrown wrapped into a TorqueException.
745      * @throws NoRowsException Primary key was not found in database.
746      * @throws TooManyRowsException Primary key was not found in database.
747      */

748     public static MITList retrieveByPK(Long JavaDoc pk, Connection JavaDoc con)
749         throws TorqueException, NoRowsException, TooManyRowsException
750     {
751         return retrieveByPK(SimpleKey.keyFor(pk), con);
752     }
753   
754     /**
755      * Retrieve a single object by pk
756      *
757      * @param pk the primary key
758      * @throws TorqueException Any exceptions caught during processing will be
759      * rethrown wrapped into a TorqueException.
760      * @throws NoRowsException Primary key was not found in database.
761      * @throws TooManyRowsException Primary key was not found in database.
762      */

763     public static MITList retrieveByPK(ObjectKey pk)
764         throws TorqueException, NoRowsException, TooManyRowsException
765     {
766         Connection JavaDoc db = null;
767         MITList retVal = null;
768         try
769         {
770             db = Torque.getConnection(DATABASE_NAME);
771             retVal = retrieveByPK(pk, db);
772         }
773         finally
774         {
775             Torque.closeConnection(db);
776         }
777         return(retVal);
778     }
779
780     /**
781      * Retrieve a single object by pk
782      *
783      * @param pk the primary key
784      * @param con the connection to use
785      * @throws TorqueException Any exceptions caught during processing will be
786      * rethrown wrapped into a TorqueException.
787      * @throws NoRowsException Primary key was not found in database.
788      * @throws TooManyRowsException Primary key was not found in database.
789      */

790     public static MITList retrieveByPK(ObjectKey pk, Connection JavaDoc con)
791         throws TorqueException, NoRowsException, TooManyRowsException
792     {
793         Criteria criteria = buildCriteria(pk);
794         List JavaDoc v = doSelect(criteria, con);
795         if (v.size() == 0)
796         {
797             throw new NoRowsException("Failed to select a row.");
798         }
799         else if (v.size() > 1)
800         {
801             throw new TooManyRowsException("Failed to select only one row.");
802         }
803         else
804         {
805             return (MITList)v.get(0);
806         }
807     }
808
809     /**
810      * Retrieve a multiple objects by pk
811      *
812      * @param pks List of primary keys
813      * @throws TorqueException Any exceptions caught during processing will be
814      * rethrown wrapped into a TorqueException.
815      */

816     public static List JavaDoc retrieveByPKs(List JavaDoc pks)
817         throws TorqueException
818     {
819         Connection JavaDoc db = null;
820         List JavaDoc retVal = null;
821         try
822         {
823            db = Torque.getConnection(DATABASE_NAME);
824            retVal = retrieveByPKs(pks, db);
825         }
826         finally
827         {
828             Torque.closeConnection(db);
829         }
830         return(retVal);
831     }
832
833     /**
834      * Retrieve a multiple objects by pk
835      *
836      * @param pks List of primary keys
837      * @param dbcon the connection to use
838      * @throws TorqueException Any exceptions caught during processing will be
839      * rethrown wrapped into a TorqueException.
840      */

841     public static List JavaDoc retrieveByPKs( List JavaDoc pks, Connection JavaDoc dbcon )
842         throws TorqueException
843     {
844         List JavaDoc objs = null;
845         if (pks == null || pks.size() == 0)
846         {
847             objs = new LinkedList JavaDoc();
848         }
849         else
850         {
851             Criteria criteria = new Criteria();
852               criteria.addIn( LIST_ID, pks );
853           objs = doSelect(criteria, dbcon);
854         }
855         return objs;
856     }
857
858  
859
860
861
862           
863                                               
864                         
865                 
866
867     /**
868      * selects a collection of MITList objects pre-filled with their
869      * ScarabUserImpl objects.
870      *
871      * This method is protected by default in order to keep the public
872      * api reasonable. You can provide public methods for those you
873      * actually need in MITListPeer.
874      *
875      * @throws TorqueException Any exceptions caught during processing will be
876      * rethrown wrapped into a TorqueException.
877      */

878     protected static List JavaDoc doSelectJoinScarabUserImpl(Criteria criteria)
879         throws TorqueException
880     {
881         setDbName(criteria);
882
883         MITListPeer.addSelectColumns(criteria);
884         int offset = numColumns + 1;
885         ScarabUserImplPeer.addSelectColumns(criteria);
886
887
888                         criteria.addJoin(MITListPeer.USER_ID,
889             ScarabUserImplPeer.USER_ID);
890         
891
892                                                               // check for conversion from boolean to int
893
if (criteria.containsKey(ACTIVE))
894         {
895             Object JavaDoc possibleBoolean = criteria.get(ACTIVE);
896             if (possibleBoolean instanceof Boolean JavaDoc)
897             {
898                 criteria.add(ACTIVE, ((Boolean JavaDoc) possibleBoolean).booleanValue() ? 1 : 0);
899             }
900          }
901                                     // check for conversion from boolean to int
902
if (criteria.containsKey(MODIFIABLE))
903         {
904             Object JavaDoc possibleBoolean = criteria.get(MODIFIABLE);
905             if (possibleBoolean instanceof Boolean JavaDoc)
906             {
907                 criteria.add(MODIFIABLE, ((Boolean JavaDoc) possibleBoolean).booleanValue() ? 1 : 0);
908             }
909          }
910                                     
911         List JavaDoc rows = BasePeer.doSelect(criteria);
912         List JavaDoc results = new ArrayList JavaDoc();
913
914         for (int i = 0; i < rows.size(); i++)
915         {
916             Record row = (Record) rows.get(i);
917
918                             Class JavaDoc omClass = MITListPeer.getOMClass();
919                     MITList obj1 = (MITList) MITListPeer
920                 .row2Object(row, 1, omClass);
921                      omClass = ScarabUserImplPeer.getOMClass();
922                     ScarabUserImpl obj2 = (ScarabUserImpl)ScarabUserImplPeer
923                 .row2Object(row, offset, omClass);
924
925             boolean newObject = true;
926             for (int j = 0; j < results.size(); j++)
927             {
928                 MITList temp_obj1 = (MITList)results.get(j);
929                 ScarabUserImpl temp_obj2 = (ScarabUserImpl)temp_obj1.getScarabUser();
930                 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey()))
931                 {
932                     newObject = false;
933                               temp_obj2.addMITList(obj1);
934                               break;
935                 }
936             }
937                       if (newObject)
938             {
939                 obj2.initMITLists();
940                 obj2.addMITList(obj1);
941             }
942                       results.add(obj1);
943         }
944         return results;
945     }
946                     
947   
948     
949   
950       /**
951      * Returns the TableMap related to this peer. This method is not
952      * needed for general use but a specific application could have a need.
953      *
954      * @throws TorqueException Any exceptions caught during processing will be
955      * rethrown wrapped into a TorqueException.
956      */

957     protected static TableMap getTableMap()
958         throws TorqueException
959     {
960         return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME);
961     }
962    
963     private static void setDbName(Criteria crit)
964     {
965         // Set the correct dbName if it has not been overridden
966
// crit.getDbName will return the same object if not set to
967
// another value so == check is okay and faster
968
if (crit.getDbName() == Torque.getDefaultDB())
969         {
970             crit.setDbName(DATABASE_NAME);
971         }
972     }
973 }
974
Popular Tags