KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61
62 import org.objectstyle.cayenne.CayenneRuntimeException;
63 import org.objectstyle.cayenne.util.Util;
64 import org.objectstyle.cayenne.util.XMLEncoder;
65
66 /**
67  * DbEntity subclass that is based on another DbEntity
68  * and allows to define complex database expressions
69  * like GROUP BY and aggregate functions.
70  *
71  * @author Andrei Adamchik
72  */

73 public class DerivedDbEntity extends DbEntity {
74     protected String JavaDoc parentEntityName;
75
76     /**
77      * Constructor for DerivedDbEntity.
78      */

79     public DerivedDbEntity() {
80         super();
81     }
82
83     /**
84      * Constructor for DerivedDbEntity.
85      * @param name
86      */

87     public DerivedDbEntity(String JavaDoc name) {
88         super(name);
89     }
90
91     /**
92      * Constructor for DerivedDbEntity. Creates
93      * a derived entity with the attribute set of a parent entity.
94      */

95     public DerivedDbEntity(String JavaDoc name, DbEntity parentEntity) {
96         super(name);
97
98         this.setParentEntity(parentEntity);
99         this.resetToParentView();
100     }
101
102     /**
103      * Prints itself as XML to the provided XMLEncoder.
104      *
105      * @since 1.1
106      */

107     public void encodeAsXML(XMLEncoder encoder) {
108         encoder.print("<db-entity name=\"" + Util.encodeXmlAttribute(getName()));
109
110         if (getSchema() != null && getSchema().trim().length() > 0) {
111             encoder.print("\" schema=\"");
112             encoder.print(Util.encodeXmlAttribute(getSchema().trim()));
113         }
114
115         if (getCatalog() != null && getCatalog().trim().length() > 0) {
116             encoder.print("\" catalog=\"");
117             encoder.print(Util.encodeXmlAttribute(getCatalog().trim()));
118         }
119
120         encoder.print("\" parentName=\"");
121         encoder.print(Util.encodeXmlAttribute(getParentEntityName()));
122         encoder.println("\">");
123
124         encoder.indent(1);
125         encoder.print(getAttributeMap());
126         encoder.indent(-1);
127         encoder.println("</db-entity>");
128     }
129
130     /**
131      * Removes all attributes and relationships,
132      * and replaces them with the data of the parent entity.
133      */

134     public void resetToParentView() {
135         this.clearAttributes();
136         this.clearRelationships();
137
138         // copy attributes
139
Iterator JavaDoc it = getParentEntity().getAttributes().iterator();
140         while (it.hasNext()) {
141             this.addAttribute(new DerivedDbAttribute(this, (DbAttribute) it.next()));
142         }
143
144         // copy relationships
145
// Iterator rit = new ArrayList(this.getParentEntity().getRelationships()).iterator();
146
Iterator JavaDoc rit = this.getParentEntity().getRelationships().iterator();
147         while (rit.hasNext()) {
148             DbRelationship protoRel = (DbRelationship) rit.next();
149             DbRelationship rel = new DbRelationship();
150             rel.setName(protoRel.getName());
151             rel.setSourceEntity(this);
152             rel.setTargetEntity(protoRel.getTargetEntity());
153
154             Iterator JavaDoc joins = protoRel.getJoins().iterator();
155             while (joins.hasNext()) {
156                 DbJoin protoJoin = (DbJoin) joins.next();
157                 DbJoin join = new DbJoin(rel);
158                 join.setSourceName(protoJoin.getSourceName());
159                 join.setTargetName(protoJoin.getTargetName());
160                 rel.addJoin(join);
161             }
162
163             this.addRelationship(rel);
164         }
165     }
166
167     /**
168      * Returns the parentEntity.
169      *
170      * @return DbEntity
171      */

172     public DbEntity getParentEntity() {
173         if (parentEntityName == null) {
174             return null;
175         }
176
177         return getNonNullNamespace().getDbEntity(parentEntityName);
178     }
179
180     /**
181      * Sets the parent entity of this derived DbEntity.
182      */

183     public void setParentEntity(DbEntity parentEntity) {
184         setParentEntityName(parentEntity != null ? parentEntity.getName() : null);
185     }
186
187     /**
188      * Returns attributes used in GROUP BY as an unmodifiable list.
189      */

190     public List JavaDoc getGroupByAttributes() {
191         List JavaDoc list = new ArrayList JavaDoc();
192         Iterator JavaDoc it = super.getAttributes().iterator();
193         while (it.hasNext()) {
194             DerivedDbAttribute attr = (DerivedDbAttribute) it.next();
195             if (attr.isGroupBy()) {
196                 list.add(attr);
197             }
198         }
199         return list;
200     }
201
202     /**
203      * @see org.objectstyle.cayenne.map.DbEntity#getFullyQualifiedName()
204      */

205     public String JavaDoc getFullyQualifiedName() {
206         return (getParentEntity() != null)
207             ? getParentEntity().getFullyQualifiedName()
208             : null;
209     }
210
211     /**
212      * Returns schema of the parent entity.
213      */

214     public String JavaDoc getSchema() {
215         return (getParentEntity() != null) ? getParentEntity().getSchema() : null;
216     }
217
218     /** Throws exception. */
219     public void setSchema(String JavaDoc schema) {
220         throw new CayenneRuntimeException("Can't change schema of a derived entity.");
221     }
222
223     /**
224      * Returns catalog of the parent entity.
225      */

226     public String JavaDoc getCatalog() {
227         return (getParentEntity() != null) ? getParentEntity().getCatalog() : null;
228     }
229
230     /** Throws exception. */
231     public void setCatalog(String JavaDoc catalog) {
232         throw new CayenneRuntimeException("Can't change catalogue of a derived entity.");
233     }
234
235     /**
236      * @see org.objectstyle.cayenne.map.Entity#removeAttribute(String)
237      */

238     public void removeAttribute(String JavaDoc attrName) {
239         super.removeAttribute(attrName);
240     }
241
242     /**
243      * Returns the parentEntityName.
244      * @return String
245      */

246     public String JavaDoc getParentEntityName() {
247         return parentEntityName;
248     }
249
250     /**
251      * Sets the parentEntityName.
252      * @param parentEntityName The parentEntityName to set
253      */

254     public void setParentEntityName(String JavaDoc parentEntityName) {
255         this.parentEntityName = parentEntityName;
256     }
257 }
258
Popular Tags