1 56 package org.objectstyle.cayenne.map; 57 58 import java.util.ArrayList ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 62 import org.objectstyle.cayenne.CayenneRuntimeException; 63 import org.objectstyle.cayenne.util.Util; 64 import org.objectstyle.cayenne.util.XMLEncoder; 65 66 73 public class DerivedDbEntity extends DbEntity { 74 protected String parentEntityName; 75 76 79 public DerivedDbEntity() { 80 super(); 81 } 82 83 87 public DerivedDbEntity(String name) { 88 super(name); 89 } 90 91 95 public DerivedDbEntity(String name, DbEntity parentEntity) { 96 super(name); 97 98 this.setParentEntity(parentEntity); 99 this.resetToParentView(); 100 } 101 102 107 public void encodeAsXML(XMLEncoder encoder) { 108 encoder.print("<db-entity name=\"" + Util.encodeXmlAttribute(getName())); 109 110 if (getSchema() != null && getSchema().trim().length() > 0) { 111 encoder.print("\" schema=\""); 112 encoder.print(Util.encodeXmlAttribute(getSchema().trim())); 113 } 114 115 if (getCatalog() != null && getCatalog().trim().length() > 0) { 116 encoder.print("\" catalog=\""); 117 encoder.print(Util.encodeXmlAttribute(getCatalog().trim())); 118 } 119 120 encoder.print("\" parentName=\""); 121 encoder.print(Util.encodeXmlAttribute(getParentEntityName())); 122 encoder.println("\">"); 123 124 encoder.indent(1); 125 encoder.print(getAttributeMap()); 126 encoder.indent(-1); 127 encoder.println("</db-entity>"); 128 } 129 130 134 public void resetToParentView() { 135 this.clearAttributes(); 136 this.clearRelationships(); 137 138 Iterator it = getParentEntity().getAttributes().iterator(); 140 while (it.hasNext()) { 141 this.addAttribute(new DerivedDbAttribute(this, (DbAttribute) it.next())); 142 } 143 144 Iterator rit = this.getParentEntity().getRelationships().iterator(); 147 while (rit.hasNext()) { 148 DbRelationship protoRel = (DbRelationship) rit.next(); 149 DbRelationship rel = new DbRelationship(); 150 rel.setName(protoRel.getName()); 151 rel.setSourceEntity(this); 152 rel.setTargetEntity(protoRel.getTargetEntity()); 153 154 Iterator joins = protoRel.getJoins().iterator(); 155 while (joins.hasNext()) { 156 DbJoin protoJoin = (DbJoin) joins.next(); 157 DbJoin join = new DbJoin(rel); 158 join.setSourceName(protoJoin.getSourceName()); 159 join.setTargetName(protoJoin.getTargetName()); 160 rel.addJoin(join); 161 } 162 163 this.addRelationship(rel); 164 } 165 } 166 167 172 public DbEntity getParentEntity() { 173 if (parentEntityName == null) { 174 return null; 175 } 176 177 return getNonNullNamespace().getDbEntity(parentEntityName); 178 } 179 180 183 public void setParentEntity(DbEntity parentEntity) { 184 setParentEntityName(parentEntity != null ? parentEntity.getName() : null); 185 } 186 187 190 public List getGroupByAttributes() { 191 List list = new ArrayList (); 192 Iterator it = super.getAttributes().iterator(); 193 while (it.hasNext()) { 194 DerivedDbAttribute attr = (DerivedDbAttribute) it.next(); 195 if (attr.isGroupBy()) { 196 list.add(attr); 197 } 198 } 199 return list; 200 } 201 202 205 public String getFullyQualifiedName() { 206 return (getParentEntity() != null) 207 ? getParentEntity().getFullyQualifiedName() 208 : null; 209 } 210 211 214 public String getSchema() { 215 return (getParentEntity() != null) ? getParentEntity().getSchema() : null; 216 } 217 218 219 public void setSchema(String schema) { 220 throw new CayenneRuntimeException("Can't change schema of a derived entity."); 221 } 222 223 226 public String getCatalog() { 227 return (getParentEntity() != null) ? getParentEntity().getCatalog() : null; 228 } 229 230 231 public void setCatalog(String catalog) { 232 throw new CayenneRuntimeException("Can't change catalogue of a derived entity."); 233 } 234 235 238 public void removeAttribute(String attrName) { 239 super.removeAttribute(attrName); 240 } 241 242 246 public String getParentEntityName() { 247 return parentEntityName; 248 } 249 250 254 public void setParentEntityName(String parentEntityName) { 255 this.parentEntityName = parentEntityName; 256 } 257 } 258 | Popular Tags |