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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */16packageorg.apache.jetspeed.om.registry.database;
1718import java.sql.Connection;
19import java.sql.SQLException;
20import java.util.ArrayList;
21import java.util.List;
2223import org.apache.jetspeed.om.BaseSecurityReference;
24import org.apache.jetspeed.om.registry.DBRegistry;
25import org.apache.jetspeed.om.registry.Parameter;
26import org.apache.jetspeed.om.registry.base.BaseCategory;
27import org.apache.jetspeed.om.registry.base.BaseContentURL;
28import org.apache.jetspeed.om.registry.base.BaseMediaType;
29import org.apache.jetspeed.om.registry.base.BaseMetaInfo;
30import org.apache.jetspeed.om.registry.base.BasePortletEntry;
31import org.apache.jetspeed.om.registry.base.BaseSecurity;
32import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
33import org.apache.jetspeed.services.logging.JetspeedLogger;
34import org.apache.torque.Torque;
35import org.apache.torque.TorqueException;
36import org.apache.torque.om.ObjectKey;
37import org.apache.torque.om.SimpleKey;
38import org.apache.torque.util.BasePeer;
39import org.apache.torque.util.Criteria;
4041import com.workingdogs.village.DataSetException;
42import com.workingdogs.village.QueryDataSet;
43import com.workingdogs.village.Record;
4445/***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 */51publicclassBaseJetspeedPortletPeerextends BasePeer implements DBRegistry52 {
5354/***55 * Static initialization of the logger for this class56 */57protectedstaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletPeer.class.getName());
5859/*** the table name for this class */60publicstaticfinal String TABLE_NAME = "PORTLET";
61/*** the column name for the PORTAL_ID field */62publicstaticfinal String PORTAL_ID;
63/*** the column name for the NAME field */64publicstaticfinal String NAME;
65/*** the column name for the HIDDEN field */66publicstaticfinal String HIDDEN;
67/*** the column name for the CLASSNAME field */68publicstaticfinal String CLASSNAME;
69/*** the column name for the TYPE field */70publicstaticfinal String TYPE;
71/*** the column name for the APPLICATION field */72publicstaticfinal String APPLICATION;
73/*** the column name for the PARENT field */74publicstaticfinal String PARENT;
75/*** the column name for the URL field */76publicstaticfinal String URL;
77/*** the column name for the CACHEDONURL field */78publicstaticfinal String CACHEDONURL;
79/*** the column name for the ROLE field */80publicstaticfinal String ROLE;
81/*** the column name for the TITLE field */82publicstaticfinal String TITLE;
83/*** the column name for the DESCRIPTION field */84publicstaticfinal String DESCRIPTION;
85/*** the column name for the IMAGE field */86publicstaticfinal String IMAGE;
87/*** the column name for the SECURITY field */88publicstaticfinal String SECURITY;
89/*** the column name for the field */90publicstaticfinal String LASTUPDATE;
91static {
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 try111 {112 getMapBuilder();113 }114 catch (Exception e)115 {116117 category.error("Could not initialize Peer", e);118 }119 }120 */121 }
122/*** number of columns for this peer */123publicstaticfinalint numColumns = 14;
124/*** A class that can be returned by this peer. */125protectedstaticfinal String CLASSNAME_DEFAULT =
126"org.apache.jetspeed.om.registry.base.BasePortletEntry";
127/*** A class that can be returned by this peer. */128protectedstaticfinal Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
129/***130 * Class object initialization method.131 *132 * @param className name of the class to initialize133 * @return the initialized class134 */135privatestatic Class initClass(String className)
136 {
137 Class c = null;
138try139 {
140 c = Class.forName(className);
141 }
142catch (Throwable 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.152if (t instanceof Error)
153 {
154throw (Error) t.fillInStackTrace();
155 }
156 }
157return c;
158 }
159/***160 * Get the list of objects for a ResultSet. Please not that your161 * resultset MUST return columns in the right order. You can use162 * getFieldNames() in BaseObject to get the correct sequence.163 *164 * @param results the ResultSet165 * @return the list of objects166 * @throws TorqueException Any exceptions caught during processing will be167 * rethrown wrapped into a TorqueException.168 */169publicstatic List resultSet2Objects(java.sql.ResultSet results)
170 throws TorqueException
171 {
172try173 {
174 QueryDataSet qds = null;
175 List rows = null;
176try177 {
178 qds = new QueryDataSet(results);
179 rows = getSelectResults(qds);
180 }
181finally182 {
183if (qds != null)
184 {
185 qds.close();
186 }
187 }
188return populateObjects(rows);
189 }
190catch (SQLException e)
191 {
192thrownew TorqueException(e);
193 }
194catch (DataSetException e)
195 {
196thrownew 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 be204 * rethrown wrapped into a TorqueException.205 */206publicstaticvoid 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 starting226 * from a specified offset. This is done so that you can select227 * other rows than just those needed for this object. You may228 * for example want to create two objects from the same row.229 *230 * @throws TorqueException Any exceptions caught during processing will be231 * rethrown wrapped into a TorqueException.232 */233publicstaticBasePortletEntry row2Object(
234 Record row,
235int offset,
236 Class cls)
237 throws TorqueException
238 {
239try240 {
241BasePortletEntry obj = (BasePortletEntry) cls.newInstance();
242 populateObject(row, offset, obj);
243//obj.setModified(false);244//obj.setNew(false);245return obj;
246 }
247catch (InstantiationException e)
248 {
249thrownew TorqueException(e);
250 }
251catch (IllegalAccessException e)
252 {
253thrownew TorqueException(e);
254 }
255 }
256/***257 * Populates an object from a resultset row starting258 * from a specified offset. This is done so that you can select259 * other rows than just those needed for this object. You may260 * for example want to create two objects from the same row.261 *262 * @throws TorqueException Any exceptions caught during processing will be263 * rethrown wrapped into a TorqueException.264 */265publicstaticvoid populateObject(
266 Record row,
267int offset,
268BasePortletEntry obj)
269 throws TorqueException
270 {
271try272 {
273int 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 security283BaseSecurity security =
284newBaseSecurity(row.getValue(offset + 9).asString());
285 obj.setSecurity(security);
286 obj.setBaseSecurity(security);
287//create the content URL288BaseContentURL baseContentURL = newBaseContentURL();
289 baseContentURL.setCachedOnURL(row.getValue(offset + 8).asBoolean());
290 baseContentURL.setURL(row.getValue(offset + 7).asString());
291 obj.setContentURL(baseContentURL);
292//create meta info293BaseMetaInfo baseMetaInfo =
294newBaseMetaInfo(
295 row.getValue(offset + 10).asString(),
296 row.getValue(offset + 11).asString(),
297 row.getValue(offset + 12).asString());
298 obj.setMetaInfo(baseMetaInfo);
299 String securityRef = row.getValue(offset + 13).asString();
300if ((securityRef != null) || ("".equals(securityRef)))
301 {
302BaseSecurityReference paramSecurityRef =
303newBaseSecurityReference();
304 paramSecurityRef.setParent(securityRef);
305 obj.setSecurityRef(paramSecurityRef);
306 }
307 buildPortletCategory(id, obj);
308 buildPortletMedia(id, obj);
309 buildPortletParameters(id, obj);
310 }
311catch (DataSetException e)
312 {
313thrownew 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 Objects321 * @throws TorqueException Any exceptions caught during processing will be322 * rethrown wrapped into a TorqueException.323 */324public List getXREGDataFromDb() throws TorqueException
325 {
326 Criteria criteria = buildCriteria();
327return doSelect(criteria);
328 }
329publicboolean isModified(String lastUpdateDate)
330 {
331returntrue;
332 }
333/***334 * Method to do selects.335 *336 * @param criteria object used to create the SELECT statement.337 * @return List of selected Objects338 * @throws TorqueException Any exceptions caught during processing will be339 * rethrown wrapped into a TorqueException.340 */341publicstatic List doSelect(Criteria criteria) throws TorqueException
342 {
343return 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 use350 * @return List of selected Objects351 * @throws TorqueException Any exceptions caught during processing will be352 * rethrown wrapped into a TorqueException.353 */354publicstatic List doSelect(Criteria criteria, Connection con)
355 throws TorqueException
356 {
357return 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 objects362 * returned by this method should be considered readonly. Do not363 * alter the data and call save(), your results may vary, but are364 * certainly likely to result in hard to track MT bugs.365 *366 * @throws TorqueException Any exceptions caught during processing will be367 * rethrown wrapped into a TorqueException.368 */369publicstatic List doSelectVillageRecords(Criteria criteria)
370 throws TorqueException
371 {
372return BaseJetspeedPortletPeer.doSelectVillageRecords(
373 criteria,
374 (Connection) null);
375 }
376/***377 * Grabs the raw Village records to be formed into objects.378 * This method should be used for transactions379 *380 * @param con the connection to use381 * @throws TorqueException Any exceptions caught during processing will be382 * rethrown wrapped into a TorqueException.383 */384publicstatic List doSelectVillageRecords(
385 Criteria criteria,
386 Connection con)
387 throws TorqueException
388 {
389if (criteria.getSelectColumns().size() == 0)
390 {
391 addSelectColumns(criteria);
392 }
393// Set the correct dbName if it has not been overridden394// criteria.getDbName will return the same object if not set to395// another value so == check is okay and faster396if (criteria.getDbName() == Torque.getDefaultDB())
397 {
398 criteria.setDbName(DATABASE_NAME);
399 }
400// BasePeer returns a List of Value (Village) arrays. The array401// order follows the order columns were placed in the Select clause.402if (con == null)
403 {
404return BasePeer.doSelect(criteria);
405 }
406else407 {
408return BasePeer.doSelect(criteria, con);
409 }
410 }
411/***412 * The returned List will contain objects of the default type or413 * objects that inherit from the default.414 *415 * @throws TorqueException Any exceptions caught during processing will be416 * rethrown wrapped into a TorqueException.417 */418publicstatic List populateObjects(List records) throws TorqueException
419 {
420 List results = new ArrayList(records.size());
421// populate the object(s)422for (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 }
431return results;
432 }
433/*** Build a Criteria object from an ObjectKey */434publicstatic Criteria buildCriteria(ObjectKey pk)
435 {
436 Criteria criteria = new Criteria();
437 criteria.add(PORTAL_ID, pk);
438return criteria;
439 }
440/*** Build a Criteria object */441publicstatic Criteria buildCriteria()
442 {
443 Criteria criteria = new Criteria();
444return criteria;
445 }
446/***447 * The class that the Peer will make instances of.448 * If the BO is abstract then you must implement this method449 * in the BO.450 *451 * @throws TorqueException Any exceptions caught during processing will be452 * rethrown wrapped into a TorqueException.453 */454publicstatic Class getOMClass() throws TorqueException
455 {
456return CLASS_DEFAULT;
457 }
458/***459 * it will make the parameters for this portlet460 * @param portlet object.461 * 462 */463publicstaticvoid buildPortletParameters(int id, BasePortletEntry obj)
464 throws TorqueException
465 {
466try467 {
468 List list =
469 BaseJetspeedPortletParameterPeer.retrieveById(
470 SimpleKey.keyFor(id));
471for (int i = 0; i < list.size(); i++)
472 {
473Parameter p = (Parameter) list.get(i);
474 obj.addParameter(p);
475 }
476 }
477catch (Exception e)
478 {
479thrownew TorqueException(e);
480 }
481 }
482/***483 * it will make the media types for this portlet484 * @param portlet object.485 * 486 */487publicstaticvoid buildPortletMedia(int id, BasePortletEntry obj)
488 throws TorqueException
489 {
490try491 {
492 List list =
493 BaseJetspeedPortletMediaTypePeer.retrieveById(
494 SimpleKey.keyFor(id));
495for (int i = 0; i < list.size(); i++)
496 {
497BaseMediaType p = (BaseMediaType) list.get(i);
498 String mediaName = p.getRef();
499if (!obj.hasMediaType(mediaName))
500 {
501 obj.addMediaType(mediaName);
502 }
503 }
504 }
505catch (Exception e)
506 {
507thrownew TorqueException(e);
508 }
509 }
510/***511 * it will make the category types for this portlet512 * @param portlet object.513 * 514 */515publicstaticvoid buildPortletCategory(int id, BasePortletEntry obj)
516 throws TorqueException
517 {
518try519 {
520 List list =
521 BaseJetspeedPortletCategoryPeer.retrieveById(
522 SimpleKey.keyFor(id));
523for (int i = 0; i < list.size(); i++)
524 {
525BaseCategory p = (BaseCategory) list.get(i);
526 String name = p.getName();
527 String group = p.getGroup();
528if (group == null)
529 {
530if (!obj.hasCategory(name))
531 obj.addCategory(name);
532 }
533else534if (!obj.hasCategory(name, group))
535 {
536 obj.addCategory(name, group);
537 }
538 }
539 }
540catch (Exception e)
541 {
542thrownew TorqueException(e);
543 }
544 }
545 }