KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
22
23 import org.apache.jetspeed.om.BaseSecurityReference;
24 import org.apache.jetspeed.om.registry.DBRegistry;
25 import org.apache.jetspeed.om.registry.Parameter;
26 import org.apache.jetspeed.om.registry.base.BaseCategory;
27 import org.apache.jetspeed.om.registry.base.BaseContentURL;
28 import org.apache.jetspeed.om.registry.base.BaseMediaType;
29 import org.apache.jetspeed.om.registry.base.BaseMetaInfo;
30 import org.apache.jetspeed.om.registry.base.BasePortletEntry;
31 import org.apache.jetspeed.om.registry.base.BaseSecurity;
32 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
33 import org.apache.jetspeed.services.logging.JetspeedLogger;
34 import org.apache.torque.Torque;
35 import org.apache.torque.TorqueException;
36 import org.apache.torque.om.ObjectKey;
37 import org.apache.torque.om.SimpleKey;
38 import org.apache.torque.util.BasePeer;
39 import org.apache.torque.util.Criteria;
40
41 import com.workingdogs.village.DataSetException;
42 import com.workingdogs.village.QueryDataSet;
43 import com.workingdogs.village.Record;
44
45 /**
46  * Base Peer for Portlet Parameter registry entries.
47  *
48  * @author <a HREF="mailto:susinha@cisco.com">Suchisubhra Sinha</a>
49  * @version $Id: BaseJetspeedPortletPeer.java,v 1.3 2004/04/06 23:00:16 morciuch Exp $
50  */

51 public class BaseJetspeedPortletPeer extends BasePeer implements DBRegistry
52 {
53     
54     /**
55      * Static initialization of the logger for this class
56      */

57     protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletPeer.class.getName());
58     
59     /** the table name for this class */
60     public static final String JavaDoc TABLE_NAME = "PORTLET";
61     /** the column name for the PORTAL_ID field */
62     public static final String JavaDoc PORTAL_ID;
63     /** the column name for the NAME field */
64     public static final String JavaDoc NAME;
65     /** the column name for the HIDDEN field */
66     public static final String JavaDoc HIDDEN;
67     /** the column name for the CLASSNAME field */
68     public static final String JavaDoc CLASSNAME;
69     /** the column name for the TYPE field */
70     public static final String JavaDoc TYPE;
71     /** the column name for the APPLICATION field */
72     public static final String JavaDoc APPLICATION;
73     /** the column name for the PARENT field */
74     public static final String JavaDoc PARENT;
75     /** the column name for the URL field */
76     public static final String JavaDoc URL;
77     /** the column name for the CACHEDONURL field */
78     public static final String JavaDoc CACHEDONURL;
79     /** the column name for the ROLE field */
80     public static final String JavaDoc ROLE;
81     /** the column name for the TITLE field */
82     public static final String JavaDoc TITLE;
83     /** the column name for the DESCRIPTION field */
84     public static final String JavaDoc DESCRIPTION;
85     /** the column name for the IMAGE field */
86     public static final String JavaDoc IMAGE;
87     /** the column name for the SECURITY field */
88     public static final String JavaDoc SECURITY;
89     /** the column name for the field */
90     public static final String JavaDoc LASTUPDATE;
91     static {
92         PORTAL_ID = "PORTLET.ID";
93         NAME = "PORTLET.NAME";
94         HIDDEN = "PORTLET.HIDDEN";
95         CLASSNAME = "PORTLET.CLASSNAME";
96         TYPE = "PORTLET.TYPE";
97         APPLICATION = "PORTLET.APPLICATION";
98         PARENT = "PORTLET.PARENT";
99         URL = "PORTLET.URL";
100         CACHEDONURL = "PORTLET.CACHEDONURL";
101         ROLE = "PORTLET.ROLE";
102         TITLE = "PORTLET.TITLE";
103         DESCRIPTION = "PORTLET.DESCRIPTION";
104         IMAGE = "PORTLET.IMAGE";
105         SECURITY = "PORTLET.SECURITY";
106         LASTUPDATE = "PORTLET.LASTUPDATE";
107         /*
108            if (Torque.isInit())
109            {
110                try
111                {
112                    getMapBuilder();
113                }
114                catch (Exception e)
115                {
116                       
117                    category.error("Could not initialize Peer", e);
118                }
119            }
120         */

121     }
122     /** number of columns for this peer */
123     public static final int numColumns = 14;
124     /** A class that can be returned by this peer. */
125     protected static final String JavaDoc CLASSNAME_DEFAULT =
126         "org.apache.jetspeed.om.registry.base.BasePortletEntry";
127     /** A class that can be returned by this peer. */
128     protected static final Class JavaDoc CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
129     /**
130      * Class object initialization method.
131      *
132      * @param className name of the class to initialize
133      * @return the initialized class
134      */

