View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.jetspeed.om.registry.database;
17  
18  import java.sql.Connection;
19  import java.sql.SQLException;
20  import java.util.ArrayList;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import org.apache.jetspeed.om.registry.base.BaseCategory;
25  import org.apache.jetspeed.om.registry.base.BasePortletEntry;
26  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
27  import org.apache.jetspeed.services.logging.JetspeedLogger;
28  import org.apache.torque.Torque;
29  import org.apache.torque.TorqueException;
30  import org.apache.torque.om.ObjectKey;
31  import org.apache.torque.om.SimpleKey;
32  import org.apache.torque.util.BasePeer;
33  import org.apache.torque.util.Criteria;
34  
35  import com.workingdogs.village.DataSetException;
36  import com.workingdogs.village.QueryDataSet;
37  import com.workingdogs.village.Record;
38  
39  /***
40   * Base Peer for Portlet Category Registry entries.
41   * 
42   * @author <a href="mailto:susinha@cisco.com">Suchisubhra Sinha</a>
43   * @version $Id: BaseJetspeedPortletCategoryPeer.java,v 1.3 2004/04/06 23:00:16 morciuch Exp $
44   */
45  public class BaseJetspeedPortletCategoryPeer extends BasePeer
46  {
47  	
48  	/***
49  	 * Static initialization of the logger for this class
50  	 */    
51  	protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletCategoryPeer.class.getName());      
52  	
53      /*** the default database name for this class */
54      public static final String DATABASE_NAME = "default";
55      /*** the table name for this class */
56      //public static final String TABLE_NAME = "PORTLET_PARAMETER";
57      /*** the column name for the NAME field */
58      public static final String PORTLET_ID;
59      /*** the column name for the NAME field */
60      public static final String NAME;
61      /*** the column name for the GROUP field */
62      public static final String GROUP;
63      static {
64          PORTLET_ID = "PORTLET_CATEGORY.ID";
65          NAME = "PORTLET_CATEGORY.NAME";
66          GROUP = "PORTLET_CATEGORY.CATEGORY_GROUP";
67          if (Torque.isInit())
68          {
69              try
70              {
71                  getMapBuilder();
72              }
73              catch (Exception e)
74              {
75                  logger.error("Could not initialize Peer", e);
76              }
77          }
78      }
79      /*** number of columns for this peer */
80      public static final int numColumns = 1;
81      /*** A class that can be returned by this peer. */
82      protected static final String CLASSNAME_DEFAULT =
83          "org.apache.jetspeed.om.registry.base.BaseCategory";
84      /*** A class that can be returned by this peer. */
85      protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
86      
87      /***
88       * Class object initialization method.
89       *
90       * @param className name of the class to initialize
91       * @return the initialized class
92       */
93      private static Class initClass(String className)
94      {
95          Class c = null;
96          try
97          {
98              c = Class.forName(className);
99          }
100         catch (Throwable t)
101         {
102             logger.error(
103                 "A FATAL ERROR has occurred which should not "
104                     + "have happened under any circumstance.  Please notify "
105                     + "the Turbine developers <turbine-dev@jakarta.apache.org> "
106                     + "and give as many details as possible (including the error "
107                     + "stack trace).",
108                 t);
109             // Error objects should always be propogated.
110             if (t instanceof Error)
111             {
112                 throw (Error) t.fillInStackTrace();
113             }
114         }
115         return c;
116     }
117     /***
118         * Get the list of objects for a ResultSet.  Please not that your
119         * resultset MUST return columns in the right order.  You can use
120         * getFieldNames() in BaseObject to get the correct sequence.
121         *
122         * @param results the ResultSet
123         * @return the list of objects
124         * @throws TorqueException Any exceptions caught during processing will be
125         *         rethrown wrapped into a TorqueException.
126         */
127     public static List resultSet2Objects(java.sql.ResultSet results)
128         throws TorqueException
129     {
130         try
131         {
132             QueryDataSet qds = null;
133             List rows = null;
134             try
135             {
136                 qds = new QueryDataSet(results);
137                 rows = getSelectResults(qds);
138             }
139             finally
140             {
141                 if (qds != null)
142                 {
143                     qds.close();
144                 }
145             }
146             return populateObjects(rows);
147         }
148         catch (SQLException e)
149         {
150             throw new TorqueException(e);
151         }
152         catch (DataSetException e)
153         {
154             throw new TorqueException(e);
155         }
156     }
157     /***
158         * Add all the columns needed to create a new object.
159         *
160         * @param criteria object containing the columns to add.
161         * @throws TorqueException Any exceptions caught during processing will be
162         *         rethrown wrapped into a TorqueException.
163         */
164     public static void addSelectColumns(Criteria criteria)
165         throws TorqueException
166     {
167         criteria.addSelectColumn(NAME);
168         criteria.addSelectColumn(GROUP);
169     }
170     /***
171          * Create a new object of type cls from a resultset row starting
172          * from a specified offset.  This is done so that you can select
173          * other rows than just those needed for this object.  You may
174          * for example want to create two objects from the same row.
175          *
176          * @throws TorqueException Any exceptions caught during processing will be
177          *         rethrown wrapped into a TorqueException.
178          */
179     public static BaseCategory row2Object(Record row, int offset, Class cls)
180         throws TorqueException
181     {
182         try
183         {
184             BaseCategory obj = (BaseCategory) cls.newInstance();
185             populateObject(row, offset, obj);
186             return obj;
187         }
188         catch (InstantiationException e)
189         {
190             throw new TorqueException(e);
191         }
192         catch (IllegalAccessException e)
193         {
194             throw new TorqueException(e);
195         }
196     }
197     /***
198      * Populates an object from a resultset row starting
199      * from a specified offset.  This is done so that you can select
200      * other rows than just those needed for this object.  You may
201      * for example want to create two objects from the same row.
202      *
203      * @throws TorqueException Any exceptions caught during processing will be
204      *         rethrown wrapped into a TorqueException.
205      */
206     public static void populateObject(Record row, int offset, BaseCategory obj)
207         throws TorqueException
208     {
209         try
210         {
211             obj.setName(row.getValue(offset + 0).asString());
212             if (row.getValue(offset + 1).asString() != null)
213                 obj.setGroup(row.getValue(offset + 1).asString());
214         }
215         catch (DataSetException e)
216         {
217             throw new TorqueException(e);
218         }
219     }
220     /***
221         * Method to do selects.
222         *
223         * @param criteria object used to create the SELECT statement.
224         * @return List of selected Objects
225         * @throws TorqueException Any exceptions caught during processing will be
226         *         rethrown wrapped into a TorqueException.
227         */
228     public static List doSelect(Criteria criteria) throws TorqueException
229     {
230         return populateObjects(doSelectVillageRecords(criteria));
231     }
232     /***
233         * Method to do selects within a transaction.
234         *
235         * @param criteria object used to create the SELECT statement.
236         * @param con the connection to use
237         * @return List of selected Objects
238         * @throws TorqueException Any exceptions caught during processing will be
239         *         rethrown wrapped into a TorqueException.
240         */
241     public static List doSelect(Criteria criteria, Connection con)
242         throws TorqueException
243     {
244         return populateObjects(doSelectVillageRecords(criteria, con));
245     }
246     /***
247        * Grabs the raw Village records to be formed into objects.
248        * This method handles connections internally.  The Record objects
249        * returned by this method should be considered readonly.  Do not
250        * alter the data and call save(), your results may vary, but are
251        * certainly likely to result in hard to track MT bugs.
252        *
253        * @throws TorqueException Any exceptions caught during processing will be
254        *         rethrown wrapped into a TorqueException.
255        */
256     public static List doSelectVillageRecords(Criteria criteria)
257         throws TorqueException
258     {
259         return BaseJetspeedPortletCategoryPeer.doSelectVillageRecords(
260             criteria,
261             (Connection) null);
262     }
263     /***
264     * Grabs the raw Village records to be formed into objects.
265      * This method should be used for transactions
266      *
267      * @param con the connection to use
268      * @throws TorqueException Any exceptions caught during processing will be
269      *         rethrown wrapped into a TorqueException.
270      */
271     public static List doSelectVillageRecords(
272         Criteria criteria,
273         Connection con)
274         throws TorqueException
275     {
276         if (criteria.getSelectColumns().size() == 0)
277         {
278             addSelectColumns(criteria);
279         }
280         // Set the correct dbName if it has not been overridden
281         // criteria.getDbName will return the same object if not set to
282         // another value so == check is okay and faster
283         if (criteria.getDbName() == Torque.getDefaultDB())
284         {
285             criteria.setDbName(DATABASE_NAME);
286         }
287         // BasePeer returns a List of Value (Village) arrays.  The array
288         // order follows the order columns were placed in the Select clause.
289         if (con == null)
290         {
291             return BasePeer.doSelect(criteria);
292         }
293         else
294         {
295             return BasePeer.doSelect(criteria, con);
296         }
297     }
298     /***
299         * The returned List will contain objects of the default type or
300         * objects that inherit from the default.
301         *
302         * @throws TorqueException Any exceptions caught during processing will be
303         *         rethrown wrapped into a TorqueException.
304         */
305     public static List populateObjects(List records) throws TorqueException
306     {
307         List results = new ArrayList(records.size());
308         // populate the object(s)
309         for (int i = 0; i < records.size(); i++)
310         {
311             Record row = (Record) records.get(i);
312             results.add(
313                 BaseJetspeedPortletCategoryPeer.row2Object(
314                     row,
315                     1,
316                     BaseJetspeedPortletCategoryPeer.getOMClass()));
317         }
318         return results;
319     }
320     /***
321      * The class that the Peer will make instances of.
322      * If the BO is abstract then you must implement this method
323      * in the BO.
324      *
325      * @throws TorqueException Any exceptions caught during processing will be
326      *         rethrown wrapped into a TorqueException.
327      */
328     public static Class getOMClass() throws TorqueException
329     {
330         return CLASS_DEFAULT;
331     }
332     /***
333      * Method to do selects
334      *
335      * @throws TorqueException Any exceptions caught during processing will be
336      *         rethrown wrapped into a TorqueException.
337      */
338     public static List doSelect(BasePortletEntry obj) throws TorqueException
339     {
340         return doSelect(buildCriteria(obj));
341     }
342     /*** Build a Criteria object from an ObjectKey */
343     public static Criteria buildCriteria(ObjectKey pk)
344     {
345         Criteria criteria = new Criteria();
346         criteria.add(PORTLET_ID, pk);
347         return criteria;
348     }
349     /*** Build a Criteria object from the data object for this peer */
350     public static Criteria buildCriteria(BasePortletEntry obj)
351     {
352         Criteria criteria = new Criteria(DATABASE_NAME);
353         //TODO match  the values here
354         return criteria;
355     }
356     /***
357         * Retrieve a single object by pk
358         *
359         * @param pk the primary key
360         * @throws TorqueException Any exceptions caught during processing will be
361         *         rethrown wrapped into a TorqueException.
362         */
363     public static BaseCategory retrieveByPK(int pk) throws TorqueException
364     {
365         return retrieveByPK(SimpleKey.keyFor(pk));
366     }
367     /***
368      * Retrieve a single object by pk
369      *
370      * @param pk the primary key
371      * @throws TorqueException Any exceptions caught during processing will be
372      *         rethrown wrapped into a TorqueException.
373      */
374     public static BaseCategory retrieveByPK(ObjectKey pk)
375         throws TorqueException
376     {
377         Connection db = null;
378         BaseCategory retVal = null;
379         try
380         {
381             db = Torque.getConnection(DATABASE_NAME);
382             retVal = retrieveByPK(pk, db);
383         }
384         finally
385         {
386             Torque.closeConnection(db);
387         }
388         return (retVal);
389     }
390     /***
391      * Retrieve a single object by pk
392      *
393      * @param pk the primary key
394      * @param con the connection to use
395      * @throws TorqueException Any exceptions caught during processing will be
396      *         rethrown wrapped into a TorqueException.
397      */
398     public static BaseCategory retrieveByPK(ObjectKey pk, Connection con)
399         throws TorqueException
400     {
401         Criteria criteria = buildCriteria(pk);
402         List v = doSelect(criteria, con);
403         if (v.size() != 1)
404         {
405             throw new TorqueException("Failed to select one and only one row.");
406         }
407         else
408         {
409             return (BaseCategory) v.get(0);
410         }
411     }
412     /***
413      * Retrieve a multiple objects by pk
414      *
415      * @param pks List of primary keys
416      * @throws TorqueException Any exceptions caught during processing will be
417      *         rethrown wrapped into a TorqueException.
418      */
419     public static List retrieveByPKs(List pks) throws TorqueException
420     {
421         Connection db = null;
422         List retVal = null;
423         try
424         {
425             db = Torque.getConnection(DATABASE_NAME);
426             retVal = retrieveByPKs(pks, db);
427         }
428         finally
429         {
430             Torque.closeConnection(db);
431         }
432         return (retVal);
433     }
434     /***
435         * Retrieve a multiple objects by pk
436         *
437         * @param pks List of primary keys
438         * @param dbcon the connection to use
439         * @throws TorqueException Any exceptions caught during processing will be
440         *         rethrown wrapped into a TorqueException.
441         */
442     public static List retrieveByPKs(List pks, Connection dbcon)
443         throws TorqueException
444     {
445         List objs = null;
446         if (pks == null || pks.size() == 0)
447         {
448             objs = new LinkedList();
449         }
450         else
451         {
452             Criteria criteria = new Criteria();
453             criteria.addIn(PORTLET_ID, pks);
454             objs = doSelect(criteria, dbcon);
455         }
456         return objs;
457     }
458     /***
459      * Retrieve a listt by portlet id
460      *
461      * @param id the portlet id
462      * @throws TorqueException Any exceptions caught during processing will be
463      *         rethrown wrapped into a TorqueException.
464      */
465     public static List retrieveById(int pk) throws TorqueException
466     {
467         return retrieveById(SimpleKey.keyFor(pk));
468     }
469     /***
470          * Retrieve a list of objects by id
471          *
472          * @param pk the portlet id
473          * @throws TorqueException Any exceptions caught during processing will be
474          *         rethrown wrapped into a TorqueException.
475          */
476     public static List retrieveById(ObjectKey pk) throws TorqueException
477     {
478         Connection db = null;
479         List retVal = null;
480         try
481         {
482             db = Torque.getConnection(DATABASE_NAME);
483             retVal = retrieveById(pk, db);
484         }
485         finally
486         {
487             Torque.closeConnection(db);
488         }
489         return (retVal);
490     }
491 
492     /***
493      * Retrieve a single object by pk
494      *
495      * @param pk the primary key
496      * @param con the connection to use
497      * @throws TorqueException Any exceptions caught during processing will be
498      *         rethrown wrapped into a TorqueException.
499      */
500     public static List retrieveById(ObjectKey pk, Connection con)
501         throws TorqueException
502     {
503         Criteria criteria = buildCriteria(pk);
504         return doSelect(criteria, con);
505     }
506 }