1 56 package org.objectstyle.cayenne.map; 57 58 import java.util.Collection ; 59 import java.util.Collections ; 60 import java.util.Iterator ; 61 import java.util.SortedMap ; 62 import java.util.StringTokenizer ; 63 64 import org.objectstyle.cayenne.CayenneRuntimeException; 65 import org.objectstyle.cayenne.exp.Expression; 66 import org.objectstyle.cayenne.exp.ExpressionException; 67 import org.objectstyle.cayenne.util.CayenneMap; 68 69 76 public abstract class Entity extends MapObject { 77 public static final String PATH_SEPARATOR = "."; 78 79 protected CayenneMap attributes = new CayenneMap(this); 83 protected SortedMap attributesMapRef = Collections.unmodifiableSortedMap(attributes); 85 protected Collection attributesRef = 87 Collections.unmodifiableCollection(attributes.values()); 88 89 protected CayenneMap relationships = new CayenneMap(this); 93 protected SortedMap relationshipsMapRef = 95 Collections.unmodifiableSortedMap(relationships); 96 protected Collection relationshipsRef = 98 Collections.unmodifiableCollection(relationships.values()); 99 100 103 public DataMap getDataMap() { 104 return (DataMap) getParent(); 105 } 106 107 110 public void setDataMap(DataMap dataMap) { 111 this.setParent(dataMap); 112 } 113 114 118 public Attribute getAttribute(String attrName) { 119 return (Attribute) attributes.get(attrName); 120 } 121 122 128 public void addAttribute(Attribute attr) { 129 if (attr.getName() == null) { 130 throw new IllegalArgumentException ("Attempt to insert unnamed attribute."); 131 } 132 133 attributes.put(attr.getName(), attr); 134 } 135 136 137 public void removeAttribute(String attrName) { 138 attributes.remove(attrName); 139 } 140 141 public void clearAttributes() { 142 attributes.clear(); 143 } 144 145 150 public Relationship getRelationship(String relName) { 151 return (Relationship) relationships.get(relName); 152 } 153 154 155 public void addRelationship(Relationship rel) { 156 relationships.put(rel.getName(), rel); 157 } 158 159 160 public void removeRelationship(String relName) { 161 relationships.remove(relName); 162 } 163 164 public void clearRelationships() { 165 relationships.clear(); 166 } 167 168 171 public SortedMap getRelationshipMap() { 172 return relationshipsMapRef; 173 } 174 175 182 public Relationship getAnyRelationship(Entity targetEntity) { 183 Collection relationships = getRelationships(); 184 if (relationships.isEmpty()) { 185 return null; 186 } 187 188 Iterator it = relationships.iterator(); 189 while (it.hasNext()) { 190 Relationship r = (Relationship) it.next(); 191 if (r.getTargetEntity() == targetEntity) { 192 return r; 193 } 194 } 195 return null; 196 } 197 198 201 public Collection getRelationships() { 202 return relationshipsRef; 203 } 204 205 209 public SortedMap getAttributeMap() { 210 return attributesMapRef; 211 } 212 213 216 public Collection getAttributes() { 217 return attributesRef; 218 } 219 220 226 public abstract Expression translateToRelatedEntity( 227 Expression expression, 228 String relationshipPath); 229 230 236 public Object lastPathComponent(Expression pathExp) { 237 Object last = null; 238 Iterator it = resolvePathComponents(pathExp); 239 while (it.hasNext()) { 240 last = it.next(); 241 } 242 243 return last; 244 } 245 246 253 public abstract Iterator resolvePathComponents(Expression pathExp) 254 throws ExpressionException; 255 256 263 public Iterator resolvePathComponents(String path) throws ExpressionException { 264 return new PathIterator(path); 265 } 266 267 final class PathIterator implements Iterator { 270 271 private StringTokenizer toks; 272 private Entity currentEnt; 273 private String path; 274 275 PathIterator(String path) { 276 super(); 277 this.currentEnt = Entity.this; 278 this.toks = new StringTokenizer (path, PATH_SEPARATOR); 279 this.path = path; 280 } 281 282 public boolean hasNext() { 283 return toks.hasMoreTokens(); 284 } 285 286 public Object next() { 287 String pathComp = toks.nextToken(); 288 289 Attribute attr = currentEnt.getAttribute(pathComp); 291 if (attr != null) { 292 if (toks.hasMoreTokens()) { 294 throw new ExpressionException( 295 "Attribute must be the last component of the path: '" 296 + pathComp 297 + "'.", 298 path, 299 null); 300 } 301 302 return attr; 303 } 304 305 Relationship rel = currentEnt.getRelationship(pathComp); 306 if (rel != null) { 307 currentEnt = rel.getTargetEntity(); 308 return rel; 309 } 310 311 StringBuffer buf = new StringBuffer (); 313 buf 314 .append("Can't resolve path component: [") 315 .append(currentEnt.getName()) 316 .append('.') 317 .append(pathComp) 318 .append("]."); 319 throw new ExpressionException(buf.toString(), path, null); 320 } 321 322 public void remove() { 323 throw new UnsupportedOperationException ("'remove' operation is not supported."); 324 } 325 } 326 327 final MappingNamespace getNonNullNamespace() { 328 MappingNamespace parent = (MappingNamespace) getParent(); 329 if (parent == null) { 330 throw new CayenneRuntimeException( 331 "Entity '" 332 + getName() 333 + "' has no parent MappingNamespace (such as DataMap)"); 334 } 335 336 return parent; 337 } 338 } 339 | Popular Tags |