1 19 20 package org.apache.cayenne.map; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.apache.cayenne.CayenneRuntimeException; 27 import org.apache.cayenne.util.Util; 28 import org.apache.cayenne.util.XMLEncoder; 29 30 37 public class DerivedDbEntity extends DbEntity { 38 protected String parentEntityName; 39 40 43 public DerivedDbEntity() { 44 super(); 45 } 46 47 51 public DerivedDbEntity(String name) { 52 super(name); 53 } 54 55 59 public DerivedDbEntity(String name, DbEntity parentEntity) { 60 super(name); 61 62 this.setParentEntity(parentEntity); 63 this.resetToParentView(); 64 } 65 66 71 public void encodeAsXML(XMLEncoder encoder) { 72 encoder.print("<db-entity name=\"" + Util.encodeXmlAttribute(getName())); 73 74 if (getSchema() != null && getSchema().trim().length() > 0) { 75 encoder.print("\" schema=\""); 76 encoder.print(Util.encodeXmlAttribute(getSchema().trim())); 77 } 78 79 if (getCatalog() != null && getCatalog().trim().length() > 0) { 80 encoder.print("\" catalog=\""); 81 encoder.print(Util.encodeXmlAttribute(getCatalog().trim())); 82 } 83 84 encoder.print("\" parentName=\""); 85 encoder.print(Util.encodeXmlAttribute(getParentEntityName())); 86 encoder.println("\">"); 87 88 encoder.indent(1); 89 encoder.print(getAttributeMap()); 90 encoder.indent(-1); 91 encoder.println("</db-entity>"); 92 } 93 94 98 public void resetToParentView() { 99 this.clearAttributes(); 100 this.clearRelationships(); 101 102 Iterator it = getParentEntity().getAttributes().iterator(); 104 while (it.hasNext()) { 105 this.addAttribute(new DerivedDbAttribute(this, (DbAttribute) it.next())); 106 } 107 108 Iterator rit = this.getParentEntity().getRelationships().iterator(); 111 while (rit.hasNext()) { 112 DbRelationship protoRel = (DbRelationship) rit.next(); 113 DbRelationship rel = new DbRelationship(); 114 rel.setName(protoRel.getName()); 115 rel.setSourceEntity(this); 116 rel.setTargetEntity(protoRel.getTargetEntity()); 117 118 Iterator joins = protoRel.getJoins().iterator(); 119 while (joins.hasNext()) { 120 DbJoin protoJoin = (DbJoin) joins.next(); 121 DbJoin join = new DbJoin(rel); 122 join.setSourceName(protoJoin.getSourceName()); 123 join.setTargetName(protoJoin.getTargetName()); 124 rel.addJoin(join); 125 } 126 127 this.addRelationship(rel); 128 } 129 } 130 131 136 public DbEntity getParentEntity() { 137 if (parentEntityName == null) { 138 return null; 139 } 140 141 return getNonNullNamespace().getDbEntity(parentEntityName); 142 } 143 144 147 public void setParentEntity(DbEntity parentEntity) { 148 setParentEntityName(parentEntity != null ? parentEntity.getName() : null); 149 } 150 151 154 public List getGroupByAttributes() { 155 List list = new ArrayList (); 156 Iterator it = super.getAttributes().iterator(); 157 while (it.hasNext()) { 158 DerivedDbAttribute attr = (DerivedDbAttribute) it.next(); 159 if (attr.isGroupBy()) { 160 list.add(attr); 161 } 162 } 163 return list; 164 } 165 166 169 public String getFullyQualifiedName() { 170 return (getParentEntity() != null) 171 ? getParentEntity().getFullyQualifiedName() 172 : null; 173 } 174 175 178 public String getSchema() { 179 return (getParentEntity() != null) ? getParentEntity().getSchema() : null; 180 } 181 182 183 public void setSchema(String schema) { 184 throw new CayenneRuntimeException("Can't change schema of a derived entity."); 185 } 186 187 190 public String getCatalog() { 191 return (getParentEntity() != null) ? getParentEntity().getCatalog() : null; 192 } 193 194 195 public void setCatalog(String catalog) { 196 throw new CayenneRuntimeException("Can't change catalogue of a derived entity."); 197 } 198 199 202 public void removeAttribute(String attrName) { 203 super.removeAttribute(attrName); 204 } 205 206 210 public String getParentEntityName() { 211 return parentEntityName; 212 } 213 214 218 public void setParentEntityName(String parentEntityName) { 219 this.parentEntityName = parentEntityName; 220 } 221 } 222 | Popular Tags |