1 package org.apache.jetspeed.om.apps.coffees;
2
3 import java.math.BigDecimal;
4 import java.sql.Connection;
5 import java.sql.SQLException;
6 import java.util.ArrayList;
7 import java.util.Date;
8 import java.util.Iterator;
9 import java.util.LinkedList;
10 import java.util.List;
11
12 import org.apache.torque.NoRowsException;
13 import org.apache.torque.TooManyRowsException;
14 import org.apache.torque.Torque;
15 import org.apache.torque.TorqueException;
16 import org.apache.torque.map.MapBuilder;
17 import org.apache.torque.map.TableMap;
18 import org.apache.torque.om.DateKey;
19 import org.apache.torque.om.NumberKey;
20 import org.apache.torque.om.StringKey;
21 import org.apache.torque.om.ObjectKey;
22 import org.apache.torque.om.SimpleKey;
23 import org.apache.torque.util.BasePeer;
24 import org.apache.torque.util.Criteria;
25
26 import com.workingdogs.village.DataSetException;
27 import com.workingdogs.village.QueryDataSet;
28 import com.workingdogs.village.Record;
29
30
31 import org.apache.jetspeed.om.apps.coffees.map.*;
32
33
34 /***
35 * This class was autogenerated by Torque on:
36 *
37 * [Thu Apr 22 23:12:36 EDT 2004]
38 *
39 */
40 public abstract class BaseCoffeesPeer
41 extends BasePeer
42 {
43
44 /*** the default database name for this class */
45 public static final String DATABASE_NAME = "default";
46
47 /*** the table name for this class */
48 public static final String TABLE_NAME = "COFFEES";
49
50 /***
51 * @return the map builder for this peer
52 * @throws TorqueException Any exceptions caught during processing will be
53 * rethrown wrapped into a TorqueException.
54 */
55 public static MapBuilder getMapBuilder()
56 throws TorqueException
57 {
58 return getMapBuilder(CoffeesMapBuilder.CLASS_NAME);
59 }
60
61 /*** the column name for the COFFEE_ID field */
62 public static final String COFFEE_ID;
63 /*** the column name for the COFFEE_NAME field */
64 public static final String COFFEE_NAME;
65 /*** the column name for the SUPPLIER_ID field */
66 public static final String SUPPLIER_ID;
67 /*** the column name for the PRICE field */
68 public static final String PRICE;
69 /*** the column name for the SALES field */
70 public static final String SALES;
71 /*** the column name for the TOTAL field */
72 public static final String TOTAL;
73
74 static
75 {
76 COFFEE_ID = "COFFEES.COFFEE_ID";
77 COFFEE_NAME = "COFFEES.COFFEE_NAME";
78 SUPPLIER_ID = "COFFEES.SUPPLIER_ID";
79 PRICE = "COFFEES.PRICE";
80 SALES = "COFFEES.SALES";
81 TOTAL = "COFFEES.TOTAL";
82 if (Torque.isInit())
83 {
84 try
85 {
86 getMapBuilder();
87 }
88 catch (Exception e)
89 {
90 log.error("Could not initialize Peer", e);
91 }
92 }
93 else
94 {
95 Torque.registerMapBuilder(CoffeesMapBuilder.CLASS_NAME);
96 }
97 }
98
99 /*** number of columns for this peer */
100 public static final int numColumns = 6;
101
102 /*** A class that can be returned by this peer. */
103 protected static final String CLASSNAME_DEFAULT =
104 "org.apache.jetspeed.om.apps.coffees.Coffees";
105
106 /*** A class that can be returned by this peer. */
107 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
108
109 /***
110 * Class object initialization method.
111 *
112 * @param className name of the class to initialize
113 * @return the initialized class
114 */
115 private static Class initClass(String className)
116 {
117 Class c = null;
118 try
119 {
120 c = Class.forName(className);
121 }
122 catch (Throwable t)
123 {
124 log.error("A FATAL ERROR has occurred which should not "
125 + "have happened under any circumstance. Please notify "
126 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> "
127 + "and give as many details as possible (including the error "
128 + "stack trace).", t);
129
130
131 if (t instanceof Error)
132 {
133 throw (Error) t.fillInStackTrace();
134 }
135 }
136 return c;
137 }
138
139 /***
140 * Get the list of objects for a ResultSet. Please not that your
141 * resultset MUST return columns in the right order. You can use
142 * getFieldNames() in BaseObject to get the correct sequence.
143 *
144 * @param results the ResultSet
145 * @return the list of objects
146 * @throws TorqueException Any exceptions caught during processing will be
147 * rethrown wrapped into a TorqueException.
148 */
149 public static List resultSet2Objects(java.sql.ResultSet results)
150 throws TorqueException
151 {
152 try
153 {
154 QueryDataSet qds = null;
155 List rows = null;
156 try
157 {
158 qds = new QueryDataSet(results);
159 rows = getSelectResults(qds);
160 }
161 finally
162 {
163 if (qds != null)
164 {
165 qds.close();
166 }
167 }
168
169 return populateObjects(rows);
170 }
171 catch (SQLException e)
172 {
173 throw new TorqueException(e);
174 }
175 catch (DataSetException e)
176 {
177 throw new TorqueException(e);
178 }
179 }
180
181
182
183 /***
184 * Method to do inserts.
185 *
186 * @param criteria object used to create the INSERT statement.
187 * @throws TorqueException Any exceptions caught during processing will be
188 * rethrown wrapped into a TorqueException.
189 */
190 public static ObjectKey doInsert(Criteria criteria)
191 throws TorqueException
192 {
193 return BaseCoffeesPeer
194 .doInsert(criteria, (Connection) null);
195 }
196
197 /***
198 * Method to do inserts. This method is to be used during a transaction,
199 * otherwise use the doInsert(Criteria) method. It will take care of
200 * the connection details internally.
201 *
202 * @param criteria object used to create the INSERT statement.
203 * @param con the connection to use
204 * @throws TorqueException Any exceptions caught during processing will be
205 * rethrown wrapped into a TorqueException.
206 */
207 public static ObjectKey doInsert(Criteria criteria, Connection con)
208 throws TorqueException
209 {
210
211
212
213
214 if (criteria.getDbName() == Torque.getDefaultDB())
215 {
216 criteria.setDbName(DATABASE_NAME);
217 }
218 if (con == null)
219 {
220 return BasePeer.doInsert(criteria);
221 }
222 else
223 {
224 return BasePeer.doInsert(criteria, con);
225 }
226 }
227
228 /***
229 * Add all the columns needed to create a new object.
230 *
231 * @param criteria object containing the columns to add.
232 * @throws TorqueException Any exceptions caught during processing will be
233 * rethrown wrapped into a TorqueException.
234 */
235 public static void addSelectColumns(Criteria criteria)
236 throws TorqueException
237 {
238 criteria.addSelectColumn(COFFEE_ID);
239 criteria.addSelectColumn(COFFEE_NAME);
240 criteria.addSelectColumn(SUPPLIER_ID);
241 criteria.addSelectColumn(PRICE);
242 criteria.addSelectColumn(SALES);
243 criteria.addSelectColumn(TOTAL);
244 }
245
246 /***
247 * Create a new object of type cls from a resultset row starting
248 * from a specified offset. This is done so that you can select
249 * other rows than just those needed for this object. You may
250 * for example want to create two objects from the same row.
251 *
252 * @throws TorqueException Any exceptions caught during processing will be
253 * rethrown wrapped into a TorqueException.
254 */
255 public static Coffees row2Object(Record row,
256 int offset,
257 Class cls)
258 throws TorqueException
259 {
260 try
261 {
262 Coffees obj = (Coffees) cls.newInstance();
263 CoffeesPeer.populateObject(row, offset, obj);
264 obj.setModified(false);
265 obj.setNew(false);
266
267 return obj;
268 }
269 catch (InstantiationException e)
270 {
271 throw new TorqueException(e);
272 }
273 catch (IllegalAccessException e)
274 {
275 throw new TorqueException(e);
276 }
277 }
278
279 /***
280 * Populates an object from a resultset row starting
281 * from a specified offset. This is done so that you can select
282 * other rows than just those needed for this object. You may
283 * for example want to create two objects from the same row.
284 *
285 * @throws TorqueException Any exceptions caught during processing will be
286 * rethrown wrapped into a TorqueException.
287 */
288 public static void populateObject(Record row,
289 int offset,
290 Coffees obj)
291 throws TorqueException
292 {
293 try
294 {
295 obj.setCoffeeId(row.getValue(offset + 0).asInt());
296 obj.setCoffeeName(row.getValue(offset + 1).asString());
297 obj.setSupplierId(row.getValue(offset + 2).asInt());
298 obj.setPrice(row.getValue(offset + 3).asDouble());
299 obj.setSales(row.getValue(offset + 4).asInt());
300 obj.setTotal(row.getValue(offset + 5).asInt());
301 }
302 catch (DataSetException e)
303 {
304 throw new TorqueException(e);
305 }
306 }
307
308 /***
309 * Method to do selects.
310 *
311 * @param criteria object used to create the SELECT statement.
312 * @return List of selected Objects
313 * @throws TorqueException Any exceptions caught during processing will be
314 * rethrown wrapped into a TorqueException.
315 */
316 public static List doSelect(Criteria criteria) throws TorqueException
317 {
318 return populateObjects(doSelectVillageRecords(criteria));
319 }
320
321 /***
322 * Method to do selects within a transaction.
323 *
324 * @param criteria object used to create the SELECT statement.
325 * @param con the connection to use
326 * @return List of selected Objects
327 * @throws TorqueException Any exceptions caught during processing will be
328 * rethrown wrapped into a TorqueException.
329 */
330 public static List doSelect(Criteria criteria, Connection con)
331 throws TorqueException
332 {
333 return populateObjects(doSelectVillageRecords(criteria, con));
334 }
335
336 /***
337 * Grabs the raw Village records to be formed into objects.
338 * This method handles connections internally. The Record objects
339 * returned by this method should be considered readonly. Do not
340 * alter the data and call save(), your results may vary, but are
341 * certainly likely to result in hard to track MT bugs.
342 *
343 * @throws TorqueException Any exceptions caught during processing will be
344 * rethrown wrapped into a TorqueException.
345 */
346 public static List doSelectVillageRecords(Criteria criteria)
347 throws TorqueException
348 {
349 return BaseCoffeesPeer
350 .doSelectVillageRecords(criteria, (Connection) null);
351 }
352
353 /***
354 * Grabs the raw Village records to be formed into objects.
355 * This method should be used for transactions
356 *
357 * @param con the connection to use
358 * @throws TorqueException Any exceptions caught during processing will be
359 * rethrown wrapped into a TorqueException.
360 */
361 public static List doSelectVillageRecords(Criteria criteria, Connection con)
362 throws TorqueException
363 {
364 if (criteria.getSelectColumns().size() == 0)
365 {
366 addSelectColumns(criteria);
367 }
368
369
370
371
372
373 if (criteria.getDbName() == Torque.getDefaultDB())
374 {
375 criteria.setDbName(DATABASE_NAME);
376 }
377
378
379 if (con == null)
380 {
381 return BasePeer.doSelect(criteria);
382 }
383 else
384 {
385 return BasePeer.doSelect(criteria, con);
386 }
387 }
388
389 /***
390 * The returned List will contain objects of the default type or
391 * objects that inherit from the default.
392 *
393 * @throws TorqueException Any exceptions caught during processing will be
394 * rethrown wrapped into a TorqueException.
395 */
396 public static List populateObjects(List records)
397 throws TorqueException
398 {
399 List results = new ArrayList(records.size());
400
401
402 for (int i = 0; i < records.size(); i++)
403 {
404 Record row = (Record) records.get(i);
405 results.add(CoffeesPeer.row2Object(row, 1,
406 CoffeesPeer.getOMClass()));
407 }
408 return results;
409 }
410
411
412 /***
413 * The class that the Peer will make instances of.
414 * If the BO is abstract then you must implement this method
415 * in the BO.
416 *
417 * @throws TorqueException Any exceptions caught during processing will be
418 * rethrown wrapped into a TorqueException.
419 */
420 public static Class getOMClass()
421 throws TorqueException
422 {
423 return CLASS_DEFAULT;
424 }
425
426 /***
427 * Method to do updates.
428 *
429 * @param criteria object containing data that is used to create the UPDATE
430 * statement.
431 * @throws TorqueException Any exceptions caught during processing will be
432 * rethrown wrapped into a TorqueException.
433 */
434 public static void doUpdate(Criteria criteria) throws TorqueException
435 {
436 BaseCoffeesPeer
437 .doUpdate(criteria, (Connection) null);
438 }
439
440 /***
441 * Method to do updates. This method is to be used during a transaction,
442 * otherwise use the doUpdate(Criteria) method. It will take care of
443 * the connection details internally.
444 *
445 * @param criteria object containing data that is used to create the UPDATE
446 * statement.
447 * @param con the connection to use
448 * @throws TorqueException Any exceptions caught during processing will be
449 * rethrown wrapped into a TorqueException.
450 */
451 public static void doUpdate(Criteria criteria, Connection con)
452 throws TorqueException
453 {
454 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2);
455 selectCriteria.put(COFFEE_ID, criteria.remove(COFFEE_ID));
456
457
458
459
460 if (criteria.getDbName() == Torque.getDefaultDB())
461 {
462 criteria.setDbName(DATABASE_NAME);
463 }
464 if (con == null)
465 {
466 BasePeer.doUpdate(selectCriteria, criteria);
467 }
468 else
469 {
470 BasePeer.doUpdate(selectCriteria, criteria, con);
471 }
472 }
473
474 /***
475 * Method to do deletes.
476 *
477 * @param criteria object containing data that is used DELETE from database.
478 * @throws TorqueException Any exceptions caught during processing will be
479 * rethrown wrapped into a TorqueException.
480 */
481 public static void doDelete(Criteria criteria) throws TorqueException
482 {
483 BaseCoffeesPeer
484 .doDelete(criteria, (Connection) null);
485 }
486
487 /***
488 * Method to do deletes. This method is to be used during a transaction,
489 * otherwise use the doDelete(Criteria) method. It will take care of
490 * the connection details internally.
491 *
492 * @param criteria object containing data that is used DELETE from 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 doDelete(Criteria criteria, Connection con)
498 throws TorqueException
499 {
500
501
502
503
504 if (criteria.getDbName() == Torque.getDefaultDB())
505 {
506 criteria.setDbName(DATABASE_NAME);
507 }
508 if (con == null)
509 {
510 BasePeer.doDelete(criteria);
511 }
512 else
513 {
514 BasePeer.doDelete(criteria, con);
515 }
516 }
517
518 /***
519 * Method to do selects
520 *
521 * @throws TorqueException Any exceptions caught during processing will be
522 * rethrown wrapped into a TorqueException.
523 */
524 public static List doSelect(Coffees obj) throws TorqueException
525 {
526 return doSelect(buildCriteria(obj));
527 }
528
529 /***
530 * Method to do inserts
531 *
532 * @throws TorqueException Any exceptions caught during processing will be
533 * rethrown wrapped into a TorqueException.
534 */
535 public static void doInsert(Coffees obj) throws TorqueException
536 {
537 obj.setPrimaryKey(doInsert(buildCriteria(obj)));
538 obj.setNew(false);
539 obj.setModified(false);
540 }
541
542 /***
543 * @param obj the data object to update in the database.
544 * @throws TorqueException Any exceptions caught during processing will be
545 * rethrown wrapped into a TorqueException.
546 */
547 public static void doUpdate(Coffees obj) throws TorqueException
548 {
549 doUpdate(buildCriteria(obj));
550 obj.setModified(false);
551 }
552
553 /***
554 * @param obj the data object to delete in the database.
555 * @throws TorqueException Any exceptions caught during processing will be
556 * rethrown wrapped into a TorqueException.
557 */
558 public static void doDelete(Coffees obj) throws TorqueException
559 {
560 doDelete(buildCriteria(obj));
561 }
562
563 /***
564 * Method to do inserts. This method is to be used during a transaction,
565 * otherwise use the doInsert(Coffees) method. It will take
566 * care of the connection details internally.
567 *
568 * @param obj the data object to insert into the database.
569 * @param con the connection to use
570 * @throws TorqueException Any exceptions caught during processing will be
571 * rethrown wrapped into a TorqueException.
572 */
573 public static void doInsert(Coffees obj, Connection con)
574 throws TorqueException
575 {
576 obj.setPrimaryKey(doInsert(buildCriteria(obj), con));
577 obj.setNew(false);
578 obj.setModified(false);
579 }
580
581 /***
582 * Method to do update. This method is to be used during a transaction,
583 * otherwise use the doUpdate(Coffees) method. It will take
584 * care of the connection details internally.
585 *
586 * @param obj the data object to update in the database.
587 * @param con the connection to use
588 * @throws TorqueException Any exceptions caught during processing will be
589 * rethrown wrapped into a TorqueException.
590 */
591 public static void doUpdate(Coffees obj, Connection con)
592 throws TorqueException
593 {
594 doUpdate(buildCriteria(obj), con);
595 obj.setModified(false);
596 }
597
598 /***
599 * Method to delete. This method is to be used during a transaction,
600 * otherwise use the doDelete(Coffees) method. It will take
601 * care of the connection details internally.
602 *
603 * @param obj the data object to delete in the database.
604 * @param con the connection to use
605 * @throws TorqueException Any exceptions caught during processing will be
606 * rethrown wrapped into a TorqueException.
607 */
608 public static void doDelete(Coffees obj, Connection con)
609 throws TorqueException
610 {
611 doDelete(buildCriteria(obj), con);
612 }
613
614 /***
615 * Method to do deletes.
616 *
617 * @param pk ObjectKey that is used DELETE from database.
618 * @throws TorqueException Any exceptions caught during processing will be
619 * rethrown wrapped into a TorqueException.
620 */
621 public static void doDelete(ObjectKey pk) throws TorqueException
622 {
623 BaseCoffeesPeer
624 .doDelete(pk, (Connection) null);
625 }
626
627 /***
628 * Method to delete. This method is to be used during a transaction,
629 * otherwise use the doDelete(ObjectKey) method. It will take
630 * care of the connection details internally.
631 *
632 * @param pk the primary key for the object to delete in the database.
633 * @param con the connection to use
634 * @throws TorqueException Any exceptions caught during processing will be
635 * rethrown wrapped into a TorqueException.
636 */
637 public static void doDelete(ObjectKey pk, Connection con)
638 throws TorqueException
639 {
640 doDelete(buildCriteria(pk), con);
641 }
642
643 /*** Build a Criteria object from an ObjectKey */
644 public static Criteria buildCriteria( ObjectKey pk )
645 {
646 Criteria criteria = new Criteria();
647 criteria.add(COFFEE_ID, pk);
648 return criteria;
649 }
650
651 /*** Build a Criteria object from the data object for this peer */
652 public static Criteria buildCriteria( Coffees obj )
653 {
654 Criteria criteria = new Criteria(DATABASE_NAME);
655 if (!obj.isNew())
656 criteria.add(COFFEE_ID, obj.getCoffeeId());
657 criteria.add(COFFEE_NAME, obj.getCoffeeName());
658 criteria.add(SUPPLIER_ID, obj.getSupplierId());
659 criteria.add(PRICE, obj.getPrice());
660 criteria.add(SALES, obj.getSales());
661 criteria.add(TOTAL, obj.getTotal());
662 return criteria;
663 }
664
665
666 /***
667 * Retrieve a single object by pk
668 *
669 * @param pk the primary key
670 * @throws TorqueException Any exceptions caught during processing will be
671 * rethrown wrapped into a TorqueException.
672 * @throws NoRowsException Primary key was not found in database.
673 * @throws TooManyRowsException Primary key was not found in database.
674 */
675 public static Coffees retrieveByPK(int pk)
676 throws TorqueException, NoRowsException, TooManyRowsException
677 {
678 return retrieveByPK(SimpleKey.keyFor(pk));
679 }
680
681 /***
682 * Retrieve a single object by pk
683 *
684 * @param pk the primary key
685 * @throws TorqueException Any exceptions caught during processing will be
686 * rethrown wrapped into a TorqueException.
687 * @throws NoRowsException Primary key was not found in database.
688 * @throws TooManyRowsException Primary key was not found in database.
689 */
690 public static Coffees retrieveByPK(ObjectKey pk)
691 throws TorqueException, NoRowsException, TooManyRowsException
692 {
693 Connection db = null;
694 Coffees retVal = null;
695 try
696 {
697 db = Torque.getConnection(DATABASE_NAME);
698 retVal = retrieveByPK(pk, db);
699 }
700 finally
701 {
702 Torque.closeConnection(db);
703 }
704 return(retVal);
705 }
706
707 /***
708 * Retrieve a single object by pk
709 *
710 * @param pk the primary key
711 * @param con the connection to use
712 * @throws TorqueException Any exceptions caught during processing will be
713 * rethrown wrapped into a TorqueException.
714 * @throws NoRowsException Primary key was not found in database.
715 * @throws TooManyRowsException Primary key was not found in database.
716 */
717 public static Coffees retrieveByPK(ObjectKey pk, Connection con)
718 throws TorqueException, NoRowsException, TooManyRowsException
719 {
720 Criteria criteria = buildCriteria(pk);
721 List v = doSelect(criteria, con);
722 if (v.size() == 0)
723 {
724 throw new NoRowsException("Failed to select a row.");
725 }
726 else if (v.size() > 1)
727 {
728 throw new TooManyRowsException("Failed to select only one row.");
729 }
730 else
731 {
732 return (Coffees)v.get(0);
733 }
734 }
735
736 /***
737 * Retrieve a multiple objects by pk
738 *
739 * @param pks List of primary keys
740 * @throws TorqueException Any exceptions caught during processing will be
741 * rethrown wrapped into a TorqueException.
742 */
743 public static List retrieveByPKs(List pks)
744 throws TorqueException
745 {
746 Connection db = null;
747 List retVal = null;
748 try
749 {
750 db = Torque.getConnection(DATABASE_NAME);
751 retVal = retrieveByPKs(pks, db);
752 }
753 finally
754 {
755 Torque.closeConnection(db);
756 }
757 return(retVal);
758 }
759
760 /***
761 * Retrieve a multiple objects by pk
762 *
763 * @param pks List of primary keys
764 * @param dbcon the connection to use
765 * @throws TorqueException Any exceptions caught during processing will be
766 * rethrown wrapped into a TorqueException.
767 */
768 public static List retrieveByPKs( List pks, Connection dbcon )
769 throws TorqueException
770 {
771 List objs = null;
772 if (pks == null || pks.size() == 0)
773 {
774 objs = new LinkedList();
775 }
776 else
777 {
778 Criteria criteria = new Criteria();
779 criteria.addIn( COFFEE_ID, pks );
780 objs = doSelect(criteria, dbcon);
781 }
782 return objs;
783 }
784
785
786
787
788
789
790
791
792
793
794 /***
795 * Returns the TableMap related to this peer. This method is not
796 * needed for general use but a specific application could have a need.
797 *
798 * @throws TorqueException Any exceptions caught during processing will be
799 * rethrown wrapped into a TorqueException.
800 */
801 protected static TableMap getTableMap()
802 throws TorqueException
803 {
804 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME);
805 }
806 }