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: 2012-01-05 14:49:29 -0800 (Thu, 05 Jan 2012) $'
|
11
|
* '$Revision: 6856 $'
|
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.io.File;
|
30
|
import java.io.FileFilter;
|
31
|
import java.sql.Connection;
|
32
|
import java.sql.Driver;
|
33
|
import java.sql.DriverManager;
|
34
|
import java.sql.PreparedStatement;
|
35
|
import java.sql.SQLException;
|
36
|
import java.util.ArrayList;
|
37
|
import java.util.List;
|
38
|
|
39
|
import org.apache.commons.io.filefilter.EmptyFileFilter;
|
40
|
import org.apache.commons.logging.Log;
|
41
|
import org.apache.commons.logging.LogFactory;
|
42
|
|
43
|
import edu.ucsb.nceas.metacat.admin.AdminException;
|
44
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
45
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
46
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
47
|
import edu.ucsb.nceas.utilities.SortedProperties;
|
48
|
|
49
|
public class UpgradeEmptyReplicatedDataFile implements UpgradeUtilityInterface {
|
50
|
|
51
|
protected static Log log = LogFactory.getLog(UpgradeEmptyReplicatedDataFile.class);
|
52
|
|
53
|
private String driver = null;
|
54
|
private String url = null;
|
55
|
private String user = null;
|
56
|
private String password = null;
|
57
|
|
58
|
public boolean upgrade() throws AdminException {
|
59
|
|
60
|
boolean success = true;
|
61
|
|
62
|
Connection sqlca = null;
|
63
|
PreparedStatement pstmt = null;
|
64
|
|
65
|
// look up the empty files from the file system
|
66
|
File dataDir = null;
|
67
|
try {
|
68
|
dataDir = new File(PropertyService.getProperty("application.datafilepath"));
|
69
|
} catch (PropertyNotFoundException e) {
|
70
|
e.printStackTrace();
|
71
|
return false;
|
72
|
}
|
73
|
File[] emptyFiles = dataDir.listFiles((FileFilter) EmptyFileFilter.EMPTY);
|
74
|
if (emptyFiles.length == 0) {
|
75
|
log.info("No empty data files found");
|
76
|
return true;
|
77
|
}
|
78
|
|
79
|
// track the ones we end up deleting from the DB
|
80
|
List<File> processedFiles = new ArrayList<File>();
|
81
|
|
82
|
try {
|
83
|
|
84
|
// get the properties
|
85
|
driver = PropertyService.getProperty("database.driver");
|
86
|
url = PropertyService.getProperty("database.connectionURI");
|
87
|
user = PropertyService.getProperty("database.user");
|
88
|
password = PropertyService.getProperty("database.password");
|
89
|
|
90
|
// Create a JDBC connection to the database
|
91
|
Driver d = (Driver) Class.forName(driver).newInstance();
|
92
|
DriverManager.registerDriver(d);
|
93
|
sqlca = DriverManager.getConnection(url, user, password);
|
94
|
sqlca.setAutoCommit(false);
|
95
|
|
96
|
for (File emptyFile: emptyFiles) {
|
97
|
|
98
|
int count = 0;
|
99
|
|
100
|
// delete each empty file from the tables
|
101
|
String emptyDocid = emptyFile.getName();
|
102
|
String docid = DocumentUtil.getDocIdFromString(emptyDocid);
|
103
|
String rev = DocumentUtil.getRevisionStringFromString(emptyDocid);
|
104
|
|
105
|
// xml_documents
|
106
|
pstmt = sqlca.prepareStatement("DELETE FROM xml_documents WHERE docid = ? and server_location != '1'");
|
107
|
pstmt.setString(1, docid);
|
108
|
count = pstmt.executeUpdate();
|
109
|
// xml_revisions
|
110
|
pstmt = sqlca.prepareStatement("DELETE FROM xml_revisions WHERE docid = ? and server_location != '1'");
|
111
|
pstmt.setString(1, docid);
|
112
|
count = count + pstmt.executeUpdate();
|
113
|
|
114
|
// if we did remove it from the the DB
|
115
|
if (count > 0) {
|
116
|
processedFiles.add(emptyFile);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
// all or nothing
|
121
|
sqlca.commit();
|
122
|
|
123
|
} catch (Exception e) {
|
124
|
try {
|
125
|
// rollback if there was even a single error
|
126
|
sqlca.rollback();
|
127
|
} catch (SQLException e1) {
|
128
|
// TODO Auto-generated catch block
|
129
|
e1.printStackTrace();
|
130
|
}
|
131
|
success = false;
|
132
|
} finally {
|
133
|
// clean up
|
134
|
if (sqlca != null) {
|
135
|
try {
|
136
|
sqlca.close();
|
137
|
} catch (SQLException e) {
|
138
|
// TODO Auto-generated catch block
|
139
|
e.printStackTrace();
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
|
144
|
// now delete the actual files that were removed from the db
|
145
|
if (success) {
|
146
|
for (File emptyFile: processedFiles) {
|
147
|
log.info("Deleting empty replicated data file from filesystem: " + emptyFile.getAbsolutePath());
|
148
|
emptyFile.delete();
|
149
|
}
|
150
|
}
|
151
|
|
152
|
return success;
|
153
|
}
|
154
|
|
155
|
public static void main(String [] ags){
|
156
|
|
157
|
try {
|
158
|
// set up the properties based on the test/deployed configuration of the workspace
|
159
|
SortedProperties testProperties =
|
160
|
new SortedProperties("test/test.properties");
|
161
|
testProperties.load();
|
162
|
String metacatContextDir = testProperties.getProperty("metacat.contextDir");
|
163
|
PropertyService.getInstance(metacatContextDir + "/WEB-INF");
|
164
|
// now run it
|
165
|
UpgradeEmptyReplicatedDataFile upgrader = new UpgradeEmptyReplicatedDataFile();
|
166
|
upgrader.upgrade();
|
167
|
|
168
|
} catch (Exception ex) {
|
169
|
System.out.println("Exception:" + ex.getMessage());
|
170
|
ex.printStackTrace();
|
171
|
}
|
172
|
}
|
173
|
}
|