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