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: 2012-01-03 10:56:38 -0800 (Tue, 03 Jan 2012) $'
11
 * '$Revision: 6826 $'
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

    
37
import org.apache.commons.io.filefilter.EmptyFileFilter;
38
import org.apache.commons.logging.Log;
39
import org.apache.commons.logging.LogFactory;
40

    
41
import edu.ucsb.nceas.metacat.admin.AdminException;
42
import edu.ucsb.nceas.metacat.properties.PropertyService;
43
import edu.ucsb.nceas.metacat.util.DocumentUtil;
44
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
45
import edu.ucsb.nceas.utilities.SortedProperties;
46

    
47
public class UpgradeEmptyReplicatedDataFile implements UpgradeUtilityInterface {
48

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

    
56
    public boolean upgrade() throws AdminException {
57
        
58
    	boolean success = true;    		
59
    	
60
        Connection sqlca = null;
61
        PreparedStatement pstmt = null;
62
        
63
        // look up the empty files from the file system
64
        File dataDir = null;
65
		try {
66
			dataDir = new File(PropertyService.getProperty("application.datafilepath"));
67
		} catch (PropertyNotFoundException e) {
68
			e.printStackTrace();
69
			return false;
70
		}
71
		File[] emptyFiles = dataDir.listFiles((FileFilter)EmptyFileFilter.EMPTY);
72
		
73
        try {
74
        	
75
        	// get the properties
76
    		driver = PropertyService.getProperty("database.driver");
77
    	    url = PropertyService.getProperty("database.connectionURI");
78
    	    user = PropertyService.getProperty("database.user");
79
    	    password = PropertyService.getProperty("database.password");
80
    	    
81
	        // Create a JDBC connection to the database    
82
	        Driver d = (Driver) Class.forName(driver).newInstance();
83
	        DriverManager.registerDriver(d);
84
	        sqlca = DriverManager.getConnection(url, user, password);
85
	        sqlca.setAutoCommit(false);        
86
	        
87
			for (File emptyFile: emptyFiles) {
88
				
89
				// delete each empty file from the tables 
90
				String emptyDocid = emptyFile.getName();
91
				String docid = DocumentUtil.getDocIdFromString(emptyDocid);
92
				String rev = DocumentUtil.getRevisionStringFromString(emptyDocid);
93
				
94
		        // xml_documents
95
				pstmt = sqlca.prepareStatement("DELETE FROM xml_documents WHERE docid = ? and server_location != '0'");
96
				pstmt.setString(1, docid);
97
				pstmt.execute();
98
				// xml_revisions
99
				pstmt = sqlca.prepareStatement("DELETE FROM xml_revisions WHERE docid = ? and server_location != '0'");
100
				pstmt.setString(1, docid);
101
				pstmt.execute();
102
								
103
			}
104
			
105
			// all or nothing
106
			sqlca.commit();
107
			
108
        } catch (Exception e) {
109
        	try {
110
        		// rollback if there was even a single error
111
				sqlca.rollback();
112
			} catch (SQLException e1) {
113
				// TODO Auto-generated catch block
114
				e1.printStackTrace();
115
			}
116
        	success = false;
117
		} finally {
118
			// clean up
119
			if (sqlca != null) {
120
				try {
121
					sqlca.close();
122
				} catch (SQLException e) {
123
					// TODO Auto-generated catch block
124
					e.printStackTrace();
125
				}
126
			}
127
		}
128
		
129
		// now delete the actual files
130
		for (File emptyFile: emptyFiles) {
131
			emptyFile.delete();
132
		}
133
            	
134
    	return success;
135
    }
136

    
137
    public static void main(String [] ags){
138

    
139
        try {
140
        	// set up the properties based on the test/deployed configuration of the workspace
141
        	SortedProperties testProperties = 
142
				new SortedProperties("test/test.properties");
143
			testProperties.load();
144
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
145
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
146
			// now run it
147
            UpgradeEmptyReplicatedDataFile upgrader = new UpgradeEmptyReplicatedDataFile();
148
	        upgrader.upgrade();
149
            
150
        } catch (Exception ex) {
151
            System.out.println("Exception:" + ex.getMessage());
152
            ex.printStackTrace();
153
        }
154
    }
155
}
(4-4/6)