Project

General

Profile

1
package edu.ucsb.nceas.metacat.admin.upgrade;
2
/**
3
 *  '$RCSfile$'
4
 *  Copyright: 2012 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matthew Jones
7
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
import java.io.IOException;
27
import java.sql.Connection;
28
import java.sql.Driver;
29
import java.sql.DriverManager;
30
import java.sql.PreparedStatement;
31
import java.sql.ResultSet;
32
import java.sql.SQLException;
33
import java.sql.Statement;
34

    
35
import org.apache.commons.logging.Log;
36
import org.apache.commons.logging.LogFactory;
37

    
38
import edu.ucsb.nceas.metacat.admin.AdminException;
39
import edu.ucsb.nceas.metacat.properties.PropertyService;
40
import edu.ucsb.nceas.metacat.shared.ServiceException;
41
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
42
import edu.ucsb.nceas.utilities.SortedProperties;
43

    
44
/**
45
 * Optionally upgrade all of the existing identifiers in the tables to be official
46
 * DOIs that are assigned by a DOI registration authority.  This class first checks
47
 * if GUIDs should be converted to DOIs, and if so, updates all relevant
48
 * tables and resources in Metacat, and then registers each of the newly minted
49
 * identifiers with the EZID service. You must have an EZID account for this to
50
 * work properly.
51
 * 
52
 * Whether or not identifiers should be converted is determined by several metacat.properties.
53
 * First, only upgrade if 'guid.assignGUIDs' is true.  Next, for each server registered in 
54
 * the xml_replication table, only convert the documents belonging to that server if
55
 * there is a corresponding property that gives the 'shoulder' or 'prefix'' to be used to
56
 * convert existing IDs to GUIDs.  For example, if servers 1 & 2 are registered in 
57
 * xml_replication, then only convert the IDs for server 1 to GUIDs if the
58
 * property 'guid.ezid.doishoulder.1' defines a valid doi shoulder.
59
 * 
60
 * NOTE: Metacat v2.0.5 has changed the name of the systemmetadata* tables to accommodate Oracle
61
 * length restrictions. The table names have been updated in this utility to enable
62
 * users to run this utility on v2.0.5+. You can only run this version of the utility on 
63
 * Metacat deployments that have been upgraded to 2.0.5+
64
 * 
65
 * @author jones
66
 */
