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