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.InvalidSystemMetadata;
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.v1.SystemMetadata;
43

    
44
import edu.emory.mathcs.backport.java.util.Collections;
45
import edu.ucsb.nceas.metacat.AccessionNumberException;
46
import edu.ucsb.nceas.metacat.DBUtil;
47
import edu.ucsb.nceas.metacat.DocumentImpl;
48
import edu.ucsb.nceas.metacat.IdentifierManager;
49
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
50
import edu.ucsb.nceas.metacat.admin.AdminException;
51
import edu.ucsb.nceas.metacat.dataone.SystemMetadataFactory;
52
import edu.ucsb.nceas.metacat.properties.PropertyService;
53
import edu.ucsb.nceas.metacat.util.DocumentUtil;
54
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
55
import edu.ucsb.nceas.utilities.SortedProperties;
56

    
57
public class GenerateSystemMetadata implements UpgradeUtilityInterface {
58

    
59
	private static Log log = LogFactory.getLog(GenerateSystemMetadata.class);
60
	
61
    public boolean upgrade() throws AdminException {
62
        boolean success = true;
63
        // include ORE?
64
        boolean includeOre = true;
65
        boolean downloadData = true;
66

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

    
152
        //generate required system metadata fields from the document
153
        try {
154
        	sm = SystemMetadataFactory.createSystemMetadata(localId, includeOre, downloadData);
155
        } catch (Exception e1) {
156
        	e1.printStackTrace();
157
        	ServiceFailure sf = new ServiceFailure("00","Exception in generateMissingSystemMetadata: " +
158
        			e1.getMessage());
159
        	sf.setStackTrace(e1.getStackTrace());
160
        	throw sf;
161
        }
162
        
163
        //insert the systemmetadata object or just update it as needed
164
        boolean exists = IdentifierManager.getInstance().systemMetadataExists(sm.getIdentifier().getValue());
165
        if (!exists) {
166
        	IdentifierManager.getInstance().createSystemMetadata(sm);        	
167
        }
168
        IdentifierManager.getInstance().updateSystemMetadata(sm);
169

    
170
        log.info("generateMissingSystemMetadata(token, localId)");
171
    }
172
    
173
    
174
    public static void main(String [] ags){
175

    
176
        try {
177
        	// set up the properties based on the test/deployed configuration of the workspace
178
        	SortedProperties testProperties = 
179
				new SortedProperties("test/test.properties");
180
			testProperties.load();
181
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
182
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
183
			// now run it
184
            GenerateSystemMetadata upgrader = new GenerateSystemMetadata();
185
	        upgrader.upgrade();
186
	        
187
        } catch (Exception ex) {
188
            System.out.println("Exception:" + ex.getMessage());
189
            ex.printStackTrace();
190
        }
191
    }
192
}
(1-1/6)