View Javadoc

1   /**
2    * Copyright 2005-2012 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  package org.kuali.rice.krad.maintainablexml;
17  
18  import java.io.BufferedReader;
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStreamReader;
22  import java.util.Arrays;
23  import java.util.HashMap;
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  import org.w3c.dom.Node;
30  import org.w3c.dom.NodeList;
31  
32  /**
33   * This is a command line utility class which upgrades 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   * <p>Instructions:</p>
37   * <ol>
38   *   <li>Backup database.</li>
39   *   <li>Add the conversion rules to the rules xml file -
40   *       ..\rice\development-tools\src\main\resources\org\kuali\rice\devtools\krad\maintainablexml\MaintainableXMLUpgradeRules.xml
41   *       See comments in the xml file to setup the rules.</li>
42   *   <li>Run this class.</li>
43   *   <li>Enter the rice config file location that has the database connection properties. Only enter the location relative
44   *       to user.dir/kuali/main.</li>
45   *   <li>Select Run mode. Mode 1 will do the xml upgrade and update the krns_maint_doc_t table with the new xml. Cannot
46   *       roll back after this has been done. Mode 2 will only print out the old xml and the new xml - this can be used
47   *       to test the rules xml setup.</li>
48   *   <li>Select the range of document numbers to upgrade. Enter % on from range prompt to upgrade all.</li>
49   * </ol>
50   *
51   * @author Kuali Rice Team (rice.collab@kuali.org)
52   */
53  public class ConvertMaintainableXML {
54  
55      public static void main(String[] args) {
56          //  prompt the user to enter settings file
57          String settingsFile = readInput("Enter config file location relative to Kuali main user directory "
58                  + "(ie. dev/sample-app-config.xml OR dev/trnapp-config.xml) : ", null, false);
59  
60          String filePath = System.getProperty("user.home") + "/kuali/main/" + settingsFile;
61  
62          File file = new File(filePath);
63          if (!file.exists()) {
64              System.out.println("The settings file location is not valid : \n" + filePath);
65              System.exit(1);
66          }
67  
68          String runMode = readInput("Run mode :\n1. Update xml in DB\n2. Print old and new xml\nEnter 1 or 2\n",
69                  new String[]{"1", "2"}, false);
70  
71          String fromRange = readInput("Please enter document start range value ('%' or '' for all) :\n",
72                  new String[]{"%", ""}, true);
73  
74          String toRange = null;
75  
76          boolean hasRangeParameters = false;
77  
78          if (!"".equals(fromRange) && !"%".equals(fromRange)) {
79              toRange = readInput("Please enter end range value :\n", null, true);
80              System.out.println("Upgrading document numbers from " + fromRange + " to " + toRange + "\n");
81              hasRangeParameters = true;
82          }
83  
84          System.out.println("Looking up settings file... " + filePath + "\n");
85  
86          try {
87              HashMap map = getSettings(filePath);
88              System.out.println("Using the following settings : " + map + "\n");
89              FileConverter fileConverter = new FileConverter();
90              fileConverter.runFileConversion(map, runMode, fromRange, toRange, hasRangeParameters);
91          } catch (Exception e) {
92              System.out.println("Error executing conversion : " + e.getMessage());
93              e.printStackTrace();
94          }
95  
96      }
97  
98      /**
99       * Displays message in command line and read user input. Checks that entered values are within valid input.
100      *
101      * @param message - the message string to print out
102      * @param validOptions - the allowed user input
103      * @return the string input from the user
104      */
105     private static String readInput(String message, String[] validOptions, boolean numeric) {
106         System.out.print(message);
107         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
108 
109         String inputString = null;
110 
111         try {
112             inputString = br.readLine();
113             if (numeric && (validOptions == null || !Arrays.asList(validOptions).contains(inputString))) {
114                 Integer.parseInt(inputString);
115                 return inputString;
116             }
117         } catch (IOException ioe) {
118             System.out.println("IO error trying to read input!");
119             System.exit(1);
120         } catch (NumberFormatException nfe) {
121             System.out.println("Invalid Option! Must be numeric.");
122             readInput(message, validOptions, numeric);
123         }
124         if (validOptions != null && !Arrays.asList(validOptions).contains(inputString)) {
125             System.out.println("Invalid Option!");
126             readInput(message, validOptions, numeric);
127         }
128         return inputString;
129     }
130 
131     /**
132      * Parses settings file and put the properties in a map.
133      *
134      * @param filePath - the location of the settings file
135      * @return a HashMap populated with the settings
136      * @throws Exception
137      */
138     public static HashMap getSettings(String filePath) throws Exception {
139         File file = new File(filePath);
140         HashMap params = new HashMap();
141         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
142         DocumentBuilder db = dbf.newDocumentBuilder();
143         Document doc = db.parse(file);
144         doc.getDocumentElement().normalize();
145         NodeList nodeLst = doc.getElementsByTagName("param");
146 
147         for (int s = 0; s < nodeLst.getLength(); s++) {
148             Node fstNode = nodeLst.item(s);
149             if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
150                 Element fstElmnt = (Element) fstNode;
151                 String paramName = fstElmnt.getAttribute("name");
152                 NodeList textFNList = fstElmnt.getChildNodes();
153                 String paramValue = ((Node) textFNList.item(0)).getNodeValue().trim();
154                 params.put(paramName, paramValue);
155             }
156         }
157         return params;
158     }
159 
160 }