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: 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.security.NoSuchAlgorithmException;
30
import java.sql.SQLException;
31
import java.util.List;
32

    
33
import org.apache.commons.logging.Log;
34
import org.apache.commons.logging.LogFactory;
35
import org.dataone.service.exceptions.InvalidRequest;
36
import org.dataone.service.exceptions.InvalidToken;
37
import org.dataone.service.exceptions.NotAuthorized;
38
import org.dataone.service.exceptions.NotFound;
39
import org.dataone.service.exceptions.NotImplemented;
40
import org.dataone.service.exceptions.ServiceFailure;
41
import org.dataone.service.types.v1.SystemMetadata;
42

    
43
import edu.ucsb.nceas.metacat.AccessionNumberException;
44
import edu.ucsb.nceas.metacat.IdentifierManager;
45
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
46
import edu.ucsb.nceas.metacat.admin.AdminException;
47
import edu.ucsb.nceas.metacat.dataone.SystemMetadataFactory;
48
import edu.ucsb.nceas.metacat.properties.PropertyService;
49
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
50
import edu.ucsb.nceas.utilities.SortedProperties;
51

    
52
public class GenerateSystemMetadata implements UpgradeUtilityInterface {
53

    
54
	private static Log log = LogFactory.getLog(GenerateSystemMetadata.class);
55
	
56
    public boolean upgrade() throws AdminException {
57
        boolean success = true;
58
        // include ORE?
59
        boolean includeOre = true;
60
        try {
61
			generateMissingSystemMetadata(includeOre);
62
		} catch (Exception e) {
63
			String msg = "Problem generating missing system metadata: " + e.getMessage();
64
			log.error(msg, e);
65
			success = false;
66
			throw new AdminException(msg);
67
		}
68
    	return success;
69
    }
70
    
71
    /**
72
     * Generate SystemMetadata for any object in the object store that does
73
     * not already have it.  SystemMetadata documents themselves, are, of course,
74
     * exempt.  This is a utility method for migration of existing object 
75
     * stores to DataONE where SystemMetadata is required for all objects.  See 
76
     * https://trac.dataone.org/ticket/591
77
     * @param includeOre 
78
     * 
79
     * @param token an authtoken with appropriate permissions to read all 
80
     * documents in the object store.  To work correctly, this should probably
81
     * be an adminstrative credential.
82
     * @throws SQLException 
83
     * @throws AccessionNumberException 
84
     * @throws NoSuchAlgorithmException 
85
     * @throws InvalidRequest 
86
     * @throws NotImplemented 
87
     * @throws NotFound 
88
     * @throws NotAuthorized 
89
     * @throws InvalidToken 
90
     * @throws PropertyNotFoundException 
91
     * @throws McdbDocNotFoundException 
92
     * @throws ServiceFailure 
93
     */
94
    public void generateMissingSystemMetadata(boolean includeOre) 
95
    throws ServiceFailure, McdbDocNotFoundException, PropertyNotFoundException, InvalidToken, NotAuthorized, 
96
    NotFound, NotImplemented, InvalidRequest, NoSuchAlgorithmException, AccessionNumberException, SQLException 
97
    {
98
        //get the list of ids with no SM
99
        List<String> idList = IdentifierManager.getInstance().getLocalIdsWithNoSystemMetadata(true);
100
        for (String localId : idList) { 
101
            //for each id, add a system metadata doc
102
        	try {
103
        		generateMissingSystemMetadata(localId, includeOre);
104
        	} catch (Exception e) {
105
				log.error("Error generating system metadata for: " + localId, e);
106
			}
107
        }
108
        log.info("generateMissingSystemMetadata(token)");
109
    }
110
    
111
    /**
112
     * Generate SystemMetadata for a particular object with identifier localId.
113
     * This is a utility method for migration of existing objects 
114
     * to DataONE where SystemMetadata is required for all objects.
115
     * 
116
     * @param token an authtoken with appropriate permissions to read all 
117
     *        documents in the object store.  To work correctly, this should
118
     *        be an adminstrative credential.
119
     * @param localId the identifier of the object to be processed
120
     * @param includeOre 
121
     * @throws ServiceFailure 
122
     * @throws SQLException 
123
     * @throws AccessionNumberException 
124
     * @throws NoSuchAlgorithmException 
125
     * @throws InvalidRequest 
126
     * @throws NotImplemented 
127
     * @throws NotFound 
128
     * @throws NotAuthorized 
129
     * @throws InvalidToken 
130
     * @throws PropertyNotFoundException 
131
     * @throws McdbDocNotFoundException 
132
     */
133
    public void generateMissingSystemMetadata(String localId, boolean includeOre) 
134
    throws ServiceFailure, McdbDocNotFoundException, PropertyNotFoundException, InvalidToken, NotAuthorized,
135
    NotFound, NotImplemented, InvalidRequest, NoSuchAlgorithmException, AccessionNumberException, SQLException 
136
    {
137
    	log.debug("generateMissingSystemMetadata() called.");
138
    	log.debug("Creating SystemMetadata for localId " + localId);
139
        SystemMetadata sm = null;
140

    
141
        //generate required system metadata fields from the document
142
        try {
143
        	sm = SystemMetadataFactory.createSystemMetadata(localId, includeOre);
144
        } catch (Exception e1) {
145
        	e1.printStackTrace();
146
        	ServiceFailure sf = new ServiceFailure("00","Exception in generateMissingSystemMetadata: " +
147
        			e1.getMessage());
148
        	sf.setStackTrace(e1.getStackTrace());
149
        	throw sf;
150
        }
151
        
152
        //insert the systemmetadata object or just update it
153
        boolean exists = IdentifierManager.getInstance().identifierExists(sm.getIdentifier().getValue());
154
        if (!exists) {
155
        	IdentifierManager.getInstance().createSystemMetadata(sm);
156
        }
157
        IdentifierManager.getInstance().updateSystemMetadata(sm);
158

    
159
        log.info("generateMissingSystemMetadata(token, localId)");
160
    }
161
    
162
    
163
    public static void main(String [] ags){
164

    
165
        try {
166
        	// set up the properties based on the test/deployed configuration of the workspace
167
        	SortedProperties testProperties = 
168
				new SortedProperties("test/test.properties");
169
			testProperties.load();
170
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
171
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
172
			// now run it
173
            GenerateSystemMetadata upgrader = new GenerateSystemMetadata();
174
	        upgrader.upgrade();
175
	        
176
        } catch (Exception ex) {
177
            System.out.println("Exception:" + ex.getMessage());
178
            ex.printStackTrace();
179
        }
180
    }
181
}
(1-1/5)