1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.dao.impl; |
17 | |
|
18 | |
import java.lang.reflect.Field; |
19 | |
import java.util.Collection; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import javax.persistence.EntityManager; |
25 | |
import javax.persistence.EntityNotFoundException; |
26 | |
import javax.persistence.PersistenceContext; |
27 | |
|
28 | |
import org.apache.commons.lang.StringUtils; |
29 | |
import org.hibernate.proxy.HibernateProxy; |
30 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.CollectionDescriptor; |
31 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.EntityDescriptor; |
32 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.JoinColumnDescriptor; |
33 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.MetadataManager; |
34 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.ObjectDescriptor; |
35 | |
import org.kuali.rice.kns.dao.PersistenceDao; |
36 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
37 | |
|
38 | 0 | public class PersistenceDaoJpa implements PersistenceDao { |
39 | 0 | static org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(PersistenceDaoJpa.class); |
40 | |
|
41 | |
@PersistenceContext |
42 | |
private EntityManager entityManager; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public void clearCache() {} |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public Object resolveProxy(Object o) { |
53 | 0 | if (o instanceof HibernateProxy) { |
54 | |
try { |
55 | 0 | final Object realObject = ((HibernateProxy) o).getHibernateLazyInitializer().getImplementation(); |
56 | 0 | return realObject; |
57 | 0 | } catch (EntityNotFoundException enfe) { |
58 | 0 | return null; |
59 | |
} |
60 | |
} |
61 | 0 | return o; |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public void retrieveAllReferences(Object o) { |
68 | 0 | EntityDescriptor ed = MetadataManager.getEntityDescriptor(o.getClass()); |
69 | 0 | for (ObjectDescriptor od : ed.getObjectRelationships()) { |
70 | 0 | retrieveReference(o, od.getAttributeName()); |
71 | |
} |
72 | 0 | for (CollectionDescriptor cd : ed.getCollectionRelationships()) { |
73 | 0 | retrieveReference(o, cd.getAttributeName()); |
74 | |
} |
75 | 0 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public void retrieveReference(Object o, String referenceName) { |
81 | |
try { |
82 | 0 | if (getEntityManager().contains(o)) { |
83 | 0 | LOG.debug("the entity manager contains the object"); |
84 | |
} |
85 | |
|
86 | 0 | Field field = getField(o.getClass(), referenceName); |
87 | 0 | field.setAccessible(true); |
88 | |
|
89 | 0 | String fk = null; |
90 | 0 | String foreignPK = null; |
91 | |
|
92 | 0 | if (isReferenceCollection(o, referenceName)) { |
93 | 0 | Collection reference = retrieveCollectionReference(o, referenceName); |
94 | 0 | field.set(o, reference); |
95 | 0 | } else { |
96 | 0 | Object reference = retrieveObjectReference(o, referenceName); |
97 | 0 | field.set(o, reference); |
98 | |
} |
99 | 0 | } catch (Exception e) { |
100 | 0 | e.printStackTrace(); |
101 | 0 | } |
102 | 0 | } |
103 | |
|
104 | |
private Field getField(Class clazz, String name) throws NoSuchFieldException { |
105 | 0 | if (clazz.equals(Object.class)) { |
106 | 0 | throw new NoSuchFieldException(name); |
107 | |
} |
108 | 0 | Field field = null; |
109 | |
try { |
110 | 0 | field = clazz.getDeclaredField(name); |
111 | 0 | } catch (Exception e) {} |
112 | 0 | if (field == null) { |
113 | 0 | field = getField(clazz.getSuperclass(), name); |
114 | |
} |
115 | 0 | return field; |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
protected boolean isReferenceCollection(Object o, String referenceName) { |
126 | 0 | EntityDescriptor ed = MetadataManager.getEntityDescriptor(o.getClass()); |
127 | 0 | return ed.getCollectionDescriptorByName(referenceName) != null; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
protected Collection retrieveCollectionReference(Object o, String referenceName) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { |
141 | 0 | final EntityDescriptor ed = MetadataManager.getEntityDescriptor(o.getClass()); |
142 | 0 | final CollectionDescriptor cd = ed.getCollectionDescriptorByName(referenceName); |
143 | 0 | final EntityDescriptor foreignEntityDescriptor = MetadataManager.getEntityDescriptor(cd.getTargetEntity()); |
144 | |
|
145 | 0 | Map<String, Object> searchKey = new HashMap<String, Object>(); |
146 | 0 | for (String foreignKey : cd.getForeignKeyFields()) { |
147 | 0 | Field localField = getField(o.getClass(), foreignKey); |
148 | 0 | localField.setAccessible(true); |
149 | 0 | final Object localValue = localField.get(o); |
150 | |
|
151 | 0 | final String foreignKeyProperty = getForeignKeyPropertyForKeyWithPossibleInverse(foreignKey, ed, foreignEntityDescriptor, cd); |
152 | |
|
153 | 0 | searchKey.put(foreignKeyProperty, localValue); |
154 | 0 | } |
155 | 0 | return KNSServiceLocator.getBusinessObjectService().findMatching(cd.getTargetEntity(), searchKey); |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
protected String getForeignKeyPropertyForKeyWithPossibleInverse(String foreignKey, EntityDescriptor localEntityDescriptor, EntityDescriptor foreignEntityDescriptor, CollectionDescriptor collectionDescriptor) { |
168 | 0 | final String foreignKeyColumn = localEntityDescriptor.getFieldByName(foreignKey).getColumn(); |
169 | |
|
170 | 0 | int count = 0; |
171 | 0 | JoinColumnDescriptor joinColumnDescriptor = null; |
172 | 0 | JoinColumnDescriptor inverseColumnDescriptor = null; |
173 | 0 | while (count < collectionDescriptor.getJoinColumnDescriptors().size() && joinColumnDescriptor == null) { |
174 | 0 | if (collectionDescriptor.getJoinColumnDescriptors().get(count).getName().equalsIgnoreCase(foreignKeyColumn)) { |
175 | 0 | joinColumnDescriptor = collectionDescriptor.getJoinColumnDescriptors().get(count); |
176 | 0 | if (count < collectionDescriptor.getInverseJoinColumnDescriptors().size()) { |
177 | 0 | inverseColumnDescriptor = collectionDescriptor.getInverseJoinColumnDescriptors().get(count); |
178 | |
} |
179 | |
} |
180 | 0 | count += 1; |
181 | |
} |
182 | |
|
183 | 0 | if (inverseColumnDescriptor != null) { |
184 | 0 | return foreignEntityDescriptor.getFieldByColumnName(inverseColumnDescriptor.getName()).getName(); |
185 | |
} |
186 | |
|
187 | 0 | if (!StringUtils.isBlank(joinColumnDescriptor.getReferencedColumName())) { |
188 | 0 | return foreignEntityDescriptor.getFieldByColumnName(joinColumnDescriptor.getReferencedColumName()).getName(); |
189 | |
} |
190 | |
|
191 | 0 | return foreignEntityDescriptor.getFieldByColumnName(joinColumnDescriptor.getName()).getName(); |
192 | |
|
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
protected Object retrieveObjectReference(Object o, String referenceName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, InstantiationException, ClassNotFoundException { |
208 | 0 | final EntityDescriptor ed = MetadataManager.getEntityDescriptor(o.getClass()); |
209 | 0 | final ObjectDescriptor od = ed.getObjectDescriptorByName(referenceName); |
210 | |
|
211 | 0 | final Object foreignKeyObject = buildForeignKeyObject(o, ed, od); |
212 | 0 | return getEntityManager().find(od.getTargetEntity(), foreignKeyObject); |
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
protected Object buildForeignKeyObject(Object o, EntityDescriptor localEntityDescriptor, ObjectDescriptor objectDescriptor) throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException, InstantiationException { |
228 | 0 | return (objectDescriptor.getForeignKeyFields().size() == 1) ? |
229 | |
buildSingleKeyForeignKeyObject(o, objectDescriptor.getForeignKeyFields().get(0)) : |
230 | |
buildCompositeForeignKeyObject(o, localEntityDescriptor, objectDescriptor); |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
private Object buildCompositeForeignKeyObject(Object o, EntityDescriptor localEntityDescriptor, ObjectDescriptor objectDescriptor) throws InstantiationException, IllegalAccessException, NoSuchFieldException { |
245 | 0 | final Map<String, JoinColumnDescriptor> joinColumnDescriptors = buildJoinColumnDescriptorMap(objectDescriptor.getJoinColumnDescriptors()); |
246 | |
|
247 | 0 | final EntityDescriptor foreignEntityDescriptor = MetadataManager.getEntityDescriptor(objectDescriptor.getTargetEntity()); |
248 | 0 | final Class foreignEntityIdClass = foreignEntityDescriptor.getIdClass(); |
249 | |
|
250 | 0 | Object foreignEntityId = foreignEntityIdClass.newInstance(); |
251 | 0 | for (String foreignKey : objectDescriptor.getForeignKeyFields()) { |
252 | |
|
253 | 0 | Field localField = getField(o.getClass(), foreignKey); |
254 | 0 | localField.setAccessible(true); |
255 | 0 | final Object localValue = localField.get(o); |
256 | |
|
257 | 0 | final String foreignKeyProperty = getForeignKeyPropertyForKey(foreignKey, localEntityDescriptor, foreignEntityDescriptor, joinColumnDescriptors); |
258 | |
|
259 | 0 | Field foreignField = getField(foreignEntityId.getClass(), foreignKeyProperty); |
260 | 0 | foreignField.setAccessible(true); |
261 | 0 | foreignField.set(foreignEntityId, localValue); |
262 | 0 | } |
263 | 0 | return foreignEntityId; |
264 | |
} |
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
protected String getForeignKeyPropertyForKey(String foreignKey, EntityDescriptor localEntityDescriptor, EntityDescriptor foreignEntityDescriptor, Map<String, JoinColumnDescriptor> joinColumnDescriptors) { |
276 | 0 | final String foreignKeyColumn = localEntityDescriptor.getFieldByName(foreignKey).getColumn(); |
277 | 0 | final JoinColumnDescriptor joinColumnDescriptor = joinColumnDescriptors.get(foreignKeyColumn); |
278 | |
|
279 | 0 | return (!StringUtils.isBlank(joinColumnDescriptor.getReferencedColumName())) ? |
280 | |
foreignEntityDescriptor.getFieldByColumnName(joinColumnDescriptor.getReferencedColumName()).getName() : |
281 | |
foreignEntityDescriptor.getFieldByColumnName(joinColumnDescriptor.getName()).getName(); |
282 | |
|
283 | |
} |
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
protected Map<String, JoinColumnDescriptor> buildJoinColumnDescriptorMap(List<JoinColumnDescriptor> joinColumnDescriptors) { |
292 | 0 | Map<String, JoinColumnDescriptor> descriptorMap = new HashMap<String, JoinColumnDescriptor>(); |
293 | 0 | for (JoinColumnDescriptor joinColumnDescriptor : joinColumnDescriptors) { |
294 | 0 | descriptorMap.put(joinColumnDescriptor.getName(), joinColumnDescriptor); |
295 | |
} |
296 | 0 | return descriptorMap; |
297 | |
} |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
protected Object buildSingleKeyForeignKeyObject(Object o, String singleForeignKeyFieldName) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { |
310 | 0 | Field singleFKField = getField(o.getClass(), singleForeignKeyFieldName); |
311 | 0 | singleFKField.setAccessible(true); |
312 | 0 | return singleFKField.get(o); |
313 | |
} |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
public boolean isProxied(Object object) { |
321 | 0 | return (object instanceof HibernateProxy); |
322 | |
} |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
public EntityManager getEntityManager() { |
328 | 0 | return this.entityManager; |
329 | |
} |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
public void setEntityManager(EntityManager entityManager) { |
335 | 0 | this.entityManager = entityManager; |
336 | 0 | } |
337 | |
} |