001: package org.tigris.scarab.om;
002:
003: import java.math.BigDecimal;
004: import java.sql.Connection;
005: import java.sql.SQLException;
006: import java.util.ArrayList;
007: import java.util.Date;
008: import java.util.Iterator;
009: import java.util.LinkedList;
010: import java.util.List;
011:
012: import org.apache.torque.NoRowsException;
013: import org.apache.torque.TooManyRowsException;
014: import org.apache.torque.Torque;
015: import org.apache.torque.TorqueException;
016: import org.apache.torque.map.MapBuilder;
017: import org.apache.torque.map.TableMap;
018: import org.apache.torque.om.DateKey;
019: import org.apache.torque.om.NumberKey;
020: import org.apache.torque.om.StringKey;
021: import org.apache.torque.om.ObjectKey;
022: import org.apache.torque.om.SimpleKey;
023: import org.apache.torque.util.BasePeer;
024: import org.apache.torque.util.Criteria;
025:
026: import com.workingdogs.village.DataSetException;
027: import com.workingdogs.village.QueryDataSet;
028: import com.workingdogs.village.Record;
029:
030: // Local classes
031: import org.tigris.scarab.om.map.*;
032:
033: /**
034: */
035: public abstract class BaseScopePeer extends BasePeer {
036:
037: /** the default database name for this class */
038: public static final String DATABASE_NAME = "scarab";
039:
040: /** the table name for this class */
041: public static final String TABLE_NAME = "SCARAB_SCOPE";
042:
043: /**
044: * @return the map builder for this peer
045: * @throws TorqueException Any exceptions caught during processing will be
046: * rethrown wrapped into a TorqueException.
047: */
048: public static MapBuilder getMapBuilder() throws TorqueException {
049: return getMapBuilder(ScopeMapBuilder.CLASS_NAME);
050: }
051:
052: /** the column name for the SCOPE_ID field */
053: public static final String SCOPE_ID;
054: /** the column name for the SCOPE_NAME field */
055: public static final String SCOPE_NAME;
056:
057: static {
058: SCOPE_ID = "SCARAB_SCOPE.SCOPE_ID";
059: SCOPE_NAME = "SCARAB_SCOPE.SCOPE_NAME";
060: if (Torque.isInit()) {
061: try {
062: getMapBuilder(ScopeMapBuilder.CLASS_NAME);
063: } catch (Exception e) {
064: log.error("Could not initialize Peer", e);
065: throw new RuntimeException(e);
066: }
067: } else {
068: Torque.registerMapBuilder(ScopeMapBuilder.CLASS_NAME);
069: }
070: }
071:
072: /** number of columns for this peer */
073: public static final int numColumns = 2;
074:
075: /** A class that can be returned by this peer. */
076: protected static final String CLASSNAME_DEFAULT = "org.tigris.scarab.om.Scope";
077:
078: /** A class that can be returned by this peer. */
079: protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
080:
081: /**
082: * Class object initialization method.
083: *
084: * @param className name of the class to initialize
085: * @return the initialized class
086: */
087: private static Class initClass(String className) {
088: Class c = null;
089: try {
090: c = Class.forName(className);
091: } catch (Throwable t) {
092: log
093: .error(
094: "A FATAL ERROR has occurred which should not "
095: + "have happened under any circumstance. Please notify "
096: + "the Torque developers <torque-dev@db.apache.org> "
097: + "and give as many details as possible (including the error "
098: + "stack trace).", t);
099:
100: // Error objects should always be propogated.
101: if (t instanceof Error) {
102: throw (Error) t.fillInStackTrace();
103: }
104: }
105: return c;
106: }
107:
108: /**
109: * Get the list of objects for a ResultSet. Please not that your
110: * resultset MUST return columns in the right order. You can use
111: * getFieldNames() in BaseObject to get the correct sequence.
112: *
113: * @param results the ResultSet
114: * @return the list of objects
115: * @throws TorqueException Any exceptions caught during processing will be
116: * rethrown wrapped into a TorqueException.
117: */
118: public static List resultSet2Objects(java.sql.ResultSet results)
119: throws TorqueException {
120: try {
121: QueryDataSet qds = null;
122: List rows = null;
123: try {
124: qds = new QueryDataSet(results);
125: rows = getSelectResults(qds);
126: } finally {
127: if (qds != null) {
128: qds.close();
129: }
130: }
131:
132: return populateObjects(rows);
133: } catch (SQLException e) {
134: throw new TorqueException(e);
135: } catch (DataSetException e) {
136: throw new TorqueException(e);
137: }
138: }
139:
140: /**
141: * Method to do inserts.
142: *
143: * @param criteria object used to create the INSERT statement.
144: * @throws TorqueException Any exceptions caught during processing will be
145: * rethrown wrapped into a TorqueException.
146: */
147: public static ObjectKey doInsert(Criteria criteria)
148: throws TorqueException {
149: return BaseScopePeer.doInsert(criteria, (Connection) null);
150: }
151:
152: /**
153: * Method to do inserts. This method is to be used during a transaction,
154: * otherwise use the doInsert(Criteria) method. It will take care of
155: * the connection details internally.
156: *
157: * @param criteria object used to create the INSERT statement.
158: * @param con the connection to use
159: * @throws TorqueException Any exceptions caught during processing will be
160: * rethrown wrapped into a TorqueException.
161: */
162: public static ObjectKey doInsert(Criteria criteria, Connection con)
163: throws TorqueException {
164: correctBooleans(criteria);
165:
166: setDbName(criteria);
167:
168: if (con == null) {
169: return BasePeer.doInsert(criteria);
170: } else {
171: return BasePeer.doInsert(criteria, con);
172: }
173: }
174:
175: /**
176: * Add all the columns needed to create a new object.
177: *
178: * @param criteria object containing the columns to add.
179: * @throws TorqueException Any exceptions caught during processing will be
180: * rethrown wrapped into a TorqueException.
181: */
182: public static void addSelectColumns(Criteria criteria)
183: throws TorqueException {
184: criteria.addSelectColumn(SCOPE_ID);
185: criteria.addSelectColumn(SCOPE_NAME);
186: }
187:
188: /**
189: * changes the boolean values in the criteria to the appropriate type,
190: * whenever a booleanchar or booleanint column is involved.
191: * This enables the user to create criteria using Boolean values
192: * for booleanchar or booleanint columns
193: * @param criteria the criteria in which the boolean values should be corrected
194: */
195: public static void correctBooleans(Criteria criteria) {
196: }
197:
198: /**
199: * Create a new object of type cls from a resultset row starting
200: * from a specified offset. This is done so that you can select
201: * other rows than just those needed for this object. You may
202: * for example want to create two objects from the same row.
203: *
204: * @throws TorqueException Any exceptions caught during processing will be
205: * rethrown wrapped into a TorqueException.
206: */
207: public static Scope row2Object(Record row, int offset, Class cls)
208: throws TorqueException {
209: try {
210: Scope obj = (Scope) cls.newInstance();
211: ScopePeer.populateObject(row, offset, obj);
212: obj.setModified(false);
213: obj.setNew(false);
214:
215: return obj;
216: } catch (InstantiationException e) {
217: throw new TorqueException(e);
218: } catch (IllegalAccessException e) {
219: throw new TorqueException(e);
220: }
221: }
222:
223: /**
224: * Populates an object from a resultset row starting
225: * from a specified offset. This is done so that you can select
226: * other rows than just those needed for this object. You may
227: * for example want to create two objects from the same row.
228: *
229: * @throws TorqueException Any exceptions caught during processing will be
230: * rethrown wrapped into a TorqueException.
231: */
232: public static void populateObject(Record row, int offset, Scope obj)
233: throws TorqueException {
234: try {
235: obj.setScopeId(row.getValue(offset + 0).asIntegerObj());
236: obj.setName(row.getValue(offset + 1).asString());
237: } catch (DataSetException e) {
238: throw new TorqueException(e);
239: }
240: }
241:
242: /**
243: * Method to do selects.
244: *
245: * @param criteria object used to create the SELECT statement.
246: * @return List of selected Objects
247: * @throws TorqueException Any exceptions caught during processing will be
248: * rethrown wrapped into a TorqueException.
249: */
250: public static List doSelect(Criteria criteria)
251: throws TorqueException {
252: return populateObjects(doSelectVillageRecords(criteria));
253: }
254:
255: /**
256: * Method to do selects within a transaction.
257: *
258: * @param criteria object used to create the SELECT statement.
259: * @param con the connection to use
260: * @return List of selected Objects
261: * @throws TorqueException Any exceptions caught during processing will be
262: * rethrown wrapped into a TorqueException.
263: */
264: public static List doSelect(Criteria criteria, Connection con)
265: throws TorqueException {
266: return populateObjects(doSelectVillageRecords(criteria, con));
267: }
268:
269: /**
270: * Grabs the raw Village records to be formed into objects.
271: * This method handles connections internally. The Record objects
272: * returned by this method should be considered readonly. Do not
273: * alter the data and call save(), your results may vary, but are
274: * certainly likely to result in hard to track MT bugs.
275: *
276: * @throws TorqueException Any exceptions caught during processing will be
277: * rethrown wrapped into a TorqueException.
278: */
279: public static List doSelectVillageRecords(Criteria criteria)
280: throws TorqueException {
281: return BaseScopePeer.doSelectVillageRecords(criteria,
282: (Connection) null);
283: }
284:
285: /**
286: * Grabs the raw Village records to be formed into objects.
287: * This method should be used for transactions
288: *
289: * @param criteria object used to create the SELECT statement.
290: * @param con the connection to use
291: * @throws TorqueException Any exceptions caught during processing will be
292: * rethrown wrapped into a TorqueException.
293: */
294: public static List doSelectVillageRecords(Criteria criteria,
295: Connection con) throws TorqueException {
296: if (criteria.getSelectColumns().size() == 0) {
297: addSelectColumns(criteria);
298: }
299: correctBooleans(criteria);
300:
301: setDbName(criteria);
302:
303: // BasePeer returns a List of Value (Village) arrays. The array
304: // order follows the order columns were placed in the Select clause.
305: if (con == null) {
306: return BasePeer.doSelect(criteria);
307: } else {
308: return BasePeer.doSelect(criteria, con);
309: }
310: }
311:
312: /**
313: * The returned List will contain objects of the default type or
314: * objects that inherit from the default.
315: *
316: * @throws TorqueException Any exceptions caught during processing will be
317: * rethrown wrapped into a TorqueException.
318: */
319: public static List populateObjects(List records)
320: throws TorqueException {
321: List results = new ArrayList(records.size());
322:
323: // populate the object(s)
324: for (int i = 0; i < records.size(); i++) {
325: Record row = (Record) records.get(i);
326: results.add(ScopePeer.row2Object(row, 1, ScopePeer
327: .getOMClass()));
328: }
329: return results;
330: }
331:
332: /**
333: * The class that the Peer will make instances of.
334: * If the BO is abstract then you must implement this method
335: * in the BO.
336: *
337: * @throws TorqueException Any exceptions caught during processing will be
338: * rethrown wrapped into a TorqueException.
339: */
340: public static Class getOMClass() throws TorqueException {
341: return CLASS_DEFAULT;
342: }
343:
344: /**
345: * Method to do updates.
346: *
347: * @param criteria object containing data that is used to create the UPDATE
348: * statement.
349: * @throws TorqueException Any exceptions caught during processing will be
350: * rethrown wrapped into a TorqueException.
351: */
352: public static void doUpdate(Criteria criteria)
353: throws TorqueException {
354: BaseScopePeer.doUpdate(criteria, (Connection) null);
355: }
356:
357: /**
358: * Method to do updates. This method is to be used during a transaction,
359: * otherwise use the doUpdate(Criteria) method. It will take care of
360: * the connection details internally.
361: *
362: * @param criteria object containing data that is used to create the UPDATE
363: * statement.
364: * @param con the connection to use
365: * @throws TorqueException Any exceptions caught during processing will be
366: * rethrown wrapped into a TorqueException.
367: */
368: public static void doUpdate(Criteria criteria, Connection con)
369: throws TorqueException {
370: Criteria selectCriteria = new Criteria(DATABASE_NAME, 2);
371: correctBooleans(criteria);
372:
373: selectCriteria.put(SCOPE_ID, criteria.remove(SCOPE_ID));
374:
375: setDbName(criteria);
376:
377: if (con == null) {
378: BasePeer.doUpdate(selectCriteria, criteria);
379: } else {
380: BasePeer.doUpdate(selectCriteria, criteria, con);
381: }
382: }
383:
384: /**
385: * Method to do deletes.
386: *
387: * @param criteria object containing data that is used DELETE from database.
388: * @throws TorqueException Any exceptions caught during processing will be
389: * rethrown wrapped into a TorqueException.
390: */
391: public static void doDelete(Criteria criteria)
392: throws TorqueException {
393: ScopePeer.doDelete(criteria, (Connection) null);
394: }
395:
396: /**
397: * Method to do deletes. This method is to be used during a transaction,
398: * otherwise use the doDelete(Criteria) method. It will take care of
399: * the connection details internally.
400: *
401: * @param criteria object containing data that is used DELETE from database.
402: * @param con the connection to use
403: * @throws TorqueException Any exceptions caught during processing will be
404: * rethrown wrapped into a TorqueException.
405: */
406: public static void doDelete(Criteria criteria, Connection con)
407: throws TorqueException {
408: correctBooleans(criteria);
409:
410: setDbName(criteria);
411:
412: if (con == null) {
413: BasePeer.doDelete(criteria);
414: } else {
415: BasePeer.doDelete(criteria, con);
416: }
417: }
418:
419: /**
420: * Method to do selects
421: *
422: * @throws TorqueException Any exceptions caught during processing will be
423: * rethrown wrapped into a TorqueException.
424: */
425: public static List doSelect(Scope obj) throws TorqueException {
426: return doSelect(buildSelectCriteria(obj));
427: }
428:
429: /**
430: * Method to do inserts
431: *
432: * @throws TorqueException Any exceptions caught during processing will be
433: * rethrown wrapped into a TorqueException.
434: */
435: public static void doInsert(Scope obj) throws TorqueException {
436: doInsert(buildCriteria(obj));
437: obj.setNew(false);
438: obj.setModified(false);
439: }
440:
441: /**
442: * @param obj the data object to update in the database.
443: * @throws TorqueException Any exceptions caught during processing will be
444: * rethrown wrapped into a TorqueException.
445: */
446: public static void doUpdate(Scope obj) throws TorqueException {
447: doUpdate(buildCriteria(obj));
448: obj.setModified(false);
449: }
450:
451: /**
452: * @param obj the data object to delete in the database.
453: * @throws TorqueException Any exceptions caught during processing will be
454: * rethrown wrapped into a TorqueException.
455: */
456: public static void doDelete(Scope obj) throws TorqueException {
457: doDelete(buildSelectCriteria(obj));
458: }
459:
460: /**
461: * Method to do inserts. This method is to be used during a transaction,
462: * otherwise use the doInsert(Scope) method. It will take
463: * care of the connection details internally.
464: *
465: * @param obj the data object to insert into the database.
466: * @param con the connection to use
467: * @throws TorqueException Any exceptions caught during processing will be
468: * rethrown wrapped into a TorqueException.
469: */
470: public static void doInsert(Scope obj, Connection con)
471: throws TorqueException {
472: doInsert(buildCriteria(obj), con);
473: obj.setNew(false);
474: obj.setModified(false);
475: }
476:
477: /**
478: * Method to do update. This method is to be used during a transaction,
479: * otherwise use the doUpdate(Scope) method. It will take
480: * care of the connection details internally.
481: *
482: * @param obj the data object to update in the database.
483: * @param con the connection to use
484: * @throws TorqueException Any exceptions caught during processing will be
485: * rethrown wrapped into a TorqueException.
486: */
487: public static void doUpdate(Scope obj, Connection con)
488: throws TorqueException {
489: doUpdate(buildCriteria(obj), con);
490: obj.setModified(false);
491: }
492:
493: /**
494: * Method to delete. This method is to be used during a transaction,
495: * otherwise use the doDelete(Scope) method. It will take
496: * care of the connection details internally.
497: *
498: * @param obj the data object to delete in the database.
499: * @param con the connection to use
500: * @throws TorqueException Any exceptions caught during processing will be
501: * rethrown wrapped into a TorqueException.
502: */
503: public static void doDelete(Scope obj, Connection con)
504: throws TorqueException {
505: doDelete(buildSelectCriteria(obj), con);
506: }
507:
508: /**
509: * Method to do deletes.
510: *
511: * @param pk ObjectKey that is used DELETE from database.
512: * @throws TorqueException Any exceptions caught during processing will be
513: * rethrown wrapped into a TorqueException.
514: */
515: public static void doDelete(ObjectKey pk) throws TorqueException {
516: BaseScopePeer.doDelete(pk, (Connection) null);
517: }
518:
519: /**
520: * Method to delete. This method is to be used during a transaction,
521: * otherwise use the doDelete(ObjectKey) method. It will take
522: * care of the connection details internally.
523: *
524: * @param pk the primary key for the object to delete in the database.
525: * @param con the connection to use
526: * @throws TorqueException Any exceptions caught during processing will be
527: * rethrown wrapped into a TorqueException.
528: */
529: public static void doDelete(ObjectKey pk, Connection con)
530: throws TorqueException {
531: doDelete(buildCriteria(pk), con);
532: }
533:
534: /** Build a Criteria object from an ObjectKey */
535: public static Criteria buildCriteria(ObjectKey pk) {
536: Criteria criteria = new Criteria();
537: criteria.add(SCOPE_ID, pk);
538: return criteria;
539: }
540:
541: /** Build a Criteria object from the data object for this peer */
542: public static Criteria buildCriteria(Scope obj) {
543: Criteria criteria = new Criteria(DATABASE_NAME);
544: criteria.add(SCOPE_ID, obj.getScopeId());
545: criteria.add(SCOPE_NAME, obj.getName());
546: return criteria;
547: }
548:
549: /** Build a Criteria object from the data object for this peer, skipping all binary columns */
550: public static Criteria buildSelectCriteria(Scope obj) {
551: Criteria criteria = new Criteria(DATABASE_NAME);
552: criteria.add(SCOPE_ID, obj.getScopeId());
553: criteria.add(SCOPE_NAME, obj.getName());
554: return criteria;
555: }
556:
557: /**
558: * Retrieve a single object by pk
559: *
560: * @param pk the primary key
561: * @throws TorqueException Any exceptions caught during processing will be
562: * rethrown wrapped into a TorqueException.
563: * @throws NoRowsException Primary key was not found in database.
564: * @throws TooManyRowsException Primary key was not found in database.
565: */
566: public static Scope retrieveByPK(Integer pk)
567: throws TorqueException, NoRowsException,
568: TooManyRowsException {
569: return retrieveByPK(SimpleKey.keyFor(pk));
570: }
571:
572: /**
573: * Retrieve a single object by pk
574: *
575: * @param pk the primary key
576: * @param con the connection to use
577: * @throws TorqueException Any exceptions caught during processing will be
578: * rethrown wrapped into a TorqueException.
579: * @throws NoRowsException Primary key was not found in database.
580: * @throws TooManyRowsException Primary key was not found in database.
581: */
582: public static Scope retrieveByPK(Integer pk, Connection con)
583: throws TorqueException, NoRowsException,
584: TooManyRowsException {
585: return retrieveByPK(SimpleKey.keyFor(pk), con);
586: }
587:
588: /**
589: * Retrieve a single object by pk
590: *
591: * @param pk the primary key
592: * @throws TorqueException Any exceptions caught during processing will be
593: * rethrown wrapped into a TorqueException.
594: * @throws NoRowsException Primary key was not found in database.
595: * @throws TooManyRowsException Primary key was not found in database.
596: */
597: public static Scope retrieveByPK(ObjectKey pk)
598: throws TorqueException, NoRowsException,
599: TooManyRowsException {
600: Connection db = null;
601: Scope retVal = null;
602: try {
603: db = Torque.getConnection(DATABASE_NAME);
604: retVal = retrieveByPK(pk, db);
605: } finally {
606: Torque.closeConnection(db);
607: }
608: return retVal;
609: }
610:
611: /**
612: * Retrieve a single object by pk
613: *
614: * @param pk the primary key
615: * @param con the connection to use
616: * @throws TorqueException Any exceptions caught during processing will be
617: * rethrown wrapped into a TorqueException.
618: * @throws NoRowsException Primary key was not found in database.
619: * @throws TooManyRowsException Primary key was not found in database.
620: */
621: public static Scope retrieveByPK(ObjectKey pk, Connection con)
622: throws TorqueException, NoRowsException,
623: TooManyRowsException {
624: Criteria criteria = buildCriteria(pk);
625: List v = doSelect(criteria, con);
626: if (v.size() == 0) {
627: throw new NoRowsException("Failed to select a row.");
628: } else if (v.size() > 1) {
629: throw new TooManyRowsException(
630: "Failed to select only one row.");
631: } else {
632: return (Scope) v.get(0);
633: }
634: }
635:
636: /**
637: * Retrieve a multiple objects by pk
638: *
639: * @param pks List of primary keys
640: * @throws TorqueException Any exceptions caught during processing will be
641: * rethrown wrapped into a TorqueException.
642: */
643: public static List retrieveByPKs(List pks) throws TorqueException {
644: Connection db = null;
645: List retVal = null;
646: try {
647: db = Torque.getConnection(DATABASE_NAME);
648: retVal = retrieveByPKs(pks, db);
649: } finally {
650: Torque.closeConnection(db);
651: }
652: return retVal;
653: }
654:
655: /**
656: * Retrieve a multiple objects by pk
657: *
658: * @param pks List of primary keys
659: * @param dbcon the connection to use
660: * @throws TorqueException Any exceptions caught during processing will be
661: * rethrown wrapped into a TorqueException.
662: */
663: public static List retrieveByPKs(List pks, Connection dbcon)
664: throws TorqueException {
665: List objs = null;
666: if (pks == null || pks.size() == 0) {
667: objs = new LinkedList();
668: } else {
669: Criteria criteria = new Criteria();
670: criteria.addIn(SCOPE_ID, pks);
671: objs = doSelect(criteria, dbcon);
672: }
673: return objs;
674: }
675:
676: /**
677: * Returns the TableMap related to this peer. This method is not
678: * needed for general use but a specific application could have a need.
679: *
680: * @throws TorqueException Any exceptions caught during processing will be
681: * rethrown wrapped into a TorqueException.
682: */
683: protected static TableMap getTableMap() throws TorqueException {
684: return Torque.getDatabaseMap(DATABASE_NAME)
685: .getTable(TABLE_NAME);
686: }
687:
688: private static void setDbName(Criteria crit) {
689: // Set the correct dbName if it has not been overridden
690: // crit.getDbName will return the same object if not set to
691: // another value so == check is okay and faster
692: if (crit.getDbName() == Torque.getDefaultDB()) {
693: crit.setDbName(DATABASE_NAME);
694: }
695: }
696: }
|