135     private static Class JavaDoc initClass(String JavaDoc className)
136     {
137         Class JavaDoc c = null;
138         try
139         {
140             c = Class.forName(className);
141         }
142         catch (Throwable JavaDoc t)
143         {
144             logger.error(
145                 "A FATAL ERROR has occurred which should not "
146                     + "have happened under any circumstance. Please notify "
147                     + "the Turbine developers <turbine-dev@jakarta.apache.org> "
148                     + "and give as many details as possible (including the error "
149                     + "stack trace).",
150                 t);
151             // Error objects should always be propogated.
152
if (t instanceof Error JavaDoc)
153             {
154                 throw (Error JavaDoc) t.fillInStackTrace();
155             }
156         }
157         return c;
158     }
159     /**
160         * Get the list of objects for a ResultSet. Please not that your
161         * resultset MUST return columns in the right order. You can use
162         * getFieldNames() in BaseObject to get the correct sequence.
163         *
164         * @param results the ResultSet
165         * @return the list of objects
166         * @throws TorqueException Any exceptions caught during processing will be
167         * rethrown wrapped into a TorqueException.
168         */

169     public static List JavaDoc resultSet2Objects(java.sql.ResultSet JavaDoc results)
170         throws TorqueException
171     {
172         try
173         {
174             QueryDataSet qds = null;
175             List JavaDoc rows = null;
176             try
177             {
178                 qds = new QueryDataSet(results);
179                 rows = getSelectResults(qds);
180             }
181             finally
182             {
183                 if (qds != null)
184                 {
185                     qds.close();
186                 }
187             }
188             return populateObjects(rows);
189         }
190         catch (SQLException JavaDoc e)
191         {
192             throw new TorqueException(e);
193         }
194         catch (DataSetException e)
195         {
196             throw new TorqueException(e);
197         }
198     }
199     /**
200         * Add all the columns needed to create a new object.
201         *
202         * @param criteria object containing the columns to add.
203         * @throws TorqueException Any exceptions caught during processing will be
204         * rethrown wrapped into a TorqueException.
205         */

206     public static void addSelectColumns(Criteria criteria)
207         throws TorqueException
208     {
209         criteria.addSelectColumn(PORTAL_ID);
210         criteria.addSelectColumn(NAME);
211         criteria.addSelectColumn(HIDDEN);
212         criteria.addSelectColumn(CLASSNAME);
213         criteria.addSelectColumn(TYPE);
214         criteria.addSelectColumn(APPLICATION);
215         criteria.addSelectColumn(PARENT);
216         criteria.addSelectColumn(URL);
217         criteria.addSelectColumn(CACHEDONURL);
218         criteria.addSelectColumn(ROLE);
219         criteria.addSelectColumn(TITLE);
220         criteria.addSelectColumn(DESCRIPTION);
221         criteria.addSelectColumn(IMAGE);
222         criteria.addSelectColumn(SECURITY);
223     }
224     /**
225          * Create a new object of type cls from a resultset row starting
226          * from a specified offset. This is done so that you can select
227          * other rows than just those needed for this object. You may
228          * for example want to create two objects from the same row.
229          *
230          * @throws TorqueException Any exceptions caught during processing will be
231          * rethrown wrapped into a TorqueException.
232          */

233     public static BasePortletEntry row2Object(
234         Record row,
235         int offset,
236         Class JavaDoc cls)
237         throws TorqueException
238     {
239         try
240         {
241             BasePortletEntry obj = (BasePortletEntry) cls.newInstance();
242             populateObject(row, offset, obj);
243             //obj.setModified(false);
244
//obj.setNew(false);
245
return obj;
246         }
247         catch (InstantiationException JavaDoc e)
248         {
249             throw new TorqueException(e);
250         }
251         catch (IllegalAccessException JavaDoc e)
252         {
253             throw new TorqueException(e);
254         }
255     }
256     /**
257      * Populates an object from a resultset row starting
258      * from a specified offset. This is done so that you can select
259      * other rows than just those needed for this object. You may
260      * for example want to create two objects from the same row.
261      *
262      * @throws TorqueException Any exceptions caught during processing will be
263      * rethrown wrapped into a TorqueException.
264      */

