1 package org.apache.turbine.services.schedule;
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.TorqueRuntimeException;
17 import org.apache.torque.map.MapBuilder;
18 import org.apache.torque.map.TableMap;
19 import org.apache.torque.om.DateKey;
20 import org.apache.torque.om.NumberKey;
21 import org.apache.torque.om.StringKey;
22 import org.apache.torque.om.ObjectKey;
23 import org.apache.torque.om.SimpleKey;
24 import org.apache.torque.util.BasePeer;
25 import org.apache.torque.util.Criteria;
26
27 import com.workingdogs.village.DataSetException;
28 import com.workingdogs.village.QueryDataSet;
29 import com.workingdogs.village.Record;
30
31
32 import org.apache.turbine.services.schedule.map.*;
33
34
35
36
37
38
39
40
41
42
43 public abstract class BaseJobEntryPeer
44 extends BasePeer
45 {
46
47 private static final long serialVersionUID = 1308842746584L;
48
49
50
51 public static final String DATABASE_NAME;
52
53
54 public static final String TABLE_NAME;
55
56
57
58
59
60
61
62 public static MapBuilder getMapBuilder()
63 throws TorqueException
64 {
65 return Torque.getMapBuilder(JobEntryMapBuilder.CLASS_NAME);
66 }
67
68
69 public static final String JOB_ID;
70
71 public static final String SECOND;
72
73 public static final String MINUTE;
74
75 public static final String HOUR;
76
77 public static final String WEEK_DAY;
78
79 public static final String DAY_OF_MONTH;
80
81 public static final String TASK;
82
83 public static final String EMAIL;
84
85 public static final String PROPERTY;
86
87 static
88 {
89 DATABASE_NAME = "default";
90 TABLE_NAME = "TURBINE_SCHEDULED_JOB";
91
92 JOB_ID = "TURBINE_SCHEDULED_JOB.JOB_ID";
93 SECOND = "TURBINE_SCHEDULED_JOB.SECOND";
94 MINUTE = "TURBINE_SCHEDULED_JOB.MINUTE";
95 HOUR = "TURBINE_SCHEDULED_JOB.HOUR";
96 WEEK_DAY = "TURBINE_SCHEDULED_JOB.WEEK_DAY";
97 DAY_OF_MONTH = "TURBINE_SCHEDULED_JOB.DAY_OF_MONTH";
98 TASK = "TURBINE_SCHEDULED_JOB.TASK";
99 EMAIL = "TURBINE_SCHEDULED_JOB.EMAIL";
100 PROPERTY = "TURBINE_SCHEDULED_JOB.PROPERTY";
101 if (Torque.isInit())
102 {
103 try
104 {
105 Torque.getMapBuilder(JobEntryMapBuilder.CLASS_NAME);
106 }
107 catch (TorqueException e)
108 {
109 log.error("Could not initialize Peer", e);
110 throw new TorqueRuntimeException(e);
111 }
112 }
113 else
114 {
115 Torque.registerMapBuilder(JobEntryMapBuilder.CLASS_NAME);
116 }
117 }
118
119
120 public static final int numColumns = 9;
121
122
123 protected static final String CLASSNAME_DEFAULT =
124 "org.apache.turbine.services.schedule.JobEntry";
125
126
127 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
128
129
130
131
132
133
134
135 private static Class initClass(String className)
136 {
137 Class c = null;
138 try
139 {
140 c = Class.forName(className);
141 }
142 catch (Throwable t)
143 {
144 log.error("A FATAL ERROR has occurred which should not "
145 + "have happened under any circumstance. Please notify "
146 + "the Torque developers <torque-dev@db.apache.org> "
147 + "and give as many details as possible (including the error "
148 + "stack trace).", t);
149
150
151 if (t instanceof Error)
152 {
153 throw (Error) t.fillInStackTrace();
154 }
155 }
156 return c;
157 }
158
159
160
161
162
163
164
165
166
167
168
169 public static List<JobEntry> resultSet2Objects(java.sql.ResultSet results)
170 throws TorqueException
171 {
172 try
173 {
174 QueryDataSet qds = null;
175 List<Record> rows = null;
176 try
177 {
178 qds = new QueryDataSet(results);
179 rows = getSelectResults(qds);
180 }
181 finally
182 {
183 if (qds != null)
184 {
185 qds.close();
186 }
187 }
188
189 return populateObjects(rows);
190 }
191 catch (SQLException e)
192 {
193 throw new TorqueException(e);
194 }
195 catch (DataSetException e)
196 {
197 throw new TorqueException(e);
198 }
199 }
200
201
202
203
204
205
206
207
208
209
210 public static ObjectKey doInsert(Criteria criteria)
211 throws TorqueException
212 {
213 return BaseJobEntryPeer
214 .doInsert(criteria, (Connection) null);
215 }
216
217
218
219
220
221
222
223
224
225
226
227 public static ObjectKey doInsert(Criteria criteria, Connection con)
228 throws TorqueException
229 {
230 correctBooleans(criteria);
231
232 setDbName(criteria);
233
234 if (con == null)
235 {
236 return BasePeer.doInsert(criteria);
237 }
238 else
239 {
240 return BasePeer.doInsert(criteria, con);
241 }
242 }
243
244
245
246
247
248
249
250
251 public static void addSelectColumns(Criteria criteria)
252 throws TorqueException
253 {
254 criteria.addSelectColumn(JOB_ID);
255 criteria.addSelectColumn(SECOND);
256 criteria.addSelectColumn(MINUTE);
257 criteria.addSelectColumn(HOUR);
258 criteria.addSelectColumn(WEEK_DAY);
259 criteria.addSelectColumn(DAY_OF_MONTH);
260 criteria.addSelectColumn(TASK);
261 criteria.addSelectColumn(EMAIL);
262 criteria.addSelectColumn(PROPERTY);
263 }
264
265
266
267
268
269
270
271
272
273
274 public static void correctBooleans(Criteria criteria) throws TorqueException
275 {
276 correctBooleans(criteria, getTableMap());
277 }
278
279
280
281
282
283
284
285
286
287
288 public static JobEntry row2Object(Record row,
289 int offset,
290 Class cls)
291 throws TorqueException
292 {
293 try
294 {
295 JobEntry obj = (JobEntry) cls.newInstance();
296 JobEntryPeer.populateObject(row, offset, obj);
297 obj.setModified(false);
298 obj.setNew(false);
299
300 return obj;
301 }
302 catch (InstantiationException e)
303 {
304 throw new TorqueException(e);
305 }
306 catch (IllegalAccessException e)
307 {
308 throw new TorqueException(e);
309 }
310 }
311
312
313
314
315
316
317
318
319
320
321 public static void populateObject(Record row,
322 int offset,
323 JobEntry obj)
324 throws TorqueException
325 {
326 try
327 {
328 obj.setJobId(row.getValue(offset + 0).asInt());
329 obj.setSecond(row.getValue(offset + 1).asInt());
330 obj.setMinute(row.getValue(offset + 2).asInt());
331 obj.setHour(row.getValue(offset + 3).asInt());
332 obj.setWeekDay(row.getValue(offset + 4).asInt());
333 obj.setDayOfMonth(row.getValue(offset + 5).asInt());
334 obj.setTask(row.getValue(offset + 6).asString());
335 obj.setEmail(row.getValue(offset + 7).asString());
336 obj.setProperty(row.getValue(offset + 8).asBytes());
337 }
338 catch (DataSetException e)
339 {
340 throw new TorqueException(e);
341 }
342 }
343
344
345
346
347
348
349
350
351
352 public static List<JobEntry> doSelect(Criteria criteria) throws TorqueException
353 {
354 return populateObjects(doSelectVillageRecords(criteria));
355 }
356
357
358
359
360
361
362
363
364
365
366 public static List<JobEntry> doSelect(Criteria criteria, Connection con)
367 throws TorqueException
368 {
369 return populateObjects(doSelectVillageRecords(criteria, con));
370 }
371
372
373
374
375
376
377
378
379
380
381
382 public static List<Record> doSelectVillageRecords(Criteria criteria)
383 throws TorqueException
384 {
385 return BaseJobEntryPeer
386 .doSelectVillageRecords(criteria, (Connection) null);
387 }
388
389
390
391
392
393
394
395
396
397
398 public static List<Record> doSelectVillageRecords(Criteria criteria, Connection con)
399 throws TorqueException
400 {
401 if (criteria.getSelectColumns().size() == 0)
402 {
403 addSelectColumns(criteria);
404 }
405 correctBooleans(criteria);
406
407 setDbName(criteria);
408
409
410
411 if (con == null)
412 {
413 return BasePeer.doSelect(criteria);
414 }
415 else
416 {
417 return BasePeer.doSelect(criteria, con);
418 }
419 }
420
421
422
423
424
425
426
427
428 public static List<JobEntry> populateObjects(List<Record> records)
429 throws TorqueException
430 {
431 List<JobEntry> results = new ArrayList<JobEntry>(records.size());
432
433
434 for (int i = 0; i < records.size(); i++)
435 {
436 Record row = records.get(i);
437 results.add(JobEntryPeer.row2Object(row, 1,
438 JobEntryPeer.getOMClass()));
439 }
440 return results;
441 }
442
443
444
445
446
447
448
449
450
451
452 public static Class getOMClass()
453 throws TorqueException
454 {
455 return CLASS_DEFAULT;
456 }
457
458
459
460
461
462
463
464
465
466 public static void doUpdate(Criteria criteria) throws TorqueException
467 {
468 BaseJobEntryPeer
469 .doUpdate(criteria, (Connection) null);
470 }
471
472
473
474
475
476
477
478
479
480
481
482
483 public static void doUpdate(Criteria criteria, Connection con)
484 throws TorqueException
485 {
486 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2);
487 correctBooleans(criteria);
488
489
490 selectCriteria.put(JOB_ID, criteria.remove(JOB_ID));
491
492
493
494
495
496
497
498
499
500 setDbName(criteria);
501
502 if (con == null)
503 {
504 BasePeer.doUpdate(selectCriteria, criteria);
505 }
506 else
507 {
508 BasePeer.doUpdate(selectCriteria, criteria, con);
509 }
510 }
511
512
513
514
515
516
517
518
519 public static void doDelete(Criteria criteria) throws TorqueException
520 {
521 JobEntryPeer
522 .doDelete(criteria, (Connection) null);
523 }
524
525
526
527
528
529
530
531
532
533
534
535 public static void doDelete(Criteria criteria, Connection con)
536 throws TorqueException
537 {
538 correctBooleans(criteria);
539
540 setDbName(criteria);
541
542 if (con == null)
543 {
544 BasePeer.doDelete(criteria, TABLE_NAME);
545 }
546 else
547 {
548 BasePeer.doDelete(criteria, TABLE_NAME, con);
549 }
550 }
551
552
553
554
555
556
557
558 public static List<JobEntry> doSelect(JobEntry obj) throws TorqueException
559 {
560 return doSelect(buildSelectCriteria(obj));
561 }
562
563
564
565
566
567
568
569 public static void doInsert(JobEntry obj) throws TorqueException
570 {
571 obj.setPrimaryKey(doInsert(buildCriteria(obj)));
572 obj.setNew(false);
573 obj.setModified(false);
574 }
575
576
577
578
579
580
581 public static void doUpdate(JobEntry obj) throws TorqueException
582 {
583 doUpdate(buildCriteria(obj));
584 obj.setModified(false);
585 }
586
587
588
589
590
591
592 public static void doDelete(JobEntry obj) throws TorqueException
593 {
594 doDelete(buildSelectCriteria(obj));
595 }
596
597
598
599
600
601
602
603
604
605
606
607 public static void doInsert(JobEntry obj, Connection con)
608 throws TorqueException
609 {
610 obj.setPrimaryKey(doInsert(buildCriteria(obj), con));
611 obj.setNew(false);
612 obj.setModified(false);
613 }
614
615
616
617
618
619
620
621
622
623
624
625 public static void doUpdate(JobEntry obj, Connection con)
626 throws TorqueException
627 {
628 doUpdate(buildCriteria(obj), con);
629 obj.setModified(false);
630 }
631
632
633
634
635
636
637
638
639
640
641
642 public static void doDelete(JobEntry obj, Connection con)
643 throws TorqueException
644 {
645 doDelete(buildSelectCriteria(obj), con);
646 }
647
648
649
650
651
652
653
654
655 public static void doDelete(ObjectKey pk) throws TorqueException
656 {
657 BaseJobEntryPeer
658 .doDelete(pk, (Connection) null);
659 }
660
661
662
663
664
665
666
667
668
669
670
671 public static void doDelete(ObjectKey pk, Connection con)
672 throws TorqueException
673 {
674 doDelete(buildCriteria(pk), con);
675 }
676
677
678 public static Criteria buildCriteria( ObjectKey pk )
679 {
680 Criteria criteria = new Criteria();
681 criteria.add(JOB_ID, pk);
682 return criteria;
683 }
684
685
686 public static Criteria buildCriteria( JobEntry obj )
687 {
688 Criteria criteria = new Criteria(DATABASE_NAME);
689 if (!obj.isNew())
690 criteria.add(JOB_ID, obj.getJobId());
691 criteria.add(SECOND, obj.getSecond());
692 criteria.add(MINUTE, obj.getMinute());
693 criteria.add(HOUR, obj.getHour());
694 criteria.add(WEEK_DAY, obj.getWeekDay());
695 criteria.add(DAY_OF_MONTH, obj.getDayOfMonth());
696 criteria.add(TASK, obj.getTask());
697 criteria.add(EMAIL, obj.getEmail());
698 criteria.add(PROPERTY, obj.getProperty());
699 return criteria;
700 }
701
702
703 public static Criteria buildSelectCriteria( JobEntry obj )
704 {
705 Criteria criteria = new Criteria(DATABASE_NAME);
706 if (!obj.isNew())
707 {
708 criteria.add(JOB_ID, obj.getJobId());
709 }
710 criteria.add(SECOND, obj.getSecond());
711 criteria.add(MINUTE, obj.getMinute());
712 criteria.add(HOUR, obj.getHour());
713 criteria.add(WEEK_DAY, obj.getWeekDay());
714 criteria.add(DAY_OF_MONTH, obj.getDayOfMonth());
715 criteria.add(TASK, obj.getTask());
716 criteria.add(EMAIL, obj.getEmail());
717 return criteria;
718 }
719
720
721
722
723
724
725
726
727
728
729
730 public static JobEntry retrieveByPK(int pk)
731 throws TorqueException, NoRowsException, TooManyRowsException
732 {
733 return retrieveByPK(SimpleKey.keyFor(pk));
734 }
735
736
737
738
739
740
741
742
743
744
745
746 public static JobEntry retrieveByPK(int pk, Connection con)
747 throws TorqueException, NoRowsException, TooManyRowsException
748 {
749 return retrieveByPK(SimpleKey.keyFor(pk), con);
750 }
751
752
753
754
755
756
757
758
759
760
761 public static JobEntry retrieveByPK(ObjectKey pk)
762 throws TorqueException, NoRowsException, TooManyRowsException
763 {
764 Connection db = null;
765 JobEntry retVal = null;
766 try
767 {
768 db = Torque.getConnection(DATABASE_NAME);
769 retVal = retrieveByPK(pk, db);
770 }
771 finally
772 {
773 Torque.closeConnection(db);
774 }
775 return retVal;
776 }
777
778
779
780
781
782
783
784
785
786
787
788 public static JobEntry retrieveByPK(ObjectKey pk, Connection con)
789 throws TorqueException, NoRowsException, TooManyRowsException
790 {
791 Criteria criteria = buildCriteria(pk);
792 List<JobEntry> v = doSelect(criteria, con);
793 if (v.size() == 0)
794 {
795 throw new NoRowsException("Failed to select a row.");
796 }
797 else if (v.size() > 1)
798 {
799 throw new TooManyRowsException("Failed to select only one row.");
800 }
801 else
802 {
803 return (JobEntry)v.get(0);
804 }
805 }
806
807
808
809
810
811
812
813
814 public static List<JobEntry> retrieveByPKs(List<ObjectKey> pks)
815 throws TorqueException
816 {
817 Connection db = null;
818 List<JobEntry> retVal = null;
819 try
820 {
821 db = Torque.getConnection(DATABASE_NAME);
822 retVal = retrieveByPKs(pks, db);
823 }
824 finally
825 {
826 Torque.closeConnection(db);
827 }
828 return retVal;
829 }
830
831
832
833
834
835
836
837
838
839 public static List<JobEntry> retrieveByPKs( List<ObjectKey> pks, Connection dbcon )
840 throws TorqueException
841 {
842 List<JobEntry> objs = null;
843 if (pks == null || pks.size() == 0)
844 {
845 objs = new LinkedList<JobEntry>();
846 }
847 else
848 {
849 Criteria criteria = new Criteria();
850 criteria.addIn( JOB_ID, pks );
851 objs = doSelect(criteria, dbcon);
852 }
853 return objs;
854 }
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871 public static TableMap getTableMap()
872 throws TorqueException
873 {
874 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME);
875 }
876
877 private static void setDbName(Criteria crit)
878 {
879
880
881
882 if (crit.getDbName() == Torque.getDefaultDB())
883 {
884 crit.setDbName(DATABASE_NAME);
885 }
886 }
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903 public static List<Record> executeQuery(String queryString) throws TorqueException
904 {
905 return BasePeer.executeQuery(queryString);
906 }
907
908
909
910
911
912
913
914
915
916
917
918
919
920 public static List<Record> executeQuery(String queryString, String dbName)
921 throws TorqueException
922 {
923 return BasePeer.executeQuery(queryString,dbName);
924 }
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939 public static List<Record> executeQuery(
940 String queryString,
941 String dbName,
942 boolean singleRecord)
943 throws TorqueException
944 {
945 return BasePeer.executeQuery(queryString,dbName,singleRecord);
946 }
947
948
949
950
951
952
953
954
955
956
957
958
959
960 public static List<Record> executeQuery(
961 String queryString,
962 boolean singleRecord,
963 Connection con)
964 throws TorqueException
965 {
966 return BasePeer.executeQuery(queryString,singleRecord,con);
967 }
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983 public static List<Record> executeQuery(
984 String queryString,
985 int start,
986 int numberOfResults,
987 String dbName,
988 boolean singleRecord)
989 throws TorqueException
990 {
991 return BasePeer.executeQuery(queryString,start,numberOfResults,dbName,singleRecord);
992 }
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008 public static List<Record> executeQuery(
1009 String queryString,
1010 int start,
1011 int numberOfResults,
1012 boolean singleRecord,
1013 Connection con)
1014 throws TorqueException
1015 {
1016 return BasePeer.executeQuery(queryString,start,numberOfResults,singleRecord,con);
1017 }
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030 public static List<Record> getSelectResults(QueryDataSet qds)
1031 throws TorqueException
1032 {
1033 return BasePeer.getSelectResults(qds);
1034 }
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048 public static List<Record> getSelectResults(QueryDataSet qds, boolean singleRecord)
1049 throws TorqueException
1050 {
1051 return BasePeer.getSelectResults(qds,singleRecord);
1052 }
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068 public static List<Record> getSelectResults(
1069 QueryDataSet qds,
1070 int numberOfResults,
1071 boolean singleRecord)
1072 throws TorqueException
1073 {
1074 return BasePeer.getSelectResults(qds,numberOfResults,singleRecord);
1075 }
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094 public static List getSelectResults(
1095 QueryDataSet qds,
1096 int start,
1097 int numberOfResults,
1098 boolean singleRecord)
1099 throws TorqueException
1100 {
1101 return BasePeer.getSelectResults(qds,start,numberOfResults,singleRecord);
1102 }
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114 public static List<Record> doPSSelect(Criteria criteria, Connection con)
1115 throws TorqueException
1116 {
1117 return BasePeer.doPSSelect(criteria,con);
1118 }
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129 public static List<Record> doPSSelect(Criteria criteria) throws TorqueException
1130 {
1131 return BasePeer.doPSSelect(criteria);
1132 }
1133 }