1 56 57 package org.objectstyle.cayenne.map; 58 59 import java.util.Iterator ; 60 61 import org.apache.commons.collections.IteratorUtils; 62 import org.apache.commons.lang.StringUtils; 63 import org.objectstyle.cayenne.CayenneException; 64 import org.objectstyle.cayenne.util.Util; 65 import org.objectstyle.cayenne.util.XMLEncoder; 66 67 73 public class ObjAttribute extends Attribute { 74 protected String type; 75 protected boolean usedForLocking; 76 private String dbAttributePath; 77 78 public ObjAttribute() { 79 } 80 81 public ObjAttribute(String name) { 82 super(name); 83 } 84 85 public ObjAttribute(String name, String type, ObjEntity entity) { 86 setName(name); 87 setType(type); 88 setEntity(entity); 89 } 90 91 96 public void encodeAsXML(XMLEncoder encoder) { 97 encoder.print("<obj-attribute name=\"" + getName() + '\"'); 98 99 if (getType() != null) { 100 encoder.print(" type=\""); 101 encoder.print(getType()); 102 encoder.print('\"'); 103 } 104 105 if(isUsedForLocking()) { 106 encoder.print(" lock=\"true\""); 107 } 108 109 if (getDbAttribute() != null) { 111 encoder.print(" db-attribute-path=\""); 112 encoder.print(Util.encodeXmlAttribute(getDbAttributePath())); 113 encoder.print('\"'); 114 } 115 116 encoder.println("/>"); 117 } 118 119 123 public String getType() { 124 return type; 125 } 126 127 130 public void setType(String type) { 131 this.type = type; 132 } 133 134 139 public boolean isUsedForLocking() { 140 return usedForLocking; 141 } 142 143 148 public void setUsedForLocking(boolean usedForLocking) { 149 this.usedForLocking = usedForLocking; 150 } 151 152 155 public DbAttribute getDbAttribute() { 156 Iterator pathIterator = getDbPathIterator(); 157 Object o = null; 158 while (pathIterator.hasNext()) { 159 o = pathIterator.next(); 160 } 161 return (DbAttribute) o; 162 } 163 164 public Iterator getDbPathIterator() { 165 if (dbAttributePath == null) { 166 return IteratorUtils.EMPTY_ITERATOR; 167 } 168 169 ObjEntity ent = (ObjEntity) getEntity(); 170 if (ent == null) { 171 return IteratorUtils.EMPTY_ITERATOR; 172 } 173 174 DbEntity dbEnt = ent.getDbEntity(); 175 if (dbEnt == null) { 176 return IteratorUtils.EMPTY_ITERATOR; 177 } 178 179 int lastPartStart = dbAttributePath.lastIndexOf('.'); 180 if (lastPartStart < 0) { 181 Attribute attribute = dbEnt.getAttribute(dbAttributePath); 182 if (attribute == null) { 183 return IteratorUtils.EMPTY_ITERATOR; 184 } 185 return IteratorUtils.singletonIterator(attribute); 186 } 187 188 return dbEnt.resolvePathComponents(dbAttributePath); 189 } 190 191 194 public void setDbAttribute(DbAttribute dbAttribute) { 195 if (dbAttribute == null) { 196 this.setDbAttributePath(null); 197 } 198 else { 199 this.setDbAttributePath(dbAttribute.getName()); 200 } 201 } 202 203 207 public String getDbAttributeName() { 208 if (dbAttributePath == null) 209 return null; 210 int lastPartStart = dbAttributePath.lastIndexOf('.'); 211 String lastPart = 212 StringUtils.substring( 213 dbAttributePath, 214 lastPartStart + 1, 215 dbAttributePath.length()); 216 return lastPart; 217 } 218 219 223 public void setDbAttributeName(String dbAttributeName) { 224 if (dbAttributePath == null || dbAttributeName == null) { 225 dbAttributePath = dbAttributeName; 226 return; 227 } 228 int lastPartStart = dbAttributePath.lastIndexOf('.'); 229 String newPath = 230 (lastPartStart > 0 ? StringUtils.chomp(dbAttributePath, ".") : ""); 231 newPath += (newPath.length() > 0 ? "." : "") + dbAttributeName; 232 this.dbAttributePath = newPath; 233 } 234 235 public void setDbAttributePath(String dbAttributePath) { 236 this.dbAttributePath = dbAttributePath; 237 } 238 239 public String getDbAttributePath() { 240 return dbAttributePath; 241 } 242 243 public boolean isCompound() { 244 return (dbAttributePath != null && dbAttributePath.indexOf('.') >= 0); 245 } 246 247 public boolean mapsToDependentDbEntity() { 248 Iterator i = getDbPathIterator(); 249 if (!i.hasNext()) 250 return false; 251 Object o = i.next(); 252 if (!i.hasNext()) 253 return false; 254 Object o1 = i.next(); 255 if (!(o1 instanceof DbAttribute)) 256 return false; 257 DbRelationship toDependent = (DbRelationship) o; 258 return toDependent.isToDependentPK(); 259 } 260 261 public void validate() throws CayenneException { 262 String head = "ObjAttribute: " + getName() + " "; 263 ObjEntity ent = (ObjEntity) getEntity(); 264 if (ent == null) { 265 throw new CayenneException(head + "Parent ObjEntity not defined."); 266 } 267 head += "ObjEntity: " + ent.getName() + " "; 268 269 if (getName() == null) 270 throw new CayenneException(head + "ObjAttribute's name not defined."); 271 272 if (getDbAttributePath() == null) 273 throw new CayenneException(head + "dbAttributePath not defined."); 274 275 try { 276 Iterator i = getDbPathIterator(); 277 boolean dbAttributeFound = false; 278 while (i.hasNext()) { 279 Object pathPart = i.next(); 280 if (pathPart instanceof DbRelationship) { 281 DbRelationship r = (DbRelationship) pathPart; 282 if (r.isToMany()) 283 throw new CayenneException( 284 head + "DbRelationship: " + r.getName() + " is to-many."); 285 } 286 else if (pathPart instanceof DbAttribute) { 287 dbAttributeFound = true; 288 } 289 } 290 if (!dbAttributeFound) 291 throw new CayenneException(head + "DbAttribute not found."); 292 } 293 catch (CayenneException ex) { 294 throw ex; 295 } 296 catch (Exception ex) { 297 throw new CayenneException(head + ex.getMessage(), ex); 298 } 299 } 300 } 301 | Popular Tags |