Project

General

Profile

1 6012 leinfelder
package edu.ucsb.nceas.metacat.admin.upgrade;
2
/**
3
 *  '$RCSfile$'
4
 *    Purpose: A Class for upgrading the database to version 1.5
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Saurabh Garg
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
28
29
import java.sql.Connection;
30
import java.sql.Driver;
31
import java.sql.ResultSet;
32
import java.sql.PreparedStatement;
33
import java.sql.Statement;
34
import java.sql.DriverManager;
35
import java.sql.Timestamp;
36 6020 leinfelder
import java.util.Calendar;
37 6012 leinfelder
38 6020 leinfelder
import javax.xml.bind.DatatypeConverter;
39
40 6012 leinfelder
import edu.ucsb.nceas.metacat.admin.AdminException;
41
import edu.ucsb.nceas.metacat.properties.PropertyService;
42
import edu.ucsb.nceas.utilities.SortedProperties;
43
44
public class Upgrade1_10_0 implements UpgradeUtilityInterface {
45
46
    private String driver = null;
47
    private String url = null;
48
    private String user = null;
49
    private String password = null;
50
51
    public boolean upgrade() throws AdminException {
52
    	try {
53
    		// get the properties
54
    		driver = PropertyService.getProperty("database.driver");
55
    	    url = PropertyService.getProperty("database.connectionURI");
56
    	    user = PropertyService.getProperty("database.user");
57
    	    password = PropertyService.getProperty("database.password");
58
59
	        // Create a JDBC connection to the database
60
	    	Connection sqlca = null;
61
	        Driver d = (Driver) Class.forName(driver).newInstance();
62
	        DriverManager.registerDriver(d);
63
	        sqlca = DriverManager.getConnection(url, user, password);
64
65
	        // three tables need to be updated here
66
	        String[] tablesToUpdate = {"xml_nodes", "xml_nodes_revisions", "xml_path_index"};
67
	        for (String tableName: tablesToUpdate) {
68
		        Statement sqlStatement = null;
69
		        PreparedStatement pstmt = null;
70
		        ResultSet rset = null;
71
72
		        sqlStatement = sqlca.createStatement();
73
		        rset = sqlStatement.executeQuery(
74
		            "SELECT DISTINCT NODEID, NODEDATA "
75
		            + "FROM " + tableName + " WHERE "
76
		            + "nodedata IS NOT NULL AND "
77
		            + "UPPER(nodedata) = LOWER(nodedata)");
78
79
		        int count = 0;
80
		        while (rset.next()) {
81
82
		            String nodeid = rset.getString(1);
83
		            String nodedata = rset.getString(2);
84
85
		            try {
86
		                if (!nodedata.trim().equals("")) {
87 6020 leinfelder
88
		                	System.out.println(nodedata);
89
90
		            		Calendar dataDateValue = DatatypeConverter.parseDateTime(nodedata);
91
		                    Timestamp dataTimestamp = new Timestamp(dataDateValue.getTimeInMillis());
92
93
		                    pstmt = sqlca.prepareStatement(
94
		                        "update " + tableName +
95
		                        " set nodedatadate = ?" +
96
		                        " where nodeid = " + nodeid);
97
		                    pstmt.setTimestamp(1, dataTimestamp);
98
		                    pstmt.execute();
99
		                    pstmt.close();
100
101
		                    count++;
102
		                    if (count%5 == 0) {
103
		                        System.out.println(count + "...");
104
		                    }
105
106 6012 leinfelder
		                }
107 6020 leinfelder
		            } catch (Exception e) {
108
		            	// do nothing, was not a valid date
109
		            	e.printStackTrace();
110 6012 leinfelder
		            }
111
		        }
112
		        System.out.println("Table: " + tableName + " complete. " + count + " values converted.");
113
114
		        rset.close();
115
		        sqlStatement.close();
116
	        }
117
	        sqlca.close();
118
    	} catch (Exception e) {
119
    		e.printStackTrace();
120
            throw new AdminException("Error upgrading system to 1.10.0: " + e.getMessage());
121
        }
122
    	return true;
123
    }
124
125
126
    public static void main(String [] ags){
127
128
        try {
129
        	// set up the properties based on the test/deployed configuration of the workspace
130
        	SortedProperties testProperties =
131
				new SortedProperties("test/test.properties");
132
			testProperties.load();
133
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
134
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
135
			// now run it
136
            Upgrade1_10_0 upgrader = new Upgrade1_10_0();
137
            upgrader.upgrade();
138 6020 leinfelder
139
            Calendar date = DatatypeConverter.parseDate("2011-03-17");
140
            System.out.println("date:" + DatatypeConverter.printDate(date));
141
142 6012 leinfelder
        } catch (Exception ex) {
143
            System.out.println("Exception:" + ex.getMessage());
144
            ex.printStackTrace();
145
        }
146
    }
147
}