1 package org.apache.ojb.broker.query;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Enumeration;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Vector;
26
27 import org.apache.ojb.broker.PersistenceBrokerFactory;
28 import org.apache.ojb.broker.metadata.FieldHelper;
29 import org.apache.ojb.broker.core.PersistenceBrokerConfiguration;
30 import org.apache.ojb.broker.util.configuration.ConfigurationException;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class Criteria implements java.io.Serializable
49 {
50 static final long serialVersionUID = 7384550404778187808L;
51
52
53 public static final int OR = 0;
54
55 public static final int AND = 1;
56
57 public static final int NONE = 9;
58
59
60 public static final String PARENT_QUERY_PREFIX = "parentQuery.";
61
62 private Vector m_criteria;
63 private int m_type;
64 private boolean m_embraced;
65 private boolean m_negative = false;
66
67
68 private List orderby = null;
69 private List groupby = null;
70 private List prefetchedRelationships = null;
71
72
73 private String m_alias = null;
74
75
76
77 private String m_aliasPath = null;
78
79
80 private UserAlias m_userAlias = null;
81
82
83 protected static final int IN_LIMIT = getSqlInLimit();
84
85 private QueryByCriteria m_query;
86 private Criteria m_parentCriteria;
87
88
89
90 private Map m_pathClasses;
91
92
93
94
95 public Criteria()
96 {
97 m_criteria = new Vector();
98 groupby = new ArrayList();
99 orderby = new ArrayList();
100 prefetchedRelationships = new ArrayList();
101 m_type = NONE;
102 m_embraced = false;
103
104 m_pathClasses = new HashMap();
105 }
106
107
108
109
110
111 public Criteria(SelectionCriteria aSelectionCriteria)
112 {
113 this();
114 addSelectionCriteria(aSelectionCriteria);
115 }
116
117
118
119
120
121
122
123
124 public Criteria copy(boolean includeGroupBy, boolean includeOrderBy, boolean includePrefetchedRelationships)
125 {
126 Criteria copy = new Criteria();
127
128 copy.m_criteria = new Vector(this.m_criteria);
129 copy.m_negative = this.m_negative;
130
131 if (includeGroupBy)
132 {
133 copy.groupby = this.groupby;
134 }
135 if (includeOrderBy)
136 {
137 copy.orderby = this.orderby;
138 }
139 if (includePrefetchedRelationships)
140 {
141 copy.prefetchedRelationships = this.prefetchedRelationships;
142 }
143
144 return copy;
145 }
146
147 protected void addSelectionCriteria(SelectionCriteria selectionCrit)
148 {
149 selectionCrit.setCriteria(this);
150 m_criteria.addElement(selectionCrit);
151 }
152
153 protected void addCriteria(Criteria crit)
154 {
155 crit.setParentCriteria(this);
156 m_criteria.addElement(crit);
157 }
158
159 protected void addCriteria(Vector criteria)
160 {
161 Object crit;
162
163 for (int i = 0; i < criteria.size(); i++)
164 {
165 crit = criteria.elementAt(i);
166 if (crit instanceof SelectionCriteria)
167 {
168 addSelectionCriteria((SelectionCriteria) crit);
169 }
170 else if (crit instanceof Criteria)
171 {
172 addCriteria((Criteria) crit);
173 }
174 }
175 }
176
177
178
179
180
181
182
183
184
185
186 protected List splitInCriteria(Object attribute, Collection values, boolean negative, int inLimit)
187 {
188 List result = new ArrayList();
189 Collection inCollection = new ArrayList();
190
191 if (values == null || values.isEmpty())
192 {
193
194 result.add(buildInCriteria(attribute, negative, values));
195 }
196 else
197 {
198 Iterator iter = values.iterator();
199
200 while (iter.hasNext())
201 {
202 inCollection.add(iter.next());
203 if (inCollection.size() == inLimit || !iter.hasNext())
204 {
205 result.add(buildInCriteria(attribute, negative, inCollection));
206 inCollection = new ArrayList();
207 }
208 }
209 }
210 return result;
211 }
212
213 private InCriteria buildInCriteria(Object attribute, boolean negative, Collection values)
214 {
215 if (negative)
216 {
217
218
219 return ValueCriteria.buildNotInCriteria(attribute, values, getUserAlias(attribute));
220 }
221 else
222 {
223
224
225 return ValueCriteria.buildInCriteria(attribute, values, getUserAlias(attribute));
226 }
227 }
228
229
230
231
232
233 public Enumeration getElements()
234 {
235 return getCriteria().elements();
236 }
237
238
239
240
241
242 protected Vector getCriteria()
243 {
244 return m_criteria;
245 }
246
247
248
249
250
251 public int getType()
252 {
253 return m_type;
254 }
255
256
257
258
259
260 public void setType(int type)
261 {
262 m_type = type;
263 }
264
265
266
267
268
269 public boolean isEmbraced()
270 {
271 return m_embraced;
272 }
273
274
275
276
277
278 public void setEmbraced(boolean embraced)
279 {
280 m_embraced = embraced;
281 }
282
283
284
285
286
287
288
289
290 public void addEqualTo(String attribute, Object value)
291 {
292
293
294 addSelectionCriteria(ValueCriteria.buildEqualToCriteria(attribute, value, getUserAlias(attribute)));
295 }
296
297
298
299
300
301
302
303
304
305 public void addColumnEqualTo(String column, Object value)
306 {
307
308
309 SelectionCriteria c = ValueCriteria.buildEqualToCriteria(column, value, getUserAlias(column));
310 c.setTranslateAttribute(false);
311 addSelectionCriteria(c);
312 }
313
314
315
316
317
318
319
320
321
322 public void addColumnEqualToField(String column, Object fieldName)
323 {
324
325
326 SelectionCriteria c = FieldCriteria.buildEqualToCriteria(column, fieldName, getUserAlias(column));
327 c.setTranslateAttribute(false);
328 addSelectionCriteria(c);
329 }
330
331
332
333
334
335
336
337
338
339
340 public void addEqualToField(String attribute, String fieldName)
341 {
342
343
344 FieldCriteria c = FieldCriteria.buildEqualToCriteria(attribute, fieldName, getUserAlias(attribute));
345 addSelectionCriteria(c);
346 }
347
348
349
350
351
352
353
354
355
356
357 public void addNotEqualToField(String attribute, String fieldName)
358 {
359
360
361 SelectionCriteria c = FieldCriteria.buildNotEqualToCriteria(attribute, fieldName, getUserAlias(attribute));
362 addSelectionCriteria(c);
363 }
364
365
366
367
368
369
370
371
372
373
374 public void addNotEqualToColumn(String attribute, String colName)
375 {
376
377
378 FieldCriteria c = FieldCriteria.buildNotEqualToCriteria(attribute, colName, getUserAlias(attribute));
379 c.setTranslateField(false);
380 addSelectionCriteria(c);
381 }
382
383
384
385
386
387
388
389
390
391
392 public void addEqualToColumn(String attribute, String colName)
393 {
394
395 FieldCriteria c = FieldCriteria.buildEqualToCriteria(attribute, colName, getUserAlias(attribute));
396 c.setTranslateField(false);
397 addSelectionCriteria(c);
398 }
399
400
401
402
403
404
405
406
407 public void addGreaterOrEqualThan(Object attribute, Object value)
408 {
409
410
411 addSelectionCriteria(ValueCriteria.buildNotLessCriteria(attribute, value, getUserAlias(attribute)));
412 }
413
414
415
416
417
418
419
420
421 public void addGreaterOrEqualThanField(String attribute, Object value)
422 {
423
424
425 addSelectionCriteria(FieldCriteria.buildNotLessCriteria(attribute, value, getUserAlias(attribute)));
426 }
427
428
429
430
431
432
433
434
435 public void addLessOrEqualThan(Object attribute, Object value)
436 {
437
438
439 addSelectionCriteria(ValueCriteria.buildNotGreaterCriteria(attribute, value, getUserAlias(attribute)));
440 }
441
442
443
444
445
446
447
448
449 public void addLessOrEqualThanField(String attribute, Object value)
450 {
451
452
453 addSelectionCriteria(FieldCriteria.buildNotGreaterCriteria(attribute, value, getUserAlias(attribute)));
454 }
455
456
457
458
459
460
461
462
463
464 public void addLike(Object attribute, Object value)
465 {
466
467
468 addSelectionCriteria(ValueCriteria.buildLikeCriteria(attribute, value, getUserAlias(attribute)));
469 }
470
471
472
473
474
475
476
477
478
479 public void addNotLike(String attribute, Object value)
480 {
481
482
483 addSelectionCriteria(ValueCriteria.buildNotLikeCriteria(attribute, value, getUserAlias(attribute)));
484 }
485
486
487
488
489
490
491
492
493 public void addNotEqualTo(Object attribute, Object value)
494 {
495
496
497 addSelectionCriteria(ValueCriteria.buildNotEqualToCriteria(attribute, value, getUserAlias(attribute)));
498 }
499
500
501
502
503
504
505
506
507 public void addGreaterThan(Object attribute, Object value)
508 {
509
510
511 addSelectionCriteria(ValueCriteria.buildGreaterCriteria(attribute, value, getUserAlias(attribute)));
512 }
513
514
515
516
517
518
519
520
521 public void addGreaterThanField(String attribute, Object value)
522 {
523
524
525 addSelectionCriteria(FieldCriteria.buildGreaterCriteria(attribute, value, getUserAlias(attribute)));
526 }
527
528
529
530
531
532
533
534
535 public void addLessThan(Object attribute, Object value)
536 {
537
538
539 addSelectionCriteria(ValueCriteria.buildLessCriteria(attribute, value, getUserAlias(attribute)));
540 }
541
542
543
544
545
546
547
548
549 public void addLessThanField(String attribute, Object value)
550 {
551
552
553 addSelectionCriteria(FieldCriteria.buildLessCriteria(attribute, value, getUserAlias(attribute)));
554 }
555
556
557
558
559
560
561 public void addOrderBy(String fieldName)
562 {
563 addOrderBy(fieldName, true);
564 }
565
566
567
568
569
570
571
572 public void addOrderBy(String fieldName, boolean sortAscending)
573 {
574 if (fieldName != null)
575 {
576 _getOrderby().add(new FieldHelper(fieldName, sortAscending));
577 }
578 }
579
580
581
582
583
584
585 public void addOrderBy(FieldHelper aField)
586 {
587 if (aField != null)
588 {
589 _getOrderby().add(aField);
590 }
591 }
592
593
594
595
596
597
598 public void addOrderByAscending(String fieldName)
599 {
600 addOrderBy(fieldName, true);
601 }
602
603
604
605
606
607
608 public void addOrderByDescending(String fieldName)
609 {
610 addOrderBy(fieldName, false);
611 }
612
613
614
615
616
617
618 List getOrderby()
619 {
620 List result = _getOrderby();
621 Iterator iter = getCriteria().iterator();
622 Object crit;
623
624 while (iter.hasNext())
625 {
626 crit = iter.next();
627 if (crit instanceof Criteria)
628 {
629 result.addAll(((Criteria) crit).getOrderby());
630 }
631 }
632
633 return result;
634 }
635
636
637
638
639
640
641 protected List _getOrderby()
642 {
643 return orderby;
644 }
645
646
647
648
649
650
651
652
653 public void addOrCriteria(Criteria pc)
654 {
655 if (!m_criteria.isEmpty())
656 {
657 pc.setEmbraced(true);
658 pc.setType(OR);
659 addCriteria(pc);
660 }
661 else
662 {
663 setEmbraced(false);
664 pc.setType(OR);
665 addCriteria(pc);
666 }
667 }
668
669
670
671
672
673
674
675 public void addIsNull(String attribute)
676 {
677
678
679 addSelectionCriteria(ValueCriteria.buildNullCriteria(attribute, getUserAlias(attribute)));
680 }
681
682
683
684
685
686
687
688
689 public void addColumnIsNull(String column)
690 {
691
692
693 SelectionCriteria c = ValueCriteria.buildNullCriteria(column, getUserAlias(column));
694 c.setTranslateAttribute(false);
695 addSelectionCriteria(c);
696 }
697
698
699
700
701
702
703
704 public void addNotNull(String attribute)
705 {
706
707
708 addSelectionCriteria(ValueCriteria.buildNotNullCriteria(attribute, getUserAlias(attribute)));
709 }
710
711
712
713
714
715
716
717
718 public void addColumnNotNull(String column)
719 {
720
721
722 SelectionCriteria c = ValueCriteria.buildNotNullCriteria(column, getUserAlias(column));
723 c.setTranslateAttribute(false);
724 addSelectionCriteria(c);
725 }
726
727
728
729
730
731
732
733
734
735 public void addBetween(Object attribute, Object value1, Object value2)
736 {
737
738
739 addSelectionCriteria(ValueCriteria.buildBeweenCriteria(attribute, value1, value2, getUserAlias(attribute)));
740 }
741
742
743
744
745
746
747
748
749
750 public void addNotBetween(Object attribute, Object value1, Object value2)
751 {
752
753
754 addSelectionCriteria(ValueCriteria.buildNotBeweenCriteria(attribute, value1, value2, getUserAlias(attribute)));
755 }
756
757
758
759
760
761
762
763
764
765
766 public void addIn(String attribute, Collection values)
767 {
768 List list = splitInCriteria(attribute, values, false, IN_LIMIT);
769 int index = 0;
770 InCriteria inCrit;
771 Criteria allInCritaria;
772
773 inCrit = (InCriteria) list.get(index);
774 allInCritaria = new Criteria(inCrit);
775
776 for (index = 1; index < list.size(); index++)
777 {
778 inCrit = (InCriteria) list.get(index);
779 allInCritaria.addOrCriteria(new Criteria(inCrit));
780 }
781
782 addAndCriteria(allInCritaria);
783 }
784
785
786
787
788
789
790
791
792
793
794
795 public void addColumnIn(String column, Collection values)
796 {
797 List list = splitInCriteria(column, values, false, IN_LIMIT);
798 int index = 0;
799 InCriteria inCrit;
800 Criteria allInCritaria;
801
802 inCrit = (InCriteria) list.get(index);
803 inCrit.setTranslateAttribute(false);
804 allInCritaria = new Criteria(inCrit);
805
806 for (index = 1; index < list.size(); index++)
807 {
808 inCrit = (InCriteria) list.get(index);
809 inCrit.setTranslateAttribute(false);
810 allInCritaria.addOrCriteria(new Criteria(inCrit));
811 }
812
813 addAndCriteria(allInCritaria);
814 }
815
816
817
818
819
820
821
822
823
824
825 public void addNotIn(String attribute, Collection values)
826 {
827 List list = splitInCriteria(attribute, values, true, IN_LIMIT);
828 InCriteria inCrit;
829 for (int index = 0; index < list.size(); index++)
830 {
831 inCrit = (InCriteria) list.get(index);
832 addSelectionCriteria(inCrit);
833 }
834 }
835
836
837
838
839
840
841 public void addIn(Object attribute, Query subQuery)
842 {
843
844
845 addSelectionCriteria(ValueCriteria.buildInCriteria(attribute, subQuery, getUserAlias(attribute)));
846 }
847
848
849
850
851
852
853 public void addNotIn(String attribute, Query subQuery)
854 {
855
856
857 addSelectionCriteria(ValueCriteria.buildNotInCriteria(attribute, subQuery, getUserAlias(attribute)));
858 }
859
860
861
862
863
864
865
866 public void addSql(String anSqlStatment)
867 {
868 addSelectionCriteria(new SqlCriteria(anSqlStatment));
869 }
870
871
872
873
874
875
876 public void addAndCriteria(Criteria pc)
877 {
878
879
880 if (!m_criteria.isEmpty())
881 {
882 this.setEmbraced(true);
883 pc.setEmbraced(true);
884 pc.setType(AND);
885 addCriteria(pc);
886 }
887 else
888 {
889 setEmbraced(false);
890 pc.setType(AND);
891 addCriteria(pc);
892 }
893 }
894
895
896
897
898
899
900 public void addExists(Query subQuery)
901 {
902 addSelectionCriteria(new ExistsCriteria(subQuery, false));
903 }
904
905
906
907
908
909
910 public void addNotExists(Query subQuery)
911 {
912 addSelectionCriteria(new ExistsCriteria(subQuery, true));
913 }
914
915
916
917
918
919 public boolean isEmpty()
920 {
921 return m_criteria.isEmpty();
922 }
923
924
925
926
927
928
929 List getGroupby()
930 {
931 List result = _getGroupby();
932 Iterator iter = getCriteria().iterator();
933 Object crit;
934
935 while (iter.hasNext())
936 {
937 crit = iter.next();
938 if (crit instanceof Criteria)
939 {
940 result.addAll(((Criteria) crit).getGroupby());
941 }
942 }
943
944 return result;
945 }
946
947
948
949
950
951
952 protected List _getGroupby()
953 {
954 return groupby;
955 }
956
957
958
959
960
961
962 public void addGroupBy(String fieldName)
963 {
964 if (fieldName != null)
965 {
966 _getGroupby().add(new FieldHelper(fieldName, false));
967 }
968 }
969
970
971
972
973
974
975 public void addGroupBy(FieldHelper aField)
976 {
977 if (aField != null)
978 {
979 _getGroupby().add(aField);
980 }
981 }
982
983
984
985
986
987
988 public void addGroupBy(String[] fieldNames)
989 {
990 for (int i = 0; i < fieldNames.length; i++)
991 {
992 addGroupBy(fieldNames[i]);
993 }
994 }
995
996
997
998
999
1000 List getPrefetchedRelationships()
1001 {
1002 return prefetchedRelationships;
1003 }
1004
1005
1006
1007
1008
1009
1010 public void addPrefetchedRelationship(String aName)
1011 {
1012 getPrefetchedRelationships().add(aName);
1013 }
1014
1015
1016
1017
1018 private static int getSqlInLimit()
1019 {
1020 try
1021 {
1022 PersistenceBrokerConfiguration config = (PersistenceBrokerConfiguration) PersistenceBrokerFactory
1023 .getConfigurator().getConfigurationFor(null);
1024 return config.getSqlInLimit();
1025 }
1026 catch (ConfigurationException e)
1027 {
1028 return 200;
1029 }
1030 }
1031
1032
1033
1034
1035 public String getAlias()
1036 {
1037 return m_alias;
1038 }
1039
1040
1041
1042
1043
1044 public UserAlias getUserAlias()
1045 {
1046 return m_userAlias;
1047 }
1048
1049
1050
1051
1052
1053
1054
1055 private UserAlias getUserAlias(Object attribute)
1056 {
1057 if (m_userAlias != null)
1058 {
1059 return m_userAlias;
1060 }
1061 if (!(attribute instanceof String))
1062 {
1063 return null;
1064 }
1065 if (m_alias == null)
1066 {
1067 return null;
1068 }
1069 if (m_aliasPath == null)
1070 {
1071 boolean allPathsAliased = true;
1072 return new UserAlias(m_alias, (String)attribute, allPathsAliased);
1073 }
1074 return new UserAlias(m_alias, (String)attribute, m_aliasPath);
1075 }
1076
1077
1078
1079
1080
1081
1082 public void setAlias(String alias)
1083 {
1084 if (alias == null || alias.trim().equals(""))
1085 {
1086 m_alias = null;
1087 }
1088 else
1089 {
1090 m_alias = alias;
1091 }
1092
1093
1094 for (int i = 0; i < m_criteria.size(); i++)
1095 {
1096 if (!(m_criteria.elementAt(i) instanceof Criteria))
1097 {
1098 ((SelectionCriteria) m_criteria.elementAt(i)).setAlias(m_alias);
1099 }
1100 }
1101 }
1102
1103
1104
1105
1106
1107
1108
1109 public void setAlias(String alias, String aliasPath)
1110 {
1111 if (alias == null || alias.trim().equals(""))
1112 {
1113 m_alias = null;
1114 }
1115 else
1116 {
1117 m_alias = alias;
1118 m_aliasPath = aliasPath;
1119 }
1120
1121
1122 for (int i = 0; i < m_criteria.size(); i++)
1123 {
1124 if (!(m_criteria.elementAt(i) instanceof Criteria))
1125 {
1126 ((SelectionCriteria) m_criteria.elementAt(i)).setAlias(m_alias, aliasPath);
1127 }
1128 }
1129 }
1130
1131
1132
1133
1134
1135
1136 public void setAlias(UserAlias userAlias)
1137 {
1138 m_alias = userAlias.getName();
1139
1140
1141 for (int i = 0; i < m_criteria.size(); i++)
1142 {
1143 if (!(m_criteria.elementAt(i) instanceof Criteria))
1144 {
1145 ((SelectionCriteria) m_criteria.elementAt(i)).setAlias(userAlias);
1146 }
1147 }
1148 }
1149
1150
1151
1152
1153
1154 public QueryByCriteria getQuery()
1155 {
1156 if (getParentCriteria() != null)
1157 {
1158 return getParentCriteria().getQuery();
1159 }
1160 else
1161 {
1162 return m_query;
1163 }
1164
1165 }
1166
1167
1168
1169
1170 void setQuery(QueryByCriteria query)
1171 {
1172 m_query = query;
1173 }
1174
1175
1176
1177
1178 public Criteria getParentCriteria()
1179 {
1180 return m_parentCriteria;
1181 }
1182
1183
1184
1185
1186 void setParentCriteria(Criteria criteria)
1187 {
1188 m_parentCriteria = criteria;
1189 }
1190
1191
1192
1193
1194 public String toString()
1195 {
1196 if (isNegative())
1197 {
1198 return "-" + m_criteria.toString();
1199 }
1200 else
1201 {
1202 return m_criteria.toString();
1203 }
1204 }
1205
1206
1207
1208
1209 public boolean isNegative()
1210 {
1211 return m_negative;
1212 }
1213
1214
1215
1216
1217
1218 public void setNegative(boolean negative)
1219 {
1220 m_negative = negative;
1221 }
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234 public void addPathClass(String aPath, Class aClass)
1235 {
1236 List pathClasses = (List) m_pathClasses.get(aPath);
1237 if(pathClasses == null)
1238 {
1239 setPathClass(aPath,aClass);
1240 }
1241 else
1242 {
1243 pathClasses.add(aClass);
1244
1245 }
1246 }
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260 public void setPathClass(String aPath, Class aClass)
1261 {
1262 List pathClasses = new ArrayList(1);
1263 pathClasses.add(aClass);
1264 m_pathClasses.put(aPath, pathClasses);
1265 }
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275 public List getClassesForPath(String aPath)
1276 {
1277 return (List)getPathClasses().get(aPath);
1278 }
1279
1280
1281
1282
1283
1284
1285
1286
1287 public Map getPathClasses()
1288 {
1289 if (m_pathClasses.isEmpty())
1290 {
1291 if (m_parentCriteria == null)
1292 {
1293 if (m_query == null)
1294 {
1295 return m_pathClasses;
1296 }
1297 else
1298 {
1299 return m_query.getPathClasses();
1300 }
1301 }
1302 else
1303 {
1304 return m_parentCriteria.getPathClasses();
1305 }
1306 }
1307 else
1308 {
1309 return m_pathClasses;
1310 }
1311 }
1312
1313
1314
1315 }