KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > pojo > EnhancedPojoDescriptorFactory


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.pojo;
20
21 import java.util.Collection JavaDoc;
22
23 import org.apache.cayenne.map.ObjEntity;
24 import org.apache.cayenne.map.ObjRelationship;
25 import org.apache.cayenne.reflect.Accessor;
26 import org.apache.cayenne.reflect.ClassDescriptor;
27 import org.apache.cayenne.reflect.ClassDescriptorFactory;
28 import org.apache.cayenne.reflect.ClassDescriptorMap;
29 import org.apache.cayenne.reflect.PersistentDescriptor;
30 import org.apache.cayenne.reflect.PersistentDescriptorFactory;
31
32 /**
33  * A {@link ClassDescriptorFactory} used to create descriptors for the enhanced POJO's.
34  *
35  * @since 3.0
36  * @author Andrus Adamchik
37  */

38 public class EnhancedPojoDescriptorFactory extends PersistentDescriptorFactory {
39
40     static final String JavaDoc PERSISTENCE_STATE_FIELD = "$cay_persistenceState";
41
42     public EnhancedPojoDescriptorFactory(ClassDescriptorMap descriptorMap) {
43         super(descriptorMap);
44     }
45
46     protected ClassDescriptor getDescriptor(ObjEntity entity, Class JavaDoc entityClass) {
47         // check whether we are dealing with enhaced pojo
48
try {
49             Integer.TYPE.equals(entityClass
50                     .getDeclaredField(PERSISTENCE_STATE_FIELD)
51                     .getType());
52         }
53         catch (Throwable JavaDoc th) {
54             return null;
55         }
56
57         return super.getDescriptor(entity, entityClass);
58     }
59
60     protected void createToManyProperty(
61             PersistentDescriptor descriptor,
62             ObjRelationship relationship) {
63
64         ClassDescriptor targetDescriptor = descriptorMap.getDescriptor(relationship
65                 .getTargetEntityName());
66         String JavaDoc reverseName = relationship.getReverseRelationshipName();
67
68         Accessor accessor = createAccessor(
69                 descriptor,
70                 relationship.getName(),
71                 Collection JavaDoc.class);
72         descriptor.addDeclaredProperty(new EnhancedPojoToManyProperty(
73                 descriptor,
74                 targetDescriptor,
75                 accessor,
76                 reverseName));
77     }
78
79     protected void createToOneProperty(
80             PersistentDescriptor descriptor,
81             ObjRelationship relationship) {
82
83         ClassDescriptor targetDescriptor = descriptorMap.getDescriptor(relationship
84                 .getTargetEntityName());
85         String JavaDoc reverseName = relationship.getReverseRelationshipName();
86
87         Accessor accessor = createAccessor(
88                 descriptor,
89                 relationship.getName(),
90                 targetDescriptor.getObjectClass());
91         descriptor.addDeclaredProperty(new EnhancedPojoToOneProperty(
92                 descriptor,
93                 targetDescriptor,
94                 accessor,
95                 reverseName));
96     }
97 }
98
Popular Tags