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.io.ByteArrayInputStream;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.security.NoSuchAlgorithmException;
33
import java.sql.SQLException;
34
import java.util.Calendar;
35
import java.util.Date;
36
import java.util.Hashtable;
37
import java.util.List;
38
import java.util.TimeZone;
39
import java.util.Timer;
40

    
41
import javax.xml.parsers.ParserConfigurationException;
42
import javax.xml.xpath.XPathExpressionException;
43

    
44
import org.apache.commons.logging.Log;
45
import org.apache.commons.logging.LogFactory;
46
import org.dataone.eml.DataoneEMLParser;
47
import org.dataone.eml.EMLDocument;
48
import org.dataone.eml.EMLDocument.DistributionMetadata;
49
import org.dataone.service.exceptions.InvalidRequest;
50
import org.dataone.service.exceptions.InvalidToken;
51
import org.dataone.service.exceptions.NotAuthorized;
52
import org.dataone.service.exceptions.NotFound;
53
import org.dataone.service.exceptions.NotImplemented;
54
import org.dataone.service.exceptions.ServiceFailure;
55
import org.dataone.service.types.AuthToken;
56
import org.dataone.service.types.Checksum;
57
import org.dataone.service.types.ChecksumAlgorithm;
58
import org.dataone.service.types.Identifier;
59
import org.dataone.service.types.NodeReference;
60
import org.dataone.service.types.ObjectFormat;
61
import org.dataone.service.types.Subject;
62
import org.dataone.service.types.SystemMetadata;
63
import org.xml.sax.SAXException;
64

    
65
import edu.ucsb.nceas.metacat.AccessionNumber;
66
import edu.ucsb.nceas.metacat.AccessionNumberException;
67
import edu.ucsb.nceas.metacat.DocumentImpl;
68
import edu.ucsb.nceas.metacat.IdentifierManager;
69
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
70
import edu.ucsb.nceas.metacat.McdbException;
71
import edu.ucsb.nceas.metacat.MetacatHandler;
72
import edu.ucsb.nceas.metacat.admin.AdminException;
73
import edu.ucsb.nceas.metacat.dataone.CrudService;
74
import edu.ucsb.nceas.metacat.properties.PropertyService;
75
import edu.ucsb.nceas.metacat.util.SessionData;
76
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
77
import edu.ucsb.nceas.utilities.SortedProperties;
78

    
79
public class GenerateSystemMetadata implements UpgradeUtilityInterface {
80

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

    
168
        //generate required system metadata fields from the document
169
        try {
170
        	sm = handler.createSystemMetadata(localId, null, null);
171
        } catch (Exception e1) {
172
        	e1.printStackTrace();
173
        	ServiceFailure sf = new ServiceFailure("00","Exception in generateMissingSystemMetadata: " +
174
        			e1.getMessage());
175
        	sf.setStackTrace(e1.getStackTrace());
176
        	throw sf;
177
        }
178
        
179
        //insert the systemmetadata object
180
        String guid = null;
181
        try {
182
        	IdentifierManager.getInstance().getLocalId(sm.getIdentifier().getValue());
183
        } catch (Exception e) {
184
			guid = null;
185
		}
186
        if (guid == null) {
187
        	IdentifierManager.getInstance().createSystemMetadataMapping(sm, localId);
188
        }
189
        IdentifierManager.getInstance().insertAdditionalSystemMetadataFields(sm);
190

    
191
        log.info("generateMissingSystemMetadata(token, localId)");
192
    }
193
    
194
    
195
    public static void main(String [] ags){
196

    
197
        try {
198
        	// set up the properties based on the test/deployed configuration of the workspace
199
        	SortedProperties testProperties = 
200
				new SortedProperties("test/test.properties");
201
			testProperties.load();
202
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
203
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
204
			// now run it
205
            GenerateSystemMetadata upgrader = new GenerateSystemMetadata();
206
	        upgrader.upgrade();
207
	        
208
        } catch (Exception ex) {
209
            System.out.println("Exception:" + ex.getMessage());
210
            ex.printStackTrace();
211
        }
212
    }
213
}
(1-1/5)