Coverage Report - org.kuali.student.common.util.jpa.LoadJpaBeanListener
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadJpaBeanListener
0%
0/20
0%
0/4
1.5
 
 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.HashMap;
 19  
 import java.util.List;
 20  
 import java.util.Map;
 21  
 import java.util.Map.Entry;
 22  
 
 23  
 import javax.persistence.EntityManager;
 24  
 import javax.persistence.EntityManagerFactory;
 25  
 
 26  
 import org.springframework.beans.BeansException;
 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.orm.jpa.EntityManagerFactoryUtils;
 33  
 import org.springframework.orm.jpa.SharedEntityManagerCreator;
 34  
 
 35  0
 public class LoadJpaBeanListener implements ApplicationListener,
 36  
                 ApplicationContextAware {
 37  
 
 38  
         private Map<String, List<Object>> preloadMap;
 39  
         private ApplicationContext applicationContext;
 40  
         private LoadJpaBean loadJpaBean;
 41  
 
 42  
         @Override
 43  
         public void onApplicationEvent(ApplicationEvent event) {
 44  0
                 if (event instanceof ContextRefreshedEvent) {
 45  0
                         for (Entry<String, List<Object>> entry : preloadMap.entrySet()) {
 46  0
                                 List<Object> beanList = entry.getValue();
 47  0
                                 EntityManagerFactory emf = EntityManagerFactoryUtils
 48  
                                                 .findEntityManagerFactory(applicationContext, entry
 49  
                                                                 .getKey());
 50  0
                                 EntityManager em = SharedEntityManagerCreator
 51  
                                                 .createSharedEntityManager(emf);
 52  
                                 try {
 53  0
                                         loadJpaBean.loadData(beanList, em);
 54  0
                                 } catch (Exception e) {
 55  
                                         //Log here, need to catch because if errors are thrown then the transaction is left hanging for some reason
 56  0
                                 }
 57  0
                         }
 58  
                 }
 59  
                 
 60  
                 // Reset the preloadMap to an empty Map after processing them
 61  
                 // This avoids multiple load of data beans when war is deployed 
 62  0
                 preloadMap = new HashMap<String, List<Object>>();
 63  0
         }
 64  
 
 65  
         /**
 66  
          * @return the preloadMap
 67  
          */
 68  
         public Map<String, List<Object>> getPreloadMap() {
 69  0
                 return preloadMap;
 70  
         }
 71  
 
 72  
         /**
 73  
          * @param preloadMap
 74  
          *            the preloadMap to set
 75  
          */
 76  
         public void setPreloadMap(Map<String, List<Object>> preloadMap) {
 77  0
                 this.preloadMap = preloadMap;
 78  0
         }
 79  
 
 80  
         @Override
 81  
         public void setApplicationContext(ApplicationContext applicationContext)
 82  
                         throws BeansException {
 83  0
                 this.applicationContext = applicationContext;
 84  0
         }
 85  
 
 86  
         /**
 87  
          * @return the loadJpaBean
 88  
          */
 89  
         public LoadJpaBean getLoadJpaBean() {
 90  0
                 return loadJpaBean;
 91  
         }
 92  
 
 93  
         /**
 94  
          * @param loadJpaBean
 95  
          *            the loadJpaBean to set
 96  
          */
 97  
         public void setLoadJpaBean(LoadJpaBean loadJpaBean) {
 98  0
                 this.loadJpaBean = loadJpaBean;
 99  0
         }
 100  
 
 101  
 }