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.LinkedList;
22import java.util.List;
2324import org.apache.jetspeed.om.registry.Parameter;
25import org.apache.jetspeed.om.registry.base.BaseMetaInfo;
26import org.apache.jetspeed.om.registry.base.BasePortletEntry;
27import org.apache.jetspeed.om.registry.base.BaseSecurity;
28import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29import org.apache.jetspeed.services.logging.JetspeedLogger;
30import org.apache.torque.Torque;
31import org.apache.torque.TorqueException;
32import org.apache.torque.om.ObjectKey;
33import org.apache.torque.om.SimpleKey;
34import org.apache.torque.util.BasePeer;
35import org.apache.torque.util.Criteria;
3637import com.workingdogs.village.DataSetException;
38import com.workingdogs.village.QueryDataSet;
39import com.workingdogs.village.Record;
4041/***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 */47publicclassBaseJetspeedPortletParameterPeerextends BasePeer
48 {
4950/***51 * Static initialization of the logger for this class52 */53protectedstaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletParameterPeer.class.getName());
5455/*** the default database name for this class */56publicstaticfinal String DATABASE_NAME = "default";
57/*** the table name for this class */58publicstaticfinal String TABLE_NAME = "PORTLET_PARAMETER";
59/*** the column name for the PORTAL_ID field */60publicstaticfinal String ID;
61/*** the column name for the NAME field */62publicstaticfinal String NAME;
63/*** the column name for the NAME field */64publicstaticfinal String VALUE;
65/*** the column name for the HIDDEN field */66publicstaticfinal String HIDDEN;
67/*** the column name for the TYPE field */68publicstaticfinal String TYPE;
69/*** the column name for the CACHEDONURL field */70publicstaticfinal String CACHEDONNAME;
71/*** the column name for the CACHEDONURL field */72publicstaticfinal String CACHEDONVALUE;
73/*** the column name for the ROLE field */74publicstaticfinal String ROLE;
75/*** the column name for the TITLE field */76publicstaticfinal String TITLE;
77/*** the column name for the DESCRIPTION field */78publicstaticfinal String DESCRIPTION;
79/*** the column name for the IMAGE field */80publicstaticfinal String IMAGE;
81/*** the portlet id for this parameter **/82publicstaticfinal String PORTLET_ID;
83static {
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 */107publicstaticfinalint numColumns = 12;
108/*** A class that can be returned by this peer. */109protectedstaticfinal String CLASSNAME_DEFAULT =
110"org.apache.jetspeed.om.registry.base.BaseParameter";
111/*** A class that can be returned by this peer. */112protectedstaticfinal Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
113/***114 * Class object initialization method.115 *116 * @param className name of the class to initialize117 * @return the initialized class118 */119privatestatic Class initClass(String className)
120 {
121 Class c = null;
122try123 {
124 c = Class.forName(className);
125 }
126catch (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.136if (t instanceof Error)
137 {
138throw (Error) t.fillInStackTrace();
139 }
140 }
141return c;
142 }
143/***144 * Get the list of objects for a ResultSet. Please not that your145 * resultset MUST return columns in the right order. You can use146 * getFieldNames() in BaseObject to get the correct sequence.147 *148 * @param results the ResultSet149 * @return the list of objects150 * @throws TorqueException Any exceptions caught during processing will be151 * rethrown wrapped into a TorqueException.152 */153publicstatic List resultSet2Objects(java.sql.ResultSet results)
154 throws TorqueException
155 {
156try157 {
158 QueryDataSet qds = null;
159 List rows = null;
160try161 {
162 qds = new QueryDataSet(results);
163 rows = getSelectResults(qds);
164 }
165finally166 {
167if (qds != null)
168 {
169 qds.close();
170 }
171 }
172return populateObjects(rows);
173 }
174catch (SQLException e)
175 {
176thrownew TorqueException(e);
177 }
178catch (DataSetException e)
179 {
180thrownew 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 be188 * rethrown wrapped into a TorqueException.189 */190publicstaticvoid 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 starting208 * from a specified offset. This is done so that you can select209 * other rows than just those needed for this object. You may210 * for example want to create two objects from the same row.211 *212 * @throws TorqueException Any exceptions caught during processing will be213 * rethrown wrapped into a TorqueException.214 */215publicstaticParameter row2Object(Record row, int offset, Class cls)
216 throws TorqueException
217 {
218try219 {
220Parameter obj = (Parameter) cls.newInstance();
221 populateObject(row, offset, obj);
222//obj.setModified(false);223//obj.setNew(false);224return obj;
225 }
226catch (InstantiationException e)
227 {
228thrownew TorqueException(e);
229 }
230catch (IllegalAccessException e)
231 {
232thrownew TorqueException(e);
233 }
234 }
235/***236 * Populates an object from a resultset row starting237 * from a specified offset. This is done so that you can select238 * other rows than just those needed for this object. You may239 * for example want to create two objects from the same row.240 *241 * @throws TorqueException Any exceptions caught during processing will be242 * rethrown wrapped into a TorqueException.243 */244publicstaticvoid populateObject(Record row, int offset, Parameter obj)
245 throws TorqueException
246 {
247try248 {
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());
253BaseMetaInfo baseMetaInfo =
254newBaseMetaInfo(
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 security260BaseSecurity security =
261newBaseSecurity(row.getValue(offset + 7).asString());
262 obj.setSecurity(security);
263 }
264catch (DataSetException e)
265 {
266thrownew 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 Objects274 * @throws TorqueException Any exceptions caught during processing will be275 * rethrown wrapped into a TorqueException.276 */277publicstatic List doSelect(Criteria criteria) throws TorqueException
278 {
279return 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 use286 * @return List of selected Objects287 * @throws TorqueException Any exceptions caught during processing will be288 * rethrown wrapped into a TorqueException.289 */290publicstatic List doSelect(Criteria criteria, Connection con)
291 throws TorqueException
292 {
293return 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 objects298 * returned by this method should be considered readonly. Do not299 * alter the data and call save(), your results may vary, but are300 * certainly likely to result in hard to track MT bugs.301 *302 * @throws TorqueException Any exceptions caught during processing will be303 * rethrown wrapped into a TorqueException.304 */305publicstatic List doSelectVillageRecords(Criteria criteria)
306 throws TorqueException
307 {
308return 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 transactions315 *316 * @param con the connection to use317 * @throws TorqueException Any exceptions caught during processing will be318 * rethrown wrapped into a TorqueException.319 */320publicstatic List doSelectVillageRecords(
321 Criteria criteria,
322 Connection con)
323 throws TorqueException
324 {
325if (criteria.getSelectColumns().size() == 0)
326 {
327 addSelectColumns(criteria);
328 }
329// Set the correct dbName if it has not been overridden330// criteria.getDbName will return the same object if not set to331// another value so == check is okay and faster332if (criteria.getDbName() == Torque.getDefaultDB())
333 {
334 criteria.setDbName(DATABASE_NAME);
335 }
336// BasePeer returns a List of Value (Village) arrays. The array337// order follows the order columns were placed in the Select clause.338if (con == null)
339 {
340return BasePeer.doSelect(criteria);
341 }
342else343 {
344return BasePeer.doSelect(criteria, con);
345 }
346 }
347/***348 * The returned List will contain objects of the default type or349 * objects that inherit from the default.350 *351 * @throws TorqueException Any exceptions caught during processing will be352 * rethrown wrapped into a TorqueException.353 */354publicstatic List populateObjects(List records) throws TorqueException
355 {
356 List results = new ArrayList(records.size());
357// populate the object(s)358for (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 }
367return results;
368 }
369/***370 * The class that the Peer will make instances of.371 * If the BO is abstract then you must implement this method372 * in the BO.373 *374 * @throws TorqueException Any exceptions caught during processing will be375 * rethrown wrapped into a TorqueException.376 */377publicstatic Class getOMClass() throws TorqueException
378 {
379return CLASS_DEFAULT;
380 }
381/***382 * Method to do selects383 *384 * @throws TorqueException Any exceptions caught during processing will be385 * rethrown wrapped into a TorqueException.386 */387publicstatic List doSelect(BasePortletEntry obj) throws TorqueException
388 {
389return doSelect(buildCriteria(obj));
390 }
391/*** Build a Criteria object from an ObjectKey */392publicstatic Criteria buildCriteria(ObjectKey pk)
393 {
394 Criteria criteria = new Criteria();
395 criteria.add(PORTLET_ID, pk);
396return criteria;
397 }
398/*** Build a Criteria object from the data object for this peer */399publicstatic 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 */412return criteria;
413 }
414/***415 * Retrieve a single object by pk416 *417 * @param pk the primary key418 * @throws TorqueException Any exceptions caught during processing will be419 * rethrown wrapped into a TorqueException.420 */421publicstaticParameter retrieveByPK(int pk) throws TorqueException
422 {
423return retrieveByPK(SimpleKey.keyFor(pk));
424 }
425/***426 * Retrieve a single object by pk427 *428 * @param pk the primary key429 * @throws TorqueException Any exceptions caught during processing will be430 * rethrown wrapped into a TorqueException.431 */432publicstaticParameter retrieveByPK(ObjectKey pk) throws TorqueException
433 {
434 Connection db = null;
435Parameter retVal = null;
436try437 {
438 db = Torque.getConnection(DATABASE_NAME);
439 retVal = retrieveByPK(pk, db);
440 }
441finally442 {
443 Torque.closeConnection(db);
444 }
445return (retVal);
446 }
447/***448 * Retrieve a single object by pk449 *450 * @param pk the primary key451 * @param con the connection to use452 * @throws TorqueException Any exceptions caught during processing will be453 * rethrown wrapped into a TorqueException.454 */455publicstaticParameter retrieveByPK(ObjectKey pk, Connection con)
456 throws TorqueException
457 {
458 Criteria criteria = buildCriteria(pk);
459 List v = doSelect(criteria, con);
460if (v.size() != 1)
461 {
462thrownew TorqueException("Failed to select one and only one row.");
463 }
464else465 {
466return (Parameter) v.get(0);
467 }
468 }
469/***470 * Retrieve a multiple objects by pk471 *472 * @param pks List of primary keys473 * @throws TorqueException Any exceptions caught during processing will be474 * rethrown wrapped into a TorqueException.475 */476publicstatic List retrieveByPKs(List pks) throws TorqueException
477 {
478 Connection db = null;
479 List retVal = null;
480try481 {
482 db = Torque.getConnection(DATABASE_NAME);
483 retVal = retrieveByPKs(pks, db);
484 }
485finally486 {
487 Torque.closeConnection(db);
488 }
489return (retVal);
490 }
491/***492 * Retrieve a multiple objects by pk493 *494 * @param pks List of primary keys495 * @param dbcon the connection to use496 * @throws TorqueException Any exceptions caught during processing will be497 * rethrown wrapped into a TorqueException.498 */499publicstatic List retrieveByPKs(List pks, Connection dbcon)
500 throws TorqueException
501 {
502 List objs = null;
503if (pks == null || pks.size() == 0)
504 {
505 objs = new LinkedList();
506 }
507else508 {
509 Criteria criteria = new Criteria();
510 criteria.addIn(PORTLET_ID, pks);
511 objs = doSelect(criteria, dbcon);
512 }
513return objs;
514 }
515/***516 * Retrieve a listt by portlet id517 *518 * @param id the portlet id519 * @throws TorqueException Any exceptions caught during processing will be520 * rethrown wrapped into a TorqueException.521 */522publicstatic List retrieveById(int pk) throws TorqueException
523 {
524return retrieveById(SimpleKey.keyFor(pk));
525 }
526/***527 * Retrieve a list of objects by id528 *529 * @param pk the portlet id530 * @throws TorqueException Any exceptions caught during processing will be531 * rethrown wrapped into a TorqueException.532 */533publicstatic List retrieveById(ObjectKey pk) throws TorqueException
534 {
535 Connection db = null;
536 List retVal = null;
537try538 {
539 db = Torque.getConnection(DATABASE_NAME);
540 retVal = retrieveById(pk, db);
541 }
542finally543 {
544 Torque.closeConnection(db);
545 }
546return (retVal);
547 }
548/***549 * Retrieve a single object by pk550 *551 * @param pk the primary key552 * @param con the connection to use553 * @throws TorqueException Any exceptions caught during processing will be554 * rethrown wrapped into a TorqueException.555 */556publicstatic List retrieveById(ObjectKey pk, Connection con)
557 throws TorqueException
558 {
559 Criteria criteria = buildCriteria(pk);
560return doSelect(criteria, con);
561 }
562 }