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 edu.ucsb.nceas.metacat.admin.AdminException;
|
42
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
43
|
import edu.ucsb.nceas.utilities.SortedProperties;
|
44
|
|
45
|
public class UpgradeNodeDataDatetime implements UpgradeUtilityInterface {
|
46
|
|
47
|
private String driver = null;
|
48
|
private String url = null;
|
49
|
private String user = null;
|
50
|
private String password = null;
|
51
|
|
52
|
public boolean upgrade() throws AdminException {
|
53
|
String[] tablesToUpdate = {"xml_nodes", "xml_nodes_revisions", "xml_path_index"};
|
54
|
boolean success = true;
|
55
|
for (String tableName: tablesToUpdate) {
|
56
|
success = success && upgradeSingleTable(tableName);
|
57
|
}
|
58
|
return success;
|
59
|
}
|
60
|
|
61
|
private boolean upgradeSingleTable(String tableName) throws AdminException {
|
62
|
try {
|
63
|
// get the properties
|
64
|
driver = PropertyService.getProperty("database.driver");
|
65
|
url = PropertyService.getProperty("database.connectionURI");
|
66
|
user = PropertyService.getProperty("database.user");
|
67
|
password = PropertyService.getProperty("database.password");
|
68
|
|
69
|
// Create a JDBC connection to the database
|
70
|
Connection sqlca = null;
|
71
|
Driver d = (Driver) Class.forName(driver).newInstance();
|
72
|
DriverManager.registerDriver(d);
|
73
|
sqlca = DriverManager.getConnection(url, user, password);
|
74
|
|
75
|
|
76
|
Statement sqlStatement = null;
|
77
|
PreparedStatement pstmt = null;
|
78
|
ResultSet rset = null;
|
79
|
String nodeid = null;
|
80
|
String nodedata = null;
|
81
|
int count = 0;
|
82
|
|
83
|
// select the nodes that are not numeric and not null
|
84
|
sqlStatement = sqlca.createStatement();
|
85
|
rset = sqlStatement.executeQuery(
|
86
|
"SELECT DISTINCT NODEID, NODEDATA "
|
87
|
+ "FROM " + tableName + " WHERE "
|
88
|
+ "nodedata IS NOT NULL "
|
89
|
//+ "AND nodedatanumerical IS NULL"
|
90
|
);
|
91
|
|
92
|
count = 0;
|
93
|
while (rset.next()) {
|
94
|
|
95
|
nodeid = rset.getString(1);
|
96
|
nodedata = rset.getString(2);
|
97
|
|
98
|
try {
|
99
|
if (!nodedata.trim().equals("")) {
|
100
|
|
101
|
|
102
|
Calendar dataDateValue = DatatypeConverter.parseDateTime(nodedata);
|
103
|
Timestamp dataTimestamp = new Timestamp(dataDateValue.getTimeInMillis());
|
104
|
|
105
|
pstmt = sqlca.prepareStatement(
|
106
|
"update " + tableName +
|
107
|
" set nodedatadate = ?" +
|
108
|
" where nodeid = " + nodeid);
|
109
|
pstmt.setTimestamp(1, dataTimestamp);
|
110
|
pstmt.execute();
|
111
|
pstmt.close();
|
112
|
|
113
|
System.out.println("processed: " + nodedata);
|
114
|
count++;
|
115
|
if (count%5 == 0) {
|
116
|
System.out.println(count + "...");
|
117
|
}
|
118
|
|
119
|
}
|
120
|
} catch (SQLException sqle) {
|
121
|
pstmt.close();
|
122
|
} catch (Exception e) {
|
123
|
// do nothing, was not a valid date
|
124
|
//e.printStackTrace();
|
125
|
}
|
126
|
}
|
127
|
System.out.println("Table: " + tableName + " complete. " + count + " values converted.");
|
128
|
|
129
|
rset.close();
|
130
|
sqlStatement.close();
|
131
|
sqlca.close();
|
132
|
} catch (Exception e) {
|
133
|
e.printStackTrace();
|
134
|
throw new AdminException("Error upgrading system to 1.10.0: " + e.getMessage());
|
135
|
}
|
136
|
return true;
|
137
|
}
|
138
|
|
139
|
|
140
|
public static void main(String [] ags){
|
141
|
|
142
|
try {
|
143
|
// set up the properties based on the test/deployed configuration of the workspace
|
144
|
SortedProperties testProperties =
|
145
|
new SortedProperties("test/test.properties");
|
146
|
testProperties.load();
|
147
|
String metacatContextDir = testProperties.getProperty("metacat.contextDir");
|
148
|
PropertyService.getInstance(metacatContextDir + "/WEB-INF");
|
149
|
// now run it
|
150
|
UpgradeNodeDataDatetime upgrader = new UpgradeNodeDataDatetime();
|
151
|
upgrader.upgrade();
|
152
|
|
153
|
// test the converter
|
154
|
//Calendar date = DatatypeConverter.parseDate("2011-03-17");
|
155
|
//System.out.println("date:" + DatatypeConverter.printDate(date));
|
156
|
|
157
|
} catch (Exception ex) {
|
158
|
System.out.println("Exception:" + ex.getMessage());
|
159
|
ex.printStackTrace();
|
160
|
}
|
161
|
}
|
162
|
}
|