Project

General

Profile

1
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: leinfelder $'
10
 *     '$Date: 2011-03-16 22:56:31 -0700 (Wed, 16 Mar 2011) $'
11
 * '$Revision: 6012 $'
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

    
36
import edu.ucsb.nceas.metacat.admin.AdminException;
37
import edu.ucsb.nceas.metacat.properties.PropertyService;
38

    
39
public class Upgrade1_5_0 implements UpgradeUtilityInterface {
40

    
41
    private String driver = null;
42
    private String database = null;
43
    private String url = null;
44
    private String user = null;
45
    private String password = null;
46

    
47
    public boolean upgrade() throws AdminException {
48
    
49
    	try {
50
	    	// get the properties
51
			driver = PropertyService.getProperty("database.driver");
52
			database = PropertyService.getProperty("database.type");
53
		    url = PropertyService.getProperty("database.connectionURI");
54
		    user = PropertyService.getProperty("database.user");
55
		    password = PropertyService.getProperty("database.password");
56
		    
57
		    // only do oracle
58
		    if (!database.equals("oracle")) {
59
		    	return true;
60
		    }
61
		    
62
	        // Create a JDBC connection to the database    	
63
	    	Connection sqlca = null;
64
	        Driver d = (Driver) Class.forName(driver).newInstance();
65
	        DriverManager.registerDriver(d);
66
	        sqlca = DriverManager.getConnection(url, user, password);
67
		    
68
	        Statement sqlStatement = null;
69
	        PreparedStatement pstmt = null;
70
	        ResultSet rset = null;
71
	
72
	        // Delete old nodedatanumerical column from xml_nodes if one exsists
73
	        try {
74
	            System.out.println(
75
	                "Deleting old nodedatanumerical column from xml_nodes...");
76
	
77
	            pstmt = sqlca.prepareStatement(
78
	                "ALTER TABLE xml_nodes DROP COLUMN nodedatanumerical");
79
	            pstmt.execute();
80
	            pstmt.close();
81
	
82
	            System.out.println("Done.");
83
	        } catch (Exception e) {
84
	            System.out.println(" column not found.");
85
	        }
86
	
87
	        // Create nodedatanumerical column in xml_nodes
88
	        System.out.println(
89
	            "Creating new nodedatanumerical column in xml_nodes...");
90
	
91
	        if (database.equals("oracle")) {
92
	            pstmt = sqlca.prepareStatement(
93
	                "ALTER TABLE xml_nodes ADD nodedatanumerical NUMBER");
94
	        }
95
	        else {
96
	            pstmt = sqlca.prepareStatement(
97
	                "ALTER TABLE xml_nodes ADD nodedatanumerical FLOAT8");
98
	        }
99
	        pstmt.execute();
100
	        pstmt.close();
101
	
102
	        System.out.println("Done.");
103
	
104
	        // Copy numerical values from nodedata to
105
	        // nodedatanumerical column in xml_nodes
106
	        System.out.println(
107
	            "Please be patient as the next upgrade step can be extremely time consuming.");
108
	        System.out.println(
109
	            "Copy numerical values from nodedata to nodedatanumerical "
110
	            + "in xml_nodes...");
111
	
112
	        sqlStatement = sqlca.createStatement();
113
	        rset = sqlStatement.executeQuery(
114
	            "SELECT DISTINCT NODEID, NODEDATA "
115
	            + "FROM xml_nodes WHERE "
116
	            + "nodedata IS NOT NULL AND "
117
	            + "UPPER(nodedata) = LOWER(nodedata)");
118
	
119
	        int count = 0;
120
	        while (rset.next()) {
121
	
122
	            String nodeid = rset.getString(1);
123
	            String nodedata = rset.getString(2);
124
	
125
	            try {
126
	                if (!nodedata.trim().equals("")) {
127
	                    double s = Double.parseDouble(nodedata);
128
	
129
	                    pstmt = sqlca.prepareStatement(
130
	                        "update xml_nodes set nodedatanumerical = " + s +
131
	                        " where nodeid=" + nodeid);
132
	                    pstmt.execute();
133
	                    pstmt.close();
134
	
135
	                    count++;
136
	                    if (count%5 == 0) {
137
	                        System.out.println(count + "...");
138
	                    }
139
	                }
140
	            } catch (NumberFormatException nfe) {
141
	
142
	            } catch (Exception e) {
143
	                System.out.println("Exception:" + e.getMessage());
144
	            }
145
	        }
146
	        System.out.println("\nDone. " + count + " values copied.");
147
	
148
	        rset.close();
149
	        sqlStatement.close();
150
	        sqlca.close();
151
    	} catch (Exception e) {
152
            throw new AdminException("Error upgrading system to 1.5: " + e.getMessage());
153
        }
154
    	return true;
155

    
156
    }
157

    
158

    
159
    public static void main(String [] ags){
160

    
161
        try {
162
            Upgrade1_5_0 upgrader = new Upgrade1_5_0();
163
            upgrader.upgrade();
164
        
165
        } catch (Exception ex) {
166
            System.out.println("Exception:" + ex.getMessage());
167
        }
168
    }
169
}
(2-2/5)