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