1 56 57 package org.objectstyle.cayenne.query; 58 59 import org.apache.commons.lang.builder.ToStringBuilder; 60 import org.apache.log4j.Level; 61 import org.objectstyle.cayenne.CayenneRuntimeException; 62 import org.objectstyle.cayenne.map.DataMap; 63 import org.objectstyle.cayenne.map.DbEntity; 64 import org.objectstyle.cayenne.map.EntityResolver; 65 import org.objectstyle.cayenne.map.ObjEntity; 66 import org.objectstyle.cayenne.map.Procedure; 67 68 73 public abstract class AbstractQuery implements Query { 74 75 79 protected Object root; 80 protected String name; 81 82 protected Level logLevel = DEFAULT_LOG_LEVEL; 83 84 89 public String getName() { 90 return name; 91 } 92 93 98 public void setName(String name) { 99 this.name = name; 100 } 101 102 106 public Level getLoggingLevel() { 107 return logLevel; 108 } 109 110 113 public void setLoggingLevel(Level logLevel) { 114 this.logLevel = logLevel; 115 } 116 117 122 public Object getRoot() { 123 return root; 124 } 125 126 133 public void setRoot(Object value) { 134 if (value == null) { 135 this.root = null; 136 } 137 138 if (!((value instanceof String ) 140 || (value instanceof ObjEntity) 141 || (value instanceof DbEntity) 142 || (value instanceof Class ) 143 || (value instanceof Procedure) || (value instanceof DataMap))) { 144 145 String rootClass = (value != null) ? value.getClass().getName() : "null"; 146 147 throw new IllegalArgumentException ( 148 getClass().getName() 149 + ": \"setRoot(..)\" takes a DataMap, String, ObjEntity, DbEntity, Procedure, " 150 + "or Class. It was passed a " 151 + rootClass); 152 } 153 154 this.root = value; 155 } 156 157 public String toString() { 158 return new ToStringBuilder(this).append("root", getRoot()).append( 159 "name", 160 getName()).toString(); 161 } 162 163 166 public abstract SQLAction createSQLAction(SQLActionVisitor visitor); 167 168 173 public Query resolve(EntityResolver resolver) { 174 return this; 175 } 176 177 184 public void route(QueryRouter router, EntityResolver resolver) { 185 DataMap map = resolver.lookupDataMap(this); 186 187 if (map == null) { 188 throw new CayenneRuntimeException("No DataMap found, can't route query " 189 + this); 190 } 191 192 router.useEngineForQuery(router.engineForDataMap(map), this); 193 } 194 } 195 | Popular Tags |