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