| 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.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 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public final class QueryFactory |
| 33 | |
{ |
| 34 | |
private static DescriptorRepository getRepository() |
| 35 | |
{ |
| 36 | |
return MetadataManager.getInstance().getRepository(); |
| 37 | |
} |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 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 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 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 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public static ReportQueryByCriteria newReportQuery(Class classToSearchFrom, Criteria criteria) |
| 72 | |
{ |
| 73 | |
return newReportQuery(classToSearchFrom, criteria, false); |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 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 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public static QueryByCriteria newQuery(Class classToSearchFrom, Criteria criteria) |
| 96 | |
{ |
| 97 | |
return newQuery(classToSearchFrom, criteria, false); |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public static QueryByIdentity newQuery(Object example_or_identity) |
| 106 | |
{ |
| 107 | |
return newQueryByIdentity(example_or_identity); |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public static QueryByIdentity newQueryByIdentity(Object example_or_identity) |
| 116 | |
{ |
| 117 | |
return new QueryByIdentity(example_or_identity); |
| 118 | |
} |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public static QueryByCriteria newQueryByExample(Object example) |
| 127 | |
{ |
| 128 | |
return new QueryByCriteria(example); |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 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 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 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 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
public static QueryBySQL newQuery(Class classToSearchFrom, String anSqlStatement) |
| 163 | |
{ |
| 164 | |
return new QueryBySQL(classToSearchFrom, anSqlStatement); |
| 165 | |
} |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
private static Collection getExtentClasses(ClassDescriptor cld) |
| 175 | |
{ |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
FieldDescriptor fd = cld.getFieldDescriptorByName(ClassDescriptor.OJB_CONCRETE_CLASS); |
| 180 | |
Collection classes = new HashSet(); |
| 181 | |
if (fd != null) |
| 182 | |
{ |
| 183 | |
classes.add(cld.getClassOfObject().getName()); |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
if (cld.isExtent()) |
| 190 | |
{ |
| 191 | |
Vector extentClasses = cld.getExtentClasses(); |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 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 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
private static Criteria addCriteriaForOjbConcreteClasses(ClassDescriptor cld, Criteria crit) |
| 217 | |
{ |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
Criteria concreteClassDiscriminator = null; |
| 222 | |
Collection classes = getExtentClasses(cld); |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 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 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
if (concreteClassDiscriminator != null) |
| 246 | |
{ |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
if (crit == null) |
| 253 | |
{ |
| 254 | |
crit = new Criteria(); |
| 255 | |
} |
| 256 | |
|
| 257 | |
crit.addAndCriteria(concreteClassDiscriminator); |
| 258 | |
} |
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
return crit; |
| 263 | |
} |
| 264 | |
|
| 265 | |
} |