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