KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.map;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.Collection JavaDoc;
60 import java.util.Collections JavaDoc;
61 import java.util.Iterator JavaDoc;
62
63 import org.objectstyle.cayenne.DataRow;
64 import org.objectstyle.cayenne.exp.Expression;
65
66 /**
67  * A tree structure representing inheritance hierarchy
68  * of an ObjEntity and its subentities.
69  *
70  * @since 1.1
71  * @author Andrei Adamchik
72  */

73 public class EntityInheritanceTree {
74     protected ObjEntity entity;
75     protected Collection JavaDoc subentities;
76     protected Expression normalizedQualifier;
77
78     public EntityInheritanceTree(ObjEntity entity) {
79         this.entity = entity;
80     }
81
82     /**
83      * Returns a qualifier Expression that matches root entity
84      * of this tree and all its subentities.
85      */

86     public Expression qualifierForEntityAndSubclasses() {
87         Expression qualifier = entity.getDeclaredQualifier();
88
89         if (qualifier == null) {
90             // match all
91
return null;
92         }
93
94         if (subentities != null) {
95             Iterator JavaDoc it = subentities.iterator();
96             while (it.hasNext()) {
97                 EntityInheritanceTree child = (EntityInheritanceTree) it.next();
98                 Expression childQualifier = child.qualifierForEntityAndSubclasses();
99
100                 // if any child qualifier is null, just return null, since no filtering is possible
101
if (childQualifier == null) {
102                     return null;
103                 }
104
105                 qualifier = qualifier.orExp(childQualifier);
106             }
107         }
108
109         return qualifier;
110     }
111
112     /**
113      * Returns the deepest possible entity in the inheritance hierarchy
114      * that can be used to create objects from a given DataRow.
115      */

116     public ObjEntity entityMatchingRow(DataRow row) {
117         // match depth first
118
if (subentities != null) {
119             Iterator JavaDoc 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         // no qualifier ... matches all rows
140
return entity;
141     }
142
143     public void addChildNode(EntityInheritanceTree node) {
144         if (subentities == null) {
145             subentities = new ArrayList JavaDoc(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 JavaDoc getChildren() {
156         return (subentities != null) ? subentities : Collections.EMPTY_LIST;
157     }
158
159     public ObjEntity getEntity() {
160         return entity;
161     }
162
163     public Collection JavaDoc allAttributes() {
164         if (subentities == null) {
165             return entity.getAttributes();
166         }
167
168         Collection JavaDoc c = new ArrayList JavaDoc();
169         appendDeclaredAttributes(c);
170
171         // add base attributes if any
172
ObjEntity superEntity = entity.getSuperEntity();
173         if (superEntity != null) {
174             c.addAll(superEntity.getAttributes());
175         }
176
177         return c;
178     }
179
180     public Collection JavaDoc allRelationships() {
181         if (subentities == null) {
182             return entity.getRelationships();
183         }
184
185         Collection JavaDoc c = new ArrayList JavaDoc();
186         appendDeclaredRelationships(c);
187
188         // add base relationships if any
189
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 JavaDoc c) {
198         c.addAll(entity.getDeclaredAttributes());
199
200         if (subentities != null) {
201             Iterator JavaDoc 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 JavaDoc c) {
210         c.addAll(entity.getDeclaredRelationships());
211
212         if (subentities != null) {
213             Iterator JavaDoc it = subentities.iterator();
214             while (it.hasNext()) {
215                 EntityInheritanceTree child = (EntityInheritanceTree) it.next();
216                 child.appendDeclaredRelationships(c);
217             }
218         }
219     }
220 }
221
Popular Tags