1 19 20 package org.apache.cayenne.map; 21 22 import java.util.Iterator ; 23 24 import org.apache.cayenne.CayenneRuntimeException; 25 import org.apache.cayenne.util.Util; 26 import org.apache.cayenne.util.XMLEncoder; 27 import org.apache.commons.collections.IteratorUtils; 28 import org.apache.commons.lang.StringUtils; 29 30 36 public class ObjAttribute extends Attribute { 37 38 protected String type; 39 protected boolean usedForLocking; 40 protected String dbAttributePath; 41 42 public ObjAttribute() { 43 } 44 45 public ObjAttribute(String name) { 46 super(name); 47 } 48 49 public ObjAttribute(String name, String type, ObjEntity entity) { 50 setName(name); 51 setType(type); 52 setEntity(entity); 53 } 54 55 59 public Class getJavaClass() { 60 if (this.getType() == null) { 61 return null; 62 } 63 64 try { 65 return Util.getJavaClass(getType()); 66 } 67 catch (ClassNotFoundException e) { 68 throw new CayenneRuntimeException("Failed to load class for name '" 69 + this.getType() 70 + "': " 71 + e.getMessage(), e); 72 } 73 } 74 75 80 public void encodeAsXML(XMLEncoder encoder) { 81 encoder.print("<obj-attribute name=\"" + getName() + '\"'); 82 83 if (getType() != null) { 84 encoder.print(" type=\""); 85 encoder.print(getType()); 86 encoder.print('\"'); 87 } 88 89 if (isUsedForLocking()) { 90 encoder.print(" lock=\"true\""); 91 } 92 93 if (getDbAttribute() != null) { 95 encoder.print(" db-attribute-path=\""); 96 encoder.print(Util.encodeXmlAttribute(getDbAttributePath())); 97 encoder.print('\"'); 98 } 99 100 encoder.println("/>"); 101 } 102 103 107 public String getType() { 108 return type; 109 } 110 111 115 public void setType(String type) { 116 this.type = type; 117 } 118 119 124 public boolean isUsedForLocking() { 125 return usedForLocking; 126 } 127 128 133 public void setUsedForLocking(boolean usedForLocking) { 134 this.usedForLocking = usedForLocking; 135 } 136 137 140 public DbAttribute getDbAttribute() { 141 Iterator pathIterator = getDbPathIterator(); 142 Object o = null; 143 while (pathIterator.hasNext()) { 144 o = pathIterator.next(); 145 } 146 return (DbAttribute) o; 147 } 148 149 public Iterator getDbPathIterator() { 150 if (dbAttributePath == null) { 151 return IteratorUtils.EMPTY_ITERATOR; 152 } 153 154 ObjEntity ent = (ObjEntity) getEntity(); 155 if (ent == null) { 156 return IteratorUtils.EMPTY_ITERATOR; 157 } 158 159 DbEntity dbEnt = ent.getDbEntity(); 160 if (dbEnt == null) { 161 return IteratorUtils.EMPTY_ITERATOR; 162 } 163 164 int lastPartStart = dbAttributePath.lastIndexOf('.'); 165 if (lastPartStart < 0) { 166 Attribute attribute = dbEnt.getAttribute(dbAttributePath); 167 if (attribute == null) { 168 return IteratorUtils.EMPTY_ITERATOR; 169 } 170 return IteratorUtils.singletonIterator(attribute); 171 } 172 173 return dbEnt.resolvePathComponents(dbAttributePath); 174 } 175 176 179 public void setDbAttribute(DbAttribute dbAttribute) { 180 if (dbAttribute == null) { 181 this.setDbAttributePath(null); 182 } 183 else { 184 this.setDbAttributePath(dbAttribute.getName()); 185 } 186 } 187 188 193 public String getDbAttributeName() { 194 if (dbAttributePath == null) 195 return null; 196 int lastPartStart = dbAttributePath.lastIndexOf('.'); 197 String lastPart = StringUtils.substring( 198 dbAttributePath, 199 lastPartStart + 1, 200 dbAttributePath.length()); 201 return lastPart; 202 } 203 204 209 public void setDbAttributeName(String dbAttributeName) { 210 if (dbAttributePath == null || dbAttributeName == null) { 211 dbAttributePath = dbAttributeName; 212 return; 213 } 214 int lastPartStart = dbAttributePath.lastIndexOf('.'); 215 String newPath = (lastPartStart > 0 216 ? StringUtils.chomp(dbAttributePath, ".") 217 : ""); 218 newPath += (newPath.length() > 0 ? "." : "") + dbAttributeName; 219 this.dbAttributePath = newPath; 220 } 221 222 public void setDbAttributePath(String dbAttributePath) { 223 this.dbAttributePath = dbAttributePath; 224 } 225 226 public String getDbAttributePath() { 227 return dbAttributePath; 228 } 229 230 public boolean isCompound() { 231 return (dbAttributePath != null && dbAttributePath.indexOf('.') >= 0); 232 } 233 234 240 public ObjAttribute getClientAttribute() { 241 ClientObjAttribute attribute = new ClientObjAttribute(getName()); 242 attribute.setType(getType()); 243 244 DbAttribute dbAttribute = getDbAttribute(); 245 if (dbAttribute != null) { 246 attribute.setMandatory(dbAttribute.isMandatory()); 247 attribute.setMaxLength(dbAttribute.getMaxLength()); 248 } 249 250 252 return attribute; 253 } 254 } 255 | Popular Tags |