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.ucsb.nceas.metacat.AccessionNumberException;
|
44
|
import edu.ucsb.nceas.metacat.DBUtil;
|
45
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
46
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
47
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
48
|
import edu.ucsb.nceas.metacat.admin.AdminException;
|
49
|
import edu.ucsb.nceas.metacat.dataone.SystemMetadataFactory;
|
50
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
51
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
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 = true;
|
63
|
try {
|
64
|
generateMissingSystemMetadata(includeOre);
|
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
|
* @param includeOre
|
81
|
*
|
82
|
* @param token an authtoken with appropriate permissions to read all
|
83
|
* documents in the object store. To work correctly, this should probably
|
84
|
* be an adminstrative credential.
|
85
|
* @throws SQLException
|
86
|
* @throws AccessionNumberException
|
87
|
* @throws NoSuchAlgorithmException
|
88
|
* @throws InvalidRequest
|
89
|
* @throws NotImplemented
|
90
|
* @throws NotFound
|
91
|
* @throws NotAuthorized
|
92
|
* @throws InvalidToken
|
93
|
* @throws PropertyNotFoundException
|
94
|
* @throws McdbDocNotFoundException
|
95
|
* @throws ServiceFailure
|
96
|
*/
|
97
|
public void generateMissingSystemMetadata(boolean includeOre)
|
98
|
throws ServiceFailure, McdbDocNotFoundException, PropertyNotFoundException, InvalidToken, NotAuthorized,
|
99
|
NotFound, NotImplemented, InvalidRequest, NoSuchAlgorithmException, AccessionNumberException, SQLException
|
100
|
{
|
101
|
//get the list of ids with no SM
|
102
|
//List<String> idList = IdentifierManager.getInstance().getLocalIdsWithNoSystemMetadata(true);
|
103
|
List<String> idList = DBUtil.getAllDocidsByType(null, true);
|
104
|
for (String localId : idList) {
|
105
|
//for each id, add a system metadata doc
|
106
|
try {
|
107
|
log.debug("generating missing system metadata for " + localId);
|
108
|
generateMissingSystemMetadata(localId, includeOre);
|
109
|
} catch (Exception e) {
|
110
|
log.error("Error generating system metadata for: " + localId, e);
|
111
|
}
|
112
|
}
|
113
|
log.info("done generating missing system metadata");
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Generate SystemMetadata for a particular object with identifier localId.
|
118
|
* This is a utility method for migration of existing objects
|
119
|
* to DataONE where SystemMetadata is required for all objects.
|
120
|
*
|
121
|
* @param token an authtoken with appropriate permissions to read all
|
122
|
* documents in the object store. To work correctly, this should
|
123
|
* be an adminstrative credential.
|
124
|
* @param localId the identifier of the object to be processed
|
125
|
* @param includeOre
|
126
|
* @throws ServiceFailure
|
127
|
* @throws SQLException
|
128
|
* @throws AccessionNumberException
|
129
|
* @throws NoSuchAlgorithmException
|
130
|
* @throws InvalidRequest
|
131
|
* @throws NotImplemented
|
132
|
* @throws NotFound
|
133
|
* @throws NotAuthorized
|
134
|
* @throws InvalidToken
|
135
|
* @throws PropertyNotFoundException
|
136
|
* @throws McdbDocNotFoundException
|
137
|
*/
|
138
|
public void generateMissingSystemMetadata(String localId, boolean includeOre)
|
139
|
throws ServiceFailure, McdbDocNotFoundException, PropertyNotFoundException, InvalidToken, NotAuthorized,
|
140
|
NotFound, NotImplemented, InvalidRequest, NoSuchAlgorithmException, AccessionNumberException, SQLException
|
141
|
{
|
142
|
log.debug("generateMissingSystemMetadata() called.");
|
143
|
log.debug("Creating SystemMetadata for localId " + localId);
|
144
|
SystemMetadata sm = null;
|
145
|
|
146
|
//generate required system metadata fields from the document
|
147
|
try {
|
148
|
sm = SystemMetadataFactory.createSystemMetadata(localId, includeOre);
|
149
|
} catch (Exception e1) {
|
150
|
e1.printStackTrace();
|
151
|
ServiceFailure sf = new ServiceFailure("00","Exception in generateMissingSystemMetadata: " +
|
152
|
e1.getMessage());
|
153
|
sf.setStackTrace(e1.getStackTrace());
|
154
|
throw sf;
|
155
|
}
|
156
|
|
157
|
//insert the systemmetadata object or just update it as needed
|
158
|
boolean exists = IdentifierManager.getInstance().systemMetadataExists(sm.getIdentifier().getValue());
|
159
|
if (!exists) {
|
160
|
IdentifierManager.getInstance().createSystemMetadata(sm);
|
161
|
}
|
162
|
IdentifierManager.getInstance().updateSystemMetadata(sm);
|
163
|
|
164
|
log.info("generateMissingSystemMetadata(token, localId)");
|
165
|
}
|
166
|
|
167
|
|
168
|
public static void main(String [] ags){
|
169
|
|
170
|
try {
|
171
|
// set up the properties based on the test/deployed configuration of the workspace
|
172
|
SortedProperties testProperties =
|
173
|
new SortedProperties("test/test.properties");
|
174
|
testProperties.load();
|
175
|
String metacatContextDir = testProperties.getProperty("metacat.contextDir");
|
176
|
PropertyService.getInstance(metacatContextDir + "/WEB-INF");
|
177
|
// now run it
|
178
|
GenerateSystemMetadata upgrader = new GenerateSystemMetadata();
|
179
|
upgrader.upgrade();
|
180
|
|
181
|
} catch (Exception ex) {
|
182
|
System.out.println("Exception:" + ex.getMessage());
|
183
|
ex.printStackTrace();
|
184
|
}
|
185
|
}
|
186
|
}
|