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

    
56
public class GenerateSystemMetadata implements UpgradeUtilityInterface {
57

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

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

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

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

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