KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > om > registry > database > BaseJetspeedSkinParameterPeer


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 JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
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.BaseSecurity;
27 import org.apache.jetspeed.om.registry.base.BaseSkinEntry;
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 Skin Parameter registry entries.
43  *
44  * @author <a HREF="mailto:susinha@cisco.com">Suchisubhra Sinha</a>
45  * @version $Id: BaseJetspeedSkinParameterPeer.java,v 1.3 2004/04/06 23:00:16 morciuch Exp $
46  */

47 public class BaseJetspeedSkinParameterPeer extends BasePeer
48 {
49     
50     /**
51      * Static initialization of the logger for this class
52      */

53     protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedSkinParameterPeer.class.getName());
54     
55     /** the default database name for this class */
56     public static final String JavaDoc DATABASE_NAME = "default";
57     /** the table name for this class */
58     public static final String JavaDoc TABLE_NAME = "SKIN_PARAMETER";
59     /** the column name for the PORTAL_ID field */
60     public static final String JavaDoc ID;
61     /** the column name for the NAME field */
62     public static final String JavaDoc NAME;
63     /** the column name for the NAME field */
64     public static final String JavaDoc VALUE;
65     /** the column name for the HIDDEN field */
66     public static final String JavaDoc HIDDEN;
67     /** the column name for the TYPE field */
68     public static final String JavaDoc TYPE;
69     /** the column name for the role field */
70     public static final String JavaDoc ROLE;
71     /** the column name for the TITLE field */
72     public static final String JavaDoc TITLE;
73     /** the column name for the DESCRIPTION field */
74     public static final String JavaDoc DESCRIPTION;
75     /** the column name for the IMAGE field */
76     public static final String JavaDoc IMAGE;
77     /** the portlet id for this parameter **/
78     public static final String JavaDoc SKIN_ID;
79     static {
80         ID = "SKIN_PARAMETER.ID";
81         NAME = "SKIN_PARAMETER.NAME";
82         VALUE = "SKIN_PARAMETER.VALUE";
83         HIDDEN = "SKIN_PARAMETER.HIDDEN";
84         TYPE = "SKIN_PARAMETER.TYPE";
85         ROLE = "SKIN_PARAMETER.ROLE";
86         TITLE = "SKIN_PARAMETER.TITLE";
87         DESCRIPTION = "SKIN_PARAMETER.DESCRIPTION";
88         IMAGE = "SKIN_PARAMETER.IMAGE";
89         SKIN_ID = "SKIN_PARAMETER.SKIN_ID";
90         if (Torque.isInit())
91         {
92             try
93             {
94                 getMapBuilder();
95             }
96             catch (Exception JavaDoc e)
97             {
98                 logger.error("Could not initialize Peer", e);
99             }
100         }
101     }
102     /** number of columns for this peer */
103     public static final int numColumns = 12;
104     /** A class that can be returned by this peer. */
105     protected static final String JavaDoc CLASSNAME_DEFAULT =
106         "org.apache.jetspeed.om.registry.base.BaseParameter";
107     /** A class that can be returned by this peer. */
108     protected static final Class JavaDoc CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
109     /**
110         * Class object initialization method.
111         *
112         * @param className name of the class to initialize
113         * @return the initialized class
114         */

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

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

186     public static void addSelectColumns(Criteria criteria)
187         throws TorqueException
188     {
189         criteria.addSelectColumn(ID);
190         criteria.addSelectColumn(NAME);
191         criteria.addSelectColumn(VALUE);
192         criteria.addSelectColumn(HIDDEN);
193         criteria.addSelectColumn(TYPE);
194         criteria.addSelectColumn(ROLE);
195         criteria.addSelectColumn(TITLE);
196         criteria.addSelectColumn(DESCRIPTION);
197         criteria.addSelectColumn(IMAGE);
198         criteria.addSelectColumn(SKIN_ID);
199     }
200     /**
201          * Create a new object of type cls from a resultset row starting
202          * from a specified offset. This is done so that you can select
203          * other rows than just those needed for this object. You may
204          * for example want to create two objects from the same row.
205          *
206          * @throws TorqueException Any exceptions caught during processing will be
207          * rethrown wrapped into a TorqueException.
208          */

