1 19 20 package org.apache.cayenne.query; 21 22 import org.apache.commons.lang.builder.ToStringBuilder; 23 import org.apache.cayenne.CayenneRuntimeException; 24 import org.apache.cayenne.map.DataMap; 25 import org.apache.cayenne.map.DbEntity; 26 import org.apache.cayenne.map.EntityResolver; 27 import org.apache.cayenne.map.ObjEntity; 28 import org.apache.cayenne.map.Procedure; 29 30 35 public abstract class AbstractQuery implements Query { 36 37 41 protected Object root; 42 protected String name; 43 44 49 public String getName() { 50 return name; 51 } 52 53 58 public void setName(String name) { 59 this.name = name; 60 } 61 62 67 public QueryMetadata getMetaData(EntityResolver resolver) { 68 BaseQueryMetadata md = new BaseQueryMetadata(); 69 md.resolve(getRoot(), resolver, getName()); 70 return md; 71 } 72 73 76 public Object getRoot() { 77 return root; 78 } 79 80 87 public void setRoot(Object value) { 88 if (value == null) { 89 this.root = null; 90 } 91 92 if (!((value instanceof String ) 94 || (value instanceof ObjEntity) 95 || (value instanceof DbEntity) 96 || (value instanceof Class ) 97 || (value instanceof Procedure) || (value instanceof DataMap))) { 98 99 String rootClass = (value != null) ? value.getClass().getName() : "null"; 100 101 throw new IllegalArgumentException ( 102 getClass().getName() 103 + ": \"setRoot(..)\" takes a DataMap, String, ObjEntity, DbEntity, Procedure, " 104 + "or Class. It was passed a " 105 + rootClass); 106 } 107 108 this.root = value; 109 } 110 111 public String toString() { 112 return new ToStringBuilder(this) 113 .append("root", root) 114 .append("name", getName()) 115 .toString(); 116 } 117 118 121 public abstract SQLAction createSQLAction(SQLActionVisitor visitor); 122 123 130 public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) { 131 DataMap map = getMetaData(resolver).getDataMap(); 132 133 if (map == null) { 134 throw new CayenneRuntimeException("No DataMap found, can't route query " 135 + this); 136 } 137 138 router.route(router.engineForDataMap(map), this, substitutedQuery); 139 } 140 } 141 | Popular Tags |