Source code: org/objectstyle/cayenne/map/DerivedDbEntity.java
1 /* ====================================================================
2 *
3 * The ObjectStyle Group Software License, Version 1.0
4 *
5 * Copyright (c) 2002-2003 The ObjectStyle Group
6 * and individual authors of the software. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution, if
21 * any, must include the following acknowlegement:
22 * "This product includes software developed by the
23 * ObjectStyle Group (http://objectstyle.org/)."
24 * Alternately, this acknowlegement may appear in the software itself,
25 * if and wherever such third-party acknowlegements normally appear.
26 *
27 * 4. The names "ObjectStyle Group" and "Cayenne"
28 * must not be used to endorse or promote products derived
29 * from this software without prior written permission. For written
30 * permission, please contact andrus@objectstyle.org.
31 *
32 * 5. Products derived from this software may not be called "ObjectStyle"
33 * nor may "ObjectStyle" appear in their names without prior written
34 * permission of the ObjectStyle Group.
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of the ObjectStyle Group. For more
52 * information on the ObjectStyle Group, please see
53 * <http://objectstyle.org/>.
54 *
55 */
56 package org.objectstyle.cayenne.map;
57
58 import java.util.ArrayList;
59 import java.util.Iterator;
60 import java.util.List;
61
62 import org.objectstyle.cayenne.CayenneRuntimeException;
63
64 /**
65 * DbEntity subclass that is based on another DbEntity
66 * and allows to define complex database expressions
67 * like GROUP BY and aggregate functions.
68 *
69 * @author Andrei Adamchik
70 */
71 public class DerivedDbEntity extends DbEntity {
72 protected String parentEntityName;
73
74 /**
75 * Constructor for DerivedDbEntity.
76 */
77 public DerivedDbEntity() {
78 super();
79 }
80
81 /**
82 * Constructor for DerivedDbEntity.
83 * @param name
84 */
85 public DerivedDbEntity(String name) {
86 super(name);
87 }
88
89 /**
90 * Constructor for DerivedDbEntity. Creates
91 * a derived entity with the attribute set of a parent entity.
92 */
93 public DerivedDbEntity(String name, DbEntity parentEntity) {
94 super(name);
95
96 this.setParentEntity(parentEntity);
97 this.resetToParentView();
98 }
99
100 /**
101 * Removes all attributes and relationships,
102 * and replaces them with the data of the parent entity.
103 */
104 public void resetToParentView() {
105 this.clearAttributes();
106 this.clearRelationships();
107
108 // copy attributes
109 Iterator it = getParentEntity().getAttributes().iterator();
110 while (it.hasNext()) {
111 this.addAttribute(new DerivedDbAttribute(this, (DbAttribute)it.next()));
112 }
113
114 // copy relationships
115 // Iterator rit = new ArrayList(this.getParentEntity().getRelationships()).iterator();
116 Iterator rit = this.getParentEntity().getRelationships().iterator();
117 while (rit.hasNext()) {
118 DbRelationship protoRel = (DbRelationship) rit.next();
119 DbRelationship rel = new DbRelationship();
120 rel.setName(protoRel.getName());
121 rel.setSourceEntity(this);
122 rel.setTargetEntity(protoRel.getTargetEntity());
123
124 Iterator joins = protoRel.getJoins().iterator();
125 while (joins.hasNext()) {
126 DbAttributePair protoJoin = (DbAttributePair) joins.next();
127 DbAttribute src = (DbAttribute)getAttribute(protoJoin.getSource().getName());
128 DbAttributePair join = new DbAttributePair(src, protoJoin.getTarget());
129 rel.addJoin(join);
130 }
131
132 this.addRelationship(rel);
133 }
134 }
135
136 /**
137 * Returns the parentEntity.
138 *
139 * @return DbEntity
140 */
141 public DbEntity getParentEntity() {
142 if(parentEntityName == null) {
143 return null;
144 }
145
146 DataMap map = getDataMap();
147 if(map == null) {
148 return null;
149 }
150
151 return map.getDbEntity(parentEntityName, true);
152 }
153
154 /**
155 * Sets the parentEntity.
156 *
157 * @param parentEntity The parentEntity to set
158 */
159 public void setParentEntity(DbEntity parentEntity) {
160 if(parentEntity == null) {
161 setParentEntityName(null);
162 }
163 else {
164 setParentEntityName(parentEntity.getName());
165 }
166 }
167
168 /**
169 * Returns attributes used in GROUP BY as an unmodifiable list.
170 */
171 public List getGroupByAttributes() {
172 List list = new ArrayList();
173 Iterator it = super.getAttributes().iterator();
174 while(it.hasNext()) {
175 DerivedDbAttribute attr = (DerivedDbAttribute)it.next();
176 if(attr.isGroupBy()) {
177 list.add(attr);
178 }
179 }
180 return list;
181 }
182
183 /**
184 * @see org.objectstyle.cayenne.map.DbEntity#getFullyQualifiedName()
185 */
186 public String getFullyQualifiedName() {
187 return (getParentEntity() != null)
188 ? getParentEntity().getFullyQualifiedName()
189 : null;
190 }
191
192 /**
193 * Returns schema of the parent entity.
194 */
195 public String getSchema() {
196 return (getParentEntity() != null)
197 ? getParentEntity().getSchema()
198 : null;
199 }
200
201 /** Throws exception. */
202 public void setSchema(String schema) {
203 throw new CayenneRuntimeException("Can't change schema of a derived entity.");
204 }
205
206 /**
207 * Returns catalog of the parent entity.
208 */
209 public String getCatalog() {
210 return (getParentEntity() != null)
211 ? getParentEntity().getCatalog()
212 : null;
213 }
214
215 /** Throws exception. */
216 public void setCatalog(String catalog) {
217 throw new CayenneRuntimeException("Can't change catalogue of a derived entity.");
218 }
219
220 /**
221 * @see org.objectstyle.cayenne.map.Entity#removeAttribute(String)
222 */
223 public void removeAttribute(String attrName) {
224 super.removeAttribute(attrName);
225 }
226
227 /**
228 * Returns the parentEntityName.
229 * @return String
230 */
231 public String getParentEntityName() {
232 return parentEntityName;
233 }
234
235
236 /**
237 * Sets the parentEntityName.
238 * @param parentEntityName The parentEntityName to set
239 */
240 public void setParentEntityName(String parentEntityName) {
241 this.parentEntityName = parentEntityName;
242 }
243 }