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 BaseDependTypePeer 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_DEPEND_TYPE";
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(DependTypeMapBuilder.CLASS_NAME);
050: }
051:
052: /** the column name for the DEPEND_TYPE_ID field */
053: public static final String DEPEND_TYPE_ID;
054: /** the column name for the DEPEND_TYPE_NAME field */
055: public static final String DEPEND_TYPE_NAME;
056:
057: static {
058: DEPEND_TYPE_ID = "SCARAB_DEPEND_TYPE.DEPEND_TYPE_ID";
059: DEPEND_TYPE_NAME = "SCARAB_DEPEND_TYPE.DEPEND_TYPE_NAME";
060: if (Torque.isInit()) {
061: try {
062: getMapBuilder(DependTypeMapBuilder.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(DependTypeMapBuilder.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.DependType";
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 BaseDependTypePeer.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(DEPEND_TYPE_ID);
185: criteria.addSelectColumn(DEPEND_TYPE_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 DependType row2Object(Record row, int offset,
208: Class cls) throws TorqueException {
209: try {
210: DependType obj = (DependType) cls.newInstance();
211: DependTypePeer.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,
233: DependType obj) throws TorqueException {
234: try {
235: obj
236: .setDependTypeId(row.getValue(offset + 0)
237: .asIntegerObj());
238: obj.setName(row.getValue(offset + 1).asString());
239: } catch (DataSetException e) {
240: throw new TorqueException(e);
241: }
242: }
243:
244: /**
245: * Method to do selects.
246: *
247: * @param criteria object used to create the SELECT statement.
248: * @return List of selected Objects
249: * @throws TorqueException Any exceptions caught during processing will be
250: * rethrown wrapped into a TorqueException.
251: */
252: public static List doSelect(Criteria criteria)
253: throws TorqueException {
254: return populateObjects(doSelectVillageRecords(criteria));
255: }
256:
257: /**
258: * Method to do selects within a transaction.
259: *
260: * @param criteria object used to create the SELECT statement.
261: * @param con the connection to use
262: * @return List of selected Objects
263: * @throws TorqueException Any exceptions caught during processing will be
264: * rethrown wrapped into a TorqueException.
265: */
266: public static List doSelect(Criteria criteria, Connection con)
267: throws TorqueException {
268: return populateObjects(doSelectVillageRecords(criteria, con));
269: }
270:
271: /**
272: * Grabs the raw Village records to be formed into objects.
273: * This method handles connections internally. The Record objects
274: * returned by this method should be considered readonly. Do not
275: * alter the data and call save(), your results may vary, but are
276: * certainly likely to result in hard to track MT bugs.
277: *
278: * @throws TorqueException Any exceptions caught during processing will be
279: * rethrown wrapped into a TorqueException.
280: */
281: public static List doSelectVillageRecords(Criteria criteria)
282: throws TorqueException {
283: return BaseDependTypePeer.doSelectVillageRecords(criteria,
284: (Connection) null);
285: }
286:
287: /**
288: * Grabs the raw Village records to be formed into objects.
289: * This method should be used for transactions
290: *
291: * @param criteria object used to create the SELECT statement.
292: * @param con the connection to use
293: * @throws TorqueException Any exceptions caught during processing will be
294: * rethrown wrapped into a TorqueException.
295: */
296: public static List doSelectVillageRecords(Criteria criteria,
297: Connection con) throws TorqueException {
298: if (criteria.getSelectColumns().size() == 0) {
299: addSelectColumns(criteria);
300: }
301: correctBooleans(criteria);
302:
303: setDbName(criteria);
304:
305: // BasePeer returns a List of Value (Village) arrays. The array
306: // order follows the order columns were placed in the Select clause.
307: if (con == null) {
308: return BasePeer.doSelect(criteria);
309: } else {
310: return BasePeer.doSelect(criteria, con);
311: }
312: }
313:
314: /**
315: * The returned List will contain objects of the default type or
316: * objects that inherit from the default.
317: *
318: * @throws TorqueException Any exceptions caught during processing will be
319: * rethrown wrapped into a TorqueException.
320: */
321: public static List populateObjects(List records)
322: throws TorqueException {
323: List results = new ArrayList(records.size());
324:
325: // populate the object(s)
326: for (int i = 0; i < records.size(); i++) {
327: Record row = (Record) records.get(i);
328: results.add(DependTypePeer.row2Object(row, 1,
329: DependTypePeer.getOMClass()));
330: }
331: return results;
332: }
333:
334: /**
335: * The class that the Peer will make instances of.
336: * If the BO is abstract then you must implement this method
337: * in the BO.
338: *
339: * @throws TorqueException Any exceptions caught during processing will be
340: * rethrown wrapped into a TorqueException.
341: */
342: public static Class getOMClass() throws TorqueException {
343: return CLASS_DEFAULT;
344: }
345:
346: /**
347: * Method to do updates.
348: *
349: * @param criteria object containing data that is used to create the UPDATE
350: * statement.
351: * @throws TorqueException Any exceptions caught during processing will be
352: * rethrown wrapped into a TorqueException.
353: */
354: public static void doUpdate(Criteria criteria)
355: throws TorqueException {
356: BaseDependTypePeer.doUpdate(criteria, (Connection) null);
357: }
358:
359: /**
360: * Method to do updates. This method is to be used during a transaction,
361: * otherwise use the doUpdate(Criteria) method. It will take care of
362: * the connection details internally.
363: *
364: * @param criteria object containing data that is used to create the UPDATE
365: * statement.
366: * @param con the connection to use
367: * @throws TorqueException Any exceptions caught during processing will be
368: * rethrown wrapped into a TorqueException.
369: */
370: public static void doUpdate(Criteria criteria, Connection con)
371: throws TorqueException {
372: Criteria selectCriteria = new Criteria(DATABASE_NAME, 2);
373: correctBooleans(criteria);
374:
375: selectCriteria.put(DEPEND_TYPE_ID, criteria
376: .remove(DEPEND_TYPE_ID));
377:
378: setDbName(criteria);
379:
380: if (con == null) {
381: BasePeer.doUpdate(selectCriteria, criteria);
382: } else {
383: BasePeer.doUpdate(selectCriteria, criteria, con);
384: }
385: }
386:
387: /**
388: * Method to do deletes.
389: *
390: * @param criteria object containing data that is used DELETE from database.
391: * @throws TorqueException Any exceptions caught during processing will be
392: * rethrown wrapped into a TorqueException.
393: */
394: public static void doDelete(Criteria criteria)
395: throws TorqueException {
396: DependTypePeer.doDelete(criteria, (Connection) null);
397: }
398:
399: /**
400: * Method to do deletes. This method is to be used during a transaction,
401: * otherwise use the doDelete(Criteria) method. It will take care of
402: * the connection details internally.
403: *
404: * @param criteria object containing data that is used DELETE from database.
405: * @param con the connection to use
406: * @throws TorqueException Any exceptions caught during processing will be
407: * rethrown wrapped into a TorqueException.
408: */
409: public static void doDelete(Criteria criteria, Connection con)
410: throws TorqueException {
411: correctBooleans(criteria);
412:
413: setDbName(criteria);
414:
415: if (con == null) {
416: BasePeer.doDelete(criteria);
417: } else {
418: BasePeer.doDelete(criteria, con);
419: }
420: }
421:
422: /**
423: * Method to do selects
424: *
425: * @throws TorqueException Any exceptions caught during processing will be
426: * rethrown wrapped into a TorqueException.
427: */
428: public static List doSelect(DependType obj) throws TorqueException {
429: return doSelect(buildSelectCriteria(obj));
430: }
431:
432: /**
433: * Method to do inserts
434: *
435: * @throws TorqueException Any exceptions caught during processing will be
436: * rethrown wrapped into a TorqueException.
437: */
438: public static void doInsert(DependType obj) throws TorqueException {
439: obj.setPrimaryKey(doInsert(buildCriteria(obj)));
440: obj.setNew(false);
441: obj.setModified(false);
442: }
443:
444: /**
445: * @param obj the data object to update in the database.
446: * @throws TorqueException Any exceptions caught during processing will be
447: * rethrown wrapped into a TorqueException.
448: */
449: public static void doUpdate(DependType obj) throws TorqueException {
450: doUpdate(buildCriteria(obj));
451: obj.setModified(false);
452: }
453:
454: /**
455: * @param obj the data object to delete in the database.
456: * @throws TorqueException Any exceptions caught during processing will be
457: * rethrown wrapped into a TorqueException.
458: */
459: public static void doDelete(DependType obj) throws TorqueException {
460: doDelete(buildSelectCriteria(obj));
461: }
462:
463: /**
464: * Method to do inserts. This method is to be used during a transaction,
465: * otherwise use the doInsert(DependType) method. It will take
466: * care of the connection details internally.
467: *
468: * @param obj the data object to insert into the database.
469: * @param con the connection to use
470: * @throws TorqueException Any exceptions caught during processing will be
471: * rethrown wrapped into a TorqueException.
472: */
473: public static void doInsert(DependType obj, Connection con)
474: throws TorqueException {
475: obj.setPrimaryKey(doInsert(buildCriteria(obj), con));
476: obj.setNew(false);
477: obj.setModified(false);
478: }
479:
480: /**
481: * Method to do update. This method is to be used during a transaction,
482: * otherwise use the doUpdate(DependType) method. It will take
483: * care of the connection details internally.
484: *
485: * @param obj the data object to update in the database.
486: * @param con the connection to use
487: * @throws TorqueException Any exceptions caught during processing will be
488: * rethrown wrapped into a TorqueException.
489: */
490: public static void doUpdate(DependType obj, Connection con)
491: throws TorqueException {
492: doUpdate(buildCriteria(obj), con);
493: obj.setModified(false);
494: }
495:
496: /**
497: * Method to delete. This method is to be used during a transaction,
498: * otherwise use the doDelete(DependType) method. It will take
499: * care of the connection details internally.
500: *
501: * @param obj the data object to delete in the database.
502: * @param con the connection to use
503: * @throws TorqueException Any exceptions caught during processing will be
504: * rethrown wrapped into a TorqueException.
505: */
506: public static void doDelete(DependType obj, Connection con)
507: throws TorqueException {
508: doDelete(buildSelectCriteria(obj), con);
509: }
510:
511: /**
512: * Method to do deletes.
513: *
514: * @param pk ObjectKey that is used DELETE from database.
515: * @throws TorqueException Any exceptions caught during processing will be
516: * rethrown wrapped into a TorqueException.
517: */
518: public static void doDelete(ObjectKey pk) throws TorqueException {
519: BaseDependTypePeer.doDelete(pk, (Connection) null);
520: }
521:
522: /**
523: * Method to delete. This method is to be used during a transaction,
524: * otherwise use the doDelete(ObjectKey) method. It will take
525: * care of the connection details internally.
526: *
527: * @param pk the primary key for the object to delete in the database.
528: * @param con the connection to use
529: * @throws TorqueException Any exceptions caught during processing will be
530: * rethrown wrapped into a TorqueException.
531: */
532: public static void doDelete(ObjectKey pk, Connection con)
533: throws TorqueException {
534: doDelete(buildCriteria(pk), con);
535: }
536:
537: /** Build a Criteria object from an ObjectKey */
538: public static Criteria buildCriteria(ObjectKey pk) {
539: Criteria criteria = new Criteria();
540: criteria.add(DEPEND_TYPE_ID, pk);
541: return criteria;
542: }
543:
544: /** Build a Criteria object from the data object for this peer */
545: public static Criteria buildCriteria(DependType obj) {
546: Criteria criteria = new Criteria(DATABASE_NAME);
547: if (!obj.isNew())
548: criteria.add(DEPEND_TYPE_ID, obj.getDependTypeId());
549: criteria.add(DEPEND_TYPE_NAME, obj.getName());
550: return criteria;
551: }
552:
553: /** Build a Criteria object from the data object for this peer, skipping all binary columns */
554: public static Criteria buildSelectCriteria(DependType obj) {
555: Criteria criteria = new Criteria(DATABASE_NAME);
556: if (!obj.isNew()) {
557: criteria.add(DEPEND_TYPE_ID, obj.getDependTypeId());
558: }
559: criteria.add(DEPEND_TYPE_NAME, obj.getName());
560: return criteria;
561: }
562:
563: /**
564: * Retrieve a single object by pk
565: *
566: * @param pk the primary key
567: * @throws TorqueException Any exceptions caught during processing will be
568: * rethrown wrapped into a TorqueException.
569: * @throws NoRowsException Primary key was not found in database.
570: * @throws TooManyRowsException Primary key was not found in database.
571: */
572: public static DependType retrieveByPK(Integer pk)
573: throws TorqueException, NoRowsException,
574: TooManyRowsException {
575: return retrieveByPK(SimpleKey.keyFor(pk));
576: }
577:
578: /**
579: * Retrieve a single object by pk
580: *
581: * @param pk the primary key
582: * @param con the connection to use
583: * @throws TorqueException Any exceptions caught during processing will be
584: * rethrown wrapped into a TorqueException.
585: * @throws NoRowsException Primary key was not found in database.
586: * @throws TooManyRowsException Primary key was not found in database.
587: */
588: public static DependType retrieveByPK(Integer pk, Connection con)
589: throws TorqueException, NoRowsException,
590: TooManyRowsException {
591: return retrieveByPK(SimpleKey.keyFor(pk), con);
592: }
593:
594: /**
595: * Retrieve a single object by pk
596: *
597: * @param pk the primary key
598: * @throws TorqueException Any exceptions caught during processing will be
599: * rethrown wrapped into a TorqueException.
600: * @throws NoRowsException Primary key was not found in database.
601: * @throws TooManyRowsException Primary key was not found in database.
602: */
603: public static DependType retrieveByPK(ObjectKey pk)
604: throws TorqueException, NoRowsException,
605: TooManyRowsException {
606: Connection db = null;
607: DependType retVal = null;
608: try {
609: db = Torque.getConnection(DATABASE_NAME);
610: retVal = retrieveByPK(pk, db);
611: } finally {
612: Torque.closeConnection(db);
613: }
614: return retVal;
615: }
616:
617: /**
618: * Retrieve a single object by pk
619: *
620: * @param pk the primary key
621: * @param con the connection to use
622: * @throws TorqueException Any exceptions caught during processing will be
623: * rethrown wrapped into a TorqueException.
624: * @throws NoRowsException Primary key was not found in database.
625: * @throws TooManyRowsException Primary key was not found in database.
626: */
627: public static DependType retrieveByPK(ObjectKey pk, Connection con)
628: throws TorqueException, NoRowsException,
629: TooManyRowsException {
630: Criteria criteria = buildCriteria(pk);
631: List v = doSelect(criteria, con);
632: if (v.size() == 0) {
633: throw new NoRowsException("Failed to select a row.");
634: } else if (v.size() > 1) {
635: throw new TooManyRowsException(
636: "Failed to select only one row.");
637: } else {
638: return (DependType) v.get(0);
639: }
640: }
641:
642: /**
643: * Retrieve a multiple objects by pk
644: *
645: * @param pks List of primary keys
646: * @throws TorqueException Any exceptions caught during processing will be
647: * rethrown wrapped into a TorqueException.
648: */
649: public static List retrieveByPKs(List pks) throws TorqueException {
650: Connection db = null;
651: List retVal = null;
652: try {
653: db = Torque.getConnection(DATABASE_NAME);
654: retVal = retrieveByPKs(pks, db);
655: } finally {
656: Torque.closeConnection(db);
657: }
658: return retVal;
659: }
660:
661: /**
662: * Retrieve a multiple objects by pk
663: *
664: * @param pks List of primary keys
665: * @param dbcon the connection to use
666: * @throws TorqueException Any exceptions caught during processing will be
667: * rethrown wrapped into a TorqueException.
668: */
669: public static List retrieveByPKs(List pks, Connection dbcon)
670: throws TorqueException {
671: List objs = null;
672: if (pks == null || pks.size() == 0) {
673: objs = new LinkedList();
674: } else {
675: Criteria criteria = new Criteria();
676: criteria.addIn(DEPEND_TYPE_ID, pks);
677: objs = doSelect(criteria, dbcon);
678: }
679: return objs;
680: }
681:
682: /**
683: * Returns the TableMap related to this peer. This method is not
684: * needed for general use but a specific application could have a need.
685: *
686: * @throws TorqueException Any exceptions caught during processing will be
687: * rethrown wrapped into a TorqueException.
688: */
689: protected static TableMap getTableMap() throws TorqueException {
690: return Torque.getDatabaseMap(DATABASE_NAME)
691: .getTable(TABLE_NAME);
692: }
693:
694: private static void setDbName(Criteria crit) {
695: // Set the correct dbName if it has not been overridden
696: // crit.getDbName will return the same object if not set to
697: // another value so == check is okay and faster
698: if (crit.getDbName() == Torque.getDefaultDB()) {
699: crit.setDbName(DATABASE_NAME);
700: }
701: }
702: }
|