1 56 package org.objectstyle.cayenne.map; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.Collections ; 61 import java.util.Iterator ; 62 63 import org.objectstyle.cayenne.DataRow; 64 import org.objectstyle.cayenne.exp.Expression; 65 66 73 public class EntityInheritanceTree { 74 protected ObjEntity entity; 75 protected Collection subentities; 76 protected Expression normalizedQualifier; 77 78 public EntityInheritanceTree(ObjEntity entity) { 79 this.entity = entity; 80 } 81 82 86 public Expression qualifierForEntityAndSubclasses() { 87 Expression qualifier = entity.getDeclaredQualifier(); 88 89 if (qualifier == null) { 90 return null; 92 } 93 94 if (subentities != null) { 95 Iterator it = subentities.iterator(); 96 while (it.hasNext()) { 97 EntityInheritanceTree child = (EntityInheritanceTree) it.next(); 98 Expression childQualifier = child.qualifierForEntityAndSubclasses(); 99 100 if (childQualifier == null) { 102 return null; 103 } 104 105 qualifier = qualifier.orExp(childQualifier); 106 } 107 } 108 109 return qualifier; 110 } 111 112 116 public ObjEntity entityMatchingRow(DataRow row) { 117 if (subentities != null) { 119 Iterator it = subentities.iterator(); 120 while (it.hasNext()) { 121 EntityInheritanceTree child = (EntityInheritanceTree) it.next(); 122 ObjEntity matched = child.entityMatchingRow(row); 123 124 if (matched != null) { 125 return matched; 126 } 127 } 128 } 129 130 Expression qualifier = entity.getDeclaredQualifier(); 131 if (qualifier != null) { 132 if (normalizedQualifier == null) { 133 normalizedQualifier = entity.translateToDbPath(qualifier); 134 } 135 136 return normalizedQualifier.match(row) ? entity : null; 137 } 138 139 return entity; 141 } 142 143 public void addChildNode(EntityInheritanceTree node) { 144 if (subentities == null) { 145 subentities = new ArrayList (2); 146 } 147 148 subentities.add(node); 149 } 150 151 public int getChildrenCount() { 152 return (subentities != null) ? subentities.size() : 0; 153 } 154 155 public Collection getChildren() { 156 return (subentities != null) ? subentities : Collections.EMPTY_LIST; 157 } 158 159 public ObjEntity getEntity() { 160 return entity; 161 } 162 163 public Collection allAttributes() { 164 if (subentities == null) { 165 return entity.getAttributes(); 166 } 167 168 Collection c = new ArrayList (); 169 appendDeclaredAttributes(c); 170 171 ObjEntity superEntity = entity.getSuperEntity(); 173 if (superEntity != null) { 174 c.addAll(superEntity.getAttributes()); 175 } 176 177 return c; 178 } 179 180 public Collection allRelationships() { 181 if (subentities == null) { 182 return entity.getRelationships(); 183 } 184 185 Collection c = new ArrayList (); 186 appendDeclaredRelationships(c); 187 188 ObjEntity superEntity = entity.getSuperEntity(); 190 if (superEntity != null) { 191 c.addAll(superEntity.getRelationships()); 192 } 193 194 return c; 195 } 196 197 protected void appendDeclaredAttributes(Collection c) { 198 c.addAll(entity.getDeclaredAttributes()); 199 200 if (subentities != null) { 201 Iterator it = subentities.iterator(); 202 while (it.hasNext()) { 203 EntityInheritanceTree child = (EntityInheritanceTree) it.next(); 204 child.appendDeclaredAttributes(c); 205 } 206 } 207 } 208 209 protected void appendDeclaredRelationships(Collection c) { 210 c.addAll(entity.getDeclaredRelationships()); 211 212 if (subentities != null) { 213 Iterator it = subentities.iterator(); 214 while (it.hasNext()) { 215 EntityInheritanceTree child = (EntityInheritanceTree) it.next(); 216 child.appendDeclaredRelationships(c); 217 } 218 } 219 } 220 } 221 | Popular Tags |