1 19 20 package org.apache.cayenne.query; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import org.apache.cayenne.access.jdbc.ColumnDescriptor; 30 import org.apache.cayenne.access.jdbc.RowDescriptor; 31 import org.apache.cayenne.map.EntityResolver; 32 import org.apache.cayenne.map.Procedure; 33 import org.apache.cayenne.map.QueryBuilder; 34 import org.apache.cayenne.util.XMLEncoder; 35 import org.apache.cayenne.util.XMLSerializable; 36 37 60 public class ProcedureQuery extends AbstractQuery implements ParameterizedQuery, 61 XMLSerializable { 62 63 66 protected String resultEntityName; 67 68 71 protected Class resultClass; 72 73 protected Map parameters = new HashMap (); 74 75 ProcedureQueryMetadata metaData = new ProcedureQueryMetadata(); 76 77 82 protected List resultDescriptors; 83 84 87 public ProcedureQuery() { 88 metaData.setFetchingDataRows(true); 90 } 91 92 95 public ProcedureQuery(Procedure procedure) { 96 metaData.setFetchingDataRows(true); 98 setRoot(procedure); 99 } 100 101 114 public ProcedureQuery(String procedureName) { 115 metaData.setFetchingDataRows(true); 117 118 setRoot(procedureName); 119 } 120 121 124 public ProcedureQuery(Procedure procedure, Class resultType) { 125 setRoot(procedure); 126 127 this.resultClass = resultType; 128 } 129 130 141 public ProcedureQuery(String procedureName, Class resultType) { 142 setRoot(procedureName); 143 144 this.resultClass = resultType; 145 } 146 147 150 public QueryMetadata getMetaData(EntityResolver resolver) { 151 152 metaData.resolve(root, resultClass != null 153 ? (Object ) resultClass 154 : resultEntityName, resolver, this); 155 return metaData; 156 } 157 158 169 public List getResultDescriptors() { 170 return resultDescriptors != null ? resultDescriptors : Collections.EMPTY_LIST; 171 } 172 173 180 public synchronized void addResultDescriptor(ColumnDescriptor[] descriptor) { 181 if (resultDescriptors == null) { 182 resultDescriptors = new ArrayList (2); 183 } 184 185 resultDescriptors.add(descriptor); 186 } 187 188 193 public void removeResultDescriptor(ColumnDescriptor[] descriptor) { 194 if (resultDescriptors != null) { 195 resultDescriptors.remove(descriptor); 196 } 197 } 198 199 204 public SQLAction createSQLAction(SQLActionVisitor visitor) { 205 return visitor.procedureAction(this); 206 } 207 208 213 public void initWithProperties(Map properties) { 214 215 if (properties == null) { 217 properties = Collections.EMPTY_MAP; 218 } 219 220 metaData.initWithProperties(properties); 221 } 222 223 228 public void encodeAsXML(XMLEncoder encoder) { 229 encoder.print("<query name=\""); 230 encoder.print(getName()); 231 encoder.print("\" factory=\""); 232 encoder.print("org.apache.cayenne.map.ProcedureQueryBuilder"); 233 234 encoder.print("\" root=\""); 235 encoder.print(QueryBuilder.PROCEDURE_ROOT); 236 237 String rootString = null; 238 239 if (root instanceof String ) { 240 rootString = root.toString(); 241 } 242 else if (root instanceof Procedure) { 243 rootString = ((Procedure) root).getName(); 244 } 245 246 if (rootString != null) { 247 encoder.print("\" root-name=\""); 248 encoder.print(rootString); 249 } 250 251 if (resultEntityName != null) { 252 encoder.print("\" result-entity=\""); 253 encoder.print(resultEntityName); 254 } 255 256 encoder.println("\">"); 257 encoder.indent(1); 258 259 metaData.encodeAsXML(encoder); 260 261 encoder.indent(-1); 262 encoder.println("</query>"); 263 } 264 265 271 public Query createQuery(Map parameters) { 272 ProcedureQuery query = new ProcedureQuery(); 274 275 if (root != null) { 276 query.setRoot(root); 277 } 278 279 query.setResultEntityName(resultEntityName); 280 query.metaData.copyFromInfo(this.metaData); 281 query.setParameters(parameters); 282 283 return query; 287 } 288 289 public String getCachePolicy() { 290 return metaData.getCachePolicy(); 291 } 292 293 public void setCachePolicy(String policy) { 294 this.metaData.setCachePolicy(policy); 295 } 296 297 300 public String [] getCacheGroups() { 301 return metaData.getCacheGroups(); 302 } 303 304 307 public void setCacheGroups(String [] cachGroups) { 308 this.metaData.setCacheGroups(cachGroups); 309 } 310 311 public int getFetchLimit() { 312 return metaData.getFetchLimit(); 313 } 314 315 public void setFetchLimit(int fetchLimit) { 316 this.metaData.setFetchLimit(fetchLimit); 317 } 318 319 public int getPageSize() { 320 return metaData.getPageSize(); 321 } 322 323 public void setPageSize(int pageSize) { 324 metaData.setPageSize(pageSize); 325 } 326 327 public void setFetchingDataRows(boolean flag) { 328 metaData.setFetchingDataRows(flag); 329 } 330 331 public boolean isFetchingDataRows() { 332 return metaData.isFetchingDataRows(); 333 } 334 335 public boolean isRefreshingObjects() { 336 return metaData.isRefreshingObjects(); 337 } 338 339 public void setRefreshingObjects(boolean flag) { 340 metaData.setRefreshingObjects(flag); 341 } 342 343 public boolean isResolvingInherited() { 344 return metaData.isResolvingInherited(); 345 } 346 347 public void setResolvingInherited(boolean b) { 348 metaData.setResolvingInherited(b); 349 } 350 351 356 public synchronized void addParameter(String name, Object value) { 357 parameters.put(name, value); 358 } 359 360 363 public synchronized void removeParameter(String name) { 364 parameters.remove(name); 365 } 366 367 372 public Map getParameters() { 373 return parameters; 374 } 375 376 381 public synchronized void setParameters(Map parameters) { 382 this.parameters.clear(); 383 384 if (parameters != null) { 385 this.parameters.putAll(parameters); 386 } 387 } 388 389 394 public synchronized void clearParameters() { 395 this.parameters.clear(); 396 } 397 398 401 public PrefetchTreeNode getPrefetchTree() { 402 return metaData.getPrefetchTree(); 403 } 404 405 410 public PrefetchTreeNode addPrefetch(String prefetchPath) { 411 return metaData.addPrefetch( 413 prefetchPath, 414 PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS); 415 } 416 417 420 public void removePrefetch(String prefetch) { 421 metaData.removePrefetch(prefetch); 422 } 423 424 429 public void addPrefetches(Collection prefetches) { 430 metaData.addPrefetches(prefetches, PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS); 431 } 432 433 438 public void clearPrefetches() { 439 metaData.clearPrefetches(); 440 } 441 442 445 public String getResultEntityName() { 446 return resultEntityName; 447 } 448 449 452 public void setResultEntityName(String resultEntityName) { 453 this.resultEntityName = resultEntityName; 454 } 455 } 456 | Popular Tags |