1 package org.apache.james.mailboxmanager.torque.om;
2
3 import org.apache.james.mailboxmanager.torque.om.map.MessageFlagsMapBuilder;
4 import org.apache.torque.NoRowsException;
5 import org.apache.torque.TooManyRowsException;
6 import org.apache.torque.Torque;
7 import org.apache.torque.TorqueException;
8 import org.apache.torque.map.MapBuilder;
9 import org.apache.torque.map.TableMap;
10 import org.apache.torque.om.ObjectKey;
11 import org.apache.torque.om.SimpleKey;
12 import org.apache.torque.util.BasePeer;
13 import org.apache.torque.util.Criteria;
14
15 import com.workingdogs.village.DataSetException;
16 import com.workingdogs.village.QueryDataSet;
17 import com.workingdogs.village.Record;
18
19 import java.sql.Connection;
20 import java.sql.SQLException;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25
26
27
28
29
30
31
32 public abstract class BaseMessageFlagsPeer extends BasePeer {
33
34 private static final long serialVersionUID = 1197222309712L;
35
36
37 public static final String DATABASE_NAME;
38
39
40 public static final String TABLE_NAME;
41
42
43
44
45
46
47
48
49
50 public static MapBuilder getMapBuilder() throws TorqueException {
51 return Torque.getMapBuilder(MessageFlagsMapBuilder.CLASS_NAME);
52 }
53
54
55 public static final String MAILBOX_ID;
56
57
58 public static final String UID;
59
60
61 public static final String ANSWERED;
62
63
64 public static final String DELETED;
65
66
67 public static final String DRAFT;
68
69
70 public static final String FLAGGED;
71
72
73 public static final String RECENT;
74
75
76 public static final String SEEN;
77
78 static {
79 DATABASE_NAME = "mailboxmanager";
80 TABLE_NAME = "message_flags";
81
82 MAILBOX_ID = "message_flags.mailbox_id";
83 UID = "message_flags.uid";
84 ANSWERED = "message_flags.answered";
85 DELETED = "message_flags.deleted";
86 DRAFT = "message_flags.draft";
87 FLAGGED = "message_flags.flagged";
88 RECENT = "message_flags.recent";
89 SEEN = "message_flags.seen";
90 if (Torque.isInit()) {
91 try {
92 Torque.getMapBuilder(MessageFlagsMapBuilder.CLASS_NAME);
93 } catch (TorqueException e) {
94 log.error("Could not initialize Peer", e);
95 throw new RuntimeException(e);
96 }
97 } else {
98 Torque.registerMapBuilder(MessageFlagsMapBuilder.CLASS_NAME);
99 }
100 }
101
102
103 public static final int numColumns = 8;
104
105
106 protected static final String CLASSNAME_DEFAULT = "org.apache.james.mailboxmanager.torque.om.MessageFlags";
107
108
109 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
110
111
112
113
114
115
116
117
118 private static Class initClass(String className) {
119 Class c = null;
120 try {
121 c = Class.forName(className);
122 } catch (Throwable t) {
123 log
124 .error(
125 "A FATAL ERROR has occurred which should not "
126 + "have happened under any circumstance. Please notify "
127 + "the Torque developers <torque-dev@db.apache.org> "
128 + "and give as many details as possible (including the error "
129 + "stack trace).", t);
130
131
132 if (t instanceof Error) {
133 throw (Error) t.fillInStackTrace();
134 }
135 }
136 return c;
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public static List resultSet2Objects(java.sql.ResultSet results)
152 throws TorqueException {
153 try {
154 QueryDataSet qds = null;
155 List rows = null;
156 try {
157 qds = new QueryDataSet(results);
158 rows = getSelectResults(qds);
159 } finally {
160 if (qds != null) {
161 qds.close();
162 }
163 }
164
165 return populateObjects(rows);
166 } catch (SQLException e) {
167 throw new TorqueException(e);
168 } catch (DataSetException e) {
169 throw new TorqueException(e);
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182 public static ObjectKey doInsert(Criteria criteria) throws TorqueException {
183 return BaseMessageFlagsPeer.doInsert(criteria, (Connection) null);
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 public static ObjectKey doInsert(Criteria criteria, Connection con)
200 throws TorqueException {
201 correctBooleans(criteria);
202
203 setDbName(criteria);
204
205 if (con == null) {
206 return BasePeer.doInsert(criteria);
207 } else {
208 return BasePeer.doInsert(criteria, con);
209 }
210 }
211
212
213
214
215
216
217
218
219
220
221 public static void addSelectColumns(Criteria criteria)
222 throws TorqueException {
223 criteria.addSelectColumn(MAILBOX_ID);
224 criteria.addSelectColumn(UID);
225 criteria.addSelectColumn(ANSWERED);
226 criteria.addSelectColumn(DELETED);
227 criteria.addSelectColumn(DRAFT);
228 criteria.addSelectColumn(FLAGGED);
229 criteria.addSelectColumn(RECENT);
230 criteria.addSelectColumn(SEEN);
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244 public static void correctBooleans(Criteria criteria)
245 throws TorqueException {
246 correctBooleans(criteria, getTableMap());
247 }
248
249
250
251
252
253
254
255
256
257
258
259 public static MessageFlags row2Object(Record row, int offset, Class cls)
260 throws TorqueException {
261 try {
262 MessageFlags obj = (MessageFlags) cls.newInstance();
263 MessageFlagsPeer.populateObject(row, offset, obj);
264 obj.setModified(false);
265 obj.setNew(false);
266
267 return obj;
268 } catch (InstantiationException e) {
269 throw new TorqueException(e);
270 } catch (IllegalAccessException e) {
271 throw new TorqueException(e);
272 }
273 }
274
275
276
277
278
279
280
281
282
283
284
285 public static void populateObject(Record row, int offset, MessageFlags obj)
286 throws TorqueException {
287 try {
288 obj.setMailboxId(row.getValue(offset + 0).asLong());
289 obj.setUid(row.getValue(offset + 1).asLong());
290 obj.setAnswered(row.getValue(offset + 2).asBoolean());
291 obj.setDeleted(row.getValue(offset + 3).asBoolean());
292 obj.setDraft(row.getValue(offset + 4).asBoolean());
293 obj.setFlagged(row.getValue(offset + 5).asBoolean());
294 obj.setRecent(row.getValue(offset + 6).asBoolean());
295 obj.setSeen(row.getValue(offset + 7).asBoolean());
296 } catch (DataSetException e) {
297 throw new TorqueException(e);
298 }
299 }
300
301
302
303
304
305
306
307
308
309
310
311 public static List doSelect(Criteria criteria) throws TorqueException {
312 return populateObjects(doSelectVillageRecords(criteria));
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326
327 public static List doSelect(Criteria criteria, Connection con)
328 throws TorqueException {
329 return populateObjects(doSelectVillageRecords(criteria, con));
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343 public static List doSelectVillageRecords(Criteria criteria)
344 throws TorqueException {
345 return BaseMessageFlagsPeer.doSelectVillageRecords(criteria,
346 (Connection) null);
347 }
348
349
350
351
352
353
354
355
356
357
358
359
360
361 public static List doSelectVillageRecords(Criteria criteria, Connection con)
362 throws TorqueException {
363 if (criteria.getSelectColumns().size() == 0) {
364 addSelectColumns(criteria);
365 }
366 correctBooleans(criteria);
367
368 setDbName(criteria);
369
370
371
372 if (con == null) {
373 return BasePeer.doSelect(criteria);
374 } else {
375 return BasePeer.doSelect(criteria, con);
376 }
377 }
378
379
380
381
382
383
384
385
386
387 public static List populateObjects(List records) throws TorqueException {
388 List results = new ArrayList(records.size());
389
390
391 for (int i = 0; i < records.size(); i++) {
392 Record row = (Record) records.get(i);
393 results.add(MessageFlagsPeer.row2Object(row, 1, MessageFlagsPeer
394 .getOMClass()));
395 }
396 return results;
397 }
398
399
400
401
402
403
404
405
406
407 public static Class getOMClass() throws TorqueException {
408 return CLASS_DEFAULT;
409 }
410
411
412
413
414
415
416
417
418
419
420
421 public static void doUpdate(Criteria criteria) throws TorqueException {
422 BaseMessageFlagsPeer.doUpdate(criteria, (Connection) null);
423 }
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439 public static void doUpdate(Criteria criteria, Connection con)
440 throws TorqueException {
441 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2);
442 correctBooleans(criteria);
443
444 selectCriteria.put(MAILBOX_ID, criteria.remove(MAILBOX_ID));
445
446 selectCriteria.put(UID, criteria.remove(UID));
447
448 setDbName(criteria);
449
450 if (con == null) {
451 BasePeer.doUpdate(selectCriteria, criteria);
452 } else {
453 BasePeer.doUpdate(selectCriteria, criteria, con);
454 }
455 }
456
457
458
459
460
461
462
463
464
465
466 public static void doDelete(Criteria criteria) throws TorqueException {
467 MessageFlagsPeer.doDelete(criteria, (Connection) null);
468 }
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483 public static void doDelete(Criteria criteria, Connection con)
484 throws TorqueException {
485 correctBooleans(criteria);
486
487 setDbName(criteria);
488
489 if (con == null) {
490 BasePeer.doDelete(criteria);
491 } else {
492 BasePeer.doDelete(criteria, con);
493 }
494 }
495
496
497
498
499
500
501
502
503 public static List doSelect(MessageFlags obj) throws TorqueException {
504 return doSelect(buildSelectCriteria(obj));
505 }
506
507
508
509
510
511
512
513
514 public static void doInsert(MessageFlags obj) throws TorqueException {
515 doInsert(buildCriteria(obj));
516 obj.setNew(false);
517 obj.setModified(false);
518 }
519
520
521
522
523
524
525
526
527 public static void doUpdate(MessageFlags obj) throws TorqueException {
528 doUpdate(buildCriteria(obj));
529 obj.setModified(false);
530 }
531
532
533
534
535
536
537
538
539 public static void doDelete(MessageFlags obj) throws TorqueException {
540 doDelete(buildSelectCriteria(obj));
541 }
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556 public static void doInsert(MessageFlags obj, Connection con)
557 throws TorqueException {
558 doInsert(buildCriteria(obj), con);
559 obj.setNew(false);
560 obj.setModified(false);
561 }
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576 public static void doUpdate(MessageFlags obj, Connection con)
577 throws TorqueException {
578 doUpdate(buildCriteria(obj), con);
579 obj.setModified(false);
580 }
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595 public static void doDelete(MessageFlags obj, Connection con)
596 throws TorqueException {
597 doDelete(buildSelectCriteria(obj), con);
598 }
599
600
601
602
603
604
605
606
607
608
609 public static void doDelete(ObjectKey pk) throws TorqueException {
610 BaseMessageFlagsPeer.doDelete(pk, (Connection) null);
611 }
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626 public static void doDelete(ObjectKey pk, Connection con)
627 throws TorqueException {
628 doDelete(buildCriteria(pk), con);
629 }
630
631
632 public static Criteria buildCriteria(ObjectKey pk) {
633 Criteria criteria = new Criteria();
634 SimpleKey[] keys = (SimpleKey[]) pk.getValue();
635 criteria.add(MAILBOX_ID, keys[0]);
636 criteria.add(UID, keys[1]);
637 return criteria;
638 }
639
640
641 public static Criteria buildCriteria(MessageFlags obj) {
642 Criteria criteria = new Criteria(DATABASE_NAME);
643 criteria.add(MAILBOX_ID, obj.getMailboxId());
644 criteria.add(UID, obj.getUid());
645 criteria.add(ANSWERED, obj.getAnswered());
646 criteria.add(DELETED, obj.getDeleted());
647 criteria.add(DRAFT, obj.getDraft());
648 criteria.add(FLAGGED, obj.getFlagged());
649 criteria.add(RECENT, obj.getRecent());
650 criteria.add(SEEN, obj.getSeen());
651 return criteria;
652 }
653
654
655
656
657
658 public static Criteria buildSelectCriteria(MessageFlags obj) {
659 Criteria criteria = new Criteria(DATABASE_NAME);
660 criteria.add(MAILBOX_ID, obj.getMailboxId());
661 criteria.add(UID, obj.getUid());
662 criteria.add(ANSWERED, obj.getAnswered());
663 criteria.add(DELETED, obj.getDeleted());
664 criteria.add(DRAFT, obj.getDraft());
665 criteria.add(FLAGGED, obj.getFlagged());
666 criteria.add(RECENT, obj.getRecent());
667 criteria.add(SEEN, obj.getSeen());
668 return criteria;
669 }
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684 public static MessageFlags retrieveByPK(ObjectKey pk)
685 throws TorqueException, NoRowsException, TooManyRowsException {
686 Connection db = null;
687 MessageFlags retVal = null;
688 try {
689 db = Torque.getConnection(DATABASE_NAME);
690 retVal = retrieveByPK(pk, db);
691 } finally {
692 Torque.closeConnection(db);
693 }
694 return retVal;
695 }
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712 public static MessageFlags retrieveByPK(ObjectKey pk, Connection con)
713 throws TorqueException, NoRowsException, TooManyRowsException {
714 Criteria criteria = buildCriteria(pk);
715 List v = doSelect(criteria, con);
716 if (v.size() == 0) {
717 throw new NoRowsException("Failed to select a row.");
718 } else if (v.size() > 1) {
719 throw new TooManyRowsException("Failed to select only one row.");
720 } else {
721 return (MessageFlags) v.get(0);
722 }
723 }
724
725
726
727
728
729
730
731
732
733
734 public static List retrieveByPKs(List pks) throws TorqueException {
735 Connection db = null;
736 List retVal = null;
737 try {
738 db = Torque.getConnection(DATABASE_NAME);
739 retVal = retrieveByPKs(pks, db);
740 } finally {
741 Torque.closeConnection(db);
742 }
743 return retVal;
744 }
745
746
747
748
749
750
751
752
753
754
755
756
757 public static List retrieveByPKs(List pks, Connection dbcon)
758 throws TorqueException {
759 List objs = null;
760 if (pks == null || pks.size() == 0) {
761 objs = new LinkedList();
762 } else {
763 Criteria criteria = new Criteria();
764 Iterator iter = pks.iterator();
765 while (iter.hasNext()) {
766 ObjectKey pk = (ObjectKey) iter.next();
767 SimpleKey[] keys = (SimpleKey[]) pk.getValue();
768 Criteria.Criterion c0 = criteria.getNewCriterion(MAILBOX_ID,
769 keys[0], Criteria.EQUAL);
770 Criteria.Criterion c1 = criteria.getNewCriterion(UID, keys[1],
771 Criteria.EQUAL);
772 c0.and(c1);
773 criteria.or(c0);
774 }
775 objs = doSelect(criteria, dbcon);
776 }
777 return objs;
778 }
779
780
781
782
783
784
785
786
787
788 public static MessageFlags retrieveByPK(long mailboxId, long uid)
789 throws TorqueException {
790 Connection db = null;
791 MessageFlags retVal = null;
792 try {
793 db = Torque.getConnection(DATABASE_NAME);
794 retVal = retrieveByPK(mailboxId, uid, db);
795 } finally {
796 Torque.closeConnection(db);
797 }
798 return retVal;
799 }
800
801
802
803
804
805
806
807
808
809
810
811 public static MessageFlags retrieveByPK(long mailboxId, long uid,
812 Connection con) throws TorqueException {
813
814 Criteria criteria = new Criteria(5);
815 criteria.add(MAILBOX_ID, mailboxId);
816 criteria.add(UID, uid);
817 List v = doSelect(criteria, con);
818 if (v.size() == 1) {
819 return (MessageFlags) v.get(0);
820 } else {
821 throw new TorqueException("Failed to select one and only one row.");
822 }
823 }
824
825
826
827
828
829
830
831
832
833
834
835
836
837 protected static List doSelectJoinMessageRow(Criteria criteria)
838 throws TorqueException {
839 return doSelectJoinMessageRow(criteria, null);
840 }
841
842
843
844
845
846
847
848
849
850
851
852
853
854 protected static List doSelectJoinMessageRow(Criteria criteria,
855 Connection conn) throws TorqueException {
856 setDbName(criteria);
857
858 MessageFlagsPeer.addSelectColumns(criteria);
859 int offset = numColumns + 1;
860 MessageRowPeer.addSelectColumns(criteria);
861
862 criteria
863 .addJoin(MessageFlagsPeer.MAILBOX_ID, MessageRowPeer.MAILBOX_ID);
864 criteria.addJoin(MessageFlagsPeer.UID, MessageRowPeer.UID);
865
866 correctBooleans(criteria);
867
868 List rows;
869 if (conn == null) {
870 rows = BasePeer.doSelect(criteria);
871 } else {
872 rows = BasePeer.doSelect(criteria, conn);
873 }
874
875 List results = new ArrayList();
876
877 for (int i = 0; i < rows.size(); i++) {
878 Record row = (Record) rows.get(i);
879
880 Class omClass = MessageFlagsPeer.getOMClass();
881 MessageFlags obj1 = (MessageFlags) MessageFlagsPeer.row2Object(row,
882 1, omClass);
883 omClass = MessageRowPeer.getOMClass();
884 MessageRow obj2 = (MessageRow) MessageRowPeer.row2Object(row,
885 offset, omClass);
886
887 boolean newObject = true;
888 for (int j = 0; j < results.size(); j++) {
889 MessageFlags temp_obj1 = (MessageFlags) results.get(j);
890 MessageRow temp_obj2 = (MessageRow) temp_obj1.getMessageRow();
891 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) {
892 newObject = false;
893 temp_obj2.addMessageFlags(obj1);
894 break;
895 }
896 }
897 if (newObject) {
898 obj2.initMessageFlagss();
899 obj2.addMessageFlags(obj1);
900 }
901 results.add(obj1);
902 }
903 return results;
904 }
905
906
907
908
909
910
911
912
913 public static TableMap getTableMap() throws TorqueException {
914 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME);
915 }
916
917 private static void setDbName(Criteria crit) {
918
919
920
921 if (crit.getDbName() == Torque.getDefaultDB()) {
922 crit.setDbName(DATABASE_NAME);
923 }
924 }
925
926 }