209     public static Parameter row2Object(Record row, int offset, Class JavaDoc cls)
210         throws TorqueException
211     {
212         try
213         {
214             Parameter obj = (Parameter) cls.newInstance();
215             populateObject(row, offset, obj);
216             //obj.setModified(false);
217
//obj.setNew(false);
218
return obj;
219         }
220         catch (InstantiationException JavaDoc e)
221         {
222             throw new TorqueException(e);
223         }
224         catch (IllegalAccessException JavaDoc e)
225         {
226             throw new TorqueException(e);
227         }
228     }
229     /**
230      * Populates an object from a resultset row starting
231      * from a specified offset. This is done so that you can select
232      * other rows than just those needed for this object. You may
233      * for example want to create two objects from the same row.
234      *
235      * @throws TorqueException Any exceptions caught during processing will be
236      * rethrown wrapped into a TorqueException.
237      */

238     public static void populateObject(Record row, int offset, Parameter obj)
239         throws TorqueException
240     {
241         try
242         {
243             obj.setName(row.getValue(offset + 1).asString());
244             obj.setValue(row.getValue(offset + 2).asString());
245             obj.setHidden(row.getValue(offset + 3).asBoolean());
246             obj.setType(row.getValue(offset + 4).asString());
247             BaseMetaInfo baseMetaInfo =
248                 new BaseMetaInfo(
249                     row.getValue(offset + 6).asString(),
250                     row.getValue(offset + 7).asString(),
251                     row.getValue(offset + 8).asString());
252             obj.setMetaInfo(baseMetaInfo);
253             //set the security
254
BaseSecurity security =
255                 new BaseSecurity(row.getValue(offset + 5).asString());
256             obj.setSecurity(security);
257         }
258         catch (DataSetException e)
259         {
260             throw new TorqueException(e);
261         }
262     }
263     /**
264         * Method to do selects.
265         *
266         * @param criteria object used to create the SELECT statement.
267         * @return List of selected Objects
268         * @throws TorqueException Any exceptions caught during processing will be
269         * rethrown wrapped into a TorqueException.
270         */

271     public static List JavaDoc doSelect(Criteria criteria) throws TorqueException
272     {
273         return populateObjects(doSelectVillageRecords(criteria));
274     }
275     /**
276         * Method to do selects within a transaction.
277         *
278         * @param criteria object used to create the SELECT statement.
279         * @param con the connection to use
280         * @return List of selected Objects
281         * @throws TorqueException Any exceptions caught during processing will be
282         * rethrown wrapped into a TorqueException.
283         */

284     public static List JavaDoc doSelect(Criteria criteria, Connection JavaDoc con)
285         throws TorqueException
286     {
287         return populateObjects(doSelectVillageRecords(criteria, con));
288     }
289     /**
290        * Grabs the raw Village records to be formed into objects.
291        * This method handles connections internally. The Record objects
292        * returned by this method should be considered readonly. Do not
293        * alter the data and call save(), your results may vary, but are
294        * certainly likely to result in hard to track MT bugs.
295        *
296        * @throws TorqueException Any exceptions caught during processing will be
297        * rethrown wrapped into a TorqueException.
298        */

299     public static List JavaDoc doSelectVillageRecords(Criteria criteria)
300         throws TorqueException
301     {
302         return BaseJetspeedSkinPeer.doSelectVillageRecords(
303             criteria,
304             (Connection JavaDoc) null);
305     }
306     /**
307     * Grabs the raw Village records to be formed into objects.
308      * This method should be used for transactions
309      *
310      * @param con the connection to use
311      * @throws TorqueException Any exceptions caught during processing will be
312      * rethrown wrapped into a TorqueException.
313      */