265     public static void populateObject(
266         Record row,
267         int offset,
268         BasePortletEntry obj)
269         throws TorqueException
270     {
271         try
272         {
273             int id = row.getValue(offset + 0).asInt();
274             obj.setName(row.getValue(offset + 1).asString());
275             obj.setHidden(row.getValue(offset + 2).asBoolean());
276             obj.setClassname(row.getValue(offset + 3).asString());
277             obj.setType(row.getValue(offset + 4).asString());
278             obj.setApplication(row.getValue(offset + 5).asBoolean());
279             obj.setParent(row.getValue(offset + 6).asString());
280             obj.setURL(row.getValue(offset + 7).asString());
281             obj.setCachedOnURL(row.getValue(offset + 8).asBoolean());
282             //set the security
283
BaseSecurity security =
284                 new BaseSecurity(row.getValue(offset + 9).asString());
285             obj.setSecurity(security);
286             obj.setBaseSecurity(security);
287             //create the content URL
288
BaseContentURL baseContentURL = new BaseContentURL();
289             baseContentURL.setCachedOnURL(row.getValue(offset + 8).asBoolean());
290             baseContentURL.setURL(row.getValue(offset + 7).asString());
291             obj.setContentURL(baseContentURL);
292             //create meta info
293
BaseMetaInfo baseMetaInfo =
294                 new BaseMetaInfo(
295                     row.getValue(offset + 10).asString(),
296                     row.getValue(offset + 11).asString(),
297                     row.getValue(offset + 12).asString());
298             obj.setMetaInfo(baseMetaInfo);
299             String JavaDoc securityRef = row.getValue(offset + 13).asString();
300             if ((securityRef != null) || ("".equals(securityRef)))
301             {
302                 BaseSecurityReference paramSecurityRef =
303                     new BaseSecurityReference();
304                 paramSecurityRef.setParent(securityRef);
305                 obj.setSecurityRef(paramSecurityRef);
306             }
307             buildPortletCategory(id, obj);
308             buildPortletMedia(id, obj);
309             buildPortletParameters(id, obj);
310         }
311         catch (DataSetException e)
312         {
313             throw new TorqueException(e);
314         }
315     }
316     /**
317         * Method to get regsitered data from database.
318         *
319         * @param criteria object used to create the SELECT statement.
320         * @return List of selected Objects
321         * @throws TorqueException Any exceptions caught during processing will be
322         * rethrown wrapped into a TorqueException.
323         */

324     public List JavaDoc getXREGDataFromDb() throws TorqueException
325     {
326         Criteria criteria = buildCriteria();
327         return doSelect(criteria);
328     }
329     public boolean isModified(String JavaDoc lastUpdateDate)
330     {
331         return true;
332     }
333     /**
334      * Method to do selects.
335      *
336      * @param criteria object used to create the SELECT statement.
337      * @return List of selected Objects
338      * @throws TorqueException Any exceptions caught during processing will be
339      * rethrown wrapped into a TorqueException.
340      */

341     public static List JavaDoc doSelect(Criteria criteria) throws TorqueException
342     {
343         return populateObjects(doSelectVillageRecords(criteria));
344     }
345     /**
346         * Method to do selects within a transaction.
347         *
348         * @param criteria object used to create the SELECT statement.
349         * @param con the connection to use
350         * @return List of selected Objects
351         * @throws TorqueException Any exceptions caught during processing will be
352         * rethrown wrapped into a TorqueException.
353         */

354     public static List JavaDoc doSelect(Criteria criteria, Connection JavaDoc con)
355         throws TorqueException
356     {
357         return populateObjects(doSelectVillageRecords(criteria, con));
358     }
359     /**
360        * Grabs the raw Village records to be formed into objects.
361        * This method handles connections internally. The Record objects
362        * returned by this method should be considered readonly. Do not
363        * alter the data and call save(), your results may vary, but are
364        * certainly likely to result in hard to track MT bugs.
365        *
366        * @throws TorqueException Any exceptions caught during processing will be
367        * rethrown wrapped into a TorqueException.
368        */

369     public static List JavaDoc doSelectVillageRecords(Criteria criteria)
370         throws TorqueException
371     {
372         return BaseJetspeedPortletPeer.doSelectVillageRecords(
373             criteria,
374             (Connection JavaDoc) null);
375     }
376     /**
377     * Grabs the raw Village records to be formed into objects.
378      * This method should be used for transactions
379      *
380      * @param con the connection to use
381      * @throws TorqueException Any exceptions caught during processing will be
382      * rethrown wrapped into a TorqueException.
383      */

