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
|
import java.util.Map;
|
39
|
import java.util.TreeMap;
|
40
|
|
41
|
import javax.xml.bind.DatatypeConverter;
|
42
|
|
43
|
import org.apache.commons.logging.Log;
|
44
|
import org.apache.commons.logging.LogFactory;
|
45
|
|
46
|
import edu.ucsb.nceas.metacat.admin.AdminException;
|
47
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
48
|
import edu.ucsb.nceas.utilities.SortedProperties;
|
49
|
|
50
|
public class UpgradeNodeDataDatetime implements UpgradeUtilityInterface {
|
51
|
|
52
|
protected static Log log = LogFactory.getLog(UpgradeNodeDataDatetime.class);
|
53
|
|
54
|
private String driver = null;
|
55
|
private String url = null;
|
56
|
private String user = null;
|
57
|
private String password = null;
|
58
|
|
59
|
public boolean upgrade() throws AdminException {
|
60
|
String[] tablesToUpdate = {"xml_nodes", "xml_nodes_revisions", "xml_path_index"};
|
61
|
boolean success = true;
|
62
|
for (String tableName: tablesToUpdate) {
|
63
|
success = success && upgradeSingleTable(tableName);
|
64
|
}
|
65
|
return success;
|
66
|
}
|
67
|
|
68
|
private Connection getConnection() throws AdminException {
|
69
|
|
70
|
Connection conn = null;
|
71
|
try {
|
72
|
// get the properties
|
73
|
driver = PropertyService.getProperty("database.driver");
|
74
|
url = PropertyService.getProperty("database.connectionURI");
|
75
|
user = PropertyService.getProperty("database.user");
|
76
|
password = PropertyService.getProperty("database.password");
|
77
|
|
78
|
// Create a JDBC connection to the database
|
79
|
Driver d = (Driver) Class.forName(driver).newInstance();
|
80
|
DriverManager.registerDriver(d);
|
81
|
conn = DriverManager.getConnection(url, user, password);
|
82
|
} catch (Exception e) {
|
83
|
String msg = "Could not get connection: " + e.getMessage();
|
84
|
log.error(msg);
|
85
|
AdminException ae = new AdminException(msg);
|
86
|
ae.initCause(e);
|
87
|
throw ae;
|
88
|
}
|
89
|
return conn;
|
90
|
|
91
|
}
|
92
|
|
93
|
private Map<String, Timestamp> getDataValues(String tableName) throws AdminException {
|
94
|
|
95
|
Map<String, Timestamp> dataValues = new TreeMap<String, Timestamp>();
|
96
|
|
97
|
try {
|
98
|
|
99
|
Connection conn = getConnection();
|
100
|
|
101
|
Statement sqlStatement = null;
|
102
|
ResultSet rset = null;
|
103
|
String nodeid = null;
|
104
|
String nodedata = null;
|
105
|
int count = 0;
|
106
|
|
107
|
// select the nodes that are not numeric and not null
|
108
|
sqlStatement = conn .createStatement();
|
109
|
rset = sqlStatement.executeQuery(
|
110
|
"SELECT DISTINCT NODEID, NODEDATA "
|
111
|
+ "FROM " + tableName + " WHERE "
|
112
|
+ "nodedata IS NOT NULL "
|
113
|
//+ "AND nodedatanumerical IS NULL"
|
114
|
);
|
115
|
|
116
|
count = 0;
|
117
|
while (rset.next()) {
|
118
|
|
119
|
nodeid = rset.getString(1);
|
120
|
nodedata = rset.getString(2);
|
121
|
|
122
|
if (!nodedata.trim().equals("")) {
|
123
|
try {
|
124
|
Calendar dataDateValue = DatatypeConverter.parseDateTime(nodedata);
|
125
|
Timestamp dataTimestamp = new Timestamp(dataDateValue.getTimeInMillis());
|
126
|
dataValues.put(nodeid, dataTimestamp);
|
127
|
} catch (Exception e) {
|
128
|
// probably not a valid date
|
129
|
}
|
130
|
}
|
131
|
|
132
|
log.debug("parsed: " + nodedata);
|
133
|
count++;
|
134
|
if (count%5 == 0) {
|
135
|
log.debug(count + "...");
|
136
|
}
|
137
|
}
|
138
|
log.warn("Table: " + tableName + " complete. " + count + " values converted.");
|
139
|
rset.close();
|
140
|
sqlStatement.close();
|
141
|
conn.close();
|
142
|
} catch (Exception e) {
|
143
|
String msg = "Error parsing date time node data: " + e.getMessage();
|
144
|
log.error(msg, e);
|
145
|
throw new AdminException(msg);
|
146
|
}
|
147
|
return dataValues;
|
148
|
}
|
149
|
|
150
|
private boolean upgradeSingleTable(String tableName) throws AdminException {
|
151
|
|
152
|
Map<String, Timestamp> dataValues = getDataValues(tableName);
|
153
|
|
154
|
Connection conn = getConnection();
|
155
|
|
156
|
for (String nodeid: dataValues.keySet()) {
|
157
|
Timestamp dataTimestamp = dataValues.get(nodeid);
|
158
|
upgradeSingleValue(tableName, nodeid, dataTimestamp , conn);
|
159
|
}
|
160
|
|
161
|
if (conn != null) {
|
162
|
try {
|
163
|
conn.close();
|
164
|
} catch (SQLException e) {
|
165
|
throw new AdminException("Error closing connection: " + e.getMessage());
|
166
|
}
|
167
|
}
|
168
|
|
169
|
return true;
|
170
|
|
171
|
}
|
172
|
|
173
|
private boolean upgradeSingleValue(String tableName, String nodeid, Timestamp dataTimestamp, Connection conn) throws AdminException {
|
174
|
try {
|
175
|
|
176
|
PreparedStatement pstmt = null;
|
177
|
|
178
|
try {
|
179
|
pstmt = conn.prepareStatement(
|
180
|
"update " + tableName +
|
181
|
" set nodedatadate = ? " +
|
182
|
" where nodeid = ? ");
|
183
|
pstmt.setTimestamp(1, dataTimestamp);
|
184
|
pstmt.setString(2, nodeid);
|
185
|
|
186
|
pstmt.execute();
|
187
|
|
188
|
log.debug("processed nodedata: " + dataTimestamp);
|
189
|
|
190
|
} catch (Exception e) {
|
191
|
// do nothing, was not a valid date
|
192
|
//e.printStackTrace();
|
193
|
} finally {
|
194
|
if (pstmt != null) {
|
195
|
pstmt.close();
|
196
|
}
|
197
|
}
|
198
|
|
199
|
} catch (Exception e) {
|
200
|
String msg = "Error updating date time node data: " + e.getMessage();
|
201
|
log.error(msg, e);
|
202
|
throw new AdminException(msg);
|
203
|
}
|
204
|
return true;
|
205
|
}
|
206
|
|
207
|
|
208
|
public static void main(String [] ags){
|
209
|
|
210
|
try {
|
211
|
// set up the properties based on the test/deployed configuration of the workspace
|
212
|
SortedProperties testProperties =
|
213
|
new SortedProperties("test/test.properties");
|
214
|
testProperties.load();
|
215
|
String metacatContextDir = testProperties.getProperty("metacat.contextDir");
|
216
|
PropertyService.getInstance(metacatContextDir + "/WEB-INF");
|
217
|
// now run it
|
218
|
UpgradeNodeDataDatetime upgrader = new UpgradeNodeDataDatetime();
|
219
|
upgrader.upgrade();
|
220
|
|
221
|
// test the converter
|
222
|
//Calendar date = DatatypeConverter.parseDate("2011-03-17");
|
223
|
//System.out.println("date:" + DatatypeConverter.printDate(date));
|
224
|
|
225
|
} catch (Exception ex) {
|
226
|
System.out.println("Exception:" + ex.getMessage());
|
227
|
ex.printStackTrace();
|
228
|
}
|
229
|
}
|
230
|
}
|