1 19 20 package org.apache.cayenne.map; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 27 import org.apache.cayenne.DataRow; 28 import org.apache.cayenne.exp.Expression; 29 30 37 public class EntityInheritanceTree { 38 protected ObjEntity entity; 39 protected Collection subentities; 40 protected Expression normalizedQualifier; 41 42 public EntityInheritanceTree(ObjEntity entity) { 43 this.entity = entity; 44 } 45 46 50 public Expression qualifierForEntityAndSubclasses() { 51 Expression qualifier = entity.getDeclaredQualifier(); 52 53 if (qualifier == null) { 54 return null; 56 } 57 58 if (subentities != null) { 59 Iterator it = subentities.iterator(); 60 while (it.hasNext()) { 61 EntityInheritanceTree child = (EntityInheritanceTree) it.next(); 62 Expression childQualifier = child.qualifierForEntityAndSubclasses(); 63 64 if (childQualifier == null) { 66 return null; 67 } 68 69 qualifier = qualifier.orExp(childQualifier); 70 } 71 } 72 73 return qualifier; 74 } 75 76 80 public ObjEntity entityMatchingRow(DataRow row) { 81 if (subentities != null) { 83 Iterator it = subentities.iterator(); 84 while (it.hasNext()) { 85 EntityInheritanceTree child = (EntityInheritanceTree) it.next(); 86 ObjEntity matched = child.entityMatchingRow(row); 87 88 if (matched != null) { 89 return matched; 90 } 91 } 92 } 93 94 Expression qualifier = entity.getDeclaredQualifier(); 95 if (qualifier != null) { 96 if (normalizedQualifier == null) { 97 normalizedQualifier = entity.translateToDbPath(qualifier); 98 } 99 100 return normalizedQualifier.match(row) ? entity : null; 101 } 102 103 return entity; 105 } 106 107 public void addChildNode(EntityInheritanceTree node) { 108 if (subentities == null) { 109 subentities = new ArrayList (2); 110 } 111 112 subentities.add(node); 113 } 114 115 public int getChildrenCount() { 116 return (subentities != null) ? subentities.size() : 0; 117 } 118 119 public Collection getChildren() { 120 return (subentities != null) ? subentities : Collections.EMPTY_LIST; 121 } 122 123 public ObjEntity getEntity() { 124 return entity; 125 } 126 127 public Collection allAttributes() { 128 if (subentities == null) { 129 return entity.getAttributes(); 130 } 131 132 Collection c = new ArrayList (); 133 appendDeclaredAttributes(c); 134 135 ObjEntity superEntity = entity.getSuperEntity(); 137 if (superEntity != null) { 138 c.addAll(superEntity.getAttributes()); 139 } 140 141 return c; 142 } 143 144 public Collection allRelationships() { 145 if (subentities == null) { 146 return entity.getRelationships(); 147 } 148 149 Collection c = new ArrayList (); 150 appendDeclaredRelationships(c); 151 152 ObjEntity superEntity = entity.getSuperEntity(); 154 if (superEntity != null) { 155 c.addAll(superEntity.getRelationships()); 156 } 157 158 return c; 159 } 160 161 protected void appendDeclaredAttributes(Collection c) { 162 c.addAll(entity.getDeclaredAttributes()); 163 164 if (subentities != null) { 165 Iterator it = subentities.iterator(); 166 while (it.hasNext()) { 167 EntityInheritanceTree child = (EntityInheritanceTree) it.next(); 168 child.appendDeclaredAttributes(c); 169 } 170 } 171 } 172 173 protected void appendDeclaredRelationships(Collection c) { 174 c.addAll(entity.getDeclaredRelationships()); 175 176 if (subentities != null) { 177 Iterator it = subentities.iterator(); 178 while (it.hasNext()) { 179 EntityInheritanceTree child = (EntityInheritanceTree) it.next(); 180 child.appendDeclaredRelationships(c); 181 } 182 } 183 } 184 } 185 | Popular Tags |