1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.core.framework.persistence.jpa; |
17 | |
|
18 | |
import org.kuali.rice.core.api.config.property.Config; |
19 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
20 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
21 | |
import org.kuali.rice.core.api.util.RiceConstants; |
22 | |
import org.kuali.rice.core.framework.persistence.jpa.annotations.Sequence; |
23 | |
import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform; |
24 | |
|
25 | |
import javax.persistence.Entity; |
26 | |
import javax.persistence.EntityManager; |
27 | |
import javax.persistence.MappedSuperclass; |
28 | |
import java.lang.reflect.Field; |
29 | |
import java.lang.reflect.Modifier; |
30 | |
import java.util.HashMap; |
31 | |
import java.util.HashSet; |
32 | |
import java.util.Map; |
33 | |
import java.util.Set; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public final class OrmUtils { |
41 | |
|
42 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrmUtils.class); |
43 | |
|
44 | 0 | private static Map<String, Boolean> cache = new HashMap<String, Boolean>(); |
45 | |
|
46 | 0 | private OrmUtils() { |
47 | 0 | throw new UnsupportedOperationException("do not call"); |
48 | |
} |
49 | |
|
50 | |
public static void populateAutoIncValue(Object entity, Long value) { |
51 | |
try { |
52 | 0 | if (entity.getClass().isAnnotationPresent(Sequence.class)) { |
53 | 0 | Sequence sequence = entity.getClass().getAnnotation(Sequence.class); |
54 | 0 | Field field = getPrivateField(entity.getClass(), sequence.property()); |
55 | 0 | field.setAccessible(true); |
56 | 0 | if (java.lang.String.class.equals(field.getType())) { |
57 | 0 | field.set(entity, value.toString()); |
58 | |
} else { |
59 | 0 | field.set(entity, value); |
60 | |
} |
61 | |
} |
62 | 0 | } catch (Exception e) { |
63 | 0 | LOG.error(e.getMessage(), e); |
64 | 0 | } |
65 | 0 | } |
66 | |
|
67 | |
public static void populateAutoIncValue(Object entity, EntityManager manager) { |
68 | 0 | if (entity.getClass().isAnnotationPresent(Sequence.class)) { |
69 | 0 | Sequence sequence = entity.getClass().getAnnotation(Sequence.class); |
70 | 0 | populateAutoIncValue(entity, getNextAutoIncValue(sequence, manager)); |
71 | |
} |
72 | 0 | } |
73 | |
|
74 | |
public static Long getNextAutoIncValue(Class entityClass, EntityManager manager) { |
75 | 0 | return getNextAutoIncValue((Sequence)entityClass.getAnnotation(Sequence.class), manager); |
76 | |
} |
77 | |
|
78 | |
private static Long getNextAutoIncValue(Sequence sequence, EntityManager manager) { |
79 | 0 | Long value = -1L; |
80 | |
try { |
81 | 0 | DatabasePlatform platform = (DatabasePlatform) GlobalResourceLoader.getService(RiceConstants.DB_PLATFORM); |
82 | 0 | value = platform.getNextValSQL(sequence.name(), manager); |
83 | 0 | } catch (Exception e) { |
84 | 0 | LOG.error(e.getMessage(), e); |
85 | 0 | } |
86 | 0 | return value; |
87 | |
} |
88 | |
|
89 | |
public static void reattach(Object detached, Object attached) { |
90 | 0 | if ( LOG.isDebugEnabled() ) { |
91 | 0 | LOG.debug("Reattaching entity: " + attached.getClass().getName()); |
92 | |
} |
93 | |
|
94 | 0 | Set<String> cachedFields = new HashSet<String>(); |
95 | 0 | Class attachedClass = detached.getClass(); |
96 | |
|
97 | |
do { |
98 | 0 | for (Field attachedField : attachedClass.getDeclaredFields()) { |
99 | |
try { |
100 | 0 | attachedField.setAccessible(true); |
101 | 0 | int mods = attachedField.getModifiers(); |
102 | 0 | if (!cachedFields.contains(attachedField.getName()) && !Modifier.isFinal(mods) && !Modifier.isStatic(mods)) { |
103 | |
|
104 | 0 | attachedField.set(attached, attachedField.get(detached)); |
105 | 0 | cachedFields.add(attachedField.getName()); |
106 | |
} |
107 | 0 | } catch (Exception e) { |
108 | 0 | LOG.error(e.getMessage(), e); |
109 | 0 | } |
110 | |
} |
111 | 0 | attachedClass = attachedClass.getSuperclass(); |
112 | 0 | } while (attachedClass != null && !(attachedClass.equals(Object.class))); |
113 | 0 | } |
114 | |
|
115 | |
public static void merge(EntityManager manager, Object entity) { |
116 | 0 | if(manager.contains(entity)) { |
117 | 0 | manager.merge(entity); |
118 | |
} |
119 | |
else { |
120 | 0 | OrmUtils.reattach(entity, manager.merge(entity)); |
121 | |
} |
122 | 0 | } |
123 | |
|
124 | |
public static boolean isJpaAnnotated(Class<?> clazz) { |
125 | 0 | if (clazz == null) { |
126 | 0 | return false; |
127 | |
} |
128 | 0 | if (!cache.containsKey(clazz.getName())) { |
129 | 0 | if (clazz.getName().contains("EnhancerByCGLIB")) { |
130 | |
try { |
131 | |
|
132 | 0 | clazz = Class.forName(clazz.getName().substring(0, clazz.getName().indexOf("$$EnhancerByCGLIB"))); |
133 | 0 | } catch (Exception e) { |
134 | 0 | LOG.error(e.getMessage(), e); |
135 | 0 | } |
136 | |
} |
137 | 0 | synchronized (cache) { |
138 | 0 | cache.put(clazz.getName(), new Boolean(clazz.isAnnotationPresent(Entity.class) || clazz.isAnnotationPresent(MappedSuperclass.class))); |
139 | 0 | } |
140 | |
} |
141 | 0 | return cache.get(clazz.getName()).booleanValue(); |
142 | |
} |
143 | |
|
144 | |
public static boolean isJpaEnabled() { |
145 | 0 | return Boolean.valueOf( ConfigContext.getCurrentContextConfig().getProperty(RiceConstants.RICE_JPA_ENABLED) ); |
146 | |
} |
147 | |
|
148 | |
public static boolean isJpaEnabled(String prefix) { |
149 | 0 | Config config = ConfigContext.getCurrentContextConfig(); |
150 | 0 | return Boolean.valueOf( config.getProperty(RiceConstants.RICE_JPA_ENABLED) ) || Boolean.valueOf( config.getProperty(prefix + RiceConstants.JPA_ENABLED_SUFFIX) ); |
151 | |
} |
152 | |
|
153 | |
private static Field getPrivateField(Class clazz, String fieldName) throws NoSuchFieldException { |
154 | 0 | if(clazz == null) { |
155 | 0 | throw new NoSuchFieldException(); |
156 | |
} |
157 | |
|
158 | |
try { |
159 | 0 | return clazz.getDeclaredField(fieldName); |
160 | |
} |
161 | 0 | catch(NoSuchFieldException exception) { |
162 | 0 | return getPrivateField(clazz.getSuperclass(), fieldName); |
163 | |
} |
164 | |
} |
165 | |
|
166 | |
} |