View Javadoc

1   package net.sourceforge.blogentis.om;
2   
3   import java.sql.Connection;
4   import java.sql.SQLException;
5   import java.util.ArrayList;
6   import java.util.LinkedList;
7   import java.util.List;
8   
9   import net.sourceforge.blogentis.om.map.TurbineGroupMapBuilder;
10  
11  import org.apache.torque.NoRowsException;
12  import org.apache.torque.TooManyRowsException;
13  import org.apache.torque.Torque;
14  import org.apache.torque.TorqueException;
15  import org.apache.torque.map.MapBuilder;
16  import org.apache.torque.map.TableMap;
17  import org.apache.torque.om.ObjectKey;
18  import org.apache.torque.om.SimpleKey;
19  import org.apache.torque.util.BasePeer;
20  import org.apache.torque.util.Criteria;
21  
22  import com.workingdogs.village.DataSetException;
23  import com.workingdogs.village.QueryDataSet;
24  import com.workingdogs.village.Record;
25  
26  
27  /***
28   */
29  public abstract class BaseTurbineGroupPeer
30      extends BasePeer
31  {
32  
33      /*** the default database name for this class */
34      public static final String DATABASE_NAME = "blogentis";
35  
36       /*** the table name for this class */
37      public static final String TABLE_NAME = "TURBINE_GROUP";
38  
39      /***
40       * @return the map builder for this peer
41       * @throws TorqueException Any exceptions caught during processing will be
42       *         rethrown wrapped into a TorqueException.
43       */
44      public static MapBuilder getMapBuilder()
45          throws TorqueException
46      {
47          return getMapBuilder(TurbineGroupMapBuilder.CLASS_NAME);
48      }
49  
50        /*** the column name for the GROUP_ID field */
51      public static final String GROUP_ID;
52        /*** the column name for the GROUP_NAME field */
53      public static final String GROUP_NAME;
54        /*** the column name for the OBJECTDATA field */
55      public static final String OBJECTDATA;
56    
57      static
58      {
59            GROUP_ID = "TURBINE_GROUP.GROUP_ID";
60            GROUP_NAME = "TURBINE_GROUP.GROUP_NAME";
61            OBJECTDATA = "TURBINE_GROUP.OBJECTDATA";
62            if (Torque.isInit())
63          {
64              try
65              {
66                  getMapBuilder(TurbineGroupMapBuilder.CLASS_NAME);
67              }
68              catch (Exception e)
69              {
70                  log.error("Could not initialize Peer", e);
71              }
72          }
73          else
74          {
75              Torque.registerMapBuilder(TurbineGroupMapBuilder.CLASS_NAME);
76          }
77      }
78   
79      /*** number of columns for this peer */
80      public static final int numColumns =  3;
81  
82      /*** A class that can be returned by this peer. */
83      protected static final String CLASSNAME_DEFAULT =
84          "net.sourceforge.blogentis.om.TurbineGroup";
85  
86      /*** A class that can be returned by this peer. */
87      protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
88  
89      /***
90       * Class object initialization method.
91       *
92       * @param className name of the class to initialize
93       * @return the initialized class
94       */
95      private static Class initClass(String className)
96      {
97          Class c = null;
98          try
99          {
100             c = Class.forName(className);
101         }
102         catch (Throwable t)
103         {
104             log.error("A FATAL ERROR has occurred which should not "
105                 + "have happened under any circumstance.  Please notify "
106                 + "the Torque developers <torque-dev@db.apache.org> "
107                 + "and give as many details as possible (including the error "
108                 + "stack trace).", t);
109 
110             // Error objects should always be propogated.
111             if (t instanceof Error)
112             {
113                 throw (Error) t.fillInStackTrace();
114             }
115         }
116         return c;
117     }
118 
119     /***
120      * Get the list of objects for a ResultSet.  Please not that your
121      * resultset MUST return columns in the right order.  You can use
122      * getFieldNames() in BaseObject to get the correct sequence.
123      *
124      * @param results the ResultSet
125      * @return the list of objects
126      * @throws TorqueException Any exceptions caught during processing will be
127      *         rethrown wrapped into a TorqueException.
128      */
129     public static List resultSet2Objects(java.sql.ResultSet results)
130             throws TorqueException
131     {
132         try
133         {
134             QueryDataSet qds = null;
135             List rows = null;
136             try
137             {
138                 qds = new QueryDataSet(results);
139                 rows = getSelectResults(qds);
140             }
141             finally
142             {
143                 if (qds != null)
144                 {
145                     qds.close();
146                 }
147             }
148 
149             return populateObjects(rows);
150         }
151         catch (SQLException e)
152         {
153             throw new TorqueException(e);
154         }
155         catch (DataSetException e)
156         {
157             throw new TorqueException(e);
158         }
159     }
160 
161 
162   
163     /***
164      * Method to do inserts.
165      *
166      * @param criteria object used to create the INSERT statement.
167      * @throws TorqueException Any exceptions caught during processing will be
168      *         rethrown wrapped into a TorqueException.
169      */
170     public static ObjectKey doInsert(Criteria criteria)
171         throws TorqueException
172     {
173         return BaseTurbineGroupPeer
174             .doInsert(criteria, (Connection) null);
175     }
176 
177     /***
178      * Method to do inserts.  This method is to be used during a transaction,
179      * otherwise use the doInsert(Criteria) method.  It will take care of
180      * the connection details internally.
181      *
182      * @param criteria object used to create the INSERT statement.
183      * @param con the connection to use
184      * @throws TorqueException Any exceptions caught during processing will be
185      *         rethrown wrapped into a TorqueException.
186      */
187     public static ObjectKey doInsert(Criteria criteria, Connection con)
188         throws TorqueException
189     {
190                     
191         // Set the correct dbName if it has not been overridden
192         // criteria.getDbName will return the same object if not set to
193         // another value so == check is okay and faster
194         if (criteria.getDbName() == Torque.getDefaultDB())
195         {
196             criteria.setDbName(DATABASE_NAME);
197         }
198         if (con == null)
199         {
200             return BasePeer.doInsert(criteria);
201         }
202         else
203         {
204             return BasePeer.doInsert(criteria, con);
205         }
206     }
207 
208     /***
209      * Add all the columns needed to create a new object.
210      *
211      * @param criteria object containing the columns to add.
212      * @throws TorqueException Any exceptions caught during processing will be
213      *         rethrown wrapped into a TorqueException.
214      */
215     public static void addSelectColumns(Criteria criteria)
216             throws TorqueException
217     {
218           criteria.addSelectColumn(GROUP_ID);
219           criteria.addSelectColumn(GROUP_NAME);
220           criteria.addSelectColumn(OBJECTDATA);
221       }
222 
223     /***
224      * Create a new object of type cls from a resultset row starting
225      * from a specified offset.  This is done so that you can select
226      * other rows than just those needed for this object.  You may
227      * for example want to create two objects from the same row.
228      *
229      * @throws TorqueException Any exceptions caught during processing will be
230      *         rethrown wrapped into a TorqueException.
231      */
232     public static TurbineGroup row2Object(Record row,
233                                              int offset,
234                                              Class cls)
235         throws TorqueException
236     {
237         try
238         {
239             TurbineGroup obj = (TurbineGroup) cls.newInstance();
240             TurbineGroupPeer.populateObject(row, offset, obj);
241                   obj.setModified(false);
242               obj.setNew(false);
243 
244             return obj;
245         }
246         catch (InstantiationException e)
247         {
248             throw new TorqueException(e);
249         }
250         catch (IllegalAccessException e)
251         {
252             throw new TorqueException(e);
253         }
254     }
255 
256     /***
257      * Populates an object from a resultset row starting
258      * from a specified offset.  This is done so that you can select
259      * other rows than just those needed for this object.  You may
260      * for example want to create two objects from the same row.
261      *
262      * @throws TorqueException Any exceptions caught during processing will be
263      *         rethrown wrapped into a TorqueException.
264      */
265     public static void populateObject(Record row,
266                                       int offset,
267                                       TurbineGroup obj)
268         throws TorqueException
269     {
270         try
271         {
272                 obj.setGroupId(row.getValue(offset + 0).asInt());
273                   obj.setName(row.getValue(offset + 1).asString());
274                   obj.setObjectdata(row.getValue(offset + 2).asBytes());
275               }
276         catch (DataSetException e)
277         {
278             throw new TorqueException(e);
279         }
280     }
281 
282     /***
283      * Method to do selects.
284      *
285      * @param criteria object used to create the SELECT statement.
286      * @return List of selected Objects
287      * @throws TorqueException Any exceptions caught during processing will be
288      *         rethrown wrapped into a TorqueException.
289      */
290     public static List doSelect(Criteria criteria) throws TorqueException
291     {
292         return populateObjects(doSelectVillageRecords(criteria));
293     }
294 
295     /***
296      * Method to do selects within a transaction.
297      *
298      * @param criteria object used to create the SELECT statement.
299      * @param con the connection to use
300      * @return List of selected Objects
301      * @throws TorqueException Any exceptions caught during processing will be
302      *         rethrown wrapped into a TorqueException.
303      */
304     public static List doSelect(Criteria criteria, Connection con)
305         throws TorqueException
306     {
307         return populateObjects(doSelectVillageRecords(criteria, con));
308     }
309 
310     /***
311      * Grabs the raw Village records to be formed into objects.
312      * This method handles connections internally.  The Record objects
313      * returned by this method should be considered readonly.  Do not
314      * alter the data and call save(), your results may vary, but are
315      * certainly likely to result in hard to track MT bugs.
316      *
317      * @throws TorqueException Any exceptions caught during processing will be
318      *         rethrown wrapped into a TorqueException.
319      */
320     public static List doSelectVillageRecords(Criteria criteria)
321         throws TorqueException
322     {
323         return BaseTurbineGroupPeer
324             .doSelectVillageRecords(criteria, (Connection) null);
325     }
326 
327     /***
328      * Grabs the raw Village records to be formed into objects.
329      * This method should be used for transactions
330      *
331      * @param con the connection to use
332      * @throws TorqueException Any exceptions caught during processing will be
333      *         rethrown wrapped into a TorqueException.
334      */
335     public static List doSelectVillageRecords(Criteria criteria, Connection con)
336         throws TorqueException
337     {
338         if (criteria.getSelectColumns().size() == 0)
339         {
340             addSelectColumns(criteria);
341         }
342 
343                     
344         // Set the correct dbName if it has not been overridden
345         // criteria.getDbName will return the same object if not set to
346         // another value so == check is okay and faster
347         if (criteria.getDbName() == Torque.getDefaultDB())
348         {
349             criteria.setDbName(DATABASE_NAME);
350         }
351         // BasePeer returns a List of Value (Village) arrays.  The array
352         // order follows the order columns were placed in the Select clause.
353         if (con == null)
354         {
355             return BasePeer.doSelect(criteria);
356         }
357         else
358         {
359             return BasePeer.doSelect(criteria, con);
360         }
361     }
362 
363     /***
364      * The returned List will contain objects of the default type or
365      * objects that inherit from the default.
366      *
367      * @throws TorqueException Any exceptions caught during processing will be
368      *         rethrown wrapped into a TorqueException.
369      */
370     public static List populateObjects(List records)
371         throws TorqueException
372     {
373         List results = new ArrayList(records.size());
374 
375         // populate the object(s)
376         for (int i = 0; i < records.size(); i++)
377         {
378             Record row = (Record) records.get(i);
379               results.add(TurbineGroupPeer.row2Object(row, 1,
380                 TurbineGroupPeer.getOMClass()));
381           }
382         return results;
383     }
384  
385 
386     /***
387      * The class that the Peer will make instances of.
388      * If the BO is abstract then you must implement this method
389      * in the BO.
390      *
391      * @throws TorqueException Any exceptions caught during processing will be
392      *         rethrown wrapped into a TorqueException.
393      */
394     public static Class getOMClass()
395         throws TorqueException
396     {
397         return CLASS_DEFAULT;
398     }
399 
400     /***
401      * Method to do updates.
402      *
403      * @param criteria object containing data that is used to create the UPDATE
404      *        statement.
405      * @throws TorqueException Any exceptions caught during processing will be
406      *         rethrown wrapped into a TorqueException.
407      */
408     public static void doUpdate(Criteria criteria) throws TorqueException
409     {
410          BaseTurbineGroupPeer
411             .doUpdate(criteria, (Connection) null);
412     }
413 
414     /***
415      * Method to do updates.  This method is to be used during a transaction,
416      * otherwise use the doUpdate(Criteria) method.  It will take care of
417      * the connection details internally.
418      *
419      * @param criteria object containing data that is used to create the UPDATE
420      *        statement.
421      * @param con the connection to use
422      * @throws TorqueException Any exceptions caught during processing will be
423      *         rethrown wrapped into a TorqueException.
424      */
425     public static void doUpdate(Criteria criteria, Connection con)
426         throws TorqueException
427     {
428         Criteria selectCriteria = new Criteria(DATABASE_NAME, 2);
429                    selectCriteria.put(GROUP_ID, criteria.remove(GROUP_ID));
430                           
431         // Set the correct dbName if it has not been overridden
432         // criteria.getDbName will return the same object if not set to
433         // another value so == check is okay and faster
434         if (criteria.getDbName() == Torque.getDefaultDB())
435         {
436             criteria.setDbName(DATABASE_NAME);
437         }
438         if (con == null)
439         {
440             BasePeer.doUpdate(selectCriteria, criteria);
441         }
442         else
443         {
444             BasePeer.doUpdate(selectCriteria, criteria, con);
445         }
446     }
447 
448     /***
449      * Method to do deletes.
450      *
451      * @param criteria object containing data that is used DELETE from database.
452      * @throws TorqueException Any exceptions caught during processing will be
453      *         rethrown wrapped into a TorqueException.
454      */
455      public static void doDelete(Criteria criteria) throws TorqueException
456      {
457          TurbineGroupPeer
458             .doDelete(criteria, (Connection) null);
459      }
460 
461     /***
462      * Method to do deletes.  This method is to be used during a transaction,
463      * otherwise use the doDelete(Criteria) method.  It will take care of
464      * the connection details internally.
465      *
466      * @param criteria object containing data that is used DELETE from database.
467      * @param con the connection to use
468      * @throws TorqueException Any exceptions caught during processing will be
469      *         rethrown wrapped into a TorqueException.
470      */
471      public static void doDelete(Criteria criteria, Connection con)
472         throws TorqueException
473      {
474                     
475         // Set the correct dbName if it has not been overridden
476         // criteria.getDbName will return the same object if not set to
477         // another value so == check is okay and faster
478         if (criteria.getDbName() == Torque.getDefaultDB())
479         {
480             criteria.setDbName(DATABASE_NAME);
481         }
482         if (con == null)
483         {
484             BasePeer.doDelete(criteria);
485         }
486         else
487         {
488             BasePeer.doDelete(criteria, con);
489         }
490      }
491 
492     /***
493      * Method to do selects
494      *
495      * @throws TorqueException Any exceptions caught during processing will be
496      *         rethrown wrapped into a TorqueException.
497      */
498     public static List doSelect(TurbineGroup obj) throws TorqueException
499     {
500         return doSelect(buildCriteria(obj));
501     }
502 
503     /***
504      * Method to do inserts
505      *
506      * @throws TorqueException Any exceptions caught during processing will be
507      *         rethrown wrapped into a TorqueException.
508      */
509     public static void doInsert(TurbineGroup obj) throws TorqueException
510     {
511           obj.setPrimaryKey(doInsert(buildCriteria(obj)));
512           obj.setNew(false);
513         obj.setModified(false);
514     }
515 
516     /***
517      * @param obj the data object to update in the database.
518      * @throws TorqueException Any exceptions caught during processing will be
519      *         rethrown wrapped into a TorqueException.
520      */
521     public static void doUpdate(TurbineGroup obj) throws TorqueException
522     {
523         doUpdate(buildCriteria(obj));
524         obj.setModified(false);
525     }
526 
527     /***
528      * @param obj the data object to delete in the database.
529      * @throws TorqueException Any exceptions caught during processing will be
530      *         rethrown wrapped into a TorqueException.
531      */
532     public static void doDelete(TurbineGroup obj) throws TorqueException
533     {
534         doDelete(buildCriteria(obj));
535     }
536 
537     /***
538      * Method to do inserts.  This method is to be used during a transaction,
539      * otherwise use the doInsert(TurbineGroup) method.  It will take
540      * care of the connection details internally.
541      *
542      * @param obj the data object to insert into the database.
543      * @param con the connection to use
544      * @throws TorqueException Any exceptions caught during processing will be
545      *         rethrown wrapped into a TorqueException.
546      */
547     public static void doInsert(TurbineGroup obj, Connection con)
548         throws TorqueException
549     {
550           obj.setPrimaryKey(doInsert(buildCriteria(obj), con));
551           obj.setNew(false);
552         obj.setModified(false);
553     }
554 
555     /***
556      * Method to do update.  This method is to be used during a transaction,
557      * otherwise use the doUpdate(TurbineGroup) method.  It will take
558      * care of the connection details internally.
559      *
560      * @param obj the data object to update 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 doUpdate(TurbineGroup obj, Connection con)
566         throws TorqueException
567     {
568         doUpdate(buildCriteria(obj), con);
569         obj.setModified(false);
570     }
571 
572     /***
573      * Method to delete.  This method is to be used during a transaction,
574      * otherwise use the doDelete(TurbineGroup) method.  It will take
575      * care of the connection details internally.
576      *
577      * @param obj the data object to delete in the database.
578      * @param con the connection to use
579      * @throws TorqueException Any exceptions caught during processing will be
580      *         rethrown wrapped into a TorqueException.
581      */
582     public static void doDelete(TurbineGroup obj, Connection con)
583         throws TorqueException
584     {
585         doDelete(buildCriteria(obj), con);
586     }
587 
588     /***
589      * Method to do deletes.
590      *
591      * @param pk ObjectKey that is used DELETE from database.
592      * @throws TorqueException Any exceptions caught during processing will be
593      *         rethrown wrapped into a TorqueException.
594      */
595     public static void doDelete(ObjectKey pk) throws TorqueException
596     {
597         BaseTurbineGroupPeer
598            .doDelete(pk, (Connection) null);
599     }
600 
601     /***
602      * Method to delete.  This method is to be used during a transaction,
603      * otherwise use the doDelete(ObjectKey) method.  It will take
604      * care of the connection details internally.
605      *
606      * @param pk the primary key for the object to delete in the database.
607      * @param con the connection to use
608      * @throws TorqueException Any exceptions caught during processing will be
609      *         rethrown wrapped into a TorqueException.
610      */
611     public static void doDelete(ObjectKey pk, Connection con)
612         throws TorqueException
613     {
614         doDelete(buildCriteria(pk), con);
615     }
616 
617     /*** Build a Criteria object from an ObjectKey */
618     public static Criteria buildCriteria( ObjectKey pk )
619     {
620         Criteria criteria = new Criteria();
621               criteria.add(GROUP_ID, pk);
622           return criteria;
623      }
624 
625     /*** Build a Criteria object from the data object for this peer */
626     public static Criteria buildCriteria( TurbineGroup obj )
627     {
628         Criteria criteria = new Criteria(DATABASE_NAME);
629               if (!obj.isNew())
630                 criteria.add(GROUP_ID, obj.getGroupId());
631                   criteria.add(GROUP_NAME, obj.getName());
632                   criteria.add(OBJECTDATA, obj.getObjectdata());
633           return criteria;
634     }
635  
636     
637         /***
638      * Retrieve a single object by pk
639      *
640      * @param pk the primary key
641      * @throws TorqueException Any exceptions caught during processing will be
642      *         rethrown wrapped into a TorqueException.
643      * @throws NoRowsException Primary key was not found in database.
644      * @throws TooManyRowsException Primary key was not found in database.
645      */
646     public static TurbineGroup retrieveByPK(int pk)
647         throws TorqueException, NoRowsException, TooManyRowsException
648     {
649         return retrieveByPK(SimpleKey.keyFor(pk));
650     }
651   
652     /***
653      * Retrieve a single object by pk
654      *
655      * @param pk the primary key
656      * @throws TorqueException Any exceptions caught during processing will be
657      *         rethrown wrapped into a TorqueException.
658      * @throws NoRowsException Primary key was not found in database.
659      * @throws TooManyRowsException Primary key was not found in database.
660      */
661     public static TurbineGroup retrieveByPK(ObjectKey pk)
662         throws TorqueException, NoRowsException, TooManyRowsException
663     {
664         Connection db = null;
665         TurbineGroup retVal = null;
666         try
667         {
668             db = Torque.getConnection(DATABASE_NAME);
669             retVal = retrieveByPK(pk, db);
670         }
671         finally
672         {
673             Torque.closeConnection(db);
674         }
675         return(retVal);
676     }
677 
678     /***
679      * Retrieve a single object by pk
680      *
681      * @param pk the primary key
682      * @param con the connection to use
683      * @throws TorqueException Any exceptions caught during processing will be
684      *         rethrown wrapped into a TorqueException.
685      * @throws NoRowsException Primary key was not found in database.
686      * @throws TooManyRowsException Primary key was not found in database.
687      */
688     public static TurbineGroup retrieveByPK(ObjectKey pk, Connection con)
689         throws TorqueException, NoRowsException, TooManyRowsException
690     {
691         Criteria criteria = buildCriteria(pk);
692         List v = doSelect(criteria, con);
693         if (v.size() == 0)
694         {
695             throw new NoRowsException("Failed to select a row.");
696         }
697         else if (v.size() > 1)
698         {
699             throw new TooManyRowsException("Failed to select only one row.");
700         }
701         else
702         {
703             return (TurbineGroup)v.get(0);
704         }
705     }
706 
707     /***
708      * Retrieve a multiple objects by pk
709      *
710      * @param pks List of primary keys
711      * @throws TorqueException Any exceptions caught during processing will be
712      *         rethrown wrapped into a TorqueException.
713      */
714     public static List retrieveByPKs(List pks)
715         throws TorqueException
716     {
717         Connection db = null;
718         List retVal = null;
719         try
720         {
721            db = Torque.getConnection(DATABASE_NAME);
722            retVal = retrieveByPKs(pks, db);
723         }
724         finally
725         {
726             Torque.closeConnection(db);
727         }
728         return(retVal);
729     }
730 
731     /***
732      * Retrieve a multiple objects by pk
733      *
734      * @param pks List of primary keys
735      * @param dbcon the connection to use
736      * @throws TorqueException Any exceptions caught during processing will be
737      *         rethrown wrapped into a TorqueException.
738      */
739     public static List retrieveByPKs( List pks, Connection dbcon )
740         throws TorqueException
741     {
742         List objs = null;
743         if (pks == null || pks.size() == 0)
744         {
745             objs = new LinkedList();
746         }
747         else
748         {
749             Criteria criteria = new Criteria();
750               criteria.addIn( GROUP_ID, pks );
751           objs = doSelect(criteria, dbcon);
752         }
753         return objs;
754     }
755 
756  
757 
758 
759 
760         
761   
762   
763     
764   
765       /***
766      * Returns the TableMap related to this peer.  This method is not
767      * needed for general use but a specific application could have a need.
768      *
769      * @throws TorqueException Any exceptions caught during processing will be
770      *         rethrown wrapped into a TorqueException.
771      */
772     protected static TableMap getTableMap()
773         throws TorqueException
774     {
775         return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME);
776     }
777    }