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.List;
22  
23  import org.apache.jetspeed.om.registry.DBRegistry;
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  /***
43   * Base Peer for Skin registry entries.
44   * 
45   * @author <a href="mailto:susinha@cisco.com">Suchisubhra Sinha</a>
46   * @version $Id: BaseJetspeedSkinPeer.java,v 1.3 2004/04/06 23:00:16 morciuch Exp $
47   */
48  public class BaseJetspeedSkinPeer extends BasePeer implements DBRegistry
49  {
50  	
51  	/***
52  	 * Static initialization of the logger for this class
53  	 */    
54  	protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedSkinPeer.class.getName());      
55  	
56      /*** the table name for this class */
57      public static final String TABLE_NAME = "SKIN";
58      /*** the column name for the PORTAL_ID field */
59      public static final String ID;
60      /*** the column name for the NAME field */
61      public static final String NAME;
62      /*** the column name for the HIDDEN field */
63      public static final String HIDDEN;
64      /*** the column name for the ROLE field */
65      public static final String ROLE;
66      /*** the column name for the TITLE field */
67      public static final String TITLE;
68      /*** the column name for the DESCRIPTION field */
69      public static final String DESCRIPTION;
70      /*** the column name for the IMAGE field */
71      public static final String IMAGE;
72      static {
73          ID = "SKIN.ID";
74          NAME = "SKIN.NAME";
75          HIDDEN = "SKIN.HIDDEN";
76          ROLE = "SKIN.ROLE";
77          TITLE = "SKIN.TITLE";
78          DESCRIPTION = "SKIN.DESCRIPTION";
79          IMAGE = "SKIN.IMAGE";
80          /*
81             if (Torque.isInit())
82             {
83                 try
84                 {
85                     getMapBuilder();
86                 }
87                 catch (Exception e)
88                 {
89                        
90                     category.error("Could not initialize Peer", e);
91                 }
92             }
93          */
94      }
95      /*** number of columns for this peer */
96      public static final int numColumns = 7;
97      /*** A class that can be returned by this peer. */
98      protected static final String CLASSNAME_DEFAULT =
99          "org.apache.jetspeed.om.registry.base.BaseSkinEntry";
100     /*** A class that can be returned by this peer. */
101     protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
102     /***
103      * Class object initialization method.
104      *
105      * @param className name of the class to initialize
106      * @return the initialized class
107      */
108     private static Class initClass(String className)
109     {
110         Class c = null;
111         try
112         {
113             c = Class.forName(className);
114         }
115         catch (Throwable t)
116         {
117             logger.error(
118                 "A FATAL ERROR has occurred which should not "
119                     + "have happened under any circumstance.  Please notify "
120                     + "the Turbine developers <turbine-dev@jakarta.apache.org> "
121                     + "and give as many details as possible (including the error "
122                     + "stack trace).",
123                 t);
124             // Error objects should always be propogated.
125             if (t instanceof Error)
126             {
127                 throw (Error) t.fillInStackTrace();
128             }
129         }
130         return c;
131     }
132     /***
133         * Get the list of objects for a ResultSet.  Please not that your
134         * resultset MUST return columns in the right order.  You can use
135         * getFieldNames() in BaseObject to get the correct sequence.
136         *
137         * @param results the ResultSet
138         * @return the list of objects
139         * @throws TorqueException Any exceptions caught during processing will be
140         *         rethrown wrapped into a TorqueException.
141         */
142     public static List resultSet2Objects(java.sql.ResultSet results)
143         throws TorqueException
144     {
145         try
146         {
147             QueryDataSet qds = null;
148             List rows = null;
149             try
150             {
151                 qds = new QueryDataSet(results);
152                 rows = getSelectResults(qds);
153             }
154             finally
155             {
156                 if (qds != null)
157                 {
158                     qds.close();
159                 }
160             }
161             return populateObjects(rows);
162         }
163         catch (SQLException e)
164         {
165             throw new TorqueException(e);
166         }
167         catch (DataSetException e)
168         {
169             throw new TorqueException(e);
170         }
171     }
172     /***
173         * Add all the columns needed to create a new object.
174         *
175         * @param criteria object containing the columns to add.
176         * @throws TorqueException Any exceptions caught during processing will be
177         *         rethrown wrapped into a TorqueException.
178         */
179     public static void addSelectColumns(Criteria criteria)
180         throws TorqueException
181     {
182         criteria.addSelectColumn(ID);
183         criteria.addSelectColumn(NAME);
184         criteria.addSelectColumn(HIDDEN);
185         criteria.addSelectColumn(ROLE);
186         criteria.addSelectColumn(TITLE);
187         criteria.addSelectColumn(DESCRIPTION);
188         criteria.addSelectColumn(IMAGE);
189     }
190     /***
191          * Create a new object of type cls from a resultset row starting
192          * from a specified offset.  This is done so that you can select
193          * other rows than just those needed for this object.  You may
194          * for example want to create two objects from the same row.
195          *
196          * @throws TorqueException Any exceptions caught during processing will be
197          *         rethrown wrapped into a TorqueException.
198          */
199     public static BaseSkinEntry row2Object(Record row, int offset, Class cls)
200         throws TorqueException
201     {
202         try
203         {
204             BaseSkinEntry obj = (BaseSkinEntry) cls.newInstance();
205             populateObject(row, offset, obj);
206             return obj;
207         }
208         catch (InstantiationException e)
209         {
210             throw new TorqueException(e);
211         }
212         catch (IllegalAccessException e)
213         {
214             throw new TorqueException(e);
215         }
216     }
217     /***
218      * Populates an object from a resultset row starting
219      * from a specified offset.  This is done so that you can select
220      * other rows than just those needed for this object.  You may
221      * for example want to create two objects from the same row.
222      *
223      * @throws TorqueException Any exceptions caught during processing will be
224      *         rethrown wrapped into a TorqueException.
225      */
226     public static void populateObject(
227         Record row,
228         int offset,
229         BaseSkinEntry obj)
230         throws TorqueException
231     {
232         try
233         {
234             int id = row.getValue(offset + 0).asInt();
235             obj.setName(row.getValue(offset + 1).asString());
236             obj.setHidden(row.getValue(offset + 2).asBoolean());
237             obj.setTitle(row.getValue(offset + 4).asString());
238             obj.setDescription(row.getValue(offset + 5).asString());
239             //set  the security
240             BaseSecurity security =
241                 new BaseSecurity(row.getValue(offset + 3).asString());
242             obj.setSecurity(security);
243             obj.setBaseSecurity(security);
244             //create meta info
245             BaseMetaInfo baseMetaInfo =
246                 new BaseMetaInfo(
247                     row.getValue(offset + 4).asString(),
248                     row.getValue(offset + 5).asString(),
249                     row.getValue(offset + 6).asString());
250             obj.setMetaInfo(baseMetaInfo);
251             buildSkinParameters(id, obj);
252         }
253         catch (DataSetException e)
254         {
255             throw new TorqueException(e);
256         }
257     }
258     /***
259         * Method to get  regsitered data from database.
260         *
261         * @param criteria object used to create the SELECT statement.
262         * @return List of selected Objects
263         * @throws TorqueException Any exceptions caught during processing will be
264         *         rethrown wrapped into a TorqueException.
265         */
266     public List getXREGDataFromDb() throws TorqueException
267     {
268         Criteria criteria = buildCriteria();
269         return doSelect(criteria);
270     }
271     public boolean isModified(String lastUpdateDate)
272     {
273         return true;
274     }
275     /***
276      * Method to do selects.
277      *
278      * @param criteria object used to create the SELECT statement.
279      * @return List of selected Objects
280      * @throws TorqueException Any exceptions caught during processing will be
281      *         rethrown wrapped into a TorqueException.
282      */
283     public static List doSelect(Criteria criteria) throws TorqueException
284     {
285         return populateObjects(doSelectVillageRecords(criteria));
286     }
287     /***
288         * Method to do selects within a transaction.
289         *
290         * @param criteria object used to create the SELECT statement.
291         * @param con the connection to use
292         * @return List of selected Objects
293         * @throws TorqueException Any exceptions caught during processing will be
294         *         rethrown wrapped into a TorqueException.
295         */
296     public static List doSelect(Criteria criteria, Connection con)
297         throws TorqueException
298     {
299         return populateObjects(doSelectVillageRecords(criteria, con));
300     }
301     /***
302        * Grabs the raw Village records to be formed into objects.
303        * This method handles connections internally.  The Record objects
304        * returned by this method should be considered readonly.  Do not
305        * alter the data and call save(), your results may vary, but are
306        * certainly likely to result in hard to track MT bugs.
307        *
308        * @throws TorqueException Any exceptions caught during processing will be
309        *         rethrown wrapped into a TorqueException.
310        */
311     public static List doSelectVillageRecords(Criteria criteria)
312         throws TorqueException
313     {
314         return BaseJetspeedSkinPeer.doSelectVillageRecords(
315             criteria,
316             (Connection) null);
317     }
318     /***
319     * Grabs the raw Village records to be formed into objects.
320      * This method should be used for transactions
321      *
322      * @param con the connection to use
323      * @throws TorqueException Any exceptions caught during processing will be
324      *         rethrown wrapped into a TorqueException.
325      */
326     public static List doSelectVillageRecords(
327         Criteria criteria,
328         Connection con)
329         throws TorqueException
330     {
331         if (criteria.getSelectColumns().size() == 0)
332         {
333             addSelectColumns(criteria);
334         }
335         // Set the correct dbName if it has not been overridden
336         // criteria.getDbName will return the same object if not set to
337         // another value so == check is okay and faster
338         if (criteria.getDbName() == Torque.getDefaultDB())
339         {
340             criteria.setDbName(DATABASE_NAME);
341         }
342         // BasePeer returns a List of Value (Village) arrays.  The array
343         // order follows the order columns were placed in the Select clause.
344         if (con == null)
345         {
346             return BasePeer.doSelect(criteria);
347         }
348         else
349         {
350             return BasePeer.doSelect(criteria, con);
351         }
352     }
353     /***
354      * The returned List will contain objects of the default type or
355      * objects that inherit from the default.
356      *
357      * @throws TorqueException Any exceptions caught during processing will be
358      *         rethrown wrapped into a TorqueException.
359      */
360     public static List populateObjects(List records) throws TorqueException
361     {
362         List results = new ArrayList(records.size());
363         // populate the object(s)
364         for (int i = 0; i < records.size(); i++)
365         {
366             Record row = (Record) records.get(i);
367             results.add(
368                 BaseJetspeedSkinPeer.row2Object(
369                     row,
370                     1,
371                     BaseJetspeedSkinPeer.getOMClass()));
372         }
373         return results;
374     }
375     /*** Build a Criteria object from an ObjectKey */
376     public static Criteria buildCriteria(ObjectKey pk)
377     {
378         Criteria criteria = new Criteria();
379         criteria.add(ID, pk);
380         return criteria;
381     }
382     /*** Build a Criteria object  */
383     public static Criteria buildCriteria()
384     {
385         Criteria criteria = new Criteria();
386         return criteria;
387     }
388     /***
389      * The class that the Peer will make instances of.
390      * If the BO is abstract then you must implement this method
391      * in the BO.
392      *
393      * @throws TorqueException Any exceptions caught during processing will be
394      *         rethrown wrapped into a TorqueException.
395      */
396     public static Class getOMClass() throws TorqueException
397     {
398         return CLASS_DEFAULT;
399     }
400     /***
401      * it will make the parameters  for this portlet
402      * @param portlet object.
403      * 
404      */
405     public static void buildSkinParameters(int id, BaseSkinEntry obj)
406         throws TorqueException
407     {
408         try
409         {
410             List list =
411                 BaseJetspeedSkinParameterPeer.retrieveById(
412                     SimpleKey.keyFor(id));
413             if (list != null)
414                 for (int i = 0; i < list.size(); i++)
415                 {
416                     Parameter p = (Parameter) list.get(i);
417                     if (obj.getParameter(p.getName()) == null)
418                         obj.addParameter(p);
419                 }
420         }
421         catch (Exception e)
422         {
423             throw new TorqueException(e);
424         }
425     }
426 }