KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > gen > EntityUtils


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
21 package org.apache.cayenne.gen;
22
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.cayenne.map.DataMap;
26 import org.apache.cayenne.map.MappingNamespace;
27 import org.apache.cayenne.map.ObjEntity;
28 import org.apache.cayenne.map.Relationship;
29
30 /**
31  * Attributes and Methods for working with ObjEntities.
32  *
33  * @since 1.2
34  * @author Mike Kienenberger
35  */

36 public class EntityUtils {
37
38     // template substitution values
39
protected String JavaDoc subClassName;
40     protected String JavaDoc superClassName;
41     protected String JavaDoc baseClassName;
42     protected String JavaDoc subPackageName;
43     protected String JavaDoc superPackageName;
44     protected String JavaDoc basePackageName;
45
46     protected DataMap primaryDataMap;
47     protected ObjEntity objEntity;
48
49     public EntityUtils(DataMap dataMap, ObjEntity objEntity, String JavaDoc fqnBaseClass,
50             String JavaDoc fqnSuperClass, String JavaDoc fqnSubClass) {
51         super();
52
53         StringUtils stringUtils = StringUtils.getInstance();
54
55         this.baseClassName = stringUtils.stripPackageName(fqnBaseClass);
56         this.basePackageName = stringUtils.stripClass(fqnBaseClass);
57         this.superClassName = stringUtils.stripPackageName(fqnSuperClass);
58         this.superPackageName = stringUtils.stripClass(fqnSuperClass);
59         this.subClassName = stringUtils.stripPackageName(fqnSubClass);
60         this.subPackageName = stringUtils.stripClass(fqnSubClass);
61
62         this.primaryDataMap = dataMap;
63
64         this.objEntity = objEntity;
65     }
66
67     /**
68      * Returns class name (without a package) of the sub class associated with this
69      * generator.
70      */

71     public String JavaDoc getSubClassName() {
72         return subClassName;
73     }
74
75     /**
76      * Returns the super class (without a package) of the data object class associated
77      * with this generator
78      */

79     public String JavaDoc getSuperClassName() {
80         return superClassName;
81     }
82
83     /**
84      * Returns the base class (without a package) of the data object class associated with
85      * this generator. Class name must not include a package.
86      */

87     public String JavaDoc getBaseClassName() {
88         return baseClassName;
89     }
90
91     /**
92      * Returns Java package name of the class associated with this generator.
93      */

94     public String JavaDoc getSubPackageName() {
95         return subPackageName;
96     }
97
98     /**
99      * Returns <code>superPackageName</code> property that defines a superclass's
100      * package name.
101      */

102     public String JavaDoc getSuperPackageName() {
103         return superPackageName;
104     }
105
106     /**
107      * Returns <code>basePackageName</code> property that defines a baseclass's
108      * (superclass superclass) package name.
109      */

110     public String JavaDoc getBasePackageName() {
111         return basePackageName;
112     }
113
114     /**
115      * @return Returns the primary DataMap.
116      * @since 1.2
117      */

118     public DataMap getPrimaryDataMap() {
119         return primaryDataMap;
120     }
121
122     /**
123      * Returns the EntityResolver for this set of DataMaps.
124      *
125      * @since 1.2
126      */

127     public MappingNamespace getEntityResolver() {
128         return primaryDataMap.getNamespace();
129     }
130
131     /**
132      * Returns true if current ObjEntity contains at least one toMany relationship.
133      */

134     public boolean hasToManyRelationships() {
135         return hasToManyRelationships(objEntity);
136     }
137
138     /**
139      * Returns true if an ObjEntity contains at least one toMany relationship.
140      */

141     public boolean hasToManyRelationships(ObjEntity anObjEntity) {
142         if (anObjEntity == null) {
143             return false;
144         }
145
146         Iterator JavaDoc it = anObjEntity.getRelationships().iterator();
147         while (it.hasNext()) {
148             Relationship r = (Relationship) it.next();
149             if (r.isToMany()) {
150                 return true;
151             }
152         }
153
154         return false;
155     }
156
157     /**
158      * Returns true if current ObjEntity contains at least one toMany relationship, ignoring
159      * those declared in superentities.
160      *
161      * @since 1.2
162      */

163     public boolean hasToManyDeclaredRelationships() {
164         return hasToManyDeclaredRelationships(objEntity);
165     }
166
167     /**
168      * Returns true if an ObjEntity contains at least one toMany relationship, ignoring
169      * those declared in superentities.
170      *
171      * @since 1.2
172      */

173     public boolean hasToManyDeclaredRelationships(ObjEntity anObjEntity) {
174         if (anObjEntity == null) {
175             return false;
176         }
177
178         Iterator JavaDoc it = anObjEntity.getDeclaredRelationships().iterator();
179         while (it.hasNext()) {
180             Relationship r = (Relationship) it.next();
181             if (r.isToMany()) {
182                 return true;
183             }
184         }
185
186         return false;
187     }
188
189     /**
190      * Returns true if current ObjEntity contains at least one toOne relationship.
191      */

192     public boolean hasToOneRelationships() {
193         return hasToOneRelationships(objEntity);
194     }
195
196     /**
197      * Returns true if an ObjEntity contains at least one toOne relationship.
198      */

199     public boolean hasToOneRelationships(ObjEntity anObjEntity) {
200         if (anObjEntity == null) {
201             return false;
202         }
203
204         Iterator JavaDoc it = anObjEntity.getRelationships().iterator();
205         while (it.hasNext()) {
206             Relationship r = (Relationship) it.next();
207             if (false == r.isToMany()) {
208                 return true;
209             }
210         }
211
212         return false;
213     }
214
215     /**
216      * Returns true if current ObjEntity contains at least one toOne relationship, ignoring
217      * those declared in superentities.
218      */

219     public boolean hasToOneDeclaredRelationships() {
220         return hasToOneDeclaredRelationships(objEntity);
221     }
222
223     /**
224      * Returns true if an ObjEntity contains at least one toOne relationship, ignoring
225      * those declared in superentities.
226      */

227     public boolean hasToOneDeclaredRelationships(ObjEntity anObjEntity) {
228         if (anObjEntity == null) {
229             return false;
230         }
231
232         Iterator JavaDoc it = anObjEntity.getDeclaredRelationships().iterator();
233         while (it.hasNext()) {
234             Relationship r = (Relationship) it.next();
235             if (!r.isToMany()) {
236                 return true;
237             }
238         }
239
240         return false;
241     }
242 }
243
Popular Tags