KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > wocompat > EOObjEntity


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.wocompat;
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 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import org.apache.cayenne.CayenneRuntimeException;
31 import org.apache.cayenne.exp.Expression;
32 import org.apache.cayenne.exp.ExpressionFactory;
33 import org.apache.cayenne.map.Entity;
34 import org.apache.cayenne.map.ObjEntity;
35 import org.apache.cayenne.query.Query;
36 import org.apache.commons.collections.Transformer;
37
38 /**
39  * An extension of ObjEntity used to accomodate extra EOModel entity properties.
40  *
41  * @author Andrus Adamchik
42  */

43 public class EOObjEntity extends ObjEntity {
44
45     protected boolean subclass;
46     protected boolean abstractEntity;
47
48     private Collection JavaDoc filteredQueries;
49     private Map JavaDoc eoMap;
50
51     public EOObjEntity() {
52     }
53
54     public EOObjEntity(String JavaDoc name) {
55         super(name);
56     }
57
58     /**
59      * @deprecated since 2.0 use setSubclass()
60      */

61     public void setHasSuperClass(boolean value) {
62         setSubclass(value);
63     }
64
65     /**
66      * @deprecated since 2.0 use isSubclass()
67      */

68     public boolean getHasSuperClass() {
69         return isSubclass();
70     }
71
72     /**
73      * @deprecated since 2.0 use setServerOnly()
74      */

75     public void setIsClientEntity(boolean value) {
76         setServerOnly(!value);
77     }
78
79     /**
80      * @deprecated since 2.0 use !isServerOnly()
81      */

82     public boolean getIsClientEntity() {
83         return !isServerOnly();
84     }
85
86     /**
87      * @deprecated since 2.0 use setAbstractEntity()
88      */

89     public void setIsAbstractEntity(boolean value) {
90         setAbstractEntity(value);
91     }
92
93     /**
94      * @deprecated since 2.0 use isAbstractEntity()
95      */

96     public boolean getIsAbstractEntity() {
97         return isAbstractEntity();
98     }
99
100     /**
101      * Returns stored EOQuery.
102      *
103      * @since 1.1
104      */

105     public EOQuery getEOQuery(String JavaDoc queryName) {
106         Query query = getDataMap().getQuery(qualifiedQueryName(queryName));
107         if (query instanceof EOQuery) {
108             return (EOQuery) query;
109         }
110
111         return null;
112     }
113
114     /**
115      * Overrides super to support translation of EO attributes that have no ObjAttributes.
116      *
117      * @since 1.2
118      */

119     public Expression translateToDbPath(Expression expression) {
120
121         if (expression == null) {
122             return null;
123         }
124
125         if (getDbEntity() == null) {
126             throw new CayenneRuntimeException(
127                     "Can't translate expression to DB_PATH, no DbEntity for '"
128                             + getName()
129                             + "'.");
130         }
131
132         // converts all OBJ_PATH expressions to DB_PATH expressions
133
// and pass control to the DB entity
134
return expression.transform(new DBPathConverter());
135     }
136
137     /**
138      * @since 1.2
139      */

140     // TODO: andrus, 5/27/2006 - make public after 1.2. Also maybe move entity
141
// initialization code from EOModelProcessor to this class, kind of like EOQuery does.
142
Map JavaDoc getEoMap() {
143         return eoMap;
144     }
145
146     /**
147      * @since 1.2
148      */

149     // TODO: andrus, 5/27/2006 - make public after 1.2. Also maybe move entity
150
// initialization code from EOModelProcessor to this class, kind of like EOQuery does.
151
void setEoMap(Map JavaDoc eoMap) {
152         this.eoMap = eoMap;
153     }
154
155     /**
156      * Returns a collection of queries for this entity.
157      *
158      * @since 1.1
159      */

160     public Collection JavaDoc getEOQueries() {
161         if (filteredQueries == null) {
162             Collection JavaDoc queries = getDataMap().getQueries();
163             if (queries.isEmpty()) {
164                 filteredQueries = Collections.EMPTY_LIST;
165             }
166             else {
167                 Map JavaDoc params = Collections.singletonMap("root", EOObjEntity.this);
168                 Expression filter = Expression
169                         .fromString("root = $root")
170                         .expWithParameters(params);
171
172                 filteredQueries = filter.filter(queries, new ArrayList JavaDoc());
173             }
174         }
175
176         return filteredQueries;
177     }
178
179     public boolean isAbstractEntity() {
180         return abstractEntity;
181     }
182
183     public void setAbstractEntity(boolean abstractEntity) {
184         this.abstractEntity = abstractEntity;
185     }
186
187     public boolean isSubclass() {
188         return subclass;
189     }
190
191     public void setSubclass(boolean subclass) {
192         this.subclass = subclass;
193     }
194
195     /**
196      * Translates query name local to the ObjEntity to the global name. This translation
197      * is needed since EOModels store queries by entity, while Cayenne DataMaps store them
198      * globally.
199      *
200      * @since 1.1
201      */

202     public String JavaDoc qualifiedQueryName(String JavaDoc queryName) {
203         return getName() + "_" + queryName;
204     }
205
206     /**
207      * @since 1.1
208      */

209     public String JavaDoc localQueryName(String JavaDoc qualifiedQueryName) {
210         return (qualifiedQueryName != null && qualifiedQueryName.startsWith(getName()
211                 + "_"))
212                 ? qualifiedQueryName.substring(getName().length() + 1)
213                 : qualifiedQueryName;
214     }
215
216     final class DBPathConverter implements Transformer {
217
218         public Object JavaDoc transform(Object JavaDoc input) {
219
220             if (!(input instanceof Expression)) {
221                 return input;
222             }
223
224             Expression expression = (Expression) input;
225
226             if (expression.getType() != Expression.OBJ_PATH) {
227                 return input;
228             }
229
230             // convert obj_path to db_path
231

232             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
233             EOObjEntity entity = EOObjEntity.this;
234             StringTokenizer JavaDoc toks = new StringTokenizer JavaDoc(expression.toString(), ".");
235             while (toks.hasMoreTokens() && entity != null) {
236                 String JavaDoc chunk = toks.nextToken();
237
238                 if (toks.hasMoreTokens()) {
239                     // this is a relationship
240
if (buffer.length() > 0) {
241                         buffer.append(Entity.PATH_SEPARATOR);
242                     }
243
244                     buffer.append(chunk);
245
246                     entity = (EOObjEntity) entity
247                             .getRelationship(chunk)
248                             .getTargetEntity();
249                 }
250                 // this is an attribute...
251
else {
252
253                     List JavaDoc attributes = (List JavaDoc) entity.getEoMap().get("attributes");
254                     Iterator JavaDoc it = attributes.iterator();
255                     while (it.hasNext()) {
256                         Map JavaDoc attribute = (Map JavaDoc) it.next();
257                         if (chunk.equals(attribute.get("name"))) {
258
259                             if (buffer.length() > 0) {
260                                 buffer.append(Entity.PATH_SEPARATOR);
261                             }
262
263                             buffer.append(attribute.get("columnName"));
264                             break;
265                         }
266                     }
267                 }
268             }
269
270             Expression exp = ExpressionFactory.expressionOfType(Expression.DB_PATH);
271             exp.setOperand(0, buffer.toString());
272             return exp;
273         }
274     }
275 }
276
Popular Tags