KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > map > EntityInheritanceTree


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.map;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.apache.cayenne.DataRow;
28 import org.apache.cayenne.exp.Expression;
29
30 /**
31  * A tree structure representing inheritance hierarchy
32  * of an ObjEntity and its subentities.
33  *
34  * @since 1.1
35  * @author Andrus Adamchik
36  */

37 public class EntityInheritanceTree {
38     protected ObjEntity entity;
39     protected Collection JavaDoc subentities;
40     protected Expression normalizedQualifier;
41
42     public EntityInheritanceTree(ObjEntity entity) {
43         this.entity = entity;
44     }
45
46     /**
47      * Returns a qualifier Expression that matches root entity
48      * of this tree and all its subentities.
49      */

50     public Expression qualifierForEntityAndSubclasses() {
51         Expression qualifier = entity.getDeclaredQualifier();
52
53         if (qualifier == null) {
54             // match all
55
return null;
56         }
57
58         if (subentities != null) {
59             Iterator JavaDoc it = subentities.iterator();
60             while (it.hasNext()) {
61                 EntityInheritanceTree child = (EntityInheritanceTree) it.next();
62                 Expression childQualifier = child.qualifierForEntityAndSubclasses();
63
64                 // if any child qualifier is null, just return null, since no filtering is possible
65
if (childQualifier == null) {
66                     return null;
67                 }
68
69                 qualifier = qualifier.orExp(childQualifier);
70             }
71         }
72
73         return qualifier;
74     }
75
76     /**
77      * Returns the deepest possible entity in the inheritance hierarchy
78      * that can be used to create objects from a given DataRow.
79      */

80     public ObjEntity entityMatchingRow(DataRow row) {
81         // match depth first
82
if (subentities != null) {
83             Iterator JavaDoc 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         // no qualifier ... matches all rows
104
return entity;
105     }
106
107     public void addChildNode(EntityInheritanceTree node) {
108         if (subentities == null) {
109             subentities = new ArrayList JavaDoc(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 JavaDoc getChildren() {
120         return (subentities != null) ? subentities : Collections.EMPTY_LIST;
121     }
122
123     public ObjEntity getEntity() {
124         return entity;
125     }
126
127     public Collection JavaDoc allAttributes() {
128         if (subentities == null) {
129             return entity.getAttributes();
130         }
131
132         Collection JavaDoc c = new ArrayList JavaDoc();
133         appendDeclaredAttributes(c);
134
135         // add base attributes if any
136
ObjEntity superEntity = entity.getSuperEntity();
137         if (superEntity != null) {
138             c.addAll(superEntity.getAttributes());
139         }
140
141         return c;
142     }
143
144     public Collection JavaDoc allRelationships() {
145         if (subentities == null) {
146             return entity.getRelationships();
147         }
148
149         Collection JavaDoc c = new ArrayList JavaDoc();
150         appendDeclaredRelationships(c);
151
152         // add base relationships if any
153
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 JavaDoc c) {
162         c.addAll(entity.getDeclaredAttributes());
163
164         if (subentities != null) {
165             Iterator JavaDoc 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 JavaDoc c) {
174         c.addAll(entity.getDeclaredRelationships());
175
176         if (subentities != null) {
177             Iterator JavaDoc it = subentities.iterator();
178             while (it.hasNext()) {
179                 EntityInheritanceTree child = (EntityInheritanceTree) it.next();
180                 child.appendDeclaredRelationships(c);
181             }
182         }
183     }
184 }
185
Popular Tags