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