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.BaseMediaType;
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;
383940/***41 * Base Peer for Portlet Media Type registry entries.42 * 43 * @author <a href="mailto:susinha@cisco.com">Suchisubhra Sinha</a>44 * @version $Id: BaseJetspeedPortletMediaTypePeer.java,v 1.3 2004/04/06 23:00:16 morciuch Exp $45 */46publicclassBaseJetspeedPortletMediaTypePeerextends BasePeer
47 {
48/***49 * Static initialization of the logger for this class50 */51protectedstaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletMediaTypePeer.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 MEDIA_NAME;
61/*** the column name for the MEDIA_ID field */62publicstaticfinal String MEDIA_ID;
63/*** the column name for the MEDIA_ID field */64publicstaticfinal String PORTLET_MEDIA_ID;
65static {
66 PORTLET_ID = "PORTLET_MEDIATYPE.ID";
67 MEDIA_NAME = "MEDIATYPE.NAME";
68 PORTLET_MEDIA_ID = "PORTLET_MEDIATYPE.MEDIA_ID";
69 MEDIA_ID = "MEDIATYPE.ID";
70if (Torque.isInit())
71 {
72try73 {
74 getMapBuilder();
75 }
76catch (Exception e)
77 {
78 logger.error("Could not initialize Peer", e);
79 }
80 }
81 }
82/*** number of columns for this peer */83publicstaticfinalint numColumns = 1;
84/*** A class that can be returned by this peer. */85protectedstaticfinal String CLASSNAME_DEFAULT =
86"org.apache.jetspeed.om.registry.base.BaseMediaType";
87/*** A class that can be returned by this peer. */88protectedstaticfinal Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
89/***90 * Class object initialization method.91 *92 * @param className name of the class to initialize93 * @return the initialized class94 */95privatestatic Class initClass(String className)
96 {
97 Class c = null;
98try99 {
100 c = Class.forName(className);
101 }
102catch (Throwable t)
103 {
104 logger.error(
105"A FATAL ERROR has occurred which should not "106 + "have happened under any circumstance. Please notify "107 + "the Turbine developers <turbine-dev@jakarta.apache.org> "108 + "and give as many details as possible (including the error "109 + "stack trace).",
110 t);
111// Error objects should always be propogated.112if (t instanceof Error)
113 {
114throw (Error) t.fillInStackTrace();
115 }
116 }
117return c;
118 }
119/***120 * Get the list of objects for a ResultSet. Please not that your121 * resultset MUST return columns in the right order. You can use122 * getFieldNames() in BaseObject to get the correct sequence.123 *124 * @param results the ResultSet125 * @return the list of objects126 * @throws TorqueException Any exceptions caught during processing will be127 * rethrown wrapped into a TorqueException.128 */129publicstatic List resultSet2Objects(java.sql.ResultSet results)
130 throws TorqueException
131 {
132try133 {
134 QueryDataSet qds = null;
135 List rows = null;
136try137 {
138 qds = new QueryDataSet(results);
139 rows = getSelectResults(qds);
140 }
141finally142 {
143if (qds != null)
144 {
145 qds.close();
146 }
147 }
148return populateObjects(rows);
149 }
150catch (SQLException e)
151 {
152thrownew TorqueException(e);
153 }
154catch (DataSetException e)
155 {
156thrownew TorqueException(e);
157 }
158 }
159/***160 * Add all the columns needed to create a new object.161 *162 * @param criteria object containing the columns to add.163 * @throws TorqueException Any exceptions caught during processing will be164 * rethrown wrapped into a TorqueException.165 */166publicstaticvoid addSelectColumns(Criteria criteria)
167 throws TorqueException
168 {
169 criteria.addSelectColumn(MEDIA_NAME);
170 criteria.addSelectColumn(MEDIA_ID);
171 }
172/***173 * Create a new object of type cls from a resultset row starting174 * from a specified offset. This is done so that you can select175 * other rows than just those needed for this object. You may176 * for example want to create two objects from the same row.177 *178 * @throws TorqueException Any exceptions caught during processing will be179 * rethrown wrapped into a TorqueException.180 */181publicstaticBaseMediaType row2Object(Record row, int offset, Class cls)
182 throws TorqueException
183 {
184try185 {
186BaseMediaType obj = (BaseMediaType) cls.newInstance();
187 populateObject(row, offset, obj);
188//obj.setModified(false);189//obj.setNew(false);190return obj;
191 }
192catch (InstantiationException e)
193 {
194thrownew TorqueException(e);
195 }
196catch (IllegalAccessException e)
197 {
198thrownew TorqueException(e);
199 }
200 }
201/***202 * Populates an object from a resultset row starting203 * from a specified offset. This is done so that you can select204 * other rows than just those needed for this object. You may205 * for example want to create two objects from the same row.206 *207 * @throws TorqueException Any exceptions caught during processing will be208 * rethrown wrapped into a TorqueException.209 */210publicstaticvoid populateObject(
211 Record row,
212int offset,
213BaseMediaType obj)
214 throws TorqueException
215 {
216try217 {
218 obj.setRef(row.getValue(offset + 0).asString());
219 }
220catch (DataSetException e)
221 {
222thrownew TorqueException(e);
223 }
224 }
225/***226 * Method to do selects.227 *228 * @param criteria object used to create the SELECT statement.229 * @return List of selected Objects230 * @throws TorqueException Any exceptions caught during processing will be231 * rethrown wrapped into a TorqueException.232 */233publicstatic List doSelect(Criteria criteria) throws TorqueException
234 {
235return populateObjects(doSelectVillageRecords(criteria));
236 }
237/***238 * Method to do selects within a transaction.239 *240 * @param criteria object used to create the SELECT statement.241 * @param con the connection to use242 * @return List of selected Objects243 * @throws TorqueException Any exceptions caught during processing will be244 * rethrown wrapped into a TorqueException.245 */246publicstatic List doSelect(Criteria criteria, Connection con)
247 throws TorqueException
248 {
249return populateObjects(doSelectVillageRecords(criteria, con));
250 }
251/***252 * Grabs the raw Village records to be formed into objects.253 * This method handles connections internally. The Record objects254 * returned by this method should be considered readonly. Do not255 * alter the data and call save(), your results may vary, but are256 * certainly likely to result in hard to track MT bugs.257 *258 * @throws TorqueException Any exceptions caught during processing will be259 * rethrown wrapped into a TorqueException.260 */261publicstatic List doSelectVillageRecords(Criteria criteria)
262 throws TorqueException
263 {
264return BaseJetspeedPortletMediaTypePeer.doSelectVillageRecords(
265 criteria,
266 (Connection) null);
267 }
268/***269 * Grabs the raw Village records to be formed into objects.270 * This method should be used for transactions271 *272 * @param con the connection to use273 * @throws TorqueException Any exceptions caught during processing will be274 * rethrown wrapped into a TorqueException.275 */276publicstatic List doSelectVillageRecords(
277 Criteria criteria,
278 Connection con)
279 throws TorqueException
280 {
281if (criteria.getSelectColumns().size() == 0)
282 {
283 addSelectColumns(criteria);
284 }
285// Set the correct dbName if it has not been overridden286// criteria.getDbName will return the same object if not set to287// another value so == check is okay and faster288if (criteria.getDbName() == Torque.getDefaultDB())
289 {
290 criteria.setDbName(DATABASE_NAME);
291 }
292// BasePeer returns a List of Value (Village) arrays. The array293// order follows the order columns were placed in the Select clause.294if (con == null)
295 {
296return BasePeer.doSelect(criteria);
297 }
298else299 {
300return BasePeer.doSelect(criteria, con);
301 }
302 }
303/***304 * The returned List will contain objects of the default type or305 * objects that inherit from the default.306 *307 * @throws TorqueException Any exceptions caught during processing will be308 * rethrown wrapped into a TorqueException.309 */310publicstatic List populateObjects(List records) throws TorqueException
311 {
312 List results = new ArrayList(records.size());
313// populate the object(s)314for (int i = 0; i < records.size(); i++)
315 {
316 Record row = (Record) records.get(i);
317 results.add(
318 BaseJetspeedPortletMediaTypePeer.row2Object(
319 row,
320 1,
321 BaseJetspeedPortletMediaTypePeer.getOMClass()));
322 }
323return results;
324 }
325/***326 * The class that the Peer will make instances of.327 * If the BO is abstract then you must implement this method328 * in the BO.329 *330 * @throws TorqueException Any exceptions caught during processing will be331 * rethrown wrapped into a TorqueException.332 */333publicstatic Class getOMClass() throws TorqueException
334 {
335return CLASS_DEFAULT;
336 }
337/***338 * Method to do selects339 *340 * @throws TorqueException Any exceptions caught during processing will be341 * rethrown wrapped into a TorqueException.342 */343publicstatic List doSelect(BasePortletEntry obj) throws TorqueException
344 {
345return doSelect(buildCriteria(obj));
346 }
347/*** Build a Criteria object from an ObjectKey */348publicstatic Criteria buildCriteria(ObjectKey pk)
349 {
350 Criteria criteria = new Criteria();
351 criteria.add(PORTLET_ID, pk);
352 criteria.addJoin(PORTLET_MEDIA_ID, MEDIA_ID);
353return criteria;
354 }
355/*** Build a Criteria object from the data object for this peer */356publicstatic Criteria buildCriteria(BasePortletEntry obj)
357 {
358 Criteria criteria = new Criteria(DATABASE_NAME);
359//TODO match the values here360return criteria;
361 }
362/***363 * Retrieve a single object by pk364 *365 * @param pk the primary key366 * @throws TorqueException Any exceptions caught during processing will be367 * rethrown wrapped into a TorqueException.368 */369publicstaticBaseMediaType retrieveByPK(int pk) throws TorqueException
370 {
371return retrieveByPK(SimpleKey.keyFor(pk));
372 }
373/***374 * Retrieve a single object by pk375 *376 * @param pk the primary key377 * @throws TorqueException Any exceptions caught during processing will be378 * rethrown wrapped into a TorqueException.379 */380publicstaticBaseMediaType retrieveByPK(ObjectKey pk)
381 throws TorqueException
382 {
383 Connection db = null;
384BaseMediaType retVal = null;
385try386 {
387 db = Torque.getConnection(DATABASE_NAME);
388 retVal = retrieveByPK(pk, db);
389 }
390finally391 {
392 Torque.closeConnection(db);
393 }
394return (retVal);
395 }
396/***397 * Retrieve a single object by pk398 *399 * @param pk the primary key400 * @param con the connection to use401 * @throws TorqueException Any exceptions caught during processing will be402 * rethrown wrapped into a TorqueException.403 */404publicstaticBaseMediaType retrieveByPK(ObjectKey pk, Connection con)
405 throws TorqueException
406 {
407 Criteria criteria = buildCriteria(pk);
408 List v = doSelect(criteria, con);
409if (v.size() != 1)
410 {
411thrownew TorqueException("Failed to select one and only one row.");
412 }
413else414 {
415return (BaseMediaType) v.get(0);
416 }
417 }
418/***419 * Retrieve a multiple objects by pk420 *421 * @param pks List of primary keys422 * @throws TorqueException Any exceptions caught during processing will be423 * rethrown wrapped into a TorqueException.424 */425publicstatic List retrieveByPKs(List pks) throws TorqueException
426 {
427 Connection db = null;
428 List retVal = null;
429try430 {
431 db = Torque.getConnection(DATABASE_NAME);
432 retVal = retrieveByPKs(pks, db);
433 }
434finally435 {
436 Torque.closeConnection(db);
437 }
438return (retVal);
439 }
440/***441 * Retrieve a multiple objects by pk442 *443 * @param pks List of primary keys444 * @param dbcon the connection to use445 * @throws TorqueException Any exceptions caught during processing will be446 * rethrown wrapped into a TorqueException.447 */448publicstatic List retrieveByPKs(List pks, Connection dbcon)
449 throws TorqueException
450 {
451 List objs = null;
452if (pks == null || pks.size() == 0)
453 {
454 objs = new LinkedList();
455 }
456else457 {
458 Criteria criteria = new Criteria();
459 criteria.addIn(PORTLET_ID, pks);
460 objs = doSelect(criteria, dbcon);
461 }
462return objs;
463 }
464/***465 * Retrieve a listt by portlet id466 *467 * @param id the portlet id468 * @throws TorqueException Any exceptions caught during processing will be469 * rethrown wrapped into a TorqueException.470 */471publicstatic List retrieveById(int pk) throws TorqueException
472 {
473return retrieveById(SimpleKey.keyFor(pk));
474 }
475/***476 * Retrieve a list of objects by id477 *478 * @param pk the portlet id479 * @throws TorqueException Any exceptions caught during processing will be480 * rethrown wrapped into a TorqueException.481 */482publicstatic List retrieveById(ObjectKey pk) throws TorqueException
483 {
484 Connection db = null;
485 List retVal = null;
486try487 {
488 db = Torque.getConnection(DATABASE_NAME);
489 retVal = retrieveById(pk, db);
490 }
491finally492 {
493 Torque.closeConnection(db);
494 }
495return (retVal);
496 }
497/***498 * Retrieve a single object by pk499 *500 * @param pk the primary key501 * @param con the connection to use502 * @throws TorqueException Any exceptions caught during processing will be503 * rethrown wrapped into a TorqueException.504 */505publicstatic List retrieveById(ObjectKey pk, Connection con)
506 throws TorqueException
507 {
508 Criteria criteria = buildCriteria(pk);
509return doSelect(criteria, con);
510 }
511 }