1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.jetspeed.om.registry.database;
17
18 import java.sql.Connection;
19 import java.sql.SQLException;
20 import java.util.ArrayList;
21 import java.util.LinkedList;
22 import java.util.List;
23
24 import org.apache.jetspeed.om.registry.base.BaseMediaType;
25 import org.apache.jetspeed.om.registry.base.BasePortletEntry;
26 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
27 import org.apache.jetspeed.services.logging.JetspeedLogger;
28 import org.apache.torque.Torque;
29 import org.apache.torque.TorqueException;
30 import org.apache.torque.om.ObjectKey;
31 import org.apache.torque.om.SimpleKey;
32 import org.apache.torque.util.BasePeer;
33 import org.apache.torque.util.Criteria;
34
35 import com.workingdogs.village.DataSetException;
36 import com.workingdogs.village.QueryDataSet;
37 import com.workingdogs.village.Record;
38
39
40 /***
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 */
46 public class BaseJetspeedPortletMediaTypePeer extends BasePeer
47 {
48 /***
49 * Static initialization of the logger for this class
50 */
51 protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletMediaTypePeer.class.getName());
52
53 /*** the default database name for this class */
54 public static final String DATABASE_NAME = "default";
55 /*** the table name for this class */
56
57 /*** the column name for the NAME field */
58 public static final String PORTLET_ID;
59 /*** the column name for the NAME field */
60 public static final String MEDIA_NAME;
61 /*** the column name for the MEDIA_ID field */
62 public static final String MEDIA_ID;
63 /*** the column name for the MEDIA_ID field */
64 public static final String PORTLET_MEDIA_ID;
65 static {
66 PORTLET_ID = "PORTLET_MEDIATYPE.ID";
67 MEDIA_NAME = "MEDIATYPE.NAME";
68 PORTLET_MEDIA_ID = "PORTLET_MEDIATYPE.MEDIA_ID";
69 MEDIA_ID = "MEDIATYPE.ID";
70 if (Torque.isInit())
71 {
72 try
73 {
74 getMapBuilder();
75 }
76 catch (Exception e)
77 {
78 logger.error("Could not initialize Peer", e);
79 }
80 }
81 }
82 /*** number of columns for this peer */
83 public static final int numColumns = 1;
84 /*** A class that can be returned by this peer. */
85 protected static final String CLASSNAME_DEFAULT =
86 "org.apache.jetspeed.om.registry.base.BaseMediaType";
87 /*** A class that can be returned by this peer. */
88 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
89 /***
90 * Class object initialization method.
91 *
92 * @param className name of the class to initialize
93 * @return the initialized class
94 */
95 private static Class initClass(String className)
96 {
97 Class c = null;
98 try
99 {
100 c = Class.forName(className);
101 }
102 catch (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
112 if (t instanceof Error)
113 {
114 throw (Error) t.fillInStackTrace();
115 }
116 }
117 return c;
118 }
119 /***
120 * Get the list of objects for a ResultSet. Please not that your
121 * resultset MUST return columns in the right order. You can use
122 * getFieldNames() in BaseObject to get the correct sequence.
123 *
124 * @param results the ResultSet
125 * @return the list of objects
126 * @throws TorqueException Any exceptions caught during processing will be
127 * rethrown wrapped into a TorqueException.
128 */
129 public static List resultSet2Objects(java.sql.ResultSet results)
130 throws TorqueException
131 {
132 try
133 {
134 QueryDataSet qds = null;
135 List rows = null;
136 try
137 {
138 qds = new QueryDataSet(results);
139 rows = getSelectResults(qds);
140 }
141 finally
142 {
143 if (qds != null)
144 {
145 qds.close();
146 }
147 }
148 return populateObjects(rows);
149 }
150 catch (SQLException e)
151 {
152 throw new TorqueException(e);
153 }
154 catch (DataSetException e)
155 {
156 throw new 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 be
164 * rethrown wrapped into a TorqueException.
165 */
166 public static void 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 starting
174 * from a specified offset. This is done so that you can select
175 * other rows than just those needed for this object. You may
176 * for example want to create two objects from the same row.
177 *
178 * @throws TorqueException Any exceptions caught during processing will be
179 * rethrown wrapped into a TorqueException.
180 */
181 public static BaseMediaType row2Object(Record row, int offset, Class cls)
182 throws TorqueException
183 {
184 try
185 {
186 BaseMediaType obj = (BaseMediaType) cls.newInstance();
187 populateObject(row, offset, obj);
188
189
190 return obj;
191 }
192 catch (InstantiationException e)
193 {
194 throw new TorqueException(e);
195 }
196 catch (IllegalAccessException e)
197 {
198 throw new TorqueException(e);
199 }
200 }
201 /***
202 * Populates an object from a resultset row starting
203 * from a specified offset. This is done so that you can select
204 * other rows than just those needed for this object. You may
205 * for example want to create two objects from the same row.
206 *
207 * @throws TorqueException Any exceptions caught during processing will be
208 * rethrown wrapped into a TorqueException.
209 */
210 public static void populateObject(
211 Record row,
212 int offset,
213 BaseMediaType obj)
214 throws TorqueException
215 {
216 try
217 {
218 obj.setRef(row.getValue(offset + 0).asString());
219 }
220 catch (DataSetException e)
221 {
222 throw new 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 Objects
230 * @throws TorqueException Any exceptions caught during processing will be
231 * rethrown wrapped into a TorqueException.
232 */
233 public static List doSelect(Criteria criteria) throws TorqueException
234 {
235 return 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 use
242 * @return List of selected Objects
243 * @throws TorqueException Any exceptions caught during processing will be
244 * rethrown wrapped into a TorqueException.
245 */
246 public static List doSelect(Criteria criteria, Connection con)
247 throws TorqueException
248 {
249 return 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 objects
254 * returned by this method should be considered readonly. Do not
255 * alter the data and call save(), your results may vary, but are
256 * certainly likely to result in hard to track MT bugs.
257 *
258 * @throws TorqueException Any exceptions caught during processing will be
259 * rethrown wrapped into a TorqueException.
260 */
261 public static List doSelectVillageRecords(Criteria criteria)
262 throws TorqueException
263 {
264 return 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 transactions
271 *
272 * @param con the connection to use
273 * @throws TorqueException Any exceptions caught during processing will be
274 * rethrown wrapped into a TorqueException.
275 */
276 public static List doSelectVillageRecords(
277 Criteria criteria,
278 Connection con)
279 throws TorqueException
280 {
281 if (criteria.getSelectColumns().size() == 0)
282 {
283 addSelectColumns(criteria);
284 }
285
286
287
288 if (criteria.getDbName() == Torque.getDefaultDB())
289 {
290 criteria.setDbName(DATABASE_NAME);
291 }
292
293
294 if (con == null)
295 {
296 return BasePeer.doSelect(criteria);
297 }
298 else
299 {
300 return BasePeer.doSelect(criteria, con);
301 }
302 }
303 /***
304 * The returned List will contain objects of the default type or
305 * objects that inherit from the default.
306 *
307 * @throws TorqueException Any exceptions caught during processing will be
308 * rethrown wrapped into a TorqueException.
309 */
310 public static List populateObjects(List records) throws TorqueException
311 {
312 List results = new ArrayList(records.size());
313
314 for (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 }
323 return results;
324 }
325 /***
326 * The class that the Peer will make instances of.
327 * If the BO is abstract then you must implement this method
328 * in the BO.
329 *
330 * @throws TorqueException Any exceptions caught during processing will be
331 * rethrown wrapped into a TorqueException.
332 */
333 public static Class getOMClass() throws TorqueException
334 {
335 return CLASS_DEFAULT;
336 }
337 /***
338 * Method to do selects
339 *
340 * @throws TorqueException Any exceptions caught during processing will be
341 * rethrown wrapped into a TorqueException.
342 */
343 public static List doSelect(BasePortletEntry obj) throws TorqueException
344 {
345 return doSelect(buildCriteria(obj));
346 }
347 /*** Build a Criteria object from an ObjectKey */
348 public static Criteria buildCriteria(ObjectKey pk)
349 {
350 Criteria criteria = new Criteria();
351 criteria.add(PORTLET_ID, pk);
352 criteria.addJoin(PORTLET_MEDIA_ID, MEDIA_ID);
353 return criteria;
354 }
355 /*** Build a Criteria object from the data object for this peer */
356 public static Criteria buildCriteria(BasePortletEntry obj)
357 {
358 Criteria criteria = new Criteria(DATABASE_NAME);
359
360 return criteria;
361 }
362 /***
363 * Retrieve a single object by pk
364 *
365 * @param pk the primary key
366 * @throws TorqueException Any exceptions caught during processing will be
367 * rethrown wrapped into a TorqueException.
368 */
369 public static BaseMediaType retrieveByPK(int pk) throws TorqueException
370 {
371 return retrieveByPK(SimpleKey.keyFor(pk));
372 }
373 /***
374 * Retrieve a single object by pk
375 *
376 * @param pk the primary key
377 * @throws TorqueException Any exceptions caught during processing will be
378 * rethrown wrapped into a TorqueException.
379 */
380 public static BaseMediaType retrieveByPK(ObjectKey pk)
381 throws TorqueException
382 {
383 Connection db = null;
384 BaseMediaType retVal = null;
385 try
386 {
387 db = Torque.getConnection(DATABASE_NAME);
388 retVal = retrieveByPK(pk, db);
389 }
390 finally
391 {
392 Torque.closeConnection(db);
393 }
394 return (retVal);
395 }
396 /***
397 * Retrieve a single object by pk
398 *
399 * @param pk the primary key
400 * @param con the connection to use
401 * @throws TorqueException Any exceptions caught during processing will be
402 * rethrown wrapped into a TorqueException.
403 */
404 public static BaseMediaType retrieveByPK(ObjectKey pk, Connection con)
405 throws TorqueException
406 {
407 Criteria criteria = buildCriteria(pk);
408 List v = doSelect(criteria, con);
409 if (v.size() != 1)
410 {
411 throw new TorqueException("Failed to select one and only one row.");
412 }
413 else
414 {
415 return (BaseMediaType) v.get(0);
416 }
417 }
418 /***
419 * Retrieve a multiple objects by pk
420 *
421 * @param pks List of primary keys
422 * @throws TorqueException Any exceptions caught during processing will be
423 * rethrown wrapped into a TorqueException.
424 */
425 public static List retrieveByPKs(List pks) throws TorqueException
426 {
427 Connection db = null;
428 List retVal = null;
429 try
430 {
431 db = Torque.getConnection(DATABASE_NAME);
432 retVal = retrieveByPKs(pks, db);
433 }
434 finally
435 {
436 Torque.closeConnection(db);
437 }
438 return (retVal);
439 }
440 /***
441 * Retrieve a multiple objects by pk
442 *
443 * @param pks List of primary keys
444 * @param dbcon the connection to use
445 * @throws TorqueException Any exceptions caught during processing will be
446 * rethrown wrapped into a TorqueException.
447 */
448 public static List retrieveByPKs(List pks, Connection dbcon)
449 throws TorqueException
450 {
451 List objs = null;
452 if (pks == null || pks.size() == 0)
453 {
454 objs = new LinkedList();
455 }
456 else
457 {
458 Criteria criteria = new Criteria();
459 criteria.addIn(PORTLET_ID, pks);
460 objs = doSelect(criteria, dbcon);
461 }
462 return objs;
463 }
464 /***
465 * Retrieve a listt by portlet id
466 *
467 * @param id the portlet id
468 * @throws TorqueException Any exceptions caught during processing will be
469 * rethrown wrapped into a TorqueException.
470 */
471 public static List retrieveById(int pk) throws TorqueException
472 {
473 return retrieveById(SimpleKey.keyFor(pk));
474 }
475 /***
476 * Retrieve a list of objects by id
477 *
478 * @param pk the portlet id
479 * @throws TorqueException Any exceptions caught during processing will be
480 * rethrown wrapped into a TorqueException.
481 */
482 public static List retrieveById(ObjectKey pk) throws TorqueException
483 {
484 Connection db = null;
485 List retVal = null;
486 try
487 {
488 db = Torque.getConnection(DATABASE_NAME);
489 retVal = retrieveById(pk, db);
490 }
491 finally
492 {
493 Torque.closeConnection(db);
494 }
495 return (retVal);
496 }
497 /***
498 * Retrieve a single object by pk
499 *
500 * @param pk the primary key
501 * @param con the connection to use
502 * @throws TorqueException Any exceptions caught during processing will be
503 * rethrown wrapped into a TorqueException.
504 */
505 public static List retrieveById(ObjectKey pk, Connection con)
506 throws TorqueException
507 {
508 Criteria criteria = buildCriteria(pk);
509 return doSelect(criteria, con);
510 }
511 }