KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.cayenne.CayenneRuntimeException;
27 import org.apache.cayenne.util.Util;
28 import org.apache.cayenne.util.XMLEncoder;
29
30 /**
31  * DbEntity subclass that is based on another DbEntity
32  * and allows to define complex database expressions
33  * like GROUP BY and aggregate functions.
34  *
35  * @author Andrus Adamchik
36  */

37 public class DerivedDbEntity extends DbEntity {
38     protected String JavaDoc parentEntityName;
39
40     /**
41      * Constructor for DerivedDbEntity.
42      */

43     public DerivedDbEntity() {
44         super();
45     }
46
47     /**
48      * Constructor for DerivedDbEntity.
49      * @param name
50      */

51     public DerivedDbEntity(String JavaDoc name) {
52         super(name);
53     }
54
55     /**
56      * Constructor for DerivedDbEntity. Creates
57      * a derived entity with the attribute set of a parent entity.
58      */

59     public DerivedDbEntity(String JavaDoc name, DbEntity parentEntity) {
60         super(name);
61
62         this.setParentEntity(parentEntity);
63         this.resetToParentView();
64     }
65
66     /**
67      * Prints itself as XML to the provided XMLEncoder.
68      *
69      * @since 1.1
70      */

71     public void encodeAsXML(XMLEncoder encoder) {
72         encoder.print("<db-entity name=\"" + Util.encodeXmlAttribute(getName()));
73
74         if (getSchema() != null && getSchema().trim().length() > 0) {
75             encoder.print("\" schema=\"");
76             encoder.print(Util.encodeXmlAttribute(getSchema().trim()));
77         }
78
79         if (getCatalog() != null && getCatalog().trim().length() > 0) {
80             encoder.print("\" catalog=\"");
81             encoder.print(Util.encodeXmlAttribute(getCatalog().trim()));
82         }
83
84         encoder.print("\" parentName=\"");
85         encoder.print(Util.encodeXmlAttribute(getParentEntityName()));
86         encoder.println("\">");
87
88         encoder.indent(1);
89         encoder.print(getAttributeMap());
90         encoder.indent(-1);
91         encoder.println("</db-entity>");
92     }
93
94     /**
95      * Removes all attributes and relationships,
96      * and replaces them with the data of the parent entity.
97      */

98     public void resetToParentView() {
99         this.clearAttributes();
100         this.clearRelationships();
101
102         // copy attributes
103
Iterator JavaDoc it = getParentEntity().getAttributes().iterator();
104         while (it.hasNext()) {
105             this.addAttribute(new DerivedDbAttribute(this, (DbAttribute) it.next()));
106         }
107
108         // copy relationships
109
// Iterator rit = new ArrayList(this.getParentEntity().getRelationships()).iterator();
110
Iterator JavaDoc rit = this.getParentEntity().getRelationships().iterator();
111         while (rit.hasNext()) {
112             DbRelationship protoRel = (DbRelationship) rit.next();
113             DbRelationship rel = new DbRelationship();
114             rel.setName(protoRel.getName());
115             rel.setSourceEntity(this);
116             rel.setTargetEntity(protoRel.getTargetEntity());
117
118             Iterator JavaDoc joins = protoRel.getJoins().iterator();
119             while (joins.hasNext()) {
120                 DbJoin protoJoin = (DbJoin) joins.next();
121                 DbJoin join = new DbJoin(rel);
122                 join.setSourceName(protoJoin.getSourceName());
123                 join.setTargetName(protoJoin.getTargetName());
124                 rel.addJoin(join);
125             }
126
127             this.addRelationship(rel);
128         }
129     }
130
131     /**
132      * Returns the parentEntity.
133      *
134      * @return DbEntity
135      */

136     public DbEntity getParentEntity() {
137         if (parentEntityName == null) {
138             return null;
139         }
140
141         return getNonNullNamespace().getDbEntity(parentEntityName);
142     }
143
144     /**
145      * Sets the parent entity of this derived DbEntity.
146      */

147     public void setParentEntity(DbEntity parentEntity) {
148         setParentEntityName(parentEntity != null ? parentEntity.getName() : null);
149     }
150
151     /**
152      * Returns attributes used in GROUP BY as an unmodifiable list.
153      */

154     public List JavaDoc getGroupByAttributes() {
155         List JavaDoc list = new ArrayList JavaDoc();
156         Iterator JavaDoc it = super.getAttributes().iterator();
157         while (it.hasNext()) {
158             DerivedDbAttribute attr = (DerivedDbAttribute) it.next();
159             if (attr.isGroupBy()) {
160                 list.add(attr);
161             }
162         }
163         return list;
164     }
165
166     /**
167      * @see org.apache.cayenne.map.DbEntity#getFullyQualifiedName()
168      */

169     public String JavaDoc getFullyQualifiedName() {
170         return (getParentEntity() != null)
171             ? getParentEntity().getFullyQualifiedName()
172             : null;
173     }
174
175     /**
176      * Returns schema of the parent entity.
177      */

178     public String JavaDoc getSchema() {
179         return (getParentEntity() != null) ? getParentEntity().getSchema() : null;
180     }
181
182     /** Throws exception. */
183     public void setSchema(String JavaDoc schema) {
184         throw new CayenneRuntimeException("Can't change schema of a derived entity.");
185     }
186
187     /**
188      * Returns catalog of the parent entity.
189      */

190     public String JavaDoc getCatalog() {
191         return (getParentEntity() != null) ? getParentEntity().getCatalog() : null;
192     }
193
194     /** Throws exception. */
195     public void setCatalog(String JavaDoc catalog) {
196         throw new CayenneRuntimeException("Can't change catalogue of a derived entity.");
197     }
198
199     /**
200      * @see org.apache.cayenne.map.Entity#removeAttribute(String)
201      */

202     public void removeAttribute(String JavaDoc attrName) {
203         super.removeAttribute(attrName);
204     }
205
206     /**
207      * Returns the parentEntityName.
208      * @return String
209      */

210     public String JavaDoc getParentEntityName() {
211         return parentEntityName;
212     }
213
214     /**
215      * Sets the parentEntityName.
216      * @param parentEntityName The parentEntityName to set
217      */

218     public void setParentEntityName(String JavaDoc parentEntityName) {
219         this.parentEntityName = parentEntityName;
220     }
221 }
222
Popular Tags