1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
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 | |
|
56 | 0 | } |
57 | 0 | } |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | 0 | preloadMap = new HashMap<String, List<Object>>(); |
63 | 0 | } |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public Map<String, List<Object>> getPreloadMap() { |
69 | 0 | return preloadMap; |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
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 | |
|
88 | |
|
89 | |
public LoadJpaBean getLoadJpaBean() { |
90 | 0 | return loadJpaBean; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public void setLoadJpaBean(LoadJpaBean loadJpaBean) { |
98 | 0 | this.loadJpaBean = loadJpaBean; |
99 | 0 | } |
100 | |
|
101 | |
} |