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.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 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
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 | |
|
102 | 0 | } |
103 | 0 | } |
104 | |
|
105 | 0 | loaded=true; |
106 | |
} |
107 | 0 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public Map<String, String> getPreloadMap() { |
113 | 0 | return preloadMap; |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
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 | |
|
132 | |
|
133 | |
public LoadJpaBean getLoadJpaBean() { |
134 | 0 | return loadJpaBean; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
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 | |
} |