1 19 20 package org.apache.cayenne.wocompat; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.StringTokenizer ; 29 30 import org.apache.cayenne.CayenneRuntimeException; 31 import org.apache.cayenne.exp.Expression; 32 import org.apache.cayenne.exp.ExpressionFactory; 33 import org.apache.cayenne.map.Entity; 34 import org.apache.cayenne.map.ObjEntity; 35 import org.apache.cayenne.query.Query; 36 import org.apache.commons.collections.Transformer; 37 38 43 public class EOObjEntity extends ObjEntity { 44 45 protected boolean subclass; 46 protected boolean abstractEntity; 47 48 private Collection filteredQueries; 49 private Map eoMap; 50 51 public EOObjEntity() { 52 } 53 54 public EOObjEntity(String name) { 55 super(name); 56 } 57 58 61 public void setHasSuperClass(boolean value) { 62 setSubclass(value); 63 } 64 65 68 public boolean getHasSuperClass() { 69 return isSubclass(); 70 } 71 72 75 public void setIsClientEntity(boolean value) { 76 setServerOnly(!value); 77 } 78 79 82 public boolean getIsClientEntity() { 83 return !isServerOnly(); 84 } 85 86 89 public void setIsAbstractEntity(boolean value) { 90 setAbstractEntity(value); 91 } 92 93 96 public boolean getIsAbstractEntity() { 97 return isAbstractEntity(); 98 } 99 100 105 public EOQuery getEOQuery(String queryName) { 106 Query query = getDataMap().getQuery(qualifiedQueryName(queryName)); 107 if (query instanceof EOQuery) { 108 return (EOQuery) query; 109 } 110 111 return null; 112 } 113 114 119 public Expression translateToDbPath(Expression expression) { 120 121 if (expression == null) { 122 return null; 123 } 124 125 if (getDbEntity() == null) { 126 throw new CayenneRuntimeException( 127 "Can't translate expression to DB_PATH, no DbEntity for '" 128 + getName() 129 + "'."); 130 } 131 132 return expression.transform(new DBPathConverter()); 135 } 136 137 140 Map getEoMap() { 143 return eoMap; 144 } 145 146 149 void setEoMap(Map eoMap) { 152 this.eoMap = eoMap; 153 } 154 155 160 public Collection getEOQueries() { 161 if (filteredQueries == null) { 162 Collection queries = getDataMap().getQueries(); 163 if (queries.isEmpty()) { 164 filteredQueries = Collections.EMPTY_LIST; 165 } 166 else { 167 Map params = Collections.singletonMap("root", EOObjEntity.this); 168 Expression filter = Expression 169 .fromString("root = $root") 170 .expWithParameters(params); 171 172 filteredQueries = filter.filter(queries, new ArrayList ()); 173 } 174 } 175 176 return filteredQueries; 177 } 178 179 public boolean isAbstractEntity() { 180 return abstractEntity; 181 } 182 183 public void setAbstractEntity(boolean abstractEntity) { 184 this.abstractEntity = abstractEntity; 185 } 186 187 public boolean isSubclass() { 188 return subclass; 189 } 190 191 public void setSubclass(boolean subclass) { 192 this.subclass = subclass; 193 } 194 195 202 public String qualifiedQueryName(String queryName) { 203 return getName() + "_" + queryName; 204 } 205 206 209 public String localQueryName(String qualifiedQueryName) { 210 return (qualifiedQueryName != null && qualifiedQueryName.startsWith(getName() 211 + "_")) 212 ? qualifiedQueryName.substring(getName().length() + 1) 213 : qualifiedQueryName; 214 } 215 216 final class DBPathConverter implements Transformer { 217 218 public Object transform(Object input) { 219 220 if (!(input instanceof Expression)) { 221 return input; 222 } 223 224 Expression expression = (Expression) input; 225 226 if (expression.getType() != Expression.OBJ_PATH) { 227 return input; 228 } 229 230 232 StringBuffer buffer = new StringBuffer (); 233 EOObjEntity entity = EOObjEntity.this; 234 StringTokenizer toks = new StringTokenizer (expression.toString(), "."); 235 while (toks.hasMoreTokens() && entity != null) { 236 String chunk = toks.nextToken(); 237 238 if (toks.hasMoreTokens()) { 239 if (buffer.length() > 0) { 241 buffer.append(Entity.PATH_SEPARATOR); 242 } 243 244 buffer.append(chunk); 245 246 entity = (EOObjEntity) entity 247 .getRelationship(chunk) 248 .getTargetEntity(); 249 } 250 else { 252 253 List attributes = (List ) entity.getEoMap().get("attributes"); 254 Iterator it = attributes.iterator(); 255 while (it.hasNext()) { 256 Map attribute = (Map ) it.next(); 257 if (chunk.equals(attribute.get("name"))) { 258 259 if (buffer.length() > 0) { 260 buffer.append(Entity.PATH_SEPARATOR); 261 } 262 263 buffer.append(attribute.get("columnName")); 264 break; 265 } 266 } 267 } 268 } 269 270 Expression exp = ExpressionFactory.expressionOfType(Expression.DB_PATH); 271 exp.setOperand(0, buffer.toString()); 272 return exp; 273 } 274 } 275 } 276 | Popular Tags |