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