| 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.ArrayList; |
| 20 | |
import java.util.HashMap; |
| 21 | |
import java.util.Map; |
| 22 | |
|
| 23 | |
import javax.persistence.EntityManager; |
| 24 | |
import javax.persistence.PersistenceContext; |
| 25 | |
|
| 26 | |
import org.kuali.rice.core.jpa.metadata.CollectionDescriptor; |
| 27 | |
import org.kuali.rice.core.jpa.metadata.EntityDescriptor; |
| 28 | |
import org.kuali.rice.core.jpa.metadata.MetadataManager; |
| 29 | |
import org.kuali.rice.core.jpa.metadata.ObjectDescriptor; |
| 30 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
| 31 | |
import org.kuali.rice.kns.dao.PersistenceDao; |
| 32 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
| 33 | |
|
| 34 | 0 | public class PersistenceDaoJpa implements PersistenceDao { |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public void clearCache() {} |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public Object resolveProxy(Object o) { |
| 45 | 0 | return o; |
| 46 | |
} |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public void retrieveAllReferences(Object o) { |
| 52 | 0 | EntityDescriptor ed = MetadataManager.getEntityDescriptor(o.getClass()); |
| 53 | 0 | for (ObjectDescriptor od : ed.getObjectRelationships()) { |
| 54 | 0 | retrieveReference(o, od.getAttributeName()); |
| 55 | |
} |
| 56 | 0 | for (CollectionDescriptor cd : ed.getCollectionRelationships()) { |
| 57 | 0 | retrieveReference(o, cd.getAttributeName()); |
| 58 | |
} |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public void retrieveReference(Object o, String referenceName) { |
| 65 | |
try { |
| 66 | 0 | Field field = getField(o.getClass(), referenceName); |
| 67 | 0 | field.setAccessible(true); |
| 68 | |
|
| 69 | 0 | String fk = null; |
| 70 | |
|
| 71 | 0 | EntityDescriptor ed = MetadataManager.getEntityDescriptor(o.getClass()); |
| 72 | 0 | CollectionDescriptor cd = ed.getCollectionDescriptorByName(referenceName); |
| 73 | 0 | if (cd == null) { |
| 74 | 0 | ObjectDescriptor od = ed.getObjectDescriptorByName(referenceName); |
| 75 | 0 | fk = od.getForeignKeyFields().get(0); |
| 76 | 0 | } else { |
| 77 | 0 | fk = cd.getForeignKeyFields().get(0); |
| 78 | |
} |
| 79 | |
|
| 80 | 0 | Field field2 = null; |
| 81 | |
try { |
| 82 | 0 | field2 = getField(o.getClass(), fk); |
| 83 | 0 | } catch (Exception e) { |
| 84 | 0 | String pk = ed.getPrimaryKeys().iterator().next().getName(); |
| 85 | 0 | field2 = getField(o.getClass(), pk); |
| 86 | 0 | } |
| 87 | 0 | field2.setAccessible(true); |
| 88 | 0 | Object fkFieldValue = field2.get(o); |
| 89 | |
|
| 90 | 0 | if ("java.util.List".equals(field.getType().getCanonicalName())) { |
| 91 | 0 | Field field3 = getField(o.getClass(), ed.getPrimaryKeys().iterator().next().getName()); |
| 92 | 0 | field3.setAccessible(true); |
| 93 | 0 | if (field3.get(o) == null) { |
| 94 | 0 | field.set(o, new ArrayList()); |
| 95 | |
} else { |
| 96 | 0 | Map pk = new HashMap(); |
| 97 | 0 | pk.put(cd.getForeignKeyFields().get(0), field3.get(o)); |
| 98 | 0 | field.set(o, KNSServiceLocator.getBusinessObjectService().findMatching(cd.getTargetEntity(), pk)); |
| 99 | |
} |
| 100 | 0 | } else { |
| 101 | 0 | PersistableBusinessObject pbo = (PersistableBusinessObject) Class.forName(field.getType().getCanonicalName()).newInstance(); |
| 102 | 0 | Map<String, Object> keys = MetadataManager.getPersistableBusinessObjectPrimaryKeyValuePairs(pbo); |
| 103 | 0 | Field field3 = getField(pbo.getClass(), keys.keySet().iterator().next()); |
| 104 | 0 | field3.setAccessible(true); |
| 105 | 0 | field3.set(pbo, fkFieldValue); |
| 106 | 0 | field.set(o, KNSServiceLocator.getBusinessObjectService().retrieve(pbo)); |
| 107 | |
} |
| 108 | 0 | } catch (Exception e) { |
| 109 | 0 | e.printStackTrace(); |
| 110 | 0 | } |
| 111 | 0 | } |
| 112 | |
|
| 113 | |
private Field getField(Class clazz, String name) throws NoSuchFieldException { |
| 114 | 0 | if (clazz.equals(Object.class)) { |
| 115 | 0 | throw new NoSuchFieldException(name); |
| 116 | |
} |
| 117 | 0 | Field field = null; |
| 118 | |
try { |
| 119 | 0 | field = clazz.getDeclaredField(name); |
| 120 | 0 | } catch (Exception e) {} |
| 121 | 0 | if (field == null) { |
| 122 | 0 | field = getField(clazz.getSuperclass(), name); |
| 123 | |
} |
| 124 | 0 | return field; |
| 125 | |
} |
| 126 | |
|
| 127 | |
} |