314     public static List JavaDoc doSelectVillageRecords(
315         Criteria criteria,
316         Connection JavaDoc con)
317         throws TorqueException
318     {
319         if (criteria.getSelectColumns().size() == 0)
320         {
321             addSelectColumns(criteria);
322         }
323         // Set the correct dbName if it has not been overridden
324
// criteria.getDbName will return the same object if not set to
325
// another value so == check is okay and faster
326
if (criteria.getDbName() == Torque.getDefaultDB())
327         {
328             criteria.setDbName(DATABASE_NAME);
329         }
330         // BasePeer returns a List of Value (Village) arrays. The array
331
// order follows the order columns were placed in the Select clause.
332
if (con == null)
333         {
334             return BasePeer.doSelect(criteria);
335         }
336         else
337         {
338             return BasePeer.doSelect(criteria, con);
339         }
340     }
341     /**
342         * The returned List will contain objects of the default type or
343         * objects that inherit from the default.
344         *
345         * @throws TorqueException Any exceptions caught during processing will be
346         * rethrown wrapped into a TorqueException.
347         */

348     public static List JavaDoc populateObjects(List JavaDoc records) throws TorqueException
349     {
350         List JavaDoc results = new ArrayList JavaDoc(records.size());
351         // populate the object(s)
352
for (int i = 0; i < records.size(); i++)
353         {
354             Record row = (Record) records.get(i);
355             results.add(
356                 BaseJetspeedSkinParameterPeer.row2Object(
357                     row,
358                     1,
359                     BaseJetspeedSkinParameterPeer.getOMClass()));
360         }
361         return results;
362     }
363     /**
364      * The class that the Peer will make instances of.
365      * If the BO is abstract then you must implement this method
366      * in the BO.
367      *
368      * @throws TorqueException Any exceptions caught during processing will be
369      * rethrown wrapped into a TorqueException.
370      */

371     public static Class JavaDoc getOMClass() throws TorqueException
372     {
373         return CLASS_DEFAULT;
374     }
375     /**
376      * Method to do selects
377      *
378      * @throws TorqueException Any exceptions caught during processing will be
379      * rethrown wrapped into a TorqueException.
380      */

381     public static List JavaDoc doSelect(BaseSkinEntry obj) throws TorqueException
382     {
383         return doSelect(buildCriteria(obj));
384     }
385     /** Build a Criteria object from an ObjectKey */
386     public static Criteria buildCriteria(ObjectKey pk)
387     {
388         Criteria criteria = new Criteria();
389         criteria.add(SKIN_ID, pk);
390         return criteria;
391     }
392     /** Build a Criteria object from the data object for this peer */
393     public static Criteria buildCriteria(BaseSkinEntry obj)
394     {
395         Criteria criteria = new Criteria(DATABASE_NAME);
396         /*
397                             if (!obj.isNew())
398                                 criteria.add(PSML_ID, obj.getPsmlId());
399                                 criteria.add(USER_NAME, obj.getUserName());
400                                 criteria.add(MEDIA_TYPE, obj.getMediaType());
401                                 criteria.add(LANGUAGE, obj.getLanguage());
402                                 criteria.add(COUNTRY, obj.getCountry());
403                                 criteria.add(PAGE, obj.getPage());
404                                 criteria.add(PROFILE, obj.getProfile());
405         */

406         return criteria;
407     }
408     /**
409         * Retrieve a single object by pk
410         *
411         * @param pk the primary key
412         * @throws TorqueException Any exceptions caught during processing will be
413         * rethrown wrapped into a TorqueException.
414         */

415     public static Parameter retrieveByPK(int pk) throws TorqueException
416     {
417         return retrieveByPK(SimpleKey.keyFor(pk));
418     }
419     /**
420      * Retrieve a single object by pk
421      *
422      * @param pk the primary key
423      * @throws TorqueException Any exceptions caught during processing will be
424      * rethrown wrapped into a TorqueException.
425      */

