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.BaseSecurity;
27import org.apache.jetspeed.om.registry.base.BaseSkinEntry;
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 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 */47publicclassBaseJetspeedSkinParameterPeerextends BasePeer
48 {
4950/***51 * Static initialization of the logger for this class52 */53protectedstaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedSkinParameterPeer.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 = "SKIN_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 role field */70publicstaticfinal String ROLE;
71/*** the column name for the TITLE field */72publicstaticfinal String TITLE;
73/*** the column name for the DESCRIPTION field */74publicstaticfinal String DESCRIPTION;
75/*** the column name for the IMAGE field */76publicstaticfinal String IMAGE;
77/*** the portlet id for this parameter **/78publicstaticfinal String SKIN_ID;
79static {
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";
90if (Torque.isInit())
91 {
92try93 {
94 getMapBuilder();
95 }
96catch (Exception e)
97 {
98 logger.error("Could not initialize Peer", e);
99 }
100 }
101 }
102/*** number of columns for this peer */103publicstaticfinalint numColumns = 12;
104/*** A class that can be returned by this peer. */105protectedstaticfinal String CLASSNAME_DEFAULT =
106"org.apache.jetspeed.om.registry.base.BaseParameter";
107/*** A class that can be returned by this peer. */108protectedstaticfinal Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
109/***110 * Class object initialization method.111 *112 * @param className name of the class to initialize113 * @return the initialized class114 */115privatestatic Class initClass(String className)
116 {
117 Class c = null;
118try119 {
120 c = Class.forName(className);
121 }
122catch (Throwable 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.132if (t instanceof Error)
133 {
134throw (Error) t.fillInStackTrace();
135 }
136 }
137return c;
138 }
139/***140 * Get the list of objects for a ResultSet. Please not that your141 * resultset MUST return columns in the right order. You can use142 * getFieldNames() in BaseObject to get the correct sequence.143 *144 * @param results the ResultSet145 * @return the list of objects146 * @throws TorqueException Any exceptions caught during processing will be147 * rethrown wrapped into a TorqueException.148 */149publicstatic List resultSet2Objects(java.sql.ResultSet results)
150 throws TorqueException
151 {
152try153 {
154 QueryDataSet qds = null;
155 List rows = null;
156try157 {
158 qds = new QueryDataSet(results);
159 rows = getSelectResults(qds);
160 }
161finally162 {
163if (qds != null)
164 {
165 qds.close();
166 }
167 }
168return populateObjects(rows);
169 }
170catch (SQLException e)
171 {
172thrownew TorqueException(e);
173 }
174catch (DataSetException e)
175 {
176thrownew 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 be184 * rethrown wrapped into a TorqueException.185 */186publicstaticvoid 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 starting202 * from a specified offset. This is done so that you can select203 * other rows than just those needed for this object. You may204 * for example want to create two objects from the same row.205 *206 * @throws TorqueException Any exceptions caught during processing will be207 * rethrown wrapped into a TorqueException.208 */209publicstaticParameter row2Object(Record row, int offset, Class cls)
210 throws TorqueException
211 {
212try213 {
214Parameter obj = (Parameter) cls.newInstance();
215 populateObject(row, offset, obj);
216//obj.setModified(false);217//obj.setNew(false);218return obj;
219 }
220catch (InstantiationException e)
221 {
222thrownew TorqueException(e);
223 }
224catch (IllegalAccessException e)
225 {
226thrownew TorqueException(e);
227 }
228 }
229/***230 * Populates an object from a resultset row starting231 * from a specified offset. This is done so that you can select232 * other rows than just those needed for this object. You may233 * for example want to create two objects from the same row.234 *235 * @throws TorqueException Any exceptions caught during processing will be236 * rethrown wrapped into a TorqueException.237 */238publicstaticvoid populateObject(Record row, int offset, Parameter obj)
239 throws TorqueException
240 {
241try242 {
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());
247BaseMetaInfo baseMetaInfo =
248newBaseMetaInfo(
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 security254BaseSecurity security =
255newBaseSecurity(row.getValue(offset + 5).asString());
256 obj.setSecurity(security);
257 }
258catch (DataSetException e)
259 {
260thrownew 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 Objects268 * @throws TorqueException Any exceptions caught during processing will be269 * rethrown wrapped into a TorqueException.270 */271publicstatic List doSelect(Criteria criteria) throws TorqueException
272 {
273return 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 use280 * @return List of selected Objects281 * @throws TorqueException Any exceptions caught during processing will be282 * rethrown wrapped into a TorqueException.283 */284publicstatic List doSelect(Criteria criteria, Connection con)
285 throws TorqueException
286 {
287return 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 objects292 * returned by this method should be considered readonly. Do not293 * alter the data and call save(), your results may vary, but are294 * certainly likely to result in hard to track MT bugs.295 *296 * @throws TorqueException Any exceptions caught during processing will be297 * rethrown wrapped into a TorqueException.298 */299publicstatic List doSelectVillageRecords(Criteria criteria)
300 throws TorqueException
301 {
302return BaseJetspeedSkinPeer.doSelectVillageRecords(
303 criteria,
304 (Connection) null);
305 }
306/***307 * Grabs the raw Village records to be formed into objects.308 * This method should be used for transactions309 *310 * @param con the connection to use311 * @throws TorqueException Any exceptions caught during processing will be312 * rethrown wrapped into a TorqueException.313 */314publicstatic List doSelectVillageRecords(
315 Criteria criteria,
316 Connection con)
317 throws TorqueException
318 {
319if (criteria.getSelectColumns().size() == 0)
320 {
321 addSelectColumns(criteria);
322 }
323// Set the correct dbName if it has not been overridden324// criteria.getDbName will return the same object if not set to325// another value so == check is okay and faster326if (criteria.getDbName() == Torque.getDefaultDB())
327 {
328 criteria.setDbName(DATABASE_NAME);
329 }
330// BasePeer returns a List of Value (Village) arrays. The array331// order follows the order columns were placed in the Select clause.332if (con == null)
333 {
334return BasePeer.doSelect(criteria);
335 }
336else337 {
338return BasePeer.doSelect(criteria, con);
339 }
340 }
341/***342 * The returned List will contain objects of the default type or343 * objects that inherit from the default.344 *345 * @throws TorqueException Any exceptions caught during processing will be346 * rethrown wrapped into a TorqueException.347 */348publicstatic List populateObjects(List records) throws TorqueException
349 {
350 List results = new ArrayList(records.size());
351// populate the object(s)352for (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 }
361return results;
362 }
363/***364 * The class that the Peer will make instances of.365 * If the BO is abstract then you must implement this method366 * in the BO.367 *368 * @throws TorqueException Any exceptions caught during processing will be369 * rethrown wrapped into a TorqueException.370 */371publicstatic Class getOMClass() throws TorqueException
372 {
373return CLASS_DEFAULT;
374 }
375/***376 * Method to do selects377 *378 * @throws TorqueException Any exceptions caught during processing will be379 * rethrown wrapped into a TorqueException.380 */381publicstatic List doSelect(BaseSkinEntry obj) throws TorqueException
382 {
383return doSelect(buildCriteria(obj));
384 }
385/*** Build a Criteria object from an ObjectKey */386publicstatic Criteria buildCriteria(ObjectKey pk)
387 {
388 Criteria criteria = new Criteria();
389 criteria.add(SKIN_ID, pk);
390return criteria;
391 }
392/*** Build a Criteria object from the data object for this peer */393publicstatic 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 */406return criteria;
407 }
408/***409 * Retrieve a single object by pk410 *411 * @param pk the primary key412 * @throws TorqueException Any exceptions caught during processing will be413 * rethrown wrapped into a TorqueException.414 */415publicstaticParameter retrieveByPK(int pk) throws TorqueException
416 {
417return retrieveByPK(SimpleKey.keyFor(pk));
418 }
419/***420 * Retrieve a single object by pk421 *422 * @param pk the primary key423 * @throws TorqueException Any exceptions caught during processing will be424 * rethrown wrapped into a TorqueException.425 */426publicstaticParameter retrieveByPK(ObjectKey pk) throws TorqueException
427 {
428 Connection db = null;
429Parameter retVal = null;
430try431 {
432 db = Torque.getConnection(DATABASE_NAME);
433 retVal = retrieveByPK(pk, db);
434 }
435finally436 {
437 Torque.closeConnection(db);
438 }
439return (retVal);
440 }
441/***442 * Retrieve a single object by pk443 *444 * @param pk the primary key445 * @param con the connection to use446 * @throws TorqueException Any exceptions caught during processing will be447 * rethrown wrapped into a TorqueException.448 */449publicstaticParameter retrieveByPK(ObjectKey pk, Connection con)
450 throws TorqueException
451 {
452 Criteria criteria = buildCriteria(pk);
453 List v = doSelect(criteria, con);
454if (v.size() != 1)
455 {
456thrownew TorqueException("Failed to select one and only one row.");
457 }
458else459 {
460return (Parameter) v.get(0);
461 }
462 }
463/***464 * Retrieve a multiple objects by pk465 *466 * @param pks List of primary keys467 * @throws TorqueException Any exceptions caught during processing will be468 * rethrown wrapped into a TorqueException.469 */470publicstatic List retrieveByPKs(List pks) throws TorqueException
471 {
472 Connection db = null;
473 List retVal = null;
474try475 {
476 db = Torque.getConnection(DATABASE_NAME);
477 retVal = retrieveByPKs(pks, db);
478 }
479finally480 {
481 Torque.closeConnection(db);
482 }
483return (retVal);
484 }
485/***486 * Retrieve a multiple objects by pk487 *488 * @param pks List of primary keys489 * @param dbcon the connection to use490 * @throws TorqueException Any exceptions caught during processing will be491 * rethrown wrapped into a TorqueException.492 */493publicstatic List retrieveByPKs(List pks, Connection dbcon)
494 throws TorqueException
495 {
496 List objs = null;
497if (pks == null || pks.size() == 0)
498 {
499 objs = new LinkedList();
500 }
501else502 {
503 Criteria criteria = new Criteria();
504 criteria.addIn(SKIN_ID, pks);
505 objs = doSelect(criteria, dbcon);
506 }
507return objs;
508 }
509/***510 * Retrieve a listt by portlet id511 *512 * @param id the portlet id513 * @throws TorqueException Any exceptions caught during processing will be514 * rethrown wrapped into a TorqueException.515 */516publicstatic List retrieveById(int pk) throws TorqueException
517 {
518return retrieveById(SimpleKey.keyFor(pk));
519 }
520/***521 * Retrieve a list of objects by id522 *523 * @param pk the portlet id524 * @throws TorqueException Any exceptions caught during processing will be525 * rethrown wrapped into a TorqueException.526 */527publicstatic List retrieveById(ObjectKey pk) throws TorqueException
528 {
529 Connection db = null;
530 List retVal = null;
531try532 {
533 db = Torque.getConnection(DATABASE_NAME);
534 retVal = retrieveById(pk, db);
535 }
536finally537 {
538 Torque.closeConnection(db);
539 }
540return (retVal);
541 }
542/***543 * Retrieve a single object by pk544 *545 * @param pk the primary key546 * @param con the connection to use547 * @throws TorqueException Any exceptions caught during processing will be548 * rethrown wrapped into a TorqueException.549 */550publicstatic List retrieveById(ObjectKey pk, Connection con)
551 throws TorqueException
552 {
553 Criteria criteria = buildCriteria(pk);
554return doSelect(criteria, con);
555 }
556 }