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.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.utilities.PropertyNotFoundException;
53
import edu.ucsb.nceas.utilities.SortedProperties;
54

    
55
public class GenerateSystemMetadata implements UpgradeUtilityInterface {
56

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

    
65
        try {
66
        	//get the list of ids with no SM
67
            //List<String> idList = IdentifierManager.getInstance().getLocalIdsWithNoSystemMetadata(true);
68
            List<String> idList = DBUtil.getAllDocidsByType(null, true);
69
            Collections.sort(idList);
70

    
71
            // generate based on this list
72
			generateSystemMetadata(idList, includeOre, downloadData);
73
		} catch (Exception e) {
74
			String msg = "Problem generating missing system metadata: " + e.getMessage();
75
			log.error(msg, e);
76
			success = false;
77
			throw new AdminException(msg);
78
		}
79
    	return success;
80
    }
81
    
82
    /**
83
     * Generate SystemMetadata for any object in the object store that does
84
     * not already have it.  SystemMetadata documents themselves, are, of course,
85
     * exempt.  This is a utility method for migration of existing object 
86
     * stores to DataONE where SystemMetadata is required for all objects.
87
     * @param idList
88
     * @param includeOre
89
     * @param downloadData
90
     * @throws ServiceFailure
91
     * @throws McdbDocNotFoundException
92
     * @throws PropertyNotFoundException
93
     * @throws InvalidToken
94
     * @throws NotAuthorized
95
     * @throws NotFound
96
     * @throws NotImplemented
97
     * @throws InvalidRequest
98
     * @throws NoSuchAlgorithmException
99
     * @throws AccessionNumberException
100
     * @throws SQLException
101
     */
102
    public void generateSystemMetadata(List<String> idList, boolean includeOre, boolean downloadData) 
103
    throws ServiceFailure, McdbDocNotFoundException, PropertyNotFoundException, InvalidToken, NotAuthorized, 
104
    NotFound, NotImplemented, InvalidRequest, NoSuchAlgorithmException, AccessionNumberException, SQLException 
105
    {
106
        
107
        for (String localId : idList) { 
108
            //for each id, add a system metadata doc
109
        	try {
110
        		log.debug("generating system metadata for " + localId);
111
        		generateSystemMetadata(localId, includeOre, downloadData);
112
        	} catch (Exception e) {
113
				log.error("Error generating system metadata for: " + localId, e);
114
			}
115
        }
116
        log.info("done generating system metadata");
117
    }
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
     * @param localId
125
     * @param includeOre
126
     * @param downloadData
127
     * @throws ServiceFailure
128
     * @throws McdbDocNotFoundException
129
     * @throws PropertyNotFoundException
130
     * @throws InvalidToken
131
     * @throws NotAuthorized
132
     * @throws NotFound
133
     * @throws NotImplemented
134
     * @throws InvalidRequest
135
     * @throws NoSuchAlgorithmException
136
     * @throws AccessionNumberException
137
     * @throws SQLException
138
     * @throws InvalidSystemMetadata
139
     */
140
    protected void generateSystemMetadata(String localId, boolean includeOre, boolean downloadData) 
141
    throws ServiceFailure, McdbDocNotFoundException, PropertyNotFoundException, InvalidToken, NotAuthorized,
142
    NotFound, NotImplemented, InvalidRequest, NoSuchAlgorithmException, AccessionNumberException, SQLException, InvalidSystemMetadata 
143
    {
144
    	log.debug("generateSystemMetadata() called.");
145
    	log.debug("Creating SystemMetadata for localId " + localId);
146
        SystemMetadata sm = null;
147

    
148
        //generate required system metadata fields from the document
149
        try {
150
        	sm = SystemMetadataFactory.createSystemMetadata(localId, includeOre, downloadData);
151
        } catch (Exception e1) {
152
        	e1.printStackTrace();
153
        	ServiceFailure sf = new ServiceFailure("00","Exception in generateSystemMetadata: " +
154
        			e1.getMessage());
155
        	sf.setStackTrace(e1.getStackTrace());
156
        	throw sf;
157
        }
158
        
159
        //insert the systemmetadata object or just update it as needed
160
        boolean exists = IdentifierManager.getInstance().systemMetadataExists(sm.getIdentifier().getValue());
161
        if (!exists) {
162
        	IdentifierManager.getInstance().createSystemMetadata(sm);
163
            log.info("Generated SystemMetadata for " + localId);
164
        } else {
165
        	IdentifierManager.getInstance().updateSystemMetadata(sm);
166
            log.info("Updated SystemMetadata for " + localId);
167
        }
168
    }
169
    
170
    
171
    public static void main(String [] ags){
172

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