Coverage Report - org.kuali.rice.core.util.OrmUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
OrmUtils
0%
0/66
0%
0/38
3.5
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * A utility for common ORM related functions.
 36  
  * 
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
             // Don't want to get parent fields if overridden in children since we are walking the tree from child to parent
 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  
                                     // Strip a proxy if found
 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  
 }