67
public class GenerateGlobalIdentifiers implements UpgradeUtilityInterface {
68

    
69
	private static Log log = LogFactory.getLog(GenerateGlobalIdentifiers.class);
70
    private String driver = null;
71
    private String url = null;
72
    private String user = null;
73
    private String password = null;
74
//    private String eziduser = null;
75
//    private String ezidPassword = null;
76

    
77
    /**
78
     * The main upgrade() procedure, which first upgrades identifiers in the database,
79
     * then registers these with EZID.
80
     */
81
    public boolean upgrade() throws AdminException {
82
        boolean success = false;
83
        log.debug("Upgrading identifiers to DOIs");
84
        
85
        // Check if assignDOIs is true, and if EZID is configured
86
        boolean shouldUpgrade = false;
87
        try {
88
            shouldUpgrade = new Boolean(PropertyService.getProperty("guid.assignGUIDs")).booleanValue();
89
            if (shouldUpgrade) {
90
                log.debug("Upgrading identifiers to DOIs");
91

    
92
                success = updateIdentifierReferences();                
93
            }
94

    
95
        } catch (PropertyNotFoundException e) {
96
            // When the guid properties are not found, do nothing
97
            shouldUpgrade = false;
98
            success = true;
99
        }
100
        		
101
    	return success;
102
    }
103
    
104
    /**
105
     * Loop through the servers registered in this instance of Metacat, and for
106
     * each server determine if we should convert to GUIDs for the documents associated
107
     * with that server.  If so, convert the identifier table to use a GUID for all
108
     * documents for that server found in xml_documents and xml_replication.
109
     * @return true if all conversions succeed
110
     * @throws AdminException
111
     */
112
    private boolean updateIdentifierReferences() throws AdminException {
113

    
114
        log.debug("Updating identifier references...");
115

    
116
        try {
117
            driver = PropertyService.getProperty("database.driver");
118
            url = PropertyService.getProperty("database.connectionURI");
119
            user = PropertyService.getProperty("database.user");
120
            password = PropertyService.getProperty("database.password");
121
//            eziduser = PropertyService.getProperty("guid.ezid.username");
122
//            ezidPassword = PropertyService.getProperty("guid.ezid.password");
123
            
124
        } catch (PropertyNotFoundException e) {
125
            String msg = "PropertyNotFound while trying to convert identifiers: " + e.getMessage();
126
            log.error(msg, e);
127
            throw new AdminException(msg);
128
        }
129

    
130
        // Create a connection, and start a transaction for which FK constraint checking is deferred
131
        Connection con = openDatabaseConnection();
132
        deferConstraints(con);
133
        // Copy the identifier.guid column for later use
134
        createDuplicateGUIDColumn(con);
135

    
136
        // Look up the maximum server number from the database
137
        int maxServerId = lookupMaxServers(con);
138
        
139
        // Loop across each replica server, checking if it is to have DOIs assigned
140
        // This is determined by the presence of a shoulder in the configuration file for
141
        // that server number
142
        for (int server = 1; server <= maxServerId; server++) {
143
            
144
            // Get the server's shoulder, or skip if not found
145
            try {
146
                String shoulder = PropertyService.getProperty("guid.ezid.doishoulder." + server);
147
                log.debug("Processing shoulder for server " + server + ": " + shoulder);
148

    
149
                // Update identifiers table (but only for local documents, not replicated documents)
150
                convertIdentifierTable(con, server, shoulder);
151
                
152
            } catch (PropertyNotFoundException e) {
153
                // The shoulder was not found for this server, so we just skip it
154
                log.debug("No shoulder found: " + e.getMessage());
155
            }
156
        }
157
        
158
        // Convert other tables once, after the identifier table conversion has established the new
159
        // global identifiers to be used
160
        // This incudes: xml_access table, including accessfileid field, systemmetadata, 
161
        // smreplicationpolicy,and smreplicationstatus tables
162
        convertIdentifierField(con, "xml_access", "guid");
163
        convertIdentifierField(con, "xml_access", "accessfileid");
164
        convertIdentifierField(con, "systemmetadata", "guid");
165
        convertIdentifierField(con, "smreplicationpolicy", "guid");
166
        convertIdentifierField(con, "smreplicationstatus", "guid");
167

    
168
        // Now clean up the temporary column we created, but note this makes it much
169
        // harder to revert our changes
170
        dropDuplicateGUIDColumn(con);
171
        
172
        commitAndCloseConnection(con);
173

    
174
        /*      
175
        // The following SQL commands will revert the changes made by this script as long as the identifier.old_guid column is intact
176
        start transaction;
177
        update smreplicationstatus set guid = id.old_guid FROM identifier id WHERE smreplicationstatus.guid = id.guid;
178
        update smreplicationpolicy set guid = id.old_guid FROM identifier id WHERE smreplicationpolicy.guid = id.guid;
179
        update systemmetadata set guid = id.old_guid FROM identifier id WHERE systemmetadata.guid = id.guid;
180
        update xml_access set accessfileid = id.old_guid FROM identifier id WHERE xml_access.accessfileid = id.guid;
181
        update xml_access set guid = id.old_guid FROM identifier id WHERE xml_access.guid = id.guid;
182
        update identifier set guid = old_guid;
183
        alter table identifier drop column old_guid;
184
        commit;
185
        */
186
        
187
        return true;
188
    }
189

    
190
    /**
191
     * Open a JDBC connection that will be used for all of the subsequent methods that
192
     * access or modify the database.  All changes are done in one transaction, so need
193
     * to be done using a single connection.
194
     * @return the Connection created
195
     * @throws AdminException
196
     */
197
    private Connection openDatabaseConnection() throws AdminException {
198
        Connection con = null;
199
            try {
200
                Driver d = (Driver) Class.forName(driver).newInstance();
201
                DriverManager.registerDriver(d);
202
                con = DriverManager.getConnection(url, user, password);
203
            } catch (InstantiationException e) {
204
                String msg = "InstantiationException updating creating DB connection: " + e.getMessage();
205
                log.error(msg, e);
206
                throw new AdminException(msg);
207
            } catch (IllegalAccessException e) {
208
                String msg = "IllegalAccessException updating creating DB connection: " + e.getMessage();
209
                log.error(msg, e);
210
                throw new AdminException(msg);
211
            } catch (ClassNotFoundException e) {
212
                String msg = "ClassNotFoundException updating creating DB connection: " + e.getMessage();
213
                log.error(msg, e);
214
                throw new AdminException(msg);
215
            } catch (SQLException e) {
216
                String msg = "SQLException updating creating DB connection: " + e.getMessage();
217
                log.error(msg, e);
218
                throw new AdminException(msg);
219
            }
220
            
221
            return con;
222
    }
223

    
224
    /**
225
     * Start a transaction on the given connection, and defer all constraints so
226
     * that changes to foreign keys will not be checked until after all of the 
227
     * identifiers have been updated in all tables.  Without this, we would get
228
     * FK constraint violations as we changed the tables.
229
     * @param con the connection to the database
230
     * @throws AdminException
231
     */
232
    private void deferConstraints(Connection con) throws AdminException {
233
                
234
        try {
235
            con.setAutoCommit(false);
236
            Statement sqlStatement = con.createStatement();
237
            boolean hasResults = sqlStatement.execute("SET CONSTRAINTS ALL DEFERRED;");
238
            log.debug("Constraints Deferred.");
239
        } catch (SQLException e) {
240
            String msg = "SQLException while disabling constraints: " + e.getMessage();
241
            log.error(msg, e);
242
            throw new AdminException(msg);
243
        }
244
    }
245

    
246
    /**
247
     * Temporarily save the current identifier.guid in a backup column (old_guid), to be used
248
     * for updating additional tables. 
249
     * @param con the connection to the database with an open transaction
250
     * @throws AdminException
251
     */
252
    private void createDuplicateGUIDColumn(Connection con) throws AdminException {
253
        try {
254
            Statement sqlStatement = con.createStatement();
255
            boolean hasResults = sqlStatement.execute("ALTER TABLE identifier ADD COLUMN old_guid TEXT;");
256
            log.debug("old_guid column added.");
257
            sqlStatement = con.createStatement();
258
            hasResults = sqlStatement.execute("UPDATE identifier SET old_guid = guid;");
259
            log.debug("Copied original identifiers to old_guid column.");
260
        } catch (SQLException e) {
261
            String msg = "SQLException while duplicating GUID column: " + e.getMessage();
262
            log.error(msg, e);
263
            throw new AdminException(msg);
264
        }        
265
    }
266
    
267
    /**
268
     * Drop the temporary GUID backup column (old_guid) once upgrading identifiers
269
     * is complete. 
270
     * @param con the connection to the database with an open transaction
271
     * @throws AdminException
272
     */
273
    private void dropDuplicateGUIDColumn(Connection con) throws AdminException {
274
        try {
275
            Statement sqlStatement = con.createStatement();
276
            boolean hasResults = sqlStatement.execute("ALTER TABLE identifier DROP COLUMN old_guid;");
277
            log.debug("old_guid column dropped.");
278
        } catch (SQLException e) {
279
            String msg = "SQLException dropping GUID column: " + e.getMessage();
280
            log.error(msg, e);
281
            throw new AdminException(msg);
282
        }        
283
    }
284
    
285
    /**
286
     * Query the database to determine which servers are registered and can be
287
     * inspected for potential identifier upgrades.
288
     * @param con the connection to the database with an open transaction
289
     * @throws AdminException
290
     */
291
    private int lookupMaxServers(Connection con) throws AdminException {
292
        int maxServers = 0;
293
        try {
294
            Statement sqlStatement = con.createStatement();
295
            boolean hasResults = sqlStatement.execute("SELECT max(serverid) AS maxserverid FROM xml_replication;");
296
            if (hasResults) {
297
                ResultSet rs = sqlStatement.getResultSet();
298
                if (rs.next()) {
299
                    maxServers = rs.getInt(1);
300
                    log.debug("MaxServerID: " + maxServers);
301
                } else {
302
                    String msg = "Could not determine max serverid; database query cursor had zero rows.";
303
                    log.error(msg);
304
                    throw new AdminException(msg);
305
                }
306
            } else {
307
                String msg = "Could not determine max serverid; database query failed to return results.";
308
                log.error(msg);
309
                throw new AdminException(msg);
310
            }
311
            
312
        } catch (SQLException e) {
313
            String msg = "SQLException while looking up serverId: " + e.getMessage();
314
            log.error(msg, e);
315
            throw new AdminException(msg);
316
        }        
317
        return maxServers;
318
    }
319

    
320
    /**
321
     * Convert identifiers to DOI GUIDs in the identifier table for any documents found in
322
     * xml_documents or xml_revisions that reside on a server for which a DOI shoulder prefix
323
     * has been assigned.
324
     * 
325
     * @param con the connection to the database with an open transaction
326
     * @param server the id of the server whose documents are to be converted
327
     * @param shoulder the DOI shoulder to be used for converting
328
     * @return true if conversion is successful
329
     * @throws AdminException
330
     */
331
    private boolean convertIdentifierTable(Connection con, int server, String shoulder) throws AdminException {
332
        PreparedStatement pstmt = null;
333
        try {
334
            
335
            String sql_1 =
336
                "UPDATE identifier " + 
337
                "SET    guid = p.doi " + 
338
                "FROM (" +
339
                       "SELECT  guid, ? || guid as doi " + 
340
                       "FROM    identifier id2, ";
341
            String sql_2 =
342
                " xd " +
343
                       "WHERE   id2.docid = xd.docid AND id2.rev = xd.rev " +   
344
                       "AND xd.server_location = ? " +
345
                     ") p " +
346
                "WHERE identifier.guid = p.guid;";         
347

    
348
            // Convert identifiers found in xml_documents
349
            pstmt = con.prepareStatement(sql_1 + "xml_documents" + sql_2);
350
            pstmt.setString(1, shoulder);
351
            pstmt.setInt(2, server);
352
            pstmt.execute();
353
            
354
            // Convert identifiers found in xml_revisions
355
            pstmt = con.prepareStatement(sql_1 + "xml_revisions" + sql_2);
356
            pstmt.setString(1, shoulder);
357
            pstmt.setInt(2, server);
358
            pstmt.execute();
359
            
360
            log.debug("Finished shoulder for server " + server + ": " + shoulder);
361

    
362
        } catch (SQLException e) {
363
            String msg = "SQLException updating identifier table: " + e.getMessage();
364
            log.error(msg, e);
365
            throw new AdminException(msg);
366
        }
367

    
368
        return true;
369
    }
370

    
371
    /**
372
     * Convert the given field in the given table to use the new identifier.guid that
373
     * has been assigned.
374
     * 
375
     * @param con the connection to the database with an open transaction
376
     * @param table the name of the table with identifiers to be converted
377
     * @param field the name of the field containing identifiers to be converted
378
     * @return true if conversion was successful
379
     * @throws AdminException
380
     */
381
    private boolean convertIdentifierField(Connection con, String table, String field) throws AdminException {
382
        PreparedStatement pstmt = null;
383
        try {
384

    
385
            String sql_1 =
386
                "UPDATE " + table + " " +
387
                "SET " + field + " = id.guid " + 
388
                "FROM identifier id " +
389
                "WHERE " + table + "."+ field + " = id.old_guid;";
390
            
391
            // Convert identifiers found in the given table and field
392
            pstmt = con.prepareStatement(sql_1);
393
            pstmt.execute();
394
                        
395
            log.debug("Finished converting table " + table + " (" + field +")" );
396

    
397
        } catch (SQLException e) {
398
            String msg = "SQLException while converting identifier field: " + e.getMessage();
399
            log.error(msg, e);
400
            throw new AdminException(msg);
401
        }
402

    
403
        return true;
404
    }
405

    
406
    /**
407
     * Commit the current transaction on the connection, and close the connection.
408
     * @param con the connection to the database with an open transaction
409
     * @return true if the connection is successfully closed
410
     * @throws AdminException
411
     */
412
    private boolean commitAndCloseConnection(Connection con) throws AdminException {
413
        try {
414
                        
415
            log.debug("Committing and closing connection.");
416
            con.commit();
417
            con.close();
418
        } catch (SQLException e) {
419
            String msg = "SQLException while commiting and closing connection: " + e.getMessage();
420
            log.error(msg, e);
421
            throw new AdminException(msg);
422
        } 
423
        
424
        return true;
425
    }
426

    
427
    /**
428
     * Main method, solely for testing.  Not used in ordinary operation.
429
     */
430
    public static void main(String [] ags){
431

    
432
        try {
433
        	// set up the properties based on the test/deployed configuration of the workspace
434
        	SortedProperties testProperties = 
435
				new SortedProperties("test/test.properties");
436
			testProperties.load();
437
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
438
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
439
			// now run it
440
            GenerateGlobalIdentifiers upgrader = new GenerateGlobalIdentifiers();
441
	        upgrader.upgrade();
442
        } catch (IOException e) {
443
            e.printStackTrace();
444
        } catch (PropertyNotFoundException e) {
445
            e.printStackTrace();
446
        } catch (ServiceException e) {
447
            e.printStackTrace();
448
        } catch (AdminException e) {
449
            e.printStackTrace();
450
        }
451
    }
452
}
(1-1/7)