1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service.impl;
17
18 import org.apache.ojb.broker.metadata.ClassDescriptor;
19 import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
20 import org.apache.ojb.broker.metadata.DescriptorRepository;
21 import org.apache.ojb.broker.metadata.FieldDescriptor;
22 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
23 import org.kuali.rice.core.api.config.property.ConfigContext;
24 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
25 import org.kuali.rice.core.framework.persistence.jpa.metadata.EntityDescriptor;
26 import org.kuali.rice.core.framework.persistence.jpa.metadata.MetadataManager;
27 import org.kuali.rice.core.framework.persistence.jpa.metadata.ObjectDescriptor;
28 import org.kuali.rice.core.framework.persistence.ojb.BaseOjbConfigurer;
29 import org.kuali.rice.krad.bo.PersistableBusinessObject;
30 import org.kuali.rice.krad.bo.PersistableBusinessObjectExtension;
31 import org.kuali.rice.krad.exception.ClassNotPersistableException;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 public class PersistenceServiceStructureImplBase {
37
38 private DescriptorRepository descriptorRepository;
39
40
41
42
43 public PersistenceServiceStructureImplBase() {
44 String ojbPropertyFileLocation = ConfigContext.getCurrentContextConfig().getProperty(BaseOjbConfigurer.RICE_OJB_PROPERTIES_PARAM);
45 String currentValue = System.getProperty(BaseOjbConfigurer.OJB_PROPERTIES_PROP);
46 try {
47 System.setProperty(BaseOjbConfigurer.OJB_PROPERTIES_PROP, ojbPropertyFileLocation);
48 org.apache.ojb.broker.metadata.MetadataManager metadataManager = org.apache.ojb.broker.metadata.MetadataManager.getInstance();
49 descriptorRepository = metadataManager.getGlobalRepository();
50 } finally {
51 if (currentValue == null) {
52 System.getProperties().remove(BaseOjbConfigurer.OJB_PROPERTIES_PROP);
53 } else {
54 System.setProperty(BaseOjbConfigurer.OJB_PROPERTIES_PROP, currentValue);
55 }
56 }
57 }
58
59
60
61
62 protected DescriptorRepository getDescriptorRepository() {
63 return descriptorRepository;
64 }
65
66
67
68
69
70
71
72
73
74
75
76 public List listPrimaryKeyFieldNames(Class clazz) {
77
78 if (isJpaEnabledForKradClass(clazz)) {
79 List fieldNames = new ArrayList();
80 EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(clazz);
81 for (org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor field : descriptor.getPrimaryKeys()) {
82 fieldNames.add(field.getName());
83 }
84 return fieldNames;
85 } else {
86
87 List fieldNamesLegacy = new ArrayList();
88 ClassDescriptor classDescriptor = getClassDescriptor(clazz);
89 FieldDescriptor keyDescriptors[] = classDescriptor.getPkFields();
90
91 for (int i = 0; i < keyDescriptors.length; ++i) {
92 FieldDescriptor keyDescriptor = keyDescriptors[i];
93 fieldNamesLegacy.add(keyDescriptor.getAttributeName());
94 }
95 return fieldNamesLegacy;
96 }
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 protected ClassDescriptor getClassDescriptor(Class persistableClass) {
133 if (persistableClass == null) {
134 throw new IllegalArgumentException("invalid (null) object");
135 }
136
137 ClassDescriptor classDescriptor = null;
138 DescriptorRepository globalRepository = getDescriptorRepository();
139 try {
140 classDescriptor = globalRepository.getDescriptorFor(persistableClass);
141 } catch (ClassNotPersistenceCapableException e) {
142 throw new ClassNotPersistableException("class '" + persistableClass.getName() + "' is not persistable", e);
143 }
144
145 return classDescriptor;
146 }
147
148
149
150
151
152
153
154 public boolean isJpaEnabledForKradClass(Class clazz) {
155 final boolean jpaAnnotated = OrmUtils.isJpaAnnotated(clazz);
156 final boolean jpaEnabled = OrmUtils.isJpaEnabled();
157 final boolean prefixJpaEnabled = OrmUtils.isJpaEnabled("rice.krad");
158 return jpaAnnotated && (jpaEnabled || prefixJpaEnabled);
159 }
160
161
162
163
164
165 public Class<? extends PersistableBusinessObjectExtension> getBusinessObjectAttributeClass(Class<? extends PersistableBusinessObject> clazz, String attributeName) {
166 String baseAttributeName = attributeName;
167 String subAttributeString = null;
168 if (attributeName.contains(".")) {
169 baseAttributeName = attributeName.substring(0, attributeName.indexOf('.'));
170 subAttributeString = attributeName.substring(attributeName.indexOf('.') + 1);
171 }
172
173
174 if (isJpaEnabledForKradClass(clazz)) {
175 Class attributeClass = null;
176 EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(clazz);
177 ObjectDescriptor objectDescriptor = descriptor.getObjectDescriptorByName(baseAttributeName);
178 if (objectDescriptor != null) {
179 attributeClass = objectDescriptor.getTargetEntity();
180 }
181
182
183 if (subAttributeString != null) {
184 attributeClass = getBusinessObjectAttributeClass(attributeClass, subAttributeString);
185 }
186
187 return attributeClass;
188 } else {
189
190 Class attributeClassLegacy = null;
191 ClassDescriptor classDescriptor = this.getClassDescriptor(clazz);
192 ObjectReferenceDescriptor refDescriptor = classDescriptor.getObjectReferenceDescriptorByName(baseAttributeName);
193
194 if (refDescriptor != null) {
195 attributeClassLegacy = refDescriptor.getItemClass();
196 }
197
198
199 if (subAttributeString != null) {
200 attributeClassLegacy = getBusinessObjectAttributeClass(attributeClassLegacy, subAttributeString);
201 }
202
203 return attributeClassLegacy;
204 }
205 }
206
207 }