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