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