View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17   * To change this template, choose Tools | Templates
18   * and open the template in the editor.
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   * Used to upgrade the maintenance document xml stored in krns_maint_doc_t.doc_cntnt
34   * to be able to still open and use any maintenance documents that were enroute at the time of an upgrade to Rice 2.0.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  
39  public class FileConverter {
40  
41      private JdbcTemplate jdbcTemplate;
42      private int totalDocs = 0;
43  
44      /**
45       * Selects all the encrypted xml documents from krns_maint_doc_t, decrypts them, runs the rules to upgrade them,
46       * encrypt them and update krns_maint_doc_t with the upgraded xml.
47       *
48       * @param settingsMap - the settings
49       * @throws Exception
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          // If user entered range add the sql parameters and filter results because DOC_HDR_ID is a varchar field.
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                  // Check that all docId's is in range
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       * Creates the data source from the settings map
86       *
87       * @param settingsMap - the settingMap containing the db connection settings
88       * @return the DataSource object
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      * Called for each row in the processRow method of the spring query. Upgrades the xml and update the
110      * krns_maint_doc_t table.
111      *
112      * @param docId - the document id string
113      * @param docCntnt - the old xml string
114      * @param encryptServ - the encryption service used to encrypt/decrypt the xml
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 }