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.Parameter;
25 import org.apache.jetspeed.om.registry.base.BaseMetaInfo;
26 import org.apache.jetspeed.om.registry.base.BaseSecurity;
27 import org.apache.jetspeed.om.registry.base.BaseSkinEntry;
28 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29 import org.apache.jetspeed.services.logging.JetspeedLogger;
30 import org.apache.torque.Torque;
31 import org.apache.torque.TorqueException;
32 import org.apache.torque.om.ObjectKey;
33 import org.apache.torque.om.SimpleKey;
34 import org.apache.torque.util.BasePeer;
35 import org.apache.torque.util.Criteria;
36
37 import com.workingdogs.village.DataSetException;
38 import com.workingdogs.village.QueryDataSet;
39 import com.workingdogs.village.Record;
40
41 /***
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 */
47 public class BaseJetspeedSkinParameterPeer extends BasePeer
48 {
49
50 /***
51 * Static initialization of the logger for this class
52 */
53 protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedSkinParameterPeer.class.getName());
54
55 /*** the default database name for this class */
56 public static final String DATABASE_NAME = "default";
57 /*** the table name for this class */
58 public static final String TABLE_NAME = "SKIN_PARAMETER";
59 /*** the column name for the PORTAL_ID field */
60 public static final String ID;
61 /*** the column name for the NAME field */
62 public static final String NAME;
63 /*** the column name for the NAME field */
64 public static final String VALUE;
65 /*** the column name for the HIDDEN field */
66 public static final String HIDDEN;
67 /*** the column name for the TYPE field */
68 public static final String TYPE;
69 /*** the column name for the role field */
70 public static final String ROLE;
71 /*** the column name for the TITLE field */
72 public static final String TITLE;
73 /*** the column name for the DESCRIPTION field */
74 public static final String DESCRIPTION;
75 /*** the column name for the IMAGE field */
76 public static final String IMAGE;
77 /*** the portlet id for this parameter **/
78 public static final String SKIN_ID;
79 static {
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";
90 if (Torque.isInit())
91 {
92 try
93 {
94 getMapBuilder();
95 }
96 catch (Exception e)
97 {
98 logger.error("Could not initialize Peer", e);
99 }
100 }
101 }
102 /*** number of columns for this peer */
103 public static final int numColumns = 12;
104 /*** A class that can be returned by this peer. */
105 protected static final String CLASSNAME_DEFAULT =
106 "org.apache.jetspeed.om.registry.base.BaseParameter";
107 /*** A class that can be returned by this peer. */
108 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
109 /***
110 * Class object initialization method.
111 *
112 * @param className name of the class to initialize
113 * @return the initialized class
114 */
115 private static Class initClass(String className)
116 {
117 Class c = null;
118 try
119 {
120 c = Class.forName(className);
121 }
122 catch (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
132 if (t instanceof Error)
133 {
134 throw (Error) t.fillInStackTrace();
135 }
136 }
137 return c;
138 }
139 /***
140 * Get the list of objects for a ResultSet. Please not that your
141 * resultset MUST return columns in the right order. You can use
142 * getFieldNames() in BaseObject to get the correct sequence.
143 *
144 * @param results the ResultSet
145 * @return the list of objects
146 * @throws TorqueException Any exceptions caught during processing will be
147 * rethrown wrapped into a TorqueException.
148 */
149 public static List resultSet2Objects(java.sql.ResultSet results)
150 throws TorqueException
151 {
152 try
153 {
154 QueryDataSet qds = null;
155 List rows = null;
156 try
157 {
158 qds = new QueryDataSet(results);
159 rows = getSelectResults(qds);
160 }
161 finally
162 {
163 if (qds != null)
164 {
165 qds.close();
166 }
167 }
168 return populateObjects(rows);
169 }
170 catch (SQLException e)
171 {
172 throw new TorqueException(e);
173 }
174 catch (DataSetException e)
175 {
176 throw new 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 be
184 * rethrown wrapped into a TorqueException.
185 */
186 public static void 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 starting
202 * from a specified offset. This is done so that you can select
203 * other rows than just those needed for this object. You may
204 * for example want to create two objects from the same row.
205 *
206 * @throws TorqueException Any exceptions caught during processing will be
207 * rethrown wrapped into a TorqueException.
208 */
209 public static Parameter row2Object(Record row, int offset, Class cls)
210 throws TorqueException
211 {
212 try
213 {
214 Parameter obj = (Parameter) cls.newInstance();
215 populateObject(row, offset, obj);
216
217
218 return obj;
219 }
220 catch (InstantiationException e)
221 {
222 throw new TorqueException(e);
223 }
224 catch (IllegalAccessException e)
225 {
226 throw new TorqueException(e);
227 }
228 }
229 /***
230 * Populates an object from a resultset row starting
231 * from a specified offset. This is done so that you can select
232 * other rows than just those needed for this object. You may
233 * for example want to create two objects from the same row.
234 *
235 * @throws TorqueException Any exceptions caught during processing will be
236 * rethrown wrapped into a TorqueException.
237 */
238 public static void populateObject(Record row, int offset, Parameter obj)
239 throws TorqueException
240 {
241 try
242 {
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());
247 BaseMetaInfo baseMetaInfo =
248 new BaseMetaInfo(
249 row.getValue(offset + 6).asString(),
250 row.getValue(offset + 7).asString(),
251 row.getValue(offset + 8).asString());
252 obj.setMetaInfo(baseMetaInfo);
253
254 BaseSecurity security =
255 new BaseSecurity(row.getValue(offset + 5).asString());
256 obj.setSecurity(security);
257 }
258 catch (DataSetException e)
259 {
260 throw new 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 Objects
268 * @throws TorqueException Any exceptions caught during processing will be
269 * rethrown wrapped into a TorqueException.
270 */
271 public static List doSelect(Criteria criteria) throws TorqueException
272 {
273 return 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 use
280 * @return List of selected Objects
281 * @throws TorqueException Any exceptions caught during processing will be
282 * rethrown wrapped into a TorqueException.
283 */
284 public static List doSelect(Criteria criteria, Connection con)
285 throws TorqueException
286 {
287 return 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 objects
292 * returned by this method should be considered readonly. Do not
293 * alter the data and call save(), your results may vary, but are
294 * certainly likely to result in hard to track MT bugs.
295 *
296 * @throws TorqueException Any exceptions caught during processing will be
297 * rethrown wrapped into a TorqueException.
298 */
299 public static List doSelectVillageRecords(Criteria criteria)
300 throws TorqueException
301 {
302 return 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 transactions
309 *
310 * @param con the connection to use
311 * @throws TorqueException Any exceptions caught during processing will be
312 * rethrown wrapped into a TorqueException.
313 */
314 public static List doSelectVillageRecords(
315 Criteria criteria,
316 Connection con)
317 throws TorqueException
318 {
319 if (criteria.getSelectColumns().size() == 0)
320 {
321 addSelectColumns(criteria);
322 }
323
324
325
326 if (criteria.getDbName() == Torque.getDefaultDB())
327 {
328 criteria.setDbName(DATABASE_NAME);
329 }
330
331
332 if (con == null)
333 {
334 return BasePeer.doSelect(criteria);
335 }
336 else
337 {
338 return BasePeer.doSelect(criteria, con);
339 }
340 }
341 /***
342 * The returned List will contain objects of the default type or
343 * objects that inherit from the default.
344 *
345 * @throws TorqueException Any exceptions caught during processing will be
346 * rethrown wrapped into a TorqueException.
347 */
348 public static List populateObjects(List records) throws TorqueException
349 {
350 List results = new ArrayList(records.size());
351
352 for (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 }
361 return results;
362 }
363 /***
364 * The class that the Peer will make instances of.
365 * If the BO is abstract then you must implement this method
366 * in the BO.
367 *
368 * @throws TorqueException Any exceptions caught during processing will be
369 * rethrown wrapped into a TorqueException.
370 */
371 public static Class getOMClass() throws TorqueException
372 {
373 return CLASS_DEFAULT;
374 }
375 /***
376 * Method to do selects
377 *
378 * @throws TorqueException Any exceptions caught during processing will be
379 * rethrown wrapped into a TorqueException.
380 */
381 public static List doSelect(BaseSkinEntry obj) throws TorqueException
382 {
383 return doSelect(buildCriteria(obj));
384 }
385 /*** Build a Criteria object from an ObjectKey */
386 public static Criteria buildCriteria(ObjectKey pk)
387 {
388 Criteria criteria = new Criteria();
389 criteria.add(SKIN_ID, pk);
390 return criteria;
391 }
392 /*** Build a Criteria object from the data object for this peer */
393 public static Criteria buildCriteria(BaseSkinEntry obj)
394 {
395 Criteria criteria = new Criteria(DATABASE_NAME);
396
397
398
399
400
401
402
403
404
405
406 return criteria;
407 }
408 /***
409 * Retrieve a single object by pk
410 *
411 * @param pk the primary key
412 * @throws TorqueException Any exceptions caught during processing will be
413 * rethrown wrapped into a TorqueException.
414 */
415 public static Parameter retrieveByPK(int pk) throws TorqueException
416 {
417 return retrieveByPK(SimpleKey.keyFor(pk));
418 }
419 /***
420 * Retrieve a single object by pk
421 *
422 * @param pk the primary key
423 * @throws TorqueException Any exceptions caught during processing will be
424 * rethrown wrapped into a TorqueException.
425 */
426 public static Parameter retrieveByPK(ObjectKey pk) throws TorqueException
427 {
428 Connection db = null;
429 Parameter retVal = null;
430 try
431 {
432 db = Torque.getConnection(DATABASE_NAME);
433 retVal = retrieveByPK(pk, db);
434 }
435 finally
436 {
437 Torque.closeConnection(db);
438 }
439 return (retVal);
440 }
441 /***
442 * Retrieve a single object by pk
443 *
444 * @param pk the primary key
445 * @param con the connection to use
446 * @throws TorqueException Any exceptions caught during processing will be
447 * rethrown wrapped into a TorqueException.
448 */
449 public static Parameter retrieveByPK(ObjectKey pk, Connection con)
450 throws TorqueException
451 {
452 Criteria criteria = buildCriteria(pk);
453 List v = doSelect(criteria, con);
454 if (v.size() != 1)
455 {
456 throw new TorqueException("Failed to select one and only one row.");
457 }
458 else
459 {
460 return (Parameter) v.get(0);
461 }
462 }
463 /***
464 * Retrieve a multiple objects by pk
465 *
466 * @param pks List of primary keys
467 * @throws TorqueException Any exceptions caught during processing will be
468 * rethrown wrapped into a TorqueException.
469 */
470 public static List retrieveByPKs(List pks) throws TorqueException
471 {
472 Connection db = null;
473 List retVal = null;
474 try
475 {
476 db = Torque.getConnection(DATABASE_NAME);
477 retVal = retrieveByPKs(pks, db);
478 }
479 finally
480 {
481 Torque.closeConnection(db);
482 }
483 return (retVal);
484 }
485 /***
486 * Retrieve a multiple objects by pk
487 *
488 * @param pks List of primary keys
489 * @param dbcon the connection to use
490 * @throws TorqueException Any exceptions caught during processing will be
491 * rethrown wrapped into a TorqueException.
492 */
493 public static List retrieveByPKs(List pks, Connection dbcon)
494 throws TorqueException
495 {
496 List objs = null;
497 if (pks == null || pks.size() == 0)
498 {
499 objs = new LinkedList();
500 }
501 else
502 {
503 Criteria criteria = new Criteria();
504 criteria.addIn(SKIN_ID, pks);
505 objs = doSelect(criteria, dbcon);
506 }
507 return objs;
508 }
509 /***
510 * Retrieve a listt by portlet id
511 *
512 * @param id the portlet id
513 * @throws TorqueException Any exceptions caught during processing will be
514 * rethrown wrapped into a TorqueException.
515 */
516 public static List retrieveById(int pk) throws TorqueException
517 {
518 return retrieveById(SimpleKey.keyFor(pk));
519 }
520 /***
521 * Retrieve a list of objects by id
522 *
523 * @param pk the portlet id
524 * @throws TorqueException Any exceptions caught during processing will be
525 * rethrown wrapped into a TorqueException.
526 */
527 public static List retrieveById(ObjectKey pk) throws TorqueException
528 {
529 Connection db = null;
530 List retVal = null;
531 try
532 {
533 db = Torque.getConnection(DATABASE_NAME);
534 retVal = retrieveById(pk, db);
535 }
536 finally
537 {
538 Torque.closeConnection(db);
539 }
540 return (retVal);
541 }
542 /***
543 * Retrieve a single object by pk
544 *
545 * @param pk the primary key
546 * @param con the connection to use
547 * @throws TorqueException Any exceptions caught during processing will be
548 * rethrown wrapped into a TorqueException.
549 */
550 public static List retrieveById(ObjectKey pk, Connection con)
551 throws TorqueException
552 {
553 Criteria criteria = buildCriteria(pk);
554 return doSelect(criteria, con);
555 }
556 }