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.base.BaseCategory;
25import org.apache.jetspeed.om.registry.base.BasePortletEntry;
26import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
27import org.apache.jetspeed.services.logging.JetspeedLogger;
28import org.apache.torque.Torque;
29import org.apache.torque.TorqueException;
30import org.apache.torque.om.ObjectKey;
31import org.apache.torque.om.SimpleKey;
32import org.apache.torque.util.BasePeer;
33import org.apache.torque.util.Criteria;
3435import com.workingdogs.village.DataSetException;
36import com.workingdogs.village.QueryDataSet;
37import com.workingdogs.village.Record;
3839/***40 * Base Peer for Portlet Category Registry entries.41 * 42 * @author <a href="mailto:susinha@cisco.com">Suchisubhra Sinha</a>43 * @version $Id: BaseJetspeedPortletCategoryPeer.java,v 1.3 2004/04/06 23:00:16 morciuch Exp $44 */45publicclassBaseJetspeedPortletCategoryPeerextends BasePeer
46 {
4748/***49 * Static initialization of the logger for this class50 */51protectedstaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletCategoryPeer.class.getName());
5253/*** the default database name for this class */54publicstaticfinal String DATABASE_NAME = "default";
55/*** the table name for this class */56//public static final String TABLE_NAME = "PORTLET_PARAMETER";57/*** the column name for the NAME field */58publicstaticfinal String PORTLET_ID;
59/*** the column name for the NAME field */60publicstaticfinal String NAME;
61/*** the column name for the GROUP field */62publicstaticfinal String GROUP;
63static {
64 PORTLET_ID = "PORTLET_CATEGORY.ID";
65 NAME = "PORTLET_CATEGORY.NAME";
66 GROUP = "PORTLET_CATEGORY.CATEGORY_GROUP";
67if (Torque.isInit())
68 {
69try70 {
71 getMapBuilder();
72 }
73catch (Exception e)
74 {
75 logger.error("Could not initialize Peer", e);
76 }
77 }
78 }
79/*** number of columns for this peer */80publicstaticfinalint numColumns = 1;
81/*** A class that can be returned by this peer. */82protectedstaticfinal String CLASSNAME_DEFAULT =
83"org.apache.jetspeed.om.registry.base.BaseCategory";
84/*** A class that can be returned by this peer. */85protectedstaticfinal Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
8687/***88 * Class object initialization method.89 *90 * @param className name of the class to initialize91 * @return the initialized class92 */93privatestatic Class initClass(String className)
94 {
95 Class c = null;
96try97 {
98 c = Class.forName(className);
99 }
100catch (Throwable t)
101 {
102 logger.error(
103"A FATAL ERROR has occurred which should not "104 + "have happened under any circumstance. Please notify "105 + "the Turbine developers <turbine-dev@jakarta.apache.org> "106 + "and give as many details as possible (including the error "107 + "stack trace).",
108 t);
109// Error objects should always be propogated.110if (t instanceof Error)
111 {
112throw (Error) t.fillInStackTrace();
113 }
114 }
115return c;
116 }
117/***118 * Get the list of objects for a ResultSet. Please not that your119 * resultset MUST return columns in the right order. You can use120 * getFieldNames() in BaseObject to get the correct sequence.121 *122 * @param results the ResultSet123 * @return the list of objects124 * @throws TorqueException Any exceptions caught during processing will be125 * rethrown wrapped into a TorqueException.126 */127publicstatic List resultSet2Objects(java.sql.ResultSet results)
128 throws TorqueException
129 {
130try131 {
132 QueryDataSet qds = null;
133 List rows = null;
134try135 {
136 qds = new QueryDataSet(results);
137 rows = getSelectResults(qds);
138 }
139finally140 {
141if (qds != null)
142 {
143 qds.close();
144 }
145 }
146return populateObjects(rows);
147 }
148catch (SQLException e)
149 {
150thrownew TorqueException(e);
151 }
152catch (DataSetException e)
153 {
154thrownew TorqueException(e);
155 }
156 }
157/***158 * Add all the columns needed to create a new object.159 *160 * @param criteria object containing the columns to add.161 * @throws TorqueException Any exceptions caught during processing will be162 * rethrown wrapped into a TorqueException.163 */164publicstaticvoid addSelectColumns(Criteria criteria)
165 throws TorqueException
166 {
167 criteria.addSelectColumn(NAME);
168 criteria.addSelectColumn(GROUP);
169 }
170/***171 * Create a new object of type cls from a resultset row starting172 * from a specified offset. This is done so that you can select173 * other rows than just those needed for this object. You may174 * for example want to create two objects from the same row.175 *176 * @throws TorqueException Any exceptions caught during processing will be177 * rethrown wrapped into a TorqueException.178 */179publicstaticBaseCategory row2Object(Record row, int offset, Class cls)
180 throws TorqueException
181 {
182try183 {
184BaseCategory obj = (BaseCategory) cls.newInstance();
185 populateObject(row, offset, obj);
186return obj;
187 }
188catch (InstantiationException e)
189 {
190thrownew TorqueException(e);
191 }
192catch (IllegalAccessException e)
193 {
194thrownew TorqueException(e);
195 }
196 }
197/***198 * Populates an object from a resultset row starting199 * from a specified offset. This is done so that you can select200 * other rows than just those needed for this object. You may201 * for example want to create two objects from the same row.202 *203 * @throws TorqueException Any exceptions caught during processing will be204 * rethrown wrapped into a TorqueException.205 */206publicstaticvoid populateObject(Record row, int offset, BaseCategory obj)
207 throws TorqueException
208 {
209try210 {
211 obj.setName(row.getValue(offset + 0).asString());
212if (row.getValue(offset + 1).asString() != null)
213 obj.setGroup(row.getValue(offset + 1).asString());
214 }
215catch (DataSetException e)
216 {
217thrownew TorqueException(e);
218 }
219 }
220/***221 * Method to do selects.222 *223 * @param criteria object used to create the SELECT statement.224 * @return List of selected Objects225 * @throws TorqueException Any exceptions caught during processing will be226 * rethrown wrapped into a TorqueException.227 */228publicstatic List doSelect(Criteria criteria) throws TorqueException
229 {
230return populateObjects(doSelectVillageRecords(criteria));
231 }
232/***233 * Method to do selects within a transaction.234 *235 * @param criteria object used to create the SELECT statement.236 * @param con the connection to use237 * @return List of selected Objects238 * @throws TorqueException Any exceptions caught during processing will be239 * rethrown wrapped into a TorqueException.240 */241publicstatic List doSelect(Criteria criteria, Connection con)
242 throws TorqueException
243 {
244return populateObjects(doSelectVillageRecords(criteria, con));
245 }
246/***247 * Grabs the raw Village records to be formed into objects.248 * This method handles connections internally. The Record objects249 * returned by this method should be considered readonly. Do not250 * alter the data and call save(), your results may vary, but are251 * certainly likely to result in hard to track MT bugs.252 *253 * @throws TorqueException Any exceptions caught during processing will be254 * rethrown wrapped into a TorqueException.255 */256publicstatic List doSelectVillageRecords(Criteria criteria)
257 throws TorqueException
258 {
259return BaseJetspeedPortletCategoryPeer.doSelectVillageRecords(
260 criteria,
261 (Connection) null);
262 }
263/***264 * Grabs the raw Village records to be formed into objects.265 * This method should be used for transactions266 *267 * @param con the connection to use268 * @throws TorqueException Any exceptions caught during processing will be269 * rethrown wrapped into a TorqueException.270 */271publicstatic List doSelectVillageRecords(
272 Criteria criteria,
273 Connection con)
274 throws TorqueException
275 {
276if (criteria.getSelectColumns().size() == 0)
277 {
278 addSelectColumns(criteria);
279 }
280// Set the correct dbName if it has not been overridden281// criteria.getDbName will return the same object if not set to282// another value so == check is okay and faster283if (criteria.getDbName() == Torque.getDefaultDB())
284 {
285 criteria.setDbName(DATABASE_NAME);
286 }
287// BasePeer returns a List of Value (Village) arrays. The array288// order follows the order columns were placed in the Select clause.289if (con == null)
290 {
291return BasePeer.doSelect(criteria);
292 }
293else294 {
295return BasePeer.doSelect(criteria, con);
296 }
297 }
298/***299 * The returned List will contain objects of the default type or300 * objects that inherit from the default.301 *302 * @throws TorqueException Any exceptions caught during processing will be303 * rethrown wrapped into a TorqueException.304 */305publicstatic List populateObjects(List records) throws TorqueException
306 {
307 List results = new ArrayList(records.size());
308// populate the object(s)309for (int i = 0; i < records.size(); i++)
310 {
311 Record row = (Record) records.get(i);
312 results.add(
313 BaseJetspeedPortletCategoryPeer.row2Object(
314 row,
315 1,
316 BaseJetspeedPortletCategoryPeer.getOMClass()));
317 }
318return results;
319 }
320/***321 * The class that the Peer will make instances of.322 * If the BO is abstract then you must implement this method323 * in the BO.324 *325 * @throws TorqueException Any exceptions caught during processing will be326 * rethrown wrapped into a TorqueException.327 */328publicstatic Class getOMClass() throws TorqueException
329 {
330return CLASS_DEFAULT;
331 }
332/***333 * Method to do selects334 *335 * @throws TorqueException Any exceptions caught during processing will be336 * rethrown wrapped into a TorqueException.337 */338publicstatic List doSelect(BasePortletEntry obj) throws TorqueException
339 {
340return doSelect(buildCriteria(obj));
341 }
342/*** Build a Criteria object from an ObjectKey */343publicstatic Criteria buildCriteria(ObjectKey pk)
344 {
345 Criteria criteria = new Criteria();
346 criteria.add(PORTLET_ID, pk);
347return criteria;
348 }
349/*** Build a Criteria object from the data object for this peer */350publicstatic Criteria buildCriteria(BasePortletEntry obj)
351 {
352 Criteria criteria = new Criteria(DATABASE_NAME);
353//TODO match the values here354return criteria;
355 }
356/***357 * Retrieve a single object by pk358 *359 * @param pk the primary key360 * @throws TorqueException Any exceptions caught during processing will be361 * rethrown wrapped into a TorqueException.362 */363publicstaticBaseCategory retrieveByPK(int pk) throws TorqueException
364 {
365return retrieveByPK(SimpleKey.keyFor(pk));
366 }
367/***368 * Retrieve a single object by pk369 *370 * @param pk the primary key371 * @throws TorqueException Any exceptions caught during processing will be372 * rethrown wrapped into a TorqueException.373 */374publicstaticBaseCategory retrieveByPK(ObjectKey pk)
375 throws TorqueException
376 {
377 Connection db = null;
378BaseCategory retVal = null;
379try380 {
381 db = Torque.getConnection(DATABASE_NAME);
382 retVal = retrieveByPK(pk, db);
383 }
384finally385 {
386 Torque.closeConnection(db);
387 }
388return (retVal);
389 }
390/***391 * Retrieve a single object by pk392 *393 * @param pk the primary key394 * @param con the connection to use395 * @throws TorqueException Any exceptions caught during processing will be396 * rethrown wrapped into a TorqueException.397 */398publicstaticBaseCategory retrieveByPK(ObjectKey pk, Connection con)
399 throws TorqueException
400 {
401 Criteria criteria = buildCriteria(pk);
402 List v = doSelect(criteria, con);
403if (v.size() != 1)
404 {
405thrownew TorqueException("Failed to select one and only one row.");
406 }
407else408 {
409return (BaseCategory) v.get(0);
410 }
411 }
412/***413 * Retrieve a multiple objects by pk414 *415 * @param pks List of primary keys416 * @throws TorqueException Any exceptions caught during processing will be417 * rethrown wrapped into a TorqueException.418 */419publicstatic List retrieveByPKs(List pks) throws TorqueException
420 {
421 Connection db = null;
422 List retVal = null;
423try424 {
425 db = Torque.getConnection(DATABASE_NAME);
426 retVal = retrieveByPKs(pks, db);
427 }
428finally429 {
430 Torque.closeConnection(db);
431 }
432return (retVal);
433 }
434/***435 * Retrieve a multiple objects by pk436 *437 * @param pks List of primary keys438 * @param dbcon the connection to use439 * @throws TorqueException Any exceptions caught during processing will be440 * rethrown wrapped into a TorqueException.441 */442publicstatic List retrieveByPKs(List pks, Connection dbcon)
443 throws TorqueException
444 {
445 List objs = null;
446if (pks == null || pks.size() == 0)
447 {
448 objs = new LinkedList();
449 }
450else451 {
452 Criteria criteria = new Criteria();
453 criteria.addIn(PORTLET_ID, pks);
454 objs = doSelect(criteria, dbcon);
455 }
456return objs;
457 }
458/***459 * Retrieve a listt by portlet id460 *461 * @param id the portlet id462 * @throws TorqueException Any exceptions caught during processing will be463 * rethrown wrapped into a TorqueException.464 */465publicstatic List retrieveById(int pk) throws TorqueException
466 {
467return retrieveById(SimpleKey.keyFor(pk));
468 }
469/***470 * Retrieve a list of objects by id471 *472 * @param pk the portlet id473 * @throws TorqueException Any exceptions caught during processing will be474 * rethrown wrapped into a TorqueException.475 */476publicstatic List retrieveById(ObjectKey pk) throws TorqueException
477 {
478 Connection db = null;
479 List retVal = null;
480try481 {
482 db = Torque.getConnection(DATABASE_NAME);
483 retVal = retrieveById(pk, db);
484 }
485finally486 {
487 Torque.closeConnection(db);
488 }
489return (retVal);
490 }
491492/***493 * Retrieve a single object by pk494 *495 * @param pk the primary key496 * @param con the connection to use497 * @throws TorqueException Any exceptions caught during processing will be498 * rethrown wrapped into a TorqueException.499 */500publicstatic List retrieveById(ObjectKey pk, Connection con)
501 throws TorqueException
502 {
503 Criteria criteria = buildCriteria(pk);
504return doSelect(criteria, con);
505 }
506 }