1 package org.apache.ojb.broker.query; 2 3 17 18 import java.util.Collection ; 19 import java.util.HashSet ; 20 import java.util.Vector ; 21 22 import org.apache.ojb.broker.metadata.ClassDescriptor; 23 import org.apache.ojb.broker.metadata.DescriptorRepository; 24 import org.apache.ojb.broker.metadata.FieldDescriptor; 25 import org.apache.ojb.broker.metadata.MetadataManager; 26 27 32 public final class QueryFactory 33 { 34 private static DescriptorRepository getRepository() 35 { 36 return MetadataManager.getInstance().getRepository(); 37 } 38 39 46 public static ReportQueryByCriteria newReportQuery(Class classToSearchFrom, String [] columns, Criteria criteria, boolean distinct) 47 { 48 criteria = addCriteriaForOjbConcreteClasses(getRepository().getDescriptorFor(classToSearchFrom), criteria); 49 return new ReportQueryByCriteria(classToSearchFrom, columns, criteria, distinct); 50 } 51 52 59 public static ReportQueryByCriteria newReportQuery(Class classToSearchFrom, Criteria criteria, boolean distinct) 60 { 61 criteria = addCriteriaForOjbConcreteClasses(getRepository().getDescriptorFor(classToSearchFrom), criteria); 62 return newReportQuery(classToSearchFrom, null, criteria, distinct); 63 } 64 65 71 public static ReportQueryByCriteria newReportQuery(Class classToSearchFrom, Criteria criteria) 72 { 73 return newReportQuery(classToSearchFrom, criteria, false); 74 } 75 76 83 public static QueryByCriteria newQuery(Class classToSearchFrom, Criteria criteria, boolean distinct) 84 { 85 criteria = addCriteriaForOjbConcreteClasses(getRepository().getDescriptorFor(classToSearchFrom), criteria); 86 return new QueryByCriteria(classToSearchFrom, criteria, distinct); 87 } 88 89 95 public static QueryByCriteria newQuery(Class classToSearchFrom, Criteria criteria) 96 { 97 return newQuery(classToSearchFrom, criteria, false); 98 } 99 100 105 public static QueryByIdentity newQuery(Object example_or_identity) 106 { 107 return newQueryByIdentity(example_or_identity); 108 } 109 110 115 public static QueryByIdentity newQueryByIdentity(Object example_or_identity) 116 { 117 return new QueryByIdentity(example_or_identity); 118 } 119 120 126 public static QueryByCriteria newQueryByExample(Object example) 127 { 128 return new QueryByCriteria(example); 129 } 130 131 138 public static QueryByMtoNCriteria newQuery(Class classToSearchFrom, String indirectionTable, Criteria criteria, boolean distinct) 139 { 140 criteria = addCriteriaForOjbConcreteClasses(getRepository().getDescriptorFor(classToSearchFrom), criteria); 141 return new QueryByMtoNCriteria(classToSearchFrom, indirectionTable, criteria, distinct); 142 } 143 144 150 public static QueryByCriteria newQuery(Class classToSearchFrom, String indirectionTable, Criteria criteria) 151 { 152 criteria = addCriteriaForOjbConcreteClasses(getRepository().getDescriptorFor(classToSearchFrom), criteria); 153 return new QueryByMtoNCriteria(classToSearchFrom, indirectionTable, criteria); 154 } 155 156 162 public static QueryBySQL newQuery(Class classToSearchFrom, String anSqlStatement) 163 { 164 return new QueryBySQL(classToSearchFrom, anSqlStatement); 165 } 166 167 174 private static Collection getExtentClasses(ClassDescriptor cld) 175 { 176 179 FieldDescriptor fd = cld.getFieldDescriptorByName(ClassDescriptor.OJB_CONCRETE_CLASS); 180 Collection classes = new HashSet (); if (fd != null) 182 { 183 classes.add(cld.getClassOfObject().getName()); 184 } 185 186 189 if (cld.isExtent()) 190 { 191 Vector extentClasses = cld.getExtentClasses(); 192 193 196 for (int i = 0; i < extentClasses.size(); i++) 197 { 198 Class ec = (Class ) extentClasses.get(i); 199 ClassDescriptor extCld = cld.getRepository().getDescriptorFor(ec); 200 classes.addAll(getExtentClasses(extCld)); 201 } 202 } 203 204 return classes; 205 } 206 207 216 private static Criteria addCriteriaForOjbConcreteClasses(ClassDescriptor cld, Criteria crit) 217 { 218 221 Criteria concreteClassDiscriminator = null; 222 Collection classes = getExtentClasses(cld); 223 224 227 if (!classes.isEmpty()) 228 { 229 concreteClassDiscriminator = new Criteria(); 230 if (classes.size() > 1) 231 { 232 concreteClassDiscriminator = new Criteria(); 233 concreteClassDiscriminator.addIn(ClassDescriptor.OJB_CONCRETE_CLASS, classes); 234 } 235 else 236 { 237 concreteClassDiscriminator.addEqualTo(ClassDescriptor.OJB_CONCRETE_CLASS, classes.toArray()[0]); 238 } 239 } 240 241 245 if (concreteClassDiscriminator != null) 246 { 247 252 if (crit == null) 253 { 254 crit = new Criteria(); 255 } 256 257 crit.addAndCriteria(concreteClassDiscriminator); 258 } 259 262 return crit; 263 } 264 265 } 266 | Popular Tags |