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-29 18:23:38 +0000 (Tue, 29 Mar 2011) $'
11
 * '$Revision: 6025 $'
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.SQLException;
34
import java.sql.Statement;
35
import java.sql.DriverManager;
36
import java.sql.Timestamp;
37
import java.util.Calendar;
38

    
39
import javax.xml.bind.DatatypeConverter;
40

    
41
import org.apache.commons.logging.Log;
42
import org.apache.commons.logging.LogFactory;
43

    
44
import edu.ucsb.nceas.metacat.admin.AdminException;
45
import edu.ucsb.nceas.metacat.properties.PropertyService;
46
import edu.ucsb.nceas.utilities.SortedProperties;
47

    
48
public class UpgradeNodeDataDatetime implements UpgradeUtilityInterface {
49

    
50
	protected static Log log = LogFactory.getLog(UpgradeNodeDataDatetime.class);
51
	
52
    private String driver = null;
53
    private String url = null;
54
    private String user = null;
55
    private String password = null;
56

    
57
    public boolean upgrade() throws AdminException {
58
    	String[] tablesToUpdate = {"xml_nodes", "xml_nodes_revisions", "xml_path_index"};
59
        boolean success = true;
60
    	for (String tableName: tablesToUpdate) {
61
        	success = success && upgradeSingleTable(tableName);
62
        }
63
    	return success;
64
    }
65
    
66
    private boolean upgradeSingleTable(String tableName) throws AdminException {
67
    	try {
68
    		// get the properties
69
    		driver = PropertyService.getProperty("database.driver");
70
    	    url = PropertyService.getProperty("database.connectionURI");
71
    	    user = PropertyService.getProperty("database.user");
72
    	    password = PropertyService.getProperty("database.password");
73
    	    
74
	        // Create a JDBC connection to the database    	
75
	    	Connection sqlca = null;
76
	        Driver d = (Driver) Class.forName(driver).newInstance();
77
	        DriverManager.registerDriver(d);
78
	        sqlca = DriverManager.getConnection(url, user, password);
79
	    	
80
	        
81
	        Statement sqlStatement = null;
82
	        PreparedStatement pstmt = null;
83
	        ResultSet rset = null;
84
	        String nodeid = null;
85
            String nodedata = null;
86
            int count = 0;
87

    
88
	        	// select the nodes that are not numeric and not null
89
		        sqlStatement = sqlca.createStatement();
90
		        rset = sqlStatement.executeQuery(
91
		            "SELECT DISTINCT NODEID, NODEDATA "
92
		            + "FROM " + tableName + " WHERE "
93
		            + "nodedata IS NOT NULL "
94
		            //+ "AND nodedatanumerical IS NULL"
95
		            );
96
		
97
		        count = 0;
98
		        while (rset.next()) {
99
		
100
		            nodeid = rset.getString(1);
101
		            nodedata = rset.getString(2);
102
		
103
		            try {
104
		                if (!nodedata.trim().equals("")) {
105
		                	
106
		                	
107
		            		Calendar dataDateValue = DatatypeConverter.parseDateTime(nodedata);
108
		                    Timestamp dataTimestamp = new Timestamp(dataDateValue.getTimeInMillis());
109
		
110
		                    pstmt = sqlca.prepareStatement(
111
		                        "update " + tableName +
112
		                        " set nodedatadate = ?" +
113
		                        " where nodeid = " + nodeid);
114
		                    pstmt.setTimestamp(1, dataTimestamp);
115
		                    pstmt.execute();
116
		                    //pstmt.close();
117

    
118
		                    log.debug("processed: " + nodedata);
119
		                    count++;
120
		                    if (count%5 == 0) {
121
		                        log.debug(count + "...");
122
		                    }
123
		            		
124
		                }
125
		            } catch (Exception e) { 
126
		            	// do nothing, was not a valid date
127
		            	//e.printStackTrace();
128
		            } finally {
129
		            	if (pstmt != null) {
130
		            		pstmt.close();
131
		            	}
132
		            }
133
		        }
134
		        log.warn("Table: " + tableName + " complete. " + count + " values converted.");
135
		
136
		        rset.close();
137
		        sqlStatement.close();
138
	        sqlca.close();
139
    	} catch (Exception e) {
140
    		String msg = "Error updating date time node data: " + e.getMessage();
141
    		log.error(msg, e);
142
            throw new AdminException(msg);
143
        }
144
    	return true;
145
    }
146

    
147

    
148
    public static void main(String [] ags){
149

    
150
        try {
151
        	// set up the properties based on the test/deployed configuration of the workspace
152
        	SortedProperties testProperties = 
153
				new SortedProperties("test/test.properties");
154
			testProperties.load();
155
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
156
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
157
			// now run it
158
            UpgradeNodeDataDatetime upgrader = new UpgradeNodeDataDatetime();
159
	        upgrader.upgrade();
160
	        
161
	        // test the converter
162
            //Calendar date = DatatypeConverter.parseDate("2011-03-17");
163
            //System.out.println("date:" + DatatypeConverter.printDate(date));
164
            
165
        } catch (Exception ex) {
166
            System.out.println("Exception:" + ex.getMessage());
167
            ex.printStackTrace();
168
        }
169
    }
170
}
(5-5/6)