384     public static List JavaDoc doSelectVillageRecords(
385         Criteria criteria,
386         Connection JavaDoc con)
387         throws TorqueException
388     {
389         if (criteria.getSelectColumns().size() == 0)
390         {
391             addSelectColumns(criteria);
392         }
393         // Set the correct dbName if it has not been overridden
394
// criteria.getDbName will return the same object if not set to
395
// another value so == check is okay and faster
396
if (criteria.getDbName() == Torque.getDefaultDB())
397         {
398             criteria.setDbName(DATABASE_NAME);
399         }
400         // BasePeer returns a List of Value (Village) arrays. The array
401
// order follows the order columns were placed in the Select clause.
402
if (con == null)
403         {
404             return BasePeer.doSelect(criteria);
405         }
406         else
407         {
408             return BasePeer.doSelect(criteria, con);
409         }
410     }
411     /**
412      * The returned List will contain objects of the default type or
413      * objects that inherit from the default.
414      *
415      * @throws TorqueException Any exceptions caught during processing will be
416      * rethrown wrapped into a TorqueException.
417      */

418     public static List JavaDoc populateObjects(List JavaDoc records) throws TorqueException
419     {
420         List JavaDoc results = new ArrayList JavaDoc(records.size());
421         // populate the object(s)
422
for (int i = 0; i < records.size(); i++)
423         {
424             Record row = (Record) records.get(i);
425             results.add(
426                 BaseJetspeedPortletPeer.row2Object(
427                     row,
428                     1,
429                     BaseJetspeedPortletPeer.getOMClass()));
430         }
431         return results;
432     }
433     /** Build a Criteria object from an ObjectKey */
434     public static Criteria buildCriteria(ObjectKey pk)
435     {
436         Criteria criteria = new Criteria();
437         criteria.add(PORTAL_ID, pk);
438         return criteria;
439     }
440     /** Build a Criteria object */
441     public static Criteria buildCriteria()
442     {
443         Criteria criteria = new Criteria();
444         return criteria;
445     }
446     /**
447      * The class that the Peer will make instances of.
448      * If the BO is abstract then you must implement this method
449      * in the BO.
450      *
451      * @throws TorqueException Any exceptions caught during processing will be
452      * rethrown wrapped into a TorqueException.
453      */

454     public static Class JavaDoc getOMClass() throws TorqueException
455     {
456         return CLASS_DEFAULT;
457     }
458     /**
459      * it will make the parameters for this portlet
460      * @param portlet object.
461      *
462      */

463     public static void buildPortletParameters(int id, BasePortletEntry obj)
464         throws TorqueException
465     {
466         try
467         {
468             List JavaDoc list =
469                 BaseJetspeedPortletParameterPeer.retrieveById(
470                     SimpleKey.keyFor(id));
471             for (int i = 0; i < list.size(); i++)
472             {
473                 Parameter p = (Parameter) list.get(i);
474                 obj.addParameter(p);
475             }
476         }
477         catch (Exception JavaDoc e)
478         {
479             throw new TorqueException(e);
480         }
481     }
482     /**
483          * it will make the media types for this portlet
484          * @param portlet object.
485          *
486          */

487     public static void buildPortletMedia(int id, BasePortletEntry obj)
488         throws TorqueException
489     {
490         try
491         {
492             List JavaDoc list =
493                 BaseJetspeedPortletMediaTypePeer.retrieveById(
494                     SimpleKey.keyFor(id));
495             for (int i = 0; i < list.size(); i++)
496             {
497                 BaseMediaType p = (BaseMediaType) list.get(i);
498                 String JavaDoc mediaName = p.getRef();
499                 if (!obj.hasMediaType(mediaName))
500                 {
501                     obj.addMediaType(mediaName);
502                 }
503             }
504         }
505         catch (Exception JavaDoc e)
506         {
507             throw new TorqueException(e);
508         }
509     }
510     /**
511          * it will make the category types for this portlet
512          * @param portlet object.
513          *
514          */

515     public static void buildPortletCategory(int id, BasePortletEntry obj)
516         throws TorqueException
517     {
518         try
519         {
520             List JavaDoc list =
521                 BaseJetspeedPortletCategoryPeer.retrieveById(
522                     SimpleKey.keyFor(id));
523             for (int i = 0; i < list.size(); i++)
524             {
525                 BaseCategory p = (BaseCategory) list.get(i);
526                 String JavaDoc name = p.getName();
527                 String JavaDoc group = p.getGroup();
528                 if (group == null)
529                 {
530                     if (!obj.hasCategory(name))
531                         obj.addCategory(name);
532                 }
533                 else
534                     if (!obj.hasCategory(name, group))
535                     {
536                         obj.addCategory(name, group);
537                     }
538             }
539         }
540         catch (Exception JavaDoc e)
541         {
542             throw new TorqueException(e);
543         }
544     }
545 }
546
Popular Tags