1 19 package org.apache.cayenne.map; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.SortedMap ; 27 import java.util.TreeMap ; 28 29 import org.apache.cayenne.CayenneRuntimeException; 30 import org.apache.cayenne.util.Util; 31 import org.apache.cayenne.util.XMLEncoder; 32 33 39 public class EmbeddedAttribute extends Attribute { 40 41 protected String type; 42 protected SortedMap attributeOverrides; 43 44 public EmbeddedAttribute() { 45 attributeOverrides = new TreeMap (); 46 } 47 48 public EmbeddedAttribute(String name) { 49 this(); 50 setName(name); 51 } 52 53 public EmbeddedAttribute(String name, String type, ObjEntity entity) { 54 this(); 55 setName(name); 56 setType(type); 57 setEntity(entity); 58 } 59 60 public void encodeAsXML(XMLEncoder encoder) { 61 encoder.print("<embedded-attribute name=\"" + getName() + '\"'); 62 if (getType() != null) { 63 encoder.print(" type=\""); 64 encoder.print(getType()); 65 encoder.print('\"'); 66 } 67 68 if (attributeOverrides.isEmpty()) { 69 encoder.println("/>"); 70 return; 71 } 72 73 encoder.println('>'); 74 75 encoder.indent(1); 76 77 78 79 Iterator it = attributeOverrides.entrySet().iterator(); 80 while (it.hasNext()) { 81 Map.Entry e = (Map.Entry ) it.next(); 82 encoder.print("<embeddable-attribute-override name=\""); 83 encoder.print(e.getKey().toString()); 84 encoder.print("\" db-attribute-path=\""); 85 encoder.print(e.getValue().toString()); 86 encoder.println("\"/>"); 87 } 88 89 encoder.indent(-1); 90 encoder.println("</embedded-attribute>"); 91 } 92 93 public Map getAttributeOverrides() { 94 return Collections.unmodifiableMap(attributeOverrides); 95 } 96 97 public Embeddable getEmbeddable() { 98 if (type == null) { 99 return null; 100 } 101 102 return getNonNullNamespace().getEmbeddable(type); 103 } 104 105 private ObjAttribute makeObjAttribute(EmbeddableAttribute embeddableAttribute) { 106 String dbPath = (String ) attributeOverrides.get(embeddableAttribute.getName()); 107 if (dbPath == null) { 108 dbPath = embeddableAttribute.getDbAttributeName(); 109 } 110 111 return makeObjAttribute(embeddableAttribute, dbPath); 112 } 113 114 private ObjAttribute makeObjAttribute( 115 EmbeddableAttribute embeddableAttribute, 116 String dbPath) { 117 String fullName = getName() + "." + embeddableAttribute.getName(); 118 119 ObjAttribute oa = new ObjAttribute( 120 fullName, 121 embeddableAttribute.getType(), 122 (ObjEntity) getEntity()); 123 oa.setDbAttributeName(dbPath); 124 return oa; 125 } 126 127 131 public ObjAttribute getAttributeForDbPath(String dbPath) { 132 133 Embeddable e = getEmbeddable(); 134 if (e == null) { 135 return null; 136 } 137 138 EmbeddableAttribute ea = null; 139 140 Iterator overrides = attributeOverrides.entrySet().iterator(); 141 while (overrides.hasNext()) { 142 Map.Entry override = (Map.Entry ) overrides.next(); 143 if (dbPath.equals(override.getValue())) { 144 ea = e.getAttribute(override.getKey().toString()); 145 break; 146 } 147 } 148 149 if (ea == null) { 150 ea = e.getAttributeForDbPath(dbPath); 151 } 152 153 if (ea != null) { 154 return makeObjAttribute(ea, dbPath); 155 } 156 157 return null; 158 } 159 160 164 public ObjAttribute getAttribute(String name) { 165 Embeddable e = getEmbeddable(); 166 if (e == null) { 167 return null; 168 } 169 170 EmbeddableAttribute ea = e.getAttribute(name); 171 if (ea == null) { 172 return null; 173 } 174 175 return makeObjAttribute(ea); 176 } 177 178 182 public Collection getAttributes() { 183 Embeddable e = getEmbeddable(); 184 if (e == null) { 185 return Collections.EMPTY_LIST; 186 } 187 188 Collection embeddableAttributes = e.getAttributes(); 189 Collection objectAttributes = new ArrayList (embeddableAttributes.size()); 190 Iterator it = embeddableAttributes.iterator(); 191 while (it.hasNext()) { 192 EmbeddableAttribute ea = (EmbeddableAttribute) it.next(); 193 objectAttributes.add(makeObjAttribute(ea)); 194 } 195 196 return objectAttributes; 197 } 198 199 public void addAttributeOverride(String name, String dbAttributeName) { 200 attributeOverrides.put(name, dbAttributeName); 201 } 202 203 public void removeAttributeOverride(String name) { 204 attributeOverrides.remove(name); 205 } 206 207 210 public String getType() { 211 return type; 212 } 213 214 218 public Class getJavaClass() { 219 if (this.getType() == null) { 220 return null; 221 } 222 223 try { 224 return Util.getJavaClass(getType()); 225 } 226 catch (ClassNotFoundException e) { 227 throw new CayenneRuntimeException("Failed to load class for name '" 228 + this.getType() 229 + "': " 230 + e.getMessage(), e); 231 } 232 } 233 234 237 public void setType(String type) { 238 this.type = type; 239 } 240 241 246 final MappingNamespace getNonNullNamespace() { 247 248 if (entity == null) { 249 throw new CayenneRuntimeException("Embedded attribute '" 250 + getName() 251 + "' has no parent Entity."); 252 } 253 254 return entity.getNonNullNamespace(); 255 } 256 } 257 | Popular Tags |