1 package org.apache.ojb.broker.query; 2 3 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 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 private List orderby = null; 69 private List groupby = null; 70 private List prefetchedRelationships = null; 71 72 private String m_alias = null; 74 75 private String m_aliasPath = null; 78 79 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 private Map m_pathClasses; 91 92 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 m_pathClasses = new HashMap (); 105 } 106 107 111 public Criteria(SelectionCriteria aSelectionCriteria) 112 { 113 this(); 114 addSelectionCriteria(aSelectionCriteria); 115 } 116 117 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 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 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 return ValueCriteria.buildNotInCriteria(attribute, values, getUserAlias(attribute)); 220 } 221 else 222 { 223 return ValueCriteria.buildInCriteria(attribute, values, getUserAlias(attribute)); 226 } 227 } 228 229 233 public Enumeration getElements() 234 { 235 return getCriteria().elements(); 236 } 237 238 242 protected Vector getCriteria() 243 { 244 return m_criteria; 245 } 246 247 251 public int getType() 252 { 253 return m_type; 254 } 255 256 260 public void setType(int type) 261 { 262 m_type = type; 263 } 264 265 269 public boolean isEmbraced() 270 { 271 return m_embraced; 272 } 273 274 278 public void setEmbraced(boolean embraced) 279 { 280 m_embraced = embraced; 281 } 282 283 290 public void addEqualTo(String attribute, Object value) 291 { 292 addSelectionCriteria(ValueCriteria.buildEqualToCriteria(attribute, value, getUserAlias(attribute))); 295 } 296 297 305 public void addColumnEqualTo(String column, Object value) 306 { 307 SelectionCriteria c = ValueCriteria.buildEqualToCriteria(column, value, getUserAlias(column)); 310 c.setTranslateAttribute(false); 311 addSelectionCriteria(c); 312 } 313 314 322 public void addColumnEqualToField(String column, Object fieldName) 323 { 324 SelectionCriteria c = FieldCriteria.buildEqualToCriteria(column, fieldName, getUserAlias(column)); 327 c.setTranslateAttribute(false); 328 addSelectionCriteria(c); 329 } 330 331 340 public void addEqualToField(String attribute, String fieldName) 341 { 342 FieldCriteria c = FieldCriteria.buildEqualToCriteria(attribute, fieldName, getUserAlias(attribute)); 345 addSelectionCriteria(c); 346 } 347 348 357 public void addNotEqualToField(String attribute, String fieldName) 358 { 359 SelectionCriteria c = FieldCriteria.buildNotEqualToCriteria(attribute, fieldName, getUserAlias(attribute)); 362 addSelectionCriteria(c); 363 } 364 365 374 public void addNotEqualToColumn(String attribute, String colName) 375 { 376 FieldCriteria c = FieldCriteria.buildNotEqualToCriteria(attribute, colName, getUserAlias(attribute)); 379 c.setTranslateField(false); 380 addSelectionCriteria(c); 381 } 382 383 392 public void addEqualToColumn(String attribute, String colName) 393 { 394 FieldCriteria c = FieldCriteria.buildEqualToCriteria(attribute, colName, getUserAlias(attribute)); 396 c.setTranslateField(false); 397 addSelectionCriteria(c); 398 } 399 400 407 public void addGreaterOrEqualThan(Object attribute, Object value) 408 { 409 addSelectionCriteria(ValueCriteria.buildNotLessCriteria(attribute, value, getUserAlias(attribute))); 412 } 413 414 421 public void addGreaterOrEqualThanField(String attribute, Object value) 422 { 423 addSelectionCriteria(FieldCriteria.buildNotLessCriteria(attribute, value, getUserAlias(attribute))); 426 } 427 428 435 public void addLessOrEqualThan(Object attribute, Object value) 436 { 437 addSelectionCriteria(ValueCriteria.buildNotGreaterCriteria(attribute, value, getUserAlias(attribute))); 440 } 441 442 449 public void addLessOrEqualThanField(String attribute, Object value) 450 { 451 addSelectionCriteria(FieldCriteria.buildNotGreaterCriteria(attribute, value, getUserAlias(attribute))); 454 } 455 456 464 public void addLike(Object attribute, Object value) 465 { 466 addSelectionCriteria(ValueCriteria.buildLikeCriteria(attribute, value, getUserAlias(attribute))); 469 } 470 471 479 public void addNotLike(String attribute, Object value) 480 { 481 addSelectionCriteria(ValueCriteria.buildNotLikeCriteria(attribute, value, getUserAlias(attribute))); 484 } 485 486 493 public void addNotEqualTo(Object attribute, Object value) 494 { 495 addSelectionCriteria(ValueCriteria.buildNotEqualToCriteria(attribute, value, getUserAlias(attribute))); 498 } 499 500 507 public void addGreaterThan(Object attribute, Object value) 508 { 509 addSelectionCriteria(ValueCriteria.buildGreaterCriteria(attribute, value, getUserAlias(attribute))); 512 } 513 514 521 public void addGreaterThanField(String attribute, Object value) 522 { 523 addSelectionCriteria(FieldCriteria.buildGreaterCriteria(attribute, value, getUserAlias(attribute))); 526 } 527 528 535 public void addLessThan(Object attribute, Object value) 536 { 537 addSelectionCriteria(ValueCriteria.buildLessCriteria(attribute, value, getUserAlias(attribute))); 540 } 541 542 549 public void addLessThanField(String attribute, Object value) 550 { 551 addSelectionCriteria(FieldCriteria.buildLessCriteria(attribute, value, getUserAlias(attribute))); 554 } 555 556 561 public void addOrderBy(String fieldName) 562 { 563 addOrderBy(fieldName, true); 564 } 565 566 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 585 public void addOrderBy(FieldHelper aField) 586 { 587 if (aField != null) 588 { 589 _getOrderby().add(aField); 590 } 591 } 592 593 598 public void addOrderByAscending(String fieldName) 599 { 600 addOrderBy(fieldName, true); 601 } 602 603 608 public void addOrderByDescending(String fieldName) 609 { 610 addOrderBy(fieldName, false); 611 } 612 613 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 641 protected List _getOrderby() 642 { 643 return orderby; 644 } 645 646 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 675 public void addIsNull(String attribute) 676 { 677 addSelectionCriteria(ValueCriteria.buildNullCriteria(attribute, getUserAlias(attribute))); 680 } 681 682 689 public void addColumnIsNull(String column) 690 { 691 SelectionCriteria c = ValueCriteria.buildNullCriteria(column, getUserAlias(column)); 694 c.setTranslateAttribute(false); 695 addSelectionCriteria(c); 696 } 697 698 704 public void addNotNull(String attribute) 705 { 706 addSelectionCriteria(ValueCriteria.buildNotNullCriteria(attribute, getUserAlias(attribute))); 709 } 710 711 718 public void addColumnNotNull(String column) 719 { 720 SelectionCriteria c = ValueCriteria.buildNotNullCriteria(column, getUserAlias(column)); 723 c.setTranslateAttribute(false); 724 addSelectionCriteria(c); 725 } 726 727 735 public void addBetween(Object attribute, Object value1, Object value2) 736 { 737 addSelectionCriteria(ValueCriteria.buildBeweenCriteria(attribute, value1, value2, getUserAlias(attribute))); 740 } 741 742 750 public void addNotBetween(Object attribute, Object value1, Object value2) 751 { 752 addSelectionCriteria(ValueCriteria.buildNotBeweenCriteria(attribute, value1, value2, getUserAlias(attribute))); 755 } 756 757 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 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 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 841 public void addIn(Object attribute, Query subQuery) 842 { 843 addSelectionCriteria(ValueCriteria.buildInCriteria(attribute, subQuery, getUserAlias(attribute))); 846 } 847 848 853 public void addNotIn(String attribute, Query subQuery) 854 { 855 addSelectionCriteria(ValueCriteria.buildNotInCriteria(attribute, subQuery, getUserAlias(attribute))); 858 } 859 860 866 public void addSql(String anSqlStatment) 867 { 868 addSelectionCriteria(new SqlCriteria(anSqlStatment)); 869 } 870 871 876 public void addAndCriteria(Criteria pc) 877 { 878 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 900 public void addExists(Query subQuery) 901 { 902 addSelectionCriteria(new ExistsCriteria(subQuery, false)); 903 } 904 905 910 public void addNotExists(Query subQuery) 911 { 912 addSelectionCriteria(new ExistsCriteria(subQuery, true)); 913 } 914 915 919 public boolean isEmpty() 920 { 921 return m_criteria.isEmpty(); 922 } 923 924 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 952 protected List _getGroupby() 953 { 954 return groupby; 955 } 956 957 962 public void addGroupBy(String fieldName) 963 { 964 if (fieldName != null) 965 { 966 _getGroupby().add(new FieldHelper(fieldName, false)); 967 } 968 } 969 970 975 public void addGroupBy(FieldHelper aField) 976 { 977 if (aField != null) 978 { 979 _getGroupby().add(aField); 980 } 981 } 982 983 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 1000 List getPrefetchedRelationships() 1001 { 1002 return prefetchedRelationships; 1003 } 1004 1005 1010 public void addPrefetchedRelationship(String aName) 1011 { 1012 getPrefetchedRelationships().add(aName); 1013 } 1014 1015 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 1035 public String getAlias() 1036 { 1037 return m_alias; 1038 } 1039 1040 1043 public UserAlias getUserAlias() 1045 { 1046 return m_userAlias; 1047 } 1048 1049 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 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 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 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 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 1136 public void setAlias(UserAlias userAlias) 1137 { 1138 m_alias = userAlias.getName(); 1139 1140 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 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 1170 void setQuery(QueryByCriteria query) 1171 { 1172 m_query = query; 1173 } 1174 1175 1178 public Criteria getParentCriteria() 1179 { 1180 return m_parentCriteria; 1181 } 1182 1183 1186 void setParentCriteria(Criteria criteria) 1187 { 1188 m_parentCriteria = criteria; 1189 } 1190 1191 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 1209 public boolean isNegative() 1210 { 1211 return m_negative; 1212 } 1213 1214 1218 public void setNegative(boolean negative) 1219 { 1220 m_negative = negative; 1221 } 1222 1223 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 } 1246 } 1247 1248 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 1275 public List getClassesForPath(String aPath) 1276 { 1277 return (List )getPathClasses().get(aPath); 1278 } 1279 1280 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} | Popular Tags |