KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > PersistentDescriptorFactory


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.reflect;
20
21 import java.util.Iterator JavaDoc;
22
23 import org.apache.cayenne.CayenneRuntimeException;
24 import org.apache.cayenne.map.EmbeddedAttribute;
25 import org.apache.cayenne.map.EntityInheritanceTree;
26 import org.apache.cayenne.map.ObjAttribute;
27 import org.apache.cayenne.map.ObjEntity;
28 import org.apache.cayenne.map.ObjRelationship;
29
30 /**
31  * A convenience superclass for {@link ClassDescriptorFactory} implementors.
32  *
33  * @since 3.0
34  * @author Andrus Adamchik
35  */

36 public abstract class PersistentDescriptorFactory implements ClassDescriptorFactory {
37
38     protected ClassDescriptorMap descriptorMap;
39
40     public PersistentDescriptorFactory(ClassDescriptorMap descriptorMap) {
41         this.descriptorMap = descriptorMap;
42     }
43
44     public ClassDescriptor getDescriptor(String JavaDoc entityName) {
45         ObjEntity entity = descriptorMap.getResolver().getObjEntity(entityName);
46         if (entity == null) {
47             throw new CayenneRuntimeException("Unmapped entity: " + entityName);
48         }
49
50         Class JavaDoc entityClass = entity.getJavaClass();
51         return getDescriptor(entity, entityClass);
52     }
53
54     protected ClassDescriptor getDescriptor(ObjEntity entity, Class JavaDoc entityClass) {
55         String JavaDoc superEntityName = entity.getSuperEntityName();
56
57         ClassDescriptor superDescriptor = (superEntityName != null) ? descriptorMap
58                 .getDescriptor(superEntityName) : null;
59
60         PersistentDescriptor descriptor = createDescriptor();
61
62         descriptor.setEntity(entity);
63         descriptor.setSuperclassDescriptor(superDescriptor);
64         descriptor.setObjectClass(entityClass);
65         descriptor.setPersistenceStateAccessor(new BeanAccessor(
66                 entityClass,
67                 "persistenceState",
68                 Integer.TYPE));
69
70         // only include this entity attributes and skip superclasses...
71
Iterator JavaDoc attributes = descriptor.getEntity().getDeclaredAttributes().iterator();
72         while (attributes.hasNext()) {
73             Object JavaDoc attribute = attributes.next();
74
75             if (attribute instanceof ObjAttribute) {
76                 createAttributeProperty(descriptor, (ObjAttribute) attribute);
77             }
78             else if (attribute instanceof EmbeddedAttribute) {
79                 EmbeddedAttribute embedded = (EmbeddedAttribute) attribute;
80                 Iterator JavaDoc embeddedAttributes = embedded.getAttributes().iterator();
81                 while (embeddedAttributes.hasNext()) {
82                     createEmbeddedAttributeProperty(
83                             descriptor,
84                             embedded,
85                             (ObjAttribute) embeddedAttributes.next());
86                 }
87             }
88         }
89
90         // only include this entity relationships and skip superclasses...
91
Iterator JavaDoc it = descriptor.getEntity().getDeclaredRelationships().iterator();
92         while (it.hasNext()) {
93
94             ObjRelationship relationship = (ObjRelationship) it.next();
95             if (relationship.isToMany()) {
96                 createToManyProperty(descriptor, relationship);
97             }
98             else {
99                 createToOneProperty(descriptor, relationship);
100             }
101         }
102
103         EntityInheritanceTree inheritanceTree = descriptorMap
104                 .getResolver()
105                 .lookupInheritanceTree(descriptor.getEntity());
106         indexSubclassDescriptors(descriptor, inheritanceTree);
107
108         return descriptor;
109     }
110
111     protected PersistentDescriptor createDescriptor() {
112         return new PersistentDescriptor();
113     }
114
115     protected void createAttributeProperty(
116             PersistentDescriptor descriptor,
117             ObjAttribute attribute) {
118         Class JavaDoc propertyType = attribute.getJavaClass();
119         Accessor accessor = createAccessor(descriptor, attribute.getName(), propertyType);
120         descriptor.addDeclaredProperty(new SimpleAttributeProperty(
121                 descriptor,
122                 accessor,
123                 attribute));
124     }
125
126     protected void createEmbeddedAttributeProperty(
127             PersistentDescriptor descriptor,
128             EmbeddedAttribute embeddedAttribute,
129             ObjAttribute attribute) {
130
131         Class JavaDoc embeddableClass = embeddedAttribute.getJavaClass();
132
133         String JavaDoc propertyPath = attribute.getName();
134         int lastDot = propertyPath.lastIndexOf('.');
135         if (lastDot <= 0 || lastDot == propertyPath.length() - 1) {
136             throw new IllegalArgumentException JavaDoc("Invalid embeddable path: " + propertyPath);
137         }
138
139         String JavaDoc embeddableName = propertyPath.substring(lastDot + 1);
140
141         Accessor embeddedAccessor = createAccessor(descriptor, embeddedAttribute
142                 .getName(), embeddableClass);
143         Accessor embeddedableAccessor = createEmbeddableAccessor(
144                 embeddableClass,
145                 embeddableName,
146                 attribute.getJavaClass());
147
148         Accessor accessor = new EmbeddedFieldAccessor(
149                 embeddableClass,
150                 embeddedAccessor,
151                 embeddedableAccessor);
152         descriptor.addDeclaredProperty(new SimpleAttributeProperty(
153                 descriptor,
154                 accessor,
155                 attribute));
156     }
157
158     protected abstract void createToOneProperty(
159             PersistentDescriptor descriptor,
160             ObjRelationship relationship);
161
162     protected abstract void createToManyProperty(
163             PersistentDescriptor descriptor,
164             ObjRelationship relationship);
165
166     protected void indexSubclassDescriptors(
167             PersistentDescriptor descriptor,
168             EntityInheritanceTree inheritanceTree) {
169
170         if (inheritanceTree != null) {
171
172             Iterator JavaDoc it = inheritanceTree.getChildren().iterator();
173             while (it.hasNext()) {
174                 EntityInheritanceTree child = (EntityInheritanceTree) it.next();
175                 descriptor.addSubclassDescriptor(descriptorMap.getDescriptor(child
176                         .getEntity()
177                         .getName()));
178
179                 indexSubclassDescriptors(descriptor, child);
180             }
181         }
182     }
183
184     /**
185      * Creates an accessor for the property.
186      */

187     protected Accessor createAccessor(
188             PersistentDescriptor descriptor,
189             String JavaDoc propertyName,
190             Class JavaDoc propertyType) throws PropertyException {
191         return new FieldAccessor(descriptor.getObjectClass(), propertyName, propertyType);
192     }
193
194     /**
195      * Creates an accessor for the property of the embeddable class.
196      */

197     protected Accessor createEmbeddableAccessor(
198             Class JavaDoc embeddableClass,
199             String JavaDoc propertyName,
200             Class JavaDoc propertyType) {
201         return new FieldAccessor(embeddableClass, propertyName, propertyType);
202     }
203 }
204
Popular Tags