1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.kuali.rice.krad.maintainablexml;
21
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.util.HashMap;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.sql.DataSource;
28 import org.springframework.jdbc.core.JdbcTemplate;
29 import org.springframework.jdbc.core.RowCallbackHandler;
30 import org.springframework.jdbc.datasource.DriverManagerDataSource;
31
32
33
34
35
36
37
38
39 public class FileConverter {
40
41 private JdbcTemplate jdbcTemplate;
42 private int totalDocs = 0;
43
44
45
46
47
48
49
50
51 public void runFileConversion(HashMap settingsMap, final String runMode, final String fromRange,
52 final String toRange, final boolean hasRangeParameters) throws Exception {
53
54 final EncryptionService encryptService = new EncryptionService((String) settingsMap.get("encryption.key"));
55
56 String docSQL = "SELECT DOC_HDR_ID, DOC_CNTNT FROM krns_maint_doc_t ";
57
58
59 if (hasRangeParameters) {
60 docSQL = docSQL.concat(" WHERE DOC_HDR_ID >= '" + fromRange + "' AND DOC_HDR_ID <= '" + toRange + "'");
61 }
62 System.out.println("SQL to run:" + docSQL);
63
64 jdbcTemplate = new JdbcTemplate(getDataSource(settingsMap));
65 jdbcTemplate.query(docSQL, new RowCallbackHandler() {
66
67 public void processRow(ResultSet rs) throws SQLException {
68
69 if (hasRangeParameters) {
70 int docId = Integer.parseInt(rs.getString(1));
71 if (docId >= Integer.parseInt(fromRange) && docId <= Integer.parseInt(toRange)) {
72 processDocumentRow(rs.getString(1), rs.getString(2), encryptService, runMode);
73 }
74 } else {
75 processDocumentRow(rs.getString(1), rs.getString(2), encryptService, runMode);
76 }
77 }
78 });
79
80 System.out.println(totalDocs + " maintenance documents upgraded.");
81
82 }
83
84
85
86
87
88
89
90 public static DataSource getDataSource(HashMap settingsMap) {
91 String driver;
92 if ("MySQL".equals(settingsMap.get("datasource.ojb.platform"))) {
93 driver = "com.mysql.jdbc.Driver";
94 } else if ("Oracle9i".equals(settingsMap.get("datasource.ojb.platform"))) {
95 driver = "oracle.jdbc.driver.OracleDriver";
96 } else {
97 driver = (String) settingsMap.get("datasource.driver.name");
98 }
99
100 DriverManagerDataSource dataSource = new DriverManagerDataSource();
101 dataSource.setDriverClassName(driver);
102 dataSource.setUrl((String) settingsMap.get("datasource.url"));
103 dataSource.setUsername((String) settingsMap.get("datasource.username"));
104 dataSource.setPassword((String) settingsMap.get("datasource.password"));
105 return dataSource;
106 }
107
108
109
110
111
112
113
114
115
116 public void processDocumentRow(String docId, String docCntnt, EncryptionService encryptServ, String runMode) {
117 System.out.println(docId);
118 try {
119 String oldXml = docCntnt;
120 if (encryptServ.isEnabled()) {
121 oldXml = encryptServ.decrypt(docCntnt);
122 }
123 if ("2".equals(runMode)) {
124 System.out.println("------ ORIGINAL DOC XML --------");
125 System.out.println(oldXml);
126 System.out.println("--------------------------------");
127 }
128
129 MaintainableXMLConversionServiceImpl maintainableXMLConversionServiceImpl = new MaintainableXMLConversionServiceImpl();
130 String newXML = maintainableXMLConversionServiceImpl.transformMaintainableXML(oldXml);
131
132 if ("2".equals(runMode)) {
133 System.out.println("******* UPGRADED DOC XML ********");
134 System.out.println(newXML);
135 System.out.println("*********************************\n");
136 }
137 if ("1".equals(runMode)) {
138 if (encryptServ.isEnabled()) {
139 jdbcTemplate.update("update krns_maint_doc_t set DOC_CNTNT = ? where DOC_HDR_ID = ?",
140 new Object[]{encryptServ.encrypt(newXML), docId});
141 } else {
142 jdbcTemplate.update("update krns_maint_doc_t set DOC_CNTNT = ? where DOC_HDR_ID = ?",
143 new Object[]{newXML, docId});
144 }
145 }
146 totalDocs++;
147 } catch (Exception ex) {
148 Logger.getLogger(FileConverter.class.getName()).log(Level.SEVERE, null, ex);
149 System.exit(1);
150 }
151 }
152 }