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
import java.util.Timer;
33

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

    
45
import edu.ucsb.nceas.metacat.AccessionNumberException;
46
import edu.ucsb.nceas.metacat.IdentifierManager;
47
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
48
import edu.ucsb.nceas.metacat.MetacatHandler;
49
import edu.ucsb.nceas.metacat.admin.AdminException;
50
import edu.ucsb.nceas.metacat.properties.PropertyService;
51
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
52
import edu.ucsb.nceas.utilities.SortedProperties;
53

    
54
public class GenerateSystemMetadata implements UpgradeUtilityInterface {
55

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

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

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

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