426     public static Parameter retrieveByPK(ObjectKey pk) throws TorqueException
427     {
428         Connection JavaDoc db = null;
429         Parameter retVal = null;
430         try
431         {
432             db = Torque.getConnection(DATABASE_NAME);
433             retVal = retrieveByPK(pk, db);
434         }
435         finally
436         {
437             Torque.closeConnection(db);
438         }
439         return (retVal);
440     }
441     /**
442      * Retrieve a single object by pk
443      *
444      * @param pk the primary key
445      * @param con the connection to use
446      * @throws TorqueException Any exceptions caught during processing will be
447      * rethrown wrapped into a TorqueException.
448      */

449     public static Parameter retrieveByPK(ObjectKey pk, Connection JavaDoc con)
450         throws TorqueException
451     {
452         Criteria criteria = buildCriteria(pk);
453         List JavaDoc v = doSelect(criteria, con);
454         if (v.size() != 1)
455         {
456             throw new TorqueException("Failed to select one and only one row.");
457         }
458         else
459         {
460             return (Parameter) v.get(0);
461         }
462     }
463     /**
464      * Retrieve a multiple objects by pk
465      *
466      * @param pks List of primary keys
467      * @throws TorqueException Any exceptions caught during processing will be
468      * rethrown wrapped into a TorqueException.
469      */

470     public static List JavaDoc retrieveByPKs(List JavaDoc pks) throws TorqueException
471     {
472         Connection JavaDoc db = null;
473         List JavaDoc retVal = null;
474         try
475         {
476             db = Torque.getConnection(DATABASE_NAME);
477             retVal = retrieveByPKs(pks, db);
478         }
479         finally
480         {
481             Torque.closeConnection(db);
482         }
483         return (retVal);
484     }
485     /**
486         * Retrieve a multiple objects by pk
487         *
488         * @param pks List of primary keys
489         * @param dbcon the connection to use
490         * @throws TorqueException Any exceptions caught during processing will be
491         * rethrown wrapped into a TorqueException.
492         */

493     public static List JavaDoc retrieveByPKs(List JavaDoc pks, Connection JavaDoc dbcon)
494         throws TorqueException
495     {
496         List JavaDoc objs = null;
497         if (pks == null || pks.size() == 0)
498         {
499             objs = new LinkedList JavaDoc();
500         }
501         else
502         {
503             Criteria criteria = new Criteria();
504             criteria.addIn(SKIN_ID, pks);
505             objs = doSelect(criteria, dbcon);
506         }
507         return objs;
508     }
509     /**
510      * Retrieve a listt by portlet id
511      *
512      * @param id the portlet id
513      * @throws TorqueException Any exceptions caught during processing will be
514      * rethrown wrapped into a TorqueException.
515      */

516     public static List JavaDoc retrieveById(int pk) throws TorqueException
517     {
518         return retrieveById(SimpleKey.keyFor(pk));
519     }
520     /**
521          * Retrieve a list of objects by id
522          *
523          * @param pk the portlet id
524          * @throws TorqueException Any exceptions caught during processing will be
525          * rethrown wrapped into a TorqueException.
526          */

527     public static List JavaDoc retrieveById(ObjectKey pk) throws TorqueException
528     {
529         Connection JavaDoc db = null;
530         List JavaDoc retVal = null;
531         try
532         {
533             db = Torque.getConnection(DATABASE_NAME);
534             retVal = retrieveById(pk, db);
535         }
536         finally
537         {
538             Torque.closeConnection(db);
539         }
540         return (retVal);
541     }
542     /**
543             * Retrieve a single object by pk
544             *
545             * @param pk the primary key
546             * @param con the connection to use
547             * @throws TorqueException Any exceptions caught during processing will be
548             * rethrown wrapped into a TorqueException.
549             */

550     public static List JavaDoc retrieveById(ObjectKey pk, Connection JavaDoc con)
551         throws TorqueException
552     {
553         Criteria criteria = buildCriteria(pk);
554         return doSelect(criteria, con);
555     }
556 }
557
Popular Tags