Coverage Report - org.kuali.student.common.util.jpa.LoadJpaBeanFileListener
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadJpaBeanFileListener
0%
0/36
0%
0/8
1.7
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.util.jpa;
 17  
 
 18  
 import java.util.Arrays;
 19  
 import java.util.Map;
 20  
 import java.util.Map.Entry;
 21  
 
 22  
 import javax.persistence.EntityManager;
 23  
 import javax.persistence.EntityManagerFactory;
 24  
 
 25  
 import org.springframework.beans.BeansException;
 26  
 import org.springframework.beans.factory.xml.XmlBeanFactory;
 27  
 import org.springframework.context.ApplicationContext;
 28  
 import org.springframework.context.ApplicationContextAware;
 29  
 import org.springframework.context.ApplicationEvent;
 30  
 import org.springframework.context.ApplicationListener;
 31  
 import org.springframework.context.event.ContextRefreshedEvent;
 32  
 import org.springframework.core.io.ClassPathResource;
 33  
 import org.springframework.core.io.FileSystemResource;
 34  
 import org.springframework.core.io.Resource;
 35  
 import org.springframework.orm.jpa.EntityManagerFactoryUtils;
 36  
 import org.springframework.orm.jpa.SharedEntityManagerCreator;
 37  
 
 38  
 /**
 39  
  * This loads a jpa message beans from a list of message bean files. Only beans
 40  
  * of entityType listed will be loaded.
 41  
  *
 42  
  * Example spring config:
 43  
  * 
 44  
  *  <bean id="messageload" class="org.kuali.student.common.util.jpa.LoadJpaBeanFileListener">
 45  
  *    <property name="loadJpaBean">
 46  
  *       <bean class="org.kuali.student.common.util.jpa.LoadJpaBean" />
 47  
  *    </property>
 48  
  *    <property name="entityClass" value="org.kuali.student.common.messages.entity.MessageEntity" />
 49  
  *    <property name="persistenceUnit" value="MessageManagement"/>
 50  
  *    <property name="preloadMap">
 51  
  *       <map>
 52  
  *          <entry key="File1" value="classpath:bean-file1.xml" />
 53  
  *          <entry key="File2" value="classpath:bean-file2.xml" />
 54  
  *       </map>
 55  
  *   </property>
 56  
  *   </bean>
 57  
  *
 58  
  * @author Kuali Student Team
 59  
  *
 60  
  */
 61  0
 public class LoadJpaBeanFileListener implements ApplicationListener,
 62  
     ApplicationContextAware { 
 63  
     
 64  
     private Map<String, String> preloadMap;
 65  
     private ApplicationContext applicationContext;
 66  
     private LoadJpaBean loadJpaBean;
 67  
     private String persistenceUnit;
 68  
     private String entityClass;
 69  
     
 70  0
     private boolean loaded = false;
 71  
 
 72  
     @SuppressWarnings("unchecked")
 73  
     @Override
 74  
     public void onApplicationEvent(ApplicationEvent event) {
 75  0
         if (event instanceof ContextRefreshedEvent && !loaded) {
 76  0
             for (Entry<String, String> entry : preloadMap.entrySet()) {
 77  0
                 String messageFileName = entry.getValue();
 78  
                 
 79  
                 Resource res;
 80  
                 try{
 81  0
                     if(messageFileName.startsWith("classpath:")){
 82  0
                         res = new ClassPathResource(messageFileName.substring("classpath:".length()));
 83  
                     }else{
 84  0
                         res = new FileSystemResource(messageFileName);
 85  
                     }
 86  0
                 } catch (Exception e) {
 87  0
                     throw new RuntimeException(e);
 88  0
                 }
 89  
                 
 90  0
                 XmlBeanFactory factory = new XmlBeanFactory(res);
 91  
 
 92  0
                 EntityManagerFactory emf = EntityManagerFactoryUtils
 93  
                         .findEntityManagerFactory(applicationContext, persistenceUnit);
 94  0
                 EntityManager em = SharedEntityManagerCreator
 95  
                         .createSharedEntityManager(emf);
 96  
                 try {
 97  0
                     Class messageClazz = Class.forName(entityClass);
 98  0
                     Object[] beanArray = factory.getBeansOfType(messageClazz).values().toArray();                    
 99  0
                     loadJpaBean.loadData(Arrays.asList(beanArray), em);
 100  0
                 } catch (Exception e) {
 101  
                     //Log here, need to catch because if errors are thrown then the transaction is left hanging for some reason
 102  0
                 }
 103  0
             }
 104  
             
 105  0
             loaded=true;
 106  
         }        
 107  0
     }
 108  
 
 109  
     /**
 110  
      * @return the preloadMap
 111  
      */
 112  
     public Map<String, String> getPreloadMap() {
 113  0
         return preloadMap;
 114  
     }
 115  
 
 116  
     /**
 117  
      * @param preloadMap
 118  
      *            the preloadMap to set
 119  
      */
 120  
     public void setPreloadMap(Map<String, String> preloadMap) {
 121  0
         this.preloadMap = preloadMap;
 122  0
     }
 123  
 
 124  
     @Override
 125  
     public void setApplicationContext(ApplicationContext applicationContext)
 126  
             throws BeansException {
 127  0
         this.applicationContext = applicationContext;
 128  0
     }
 129  
 
 130  
     /**
 131  
      * @return the loadJpaBean
 132  
      */
 133  
     public LoadJpaBean getLoadJpaBean() {
 134  0
         return loadJpaBean;
 135  
     }
 136  
 
 137  
     /**
 138  
      * @param loadJpaBean
 139  
      *            the loadJpaBean to set
 140  
      */
 141  
     public void setLoadJpaBean(LoadJpaBean loadJpaBean) {
 142  0
         this.loadJpaBean = loadJpaBean;
 143  0
     }
 144  
 
 145  
     public String getPersistenceUnit() {
 146  0
         return persistenceUnit;
 147  
     }
 148  
 
 149  
     public void setPersistenceUnit(String persistenceUnit) {
 150  0
         this.persistenceUnit = persistenceUnit;
 151  0
     }
 152  
 
 153  
     public String getEntityClass() {
 154  0
         return entityClass;
 155  
     }
 156  
 
 157  
     public void setEntityClass(String entityClass) {
 158  0
         this.entityClass = entityClass;
 159  0
     }
 160  
     
 161  
 }