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.io.BufferedReader; |
19 | |
import java.io.File; |
20 | |
import java.io.FileReader; |
21 | |
import java.io.IOException; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
import java.util.Map.Entry; |
25 | |
|
26 | |
import javax.persistence.EntityManager; |
27 | |
import javax.persistence.EntityManagerFactory; |
28 | |
|
29 | |
import org.apache.commons.lang.StringUtils; |
30 | |
import org.slf4j.Logger; |
31 | |
import org.slf4j.LoggerFactory; |
32 | |
import org.springframework.beans.BeansException; |
33 | |
import org.springframework.context.ApplicationContext; |
34 | |
import org.springframework.context.ApplicationContextAware; |
35 | |
import org.springframework.context.ApplicationEvent; |
36 | |
import org.springframework.context.ApplicationListener; |
37 | |
import org.springframework.context.event.ContextRefreshedEvent; |
38 | |
import org.springframework.core.io.ClassPathResource; |
39 | |
import org.springframework.orm.jpa.EntityManagerFactoryUtils; |
40 | |
import org.springframework.orm.jpa.SharedEntityManagerCreator; |
41 | |
import org.springframework.transaction.TransactionDefinition; |
42 | |
import org.springframework.transaction.TransactionStatus; |
43 | |
import org.springframework.transaction.jta.JtaTransactionManager; |
44 | |
import org.springframework.transaction.support.DefaultTransactionDefinition; |
45 | |
|
46 | 0 | public class LoadSqlListener implements ApplicationListener, |
47 | |
ApplicationContextAware { |
48 | |
|
49 | 0 | final static Logger logger = LoggerFactory.getLogger(LoadSqlListener.class); |
50 | |
|
51 | |
private ApplicationContext applicationContext; |
52 | |
|
53 | 0 | private boolean loaded = false; |
54 | |
|
55 | |
private Map<String,Object> preloadMap; |
56 | |
private JtaTransactionManager jtaTxManager; |
57 | |
|
58 | 0 | private boolean shouldLoadData = false; |
59 | |
|
60 | |
@Override |
61 | |
@SuppressWarnings("unchecked") |
62 | |
public void onApplicationEvent(ApplicationEvent event) { |
63 | 0 | if (event instanceof ContextRefreshedEvent && !loaded && shouldLoadData) { |
64 | |
|
65 | 0 | for (Entry<String, Object> entry : preloadMap.entrySet()) { |
66 | 0 | if(entry.getValue() instanceof java.util.List<?>) { |
67 | 0 | List<String> sqlFileList = (List<String>) entry.getValue(); |
68 | 0 | for(String sqlFile : sqlFileList) { |
69 | 0 | process(entry.getKey(), sqlFile); |
70 | |
} |
71 | 0 | } else { |
72 | 0 | process(entry.getKey(), entry.getValue().toString()); |
73 | |
} |
74 | |
} |
75 | 0 | loaded=true; |
76 | |
} |
77 | 0 | } |
78 | |
|
79 | |
private void process(String entityKey, String sqlFileName) { |
80 | 0 | EntityManagerFactory emf = EntityManagerFactoryUtils |
81 | |
.findEntityManagerFactory(applicationContext, entityKey); |
82 | 0 | EntityManager em = SharedEntityManagerCreator |
83 | |
.createSharedEntityManager(emf); |
84 | |
|
85 | |
File sqlFile; |
86 | |
BufferedReader in; |
87 | |
try{ |
88 | 0 | if(sqlFileName.startsWith("classpath:")){ |
89 | 0 | sqlFile = new ClassPathResource(sqlFileName.substring("classpath:".length())).getFile(); |
90 | |
}else{ |
91 | 0 | sqlFile = new File(sqlFileName); |
92 | |
} |
93 | 0 | in = new BufferedReader(new FileReader(sqlFile)); |
94 | 0 | } catch (Exception e) { |
95 | 0 | throw new RuntimeException(e); |
96 | 0 | } |
97 | |
|
98 | 0 | String ln = ""; |
99 | |
|
100 | 0 | TransactionDefinition txDefinition = new DefaultTransactionDefinition() ; |
101 | 0 | TransactionStatus txStatus = jtaTxManager.getTransaction(txDefinition); |
102 | |
|
103 | |
try { |
104 | 0 | while((ln=in.readLine())!=null){ |
105 | 0 | if(!ln.startsWith("/")&&!ln.startsWith("--")&&StringUtils.isNotBlank(ln)){ |
106 | 0 | ln=ln.replaceFirst("[;/]\\s*$",""); |
107 | 0 | em.createNativeQuery(ln).executeUpdate(); |
108 | |
} |
109 | |
} |
110 | 0 | jtaTxManager.commit(txStatus); |
111 | 0 | } catch (Exception e) { |
112 | 0 | logger.error("Error loading sql file "+sqlFileName+". Failing statement was '" + ln + "'",e); |
113 | 0 | jtaTxManager.rollback(txStatus); |
114 | |
} |
115 | |
finally{ |
116 | 0 | try { |
117 | 0 | in.close(); |
118 | 0 | } catch (IOException e) { |
119 | 0 | logger.error("IO Stream closed " + e); |
120 | 0 | } |
121 | 0 | } |
122 | 0 | } |
123 | |
|
124 | |
@Override |
125 | |
public void setApplicationContext(ApplicationContext applicationContext) |
126 | |
throws BeansException { |
127 | 0 | this.applicationContext = applicationContext; |
128 | 0 | } |
129 | |
|
130 | |
public JtaTransactionManager getJtaTxManager() { |
131 | 0 | return jtaTxManager; |
132 | |
} |
133 | |
|
134 | |
public void setJtaTxManager(JtaTransactionManager jtaTxManager) { |
135 | 0 | this.jtaTxManager = jtaTxManager; |
136 | 0 | } |
137 | |
|
138 | |
public Map<String, Object> getPreloadMap() { |
139 | 0 | return preloadMap; |
140 | |
} |
141 | |
|
142 | |
public void setPreloadMap(Map<String, Object> preloadMap) { |
143 | 0 | this.preloadMap = preloadMap; |
144 | 0 | } |
145 | |
|
146 | |
public void setShouldLoadData(boolean shouldLoadData) { |
147 | 0 | this.shouldLoadData = shouldLoadData; |
148 | 0 | } |
149 | |
|
150 | |
} |