KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > map > JpaAttributes


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 package org.apache.cayenne.jpa.map;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23
24 import org.apache.cayenne.util.TreeNodeChild;
25
26 /**
27  * An attribute container.
28  *
29  * @author Andrus Adamchik
30  */

31 public class JpaAttributes {
32
33     protected Collection JavaDoc<JpaId> ids;
34     protected JpaEmbeddedId embeddedId;
35     protected Collection JavaDoc<JpaBasic> basicAttributes;
36     protected Collection JavaDoc<JpaVersion> versionAttributes;
37     protected Collection JavaDoc<JpaManyToOne> manyToOneRelationships;
38     protected Collection JavaDoc<JpaOneToMany> oneToManyRelationships;
39     protected Collection JavaDoc<JpaOneToOne> oneToOneRelationships;
40     protected Collection JavaDoc<JpaManyToMany> manyToManyRelationships;
41     protected Collection JavaDoc<JpaEmbedded> embeddedAttributes;
42     protected Collection JavaDoc<JpaTransient> transientAttributes;
43
44     public JpaAttribute getAttribute(String JavaDoc name) {
45         if (name == null) {
46             return null;
47         }
48
49         if (embeddedId != null && name.equals(embeddedId.getName())) {
50             return embeddedId;
51         }
52
53         JpaAttribute attribute;
54
55         attribute = getId(name);
56         if (attribute != null) {
57             return attribute;
58         }
59
60         attribute = getBasicAttribute(name);
61         if (attribute != null) {
62             return attribute;
63         }
64
65         attribute = getVersionAttribute(name);
66         if (attribute != null) {
67             return attribute;
68         }
69
70         attribute = getManyToOneRelationship(name);
71         if (attribute != null) {
72             return attribute;
73         }
74
75         attribute = getOneToManyRelationship(name);
76         if (attribute != null) {
77             return attribute;
78         }
79
80         attribute = getOneToOneRelationship(name);
81         if (attribute != null) {
82             return attribute;
83         }
84
85         attribute = getManyToManyRelationship(name);
86         if (attribute != null) {
87             return attribute;
88         }
89
90         attribute = getTransientAttribute(name);
91         if (attribute != null) {
92             return attribute;
93         }
94
95         attribute = getEmbeddedAttribute(name);
96         if (attribute != null) {
97             return attribute;
98         }
99
100         return null;
101     }
102
103     /**
104      * Returns combined count of all attributes and relationships.
105      */

106     public int size() {
107         int size = 0;
108
109         if (embeddedId != null) {
110             size++;
111         }
112
113         if (ids != null) {
114             size += ids.size();
115         }
116         if (basicAttributes != null) {
117             size += basicAttributes.size();
118         }
119         if (versionAttributes != null) {
120             size += versionAttributes.size();
121         }
122         if (manyToOneRelationships != null) {
123             size += manyToOneRelationships.size();
124         }
125         if (oneToManyRelationships != null) {
126             size += oneToManyRelationships.size();
127         }
128         if (oneToOneRelationships != null) {
129             size += oneToOneRelationships.size();
130         }
131         if (manyToManyRelationships != null) {
132             size += manyToManyRelationships.size();
133         }
134         if (embeddedAttributes != null) {
135             size += embeddedAttributes.size();
136         }
137         if (transientAttributes != null) {
138             size += transientAttributes.size();
139         }
140         return size;
141     }
142
143     public JpaId getId(String JavaDoc idName) {
144         if (idName == null) {
145             throw new IllegalArgumentException JavaDoc("Null id name");
146         }
147
148         if (ids != null) {
149             for (JpaId id : ids) {
150                 if (idName.equals(id.getName())) {
151                     return id;
152                 }
153             }
154         }
155
156         return null;
157     }
158
159     /**
160      * Returns a JpaAttribute for a given property name
161      */

162     public JpaBasic getBasicAttribute(String JavaDoc attributeName) {
163         if (attributeName == null) {
164             throw new IllegalArgumentException JavaDoc("Null attribute name");
165         }
166
167         if (basicAttributes != null) {
168             for (JpaBasic attribute : basicAttributes) {
169                 if (attributeName.equals(attribute.getName())) {
170                     return attribute;
171                 }
172             }
173         }
174
175         return null;
176     }
177
178     public JpaManyToOne getManyToOneRelationship(String JavaDoc attributeName) {
179         if (attributeName == null) {
180             throw new IllegalArgumentException JavaDoc("Null attribute name");
181         }
182
183         if (manyToOneRelationships != null) {
184             for (JpaManyToOne attribute : manyToOneRelationships) {
185                 if (attributeName.equals(attribute.getName())) {
186                     return attribute;
187                 }
188             }
189         }
190
191         return null;
192     }
193
194     public JpaOneToMany getOneToManyRelationship(String JavaDoc attributeName) {
195         if (attributeName == null) {
196             throw new IllegalArgumentException JavaDoc("Null attribute name");
197         }
198
199         if (oneToManyRelationships != null) {
200             for (JpaOneToMany attribute : oneToManyRelationships) {
201                 if (attributeName.equals(attribute.getName())) {
202                     return attribute;
203                 }
204             }
205         }
206
207         return null;
208     }
209
210     @TreeNodeChild(type = JpaId.class)
211     public Collection JavaDoc<JpaId> getIds() {
212         if (ids == null) {
213             ids = new ArrayList JavaDoc<JpaId>();
214         }
215
216         return ids;
217     }
218
219     @TreeNodeChild
220     public JpaEmbeddedId getEmbeddedId() {
221         return embeddedId;
222     }
223
224     public void setEmbeddedId(JpaEmbeddedId embeddedId) {
225         this.embeddedId = embeddedId;
226     }
227
228     @TreeNodeChild(type = JpaBasic.class)
229     public Collection JavaDoc<JpaBasic> getBasicAttributes() {
230         if (basicAttributes == null) {
231             basicAttributes = new ArrayList JavaDoc<JpaBasic>();
232         }
233         return basicAttributes;
234     }
235
236     @TreeNodeChild(type = JpaEmbedded.class)
237     public Collection JavaDoc<JpaEmbedded> getEmbeddedAttributes() {
238         if (embeddedAttributes == null) {
239             embeddedAttributes = new ArrayList JavaDoc<JpaEmbedded>();
240         }
241         return embeddedAttributes;
242     }
243
244     public JpaEmbedded getEmbeddedAttribute(String JavaDoc attributeName) {
245         if (attributeName == null) {
246             throw new IllegalArgumentException JavaDoc("Null attribute name");
247         }
248
249         if (embeddedAttributes != null) {
250             for (JpaEmbedded attribute : embeddedAttributes) {
251                 if (attributeName.equals(attribute.getName())) {
252                     return attribute;
253                 }
254             }
255         }
256
257         return null;
258     }
259
260     @TreeNodeChild(type = JpaManyToMany.class)
261     public Collection JavaDoc<JpaManyToMany> getManyToManyRelationships() {
262         if (manyToManyRelationships == null) {
263             manyToManyRelationships = new ArrayList JavaDoc<JpaManyToMany>();
264         }
265         return manyToManyRelationships;
266     }
267
268     public JpaManyToMany getManyToManyRelationship(String JavaDoc attributeName) {
269         if (attributeName == null) {
270             throw new IllegalArgumentException JavaDoc("Null attribute name");
271         }
272
273         if (manyToManyRelationships != null) {
274             for (JpaManyToMany attribute : manyToManyRelationships) {
275                 if (attributeName.equals(attribute.getName())) {
276                     return attribute;
277                 }
278             }
279         }
280
281         return null;
282     }
283
284     @TreeNodeChild(type = JpaManyToOne.class)
285     public Collection JavaDoc<JpaManyToOne> getManyToOneRelationships() {
286         if (manyToOneRelationships == null) {
287             manyToOneRelationships = new ArrayList JavaDoc<JpaManyToOne>();
288         }
289         return manyToOneRelationships;
290     }
291
292     @TreeNodeChild(type = JpaOneToMany.class)
293     public Collection JavaDoc<JpaOneToMany> getOneToManyRelationships() {
294         if (oneToManyRelationships == null) {
295             oneToManyRelationships = new ArrayList JavaDoc<JpaOneToMany>();
296         }
297         return oneToManyRelationships;
298     }
299
300     @TreeNodeChild(type = JpaOneToOne.class)
301     public Collection JavaDoc<JpaOneToOne> getOneToOneRelationships() {
302         if (oneToOneRelationships == null) {
303             oneToOneRelationships = new ArrayList JavaDoc<JpaOneToOne>();
304         }
305         return oneToOneRelationships;
306     }
307
308     public JpaOneToOne getOneToOneRelationship(String JavaDoc attributeName) {
309         if (attributeName == null) {
310             throw new IllegalArgumentException JavaDoc("Null attribute name");
311         }
312
313         if (oneToOneRelationships != null) {
314             for (JpaOneToOne attribute : oneToOneRelationships) {
315                 if (attributeName.equals(attribute.getName())) {
316                     return attribute;
317                 }
318             }
319         }
320
321         return null;
322     }
323
324     @TreeNodeChild(type = JpaTransient.class)
325     public Collection JavaDoc<JpaTransient> getTransientAttributes() {
326         if (transientAttributes == null) {
327             transientAttributes = new ArrayList JavaDoc<JpaTransient>();
328         }
329         return transientAttributes;
330     }
331
332     /**
333      * Returns a JpaTransient for a given property name
334      */

335     public JpaTransient getTransientAttribute(String JavaDoc attributeName) {
336         if (attributeName == null) {
337             throw new IllegalArgumentException JavaDoc("Null attribute name");
338         }
339
340         if (transientAttributes != null) {
341             for (JpaTransient attribute : transientAttributes) {
342                 if (attributeName.equals(attribute.getName())) {
343                     return attribute;
344                 }
345             }
346         }
347
348         return null;
349     }
350
351     @TreeNodeChild(type = JpaVersion.class)
352     public Collection JavaDoc<JpaVersion> getVersionAttributes() {
353         if (versionAttributes == null) {
354             versionAttributes = new ArrayList JavaDoc<JpaVersion>();
355         }
356         return versionAttributes;
357     }
358
359     /**
360      * Returns a JpaTransient for a given property name
361      */

362     public JpaVersion getVersionAttribute(String JavaDoc attributeName) {
363         if (attributeName == null) {
364             throw new IllegalArgumentException JavaDoc("Null attribute name");
365         }
366
367         if (versionAttributes != null) {
368             for (JpaVersion attribute : versionAttributes) {
369                 if (attributeName.equals(attribute.getName())) {
370                     return attribute;
371                 }
372             }
373         }
374
375         return null;
376     }
377 }
378
Popular Tags