1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class for upgrading the database to version 1.5
|
4
|
* Copyright: 2000 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Saurabh Garg
|
7
|
*
|
8
|
* '$Author: leinfelder $'
|
9
|
* '$Date: 2012-01-26 21:35:35 -0800 (Thu, 26 Jan 2012) $'
|
10
|
* '$Revision: 6960 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
package edu.ucsb.nceas.metacat.dataone;
|
27
|
|
28
|
import java.io.IOException;
|
29
|
import java.io.InputStream;
|
30
|
import java.math.BigInteger;
|
31
|
import java.net.URL;
|
32
|
import java.net.URLConnection;
|
33
|
import java.security.NoSuchAlgorithmException;
|
34
|
import java.sql.SQLException;
|
35
|
import java.util.ArrayList;
|
36
|
import java.util.Collections;
|
37
|
import java.util.Date;
|
38
|
import java.util.HashMap;
|
39
|
import java.util.Hashtable;
|
40
|
import java.util.List;
|
41
|
import java.util.Map;
|
42
|
import java.util.Vector;
|
43
|
|
44
|
import javax.xml.parsers.ParserConfigurationException;
|
45
|
import javax.xml.xpath.XPathExpressionException;
|
46
|
|
47
|
import org.apache.commons.beanutils.BeanUtils;
|
48
|
import org.apache.commons.io.IOUtils;
|
49
|
import org.apache.log4j.Logger;
|
50
|
import org.apache.wicket.protocol.http.MockHttpServletRequest;
|
51
|
import org.dataone.client.ObjectFormatCache;
|
52
|
import org.dataone.eml.DataoneEMLParser;
|
53
|
import org.dataone.eml.EMLDocument;
|
54
|
import org.dataone.eml.EMLDocument.DistributionMetadata;
|
55
|
import org.dataone.ore.ResourceMapFactory;
|
56
|
import org.dataone.service.exceptions.BaseException;
|
57
|
import org.dataone.service.exceptions.InvalidRequest;
|
58
|
import org.dataone.service.exceptions.InvalidSystemMetadata;
|
59
|
import org.dataone.service.exceptions.InvalidToken;
|
60
|
import org.dataone.service.exceptions.NotAuthorized;
|
61
|
import org.dataone.service.exceptions.NotFound;
|
62
|
import org.dataone.service.exceptions.NotImplemented;
|
63
|
import org.dataone.service.exceptions.ServiceFailure;
|
64
|
import org.dataone.service.types.v1.AccessPolicy;
|
65
|
import org.dataone.service.types.v1.Checksum;
|
66
|
import org.dataone.service.types.v1.Identifier;
|
67
|
import org.dataone.service.types.v1.NodeReference;
|
68
|
import org.dataone.service.types.v1.ObjectFormatIdentifier;
|
69
|
import org.dataone.service.types.v1.Session;
|
70
|
import org.dataone.service.types.v1.Subject;
|
71
|
import org.dataone.service.types.v1.SystemMetadata;
|
72
|
import org.dataone.service.types.v1.util.ChecksumUtil;
|
73
|
import org.dataone.service.util.DateTimeMarshaller;
|
74
|
import org.dspace.foresite.ResourceMap;
|
75
|
import org.jibx.runtime.JiBXException;
|
76
|
import org.xml.sax.SAXException;
|
77
|
|
78
|
import edu.ucsb.nceas.metacat.AccessionNumber;
|
79
|
import edu.ucsb.nceas.metacat.AccessionNumberException;
|
80
|
import edu.ucsb.nceas.metacat.DBUtil;
|
81
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
82
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
83
|
import edu.ucsb.nceas.metacat.McdbException;
|
84
|
import edu.ucsb.nceas.metacat.MetaCatServlet;
|
85
|
import edu.ucsb.nceas.metacat.MetacatHandler;
|
86
|
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
|
87
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
88
|
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
|
89
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
90
|
import edu.ucsb.nceas.metacat.replication.ReplicationService;
|
91
|
import edu.ucsb.nceas.metacat.shared.AccessException;
|
92
|
import edu.ucsb.nceas.metacat.shared.HandlerException;
|
93
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
94
|
import edu.ucsb.nceas.utilities.ParseLSIDException;
|
95
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
96
|
|
97
|
public class SystemMetadataFactory {
|
98
|
|
99
|
private static final String resourceMapPrefix = "resourceMap_";
|
100
|
private static Logger logMetacat = Logger.getLogger(SystemMetadataFactory.class);
|
101
|
|
102
|
/**
|
103
|
* Creates a system metadata object for insertion into metacat
|
104
|
*
|
105
|
* @param localId
|
106
|
* The local document identifier
|
107
|
* @param user
|
108
|
* The user submitting the system metadata document
|
109
|
* @param groups
|
110
|
* The groups the user belongs to
|
111
|
*
|
112
|
* @return sysMeta The system metadata object created
|
113
|
* @throws SAXException
|
114
|
* @throws HandlerException
|
115
|
* @throws AccessControlException
|
116
|
* @throws AccessException
|
117
|
*/
|
118
|
public static SystemMetadata createSystemMetadata(String localId, boolean includeORE, boolean downloadData)
|
119
|
throws McdbException, McdbDocNotFoundException, SQLException,
|
120
|
IOException, AccessionNumberException, ClassNotFoundException,
|
121
|
InsufficientKarmaException, ParseLSIDException,
|
122
|
PropertyNotFoundException, BaseException, NoSuchAlgorithmException,
|
123
|
JiBXException, AccessControlException, HandlerException, SAXException, AccessException {
|
124
|
|
125
|
logMetacat.debug("MetacatHandler.createSystemMetadata() called.");
|
126
|
logMetacat.debug("provided localId: " + localId);
|
127
|
|
128
|
// create system metadata for the document
|
129
|
SystemMetadata sysMeta = new SystemMetadata();
|
130
|
sysMeta.setSerialVersion(BigInteger.valueOf(1));
|
131
|
sysMeta.setArchived(false);
|
132
|
AccessionNumber accNum = new AccessionNumber(localId, "NONE");
|
133
|
int rev = Integer.valueOf(accNum.getRev());
|
134
|
|
135
|
// get the data or metadata object
|
136
|
InputStream inputStream;
|
137
|
try {
|
138
|
inputStream = MetacatHandler.read(localId);
|
139
|
} catch (ParseLSIDException ple) {
|
140
|
logMetacat.debug("There was a problem parsing the LSID from "
|
141
|
+ localId + ". The error message was: " + ple.getMessage());
|
142
|
throw ple;
|
143
|
|
144
|
} catch (PropertyNotFoundException pnfe) {
|
145
|
logMetacat.debug("There was a problem finding a property. "
|
146
|
+ "The error message was: " + pnfe.getMessage());
|
147
|
throw pnfe;
|
148
|
|
149
|
} catch (McdbException me) {
|
150
|
logMetacat.debug("There was a Metacat problem. "
|
151
|
+ "The error message was: " + me.getMessage());
|
152
|
throw me;
|
153
|
|
154
|
} catch (SQLException sqle) {
|
155
|
logMetacat.debug("There was a SQL problem. "
|
156
|
+ "The error message was: " + sqle.getMessage());
|
157
|
throw sqle;
|
158
|
|
159
|
} catch (ClassNotFoundException cnfe) {
|
160
|
logMetacat.debug("There was a problem finding a class. "
|
161
|
+ "The error message was: " + cnfe.getMessage());
|
162
|
throw cnfe;
|
163
|
|
164
|
} catch (IOException ioe) {
|
165
|
logMetacat.debug("There was an I/O exception. "
|
166
|
+ "The error message was: " + ioe.getMessage());
|
167
|
throw ioe;
|
168
|
|
169
|
}
|
170
|
|
171
|
// get/make the guid
|
172
|
String guid = null;
|
173
|
try {
|
174
|
// get the guid if it exists
|
175
|
guid = IdentifierManager.getInstance().getGUID(accNum.getDocid(), rev);
|
176
|
} catch (McdbDocNotFoundException dnfe) {
|
177
|
// otherwise create the mapping
|
178
|
logMetacat.debug("There was a problem getting the guid from "
|
179
|
+ "the given localId (docid and revision). The error message was: "
|
180
|
+ dnfe.getMessage());
|
181
|
logMetacat.debug("No guid in the identifier table. adding it for " + localId);
|
182
|
IdentifierManager.getInstance().createMapping(localId, localId);
|
183
|
logMetacat.debug("Mapping created for " + localId);
|
184
|
logMetacat.debug("accessionNumber: " + accNum);
|
185
|
guid = IdentifierManager.getInstance().getGUID(accNum.getDocid(), rev);
|
186
|
}
|
187
|
Identifier identifier = new Identifier();
|
188
|
identifier.setValue(guid);
|
189
|
|
190
|
// set the id
|
191
|
sysMeta.setIdentifier(identifier);
|
192
|
|
193
|
// get additional docinfo
|
194
|
Hashtable<String, String> docInfo = ReplicationService.getDocumentInfoMap(localId);
|
195
|
// set the default object format
|
196
|
String doctype = docInfo.get("doctype");
|
197
|
ObjectFormatIdentifier fmtid = null;
|
198
|
|
199
|
// set the object format, fall back to defaults
|
200
|
try {
|
201
|
fmtid = ObjectFormatCache.getInstance().getFormat(doctype).getFormatId();
|
202
|
} catch (NotFound nfe) {
|
203
|
|
204
|
try {
|
205
|
// format is not registered, use default
|
206
|
if (doctype.trim().equals("BIN")) {
|
207
|
fmtid = ObjectFormatCache.getInstance().getFormat(
|
208
|
"application/octet-stream").getFormatId();
|
209
|
|
210
|
} else {
|
211
|
fmtid = ObjectFormatCache.getInstance().getFormat(
|
212
|
"text/plain").getFormatId();
|
213
|
}
|
214
|
|
215
|
} catch (NotFound nf) {
|
216
|
logMetacat.error("There was a problem getting the default format "
|
217
|
+ "from the ObjectFormatCache: "
|
218
|
+ nf.getMessage());
|
219
|
throw nf;
|
220
|
}
|
221
|
|
222
|
}
|
223
|
|
224
|
sysMeta.setFormatId(fmtid);
|
225
|
logMetacat.debug("The ObjectFormat for " + localId + " is " + fmtid.getValue());
|
226
|
|
227
|
// create the checksum
|
228
|
inputStream = MetacatHandler.read(localId);
|
229
|
String algorithm = "MD5";
|
230
|
Checksum checksum = ChecksumUtil.checksum(inputStream, algorithm);
|
231
|
sysMeta.setChecksum(checksum);
|
232
|
|
233
|
// set the size
|
234
|
inputStream = MetacatHandler.read(localId);
|
235
|
String sizeStr = new Long(sizeOfStream(inputStream)).toString();
|
236
|
sysMeta.setSize(new BigInteger(sizeStr));
|
237
|
|
238
|
// submitter
|
239
|
Subject submitter = new Subject();
|
240
|
submitter.setValue(docInfo.get("user_updated"));
|
241
|
sysMeta.setSubmitter(submitter);
|
242
|
|
243
|
// rights holder
|
244
|
Subject owner = new Subject();
|
245
|
owner.setValue(docInfo.get("user_owner"));
|
246
|
sysMeta.setRightsHolder(owner);
|
247
|
|
248
|
// dates
|
249
|
String createdDateString = docInfo.get("date_created");
|
250
|
String updatedDateString = docInfo.get("date_updated");
|
251
|
Date createdDate = DateTimeMarshaller.deserializeDateToUTC(createdDateString);
|
252
|
Date updatedDate = DateTimeMarshaller.deserializeDateToUTC(updatedDateString);
|
253
|
sysMeta.setDateUploaded(createdDate);
|
254
|
sysMeta.setDateSysMetadataModified(updatedDate);
|
255
|
|
256
|
// set the revision history
|
257
|
String docidWithoutRev = accNum.getDocid();
|
258
|
Identifier obsoletedBy = null;
|
259
|
Identifier obsoletes = null;
|
260
|
Vector<Integer> revisions = DBUtil.getRevListFromRevisionTable(docidWithoutRev);
|
261
|
// ensure this ordering since processing depends on it
|
262
|
Collections.sort(revisions);
|
263
|
for (int existingRev: revisions) {
|
264
|
// use the docid+rev as the guid
|
265
|
String existingPid = docidWithoutRev + "." + existingRev;
|
266
|
if (existingRev < rev) {
|
267
|
// it's the old docid, until it's not
|
268
|
obsoletes = new Identifier();
|
269
|
obsoletes.setValue(existingPid);
|
270
|
}
|
271
|
if (existingRev > rev) {
|
272
|
// it's the newer docid
|
273
|
obsoletedBy = new Identifier();
|
274
|
obsoletedBy.setValue(existingPid);
|
275
|
// only want the version just after it
|
276
|
break;
|
277
|
}
|
278
|
}
|
279
|
// set them on our object
|
280
|
sysMeta.setObsoletedBy(obsoletedBy);
|
281
|
sysMeta.setObsoletes(obsoletes);
|
282
|
|
283
|
// update the system metadata for the object[s] we are revising
|
284
|
if (obsoletedBy != null) {
|
285
|
//SystemMetadata obsoletedBySysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(obsoletedBy);
|
286
|
SystemMetadata obsoletedBySysMeta = IdentifierManager.getInstance().getSystemMetadata(obsoletedBy.getValue());
|
287
|
if (obsoletedBySysMeta != null) {
|
288
|
obsoletedBySysMeta.setObsoletes(identifier);
|
289
|
HazelcastService.getInstance().getSystemMetadataMap().put(obsoletedBy, obsoletedBySysMeta);
|
290
|
}
|
291
|
}
|
292
|
if (obsoletes != null) {
|
293
|
//SystemMetadata obsoletesSysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(obsoletes);
|
294
|
SystemMetadata obsoletesSysMeta = IdentifierManager.getInstance().getSystemMetadata(obsoletes.getValue());
|
295
|
if (obsoletesSysMeta != null) {
|
296
|
obsoletesSysMeta.setObsoletedBy(identifier);
|
297
|
obsoletesSysMeta.setArchived(true);
|
298
|
HazelcastService.getInstance().getSystemMetadataMap().put(obsoletes, obsoletesSysMeta);
|
299
|
}
|
300
|
}
|
301
|
|
302
|
// look up the access control policy we have in metacat
|
303
|
AccessPolicy accessPolicy = IdentifierManager.getInstance().getAccessPolicy(guid);
|
304
|
sysMeta.setAccessPolicy(accessPolicy);
|
305
|
|
306
|
// authoritative node
|
307
|
NodeReference nr = new NodeReference();
|
308
|
nr.setValue(PropertyService.getProperty("dataone.memberNodeId"));
|
309
|
sysMeta.setOriginMemberNode(nr);
|
310
|
sysMeta.setAuthoritativeMemberNode(nr);
|
311
|
|
312
|
// further parse EML documents to get data object format,
|
313
|
// describes and describedBy information
|
314
|
if (fmtid == ObjectFormatCache.getInstance().getFormat(
|
315
|
"eml://ecoinformatics.org/eml-2.0.0").getFormatId()
|
316
|
|| fmtid == ObjectFormatCache.getInstance().getFormat(
|
317
|
"eml://ecoinformatics.org/eml-2.0.1").getFormatId()
|
318
|
|| fmtid == ObjectFormatCache.getInstance().getFormat(
|
319
|
"eml://ecoinformatics.org/eml-2.1.0").getFormatId()
|
320
|
|| fmtid == ObjectFormatCache.getInstance().getFormat(
|
321
|
"eml://ecoinformatics.org/eml-2.1.1").getFormatId()) {
|
322
|
|
323
|
try {
|
324
|
inputStream = MetacatHandler.read(localId);
|
325
|
|
326
|
DataoneEMLParser emlParser = DataoneEMLParser.getInstance();
|
327
|
EMLDocument emlDocument = emlParser.parseDocument(inputStream);
|
328
|
|
329
|
// iterate through the data objects in the EML doc and add sysmeta
|
330
|
logMetacat.debug("In createSystemMetadata() the number of data "
|
331
|
+ "entities is: "
|
332
|
+ emlDocument.distributionMetadata);
|
333
|
|
334
|
// for generating the ORE map
|
335
|
Map<Identifier, List<Identifier>> idMap = new HashMap<Identifier, List<Identifier>>();
|
336
|
List<Identifier> dataIds = new ArrayList<Identifier>();
|
337
|
|
338
|
// iterate through data objects described by the EML
|
339
|
if (emlDocument.distributionMetadata != null) {
|
340
|
for (int j = 0; j < emlDocument.distributionMetadata.size(); j++) {
|
341
|
|
342
|
DistributionMetadata distMetadata = emlDocument.distributionMetadata.elementAt(j);
|
343
|
String dataDocUrl = distMetadata.url;
|
344
|
String dataDocMimeType = distMetadata.mimeType;
|
345
|
// default to binary
|
346
|
if (dataDocMimeType == null) {
|
347
|
dataDocMimeType = ObjectFormatCache.getInstance()
|
348
|
.getFormat("application/octet-stream")
|
349
|
.getFormatId().getValue();
|
350
|
}
|
351
|
|
352
|
// process the data
|
353
|
boolean remoteData = false;
|
354
|
String dataDocLocalId = null;
|
355
|
Identifier dataGuid = new Identifier();
|
356
|
|
357
|
// handle ecogrid, or downloadable data
|
358
|
String ecogridPrefix = "ecogrid://knb/";
|
359
|
if (dataDocUrl.trim().startsWith(ecogridPrefix)) {
|
360
|
dataDocLocalId = dataDocUrl.substring(dataDocUrl.indexOf(ecogridPrefix) + ecogridPrefix.length());
|
361
|
} else {
|
362
|
// should we try downloading the remote data?
|
363
|
if (downloadData) {
|
364
|
InputStream dataObject = null;
|
365
|
try {
|
366
|
// download the data from the URL
|
367
|
URL dataURL = new URL(dataDocUrl);
|
368
|
URLConnection dataConnection = dataURL.openConnection();
|
369
|
|
370
|
// default is to download the data
|
371
|
dataObject = dataConnection.getInputStream();
|
372
|
|
373
|
String detectedContentType = dataConnection.getContentType();
|
374
|
logMetacat.info("Detected content type: " + detectedContentType);
|
375
|
|
376
|
if (detectedContentType != null) {
|
377
|
// seems to be HTML from the remote location
|
378
|
if (detectedContentType.contains("html")) {
|
379
|
// if we are not expecting it, we skip it
|
380
|
if (!dataDocMimeType.contains("html")) {
|
381
|
// set to null so we don't download it
|
382
|
dataObject = null;
|
383
|
logMetacat.warn("Skipping remote resource, unexpected HTML content type at: " + dataDocUrl);
|
384
|
}
|
385
|
}
|
386
|
|
387
|
// TODO: any other special processing (csv, images, etc)?
|
388
|
} else {
|
389
|
// if we don't know what it is, should we skip it?
|
390
|
dataObject = null;
|
391
|
logMetacat.warn("Skipping remote resource, unknown content type at: " + dataDocUrl);
|
392
|
}
|
393
|
|
394
|
} catch (Exception e) {
|
395
|
// error with the download
|
396
|
logMetacat.warn("Error downloading remote data. " + e.getMessage());
|
397
|
}
|
398
|
|
399
|
if (dataObject != null) {
|
400
|
// create the local version of it
|
401
|
dataDocLocalId = DocumentUtil.generateDocumentId(1);
|
402
|
IdentifierManager.getInstance().createMapping(dataDocLocalId, dataDocLocalId);
|
403
|
dataGuid.setValue(dataDocLocalId);
|
404
|
|
405
|
// save it locally
|
406
|
Session session = new Session();
|
407
|
session.setSubject(submitter);
|
408
|
MockHttpServletRequest request = new MockHttpServletRequest(null, null, null);
|
409
|
MNodeService.getInstance(request).insertDataObject(dataObject, dataGuid, session);
|
410
|
|
411
|
remoteData = true;
|
412
|
}
|
413
|
}
|
414
|
|
415
|
}
|
416
|
|
417
|
logMetacat.debug("Data local ID: " + dataDocLocalId);
|
418
|
logMetacat.debug("Data URL : " + dataDocUrl);
|
419
|
logMetacat.debug("Data mime : " + dataDocMimeType);
|
420
|
|
421
|
// now we have a local id for the data
|
422
|
if (dataDocLocalId != null) {
|
423
|
|
424
|
// look up the guid for the data
|
425
|
String dataDocid = DocumentUtil.getSmartDocId(dataDocLocalId);
|
426
|
int dataRev = DocumentUtil.getRevisionFromAccessionNumber(dataDocLocalId);
|
427
|
|
428
|
// check if data system metadata exists already
|
429
|
SystemMetadata dataSysMeta = null;
|
430
|
String dataGuidString = null;
|
431
|
try {
|
432
|
// look for the identifier
|
433
|
dataGuidString = IdentifierManager.getInstance().getGUID(dataDocid, dataRev);
|
434
|
// set it
|
435
|
dataGuid.setValue(dataGuidString);
|
436
|
// look up the system metadata
|
437
|
try {
|
438
|
dataSysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(dataGuid);
|
439
|
} catch (Exception e) {
|
440
|
// probably not in the system
|
441
|
dataSysMeta = null;
|
442
|
}
|
443
|
//dataSysMeta = IdentifierManager.getInstance().getSystemMetadata(dataGuidString);
|
444
|
} catch (McdbDocNotFoundException nf) {
|
445
|
// we didn't find it
|
446
|
dataSysMeta = null;
|
447
|
}
|
448
|
|
449
|
// we'll have to generate it
|
450
|
if (dataSysMeta == null) {
|
451
|
// System metadata for data doesn't exist yet, so create it
|
452
|
logMetacat.debug("There was not an existing system metadata document for " + dataDocLocalId);
|
453
|
try {
|
454
|
logMetacat.debug("Creating a system metadata " + "document for " + dataDocLocalId);
|
455
|
dataSysMeta = createSystemMetadata(dataDocLocalId, includeORE, false);
|
456
|
|
457
|
// now look it up again
|
458
|
dataGuidString = IdentifierManager.getInstance().getGUID(dataDocid, dataRev);
|
459
|
|
460
|
// set the guid
|
461
|
dataGuid.setValue(dataGuidString);
|
462
|
|
463
|
// set object format
|
464
|
logMetacat.debug("Updating system metadata for "
|
465
|
+ dataGuid.getValue() + " to "
|
466
|
+ dataDocMimeType);
|
467
|
try {
|
468
|
ObjectFormatIdentifier fmt =
|
469
|
ObjectFormatCache.getInstance().getFormat(dataDocMimeType).getFormatId();
|
470
|
dataSysMeta.setFormatId(fmt);
|
471
|
} catch (NotFound nfe) {
|
472
|
logMetacat.debug("Couldn't find format identifier for: "
|
473
|
+ dataDocMimeType
|
474
|
+ ". Setting it to application/octet-stream.");
|
475
|
ObjectFormatIdentifier newFmtid = new ObjectFormatIdentifier();
|
476
|
newFmtid.setValue("application/octet-stream");
|
477
|
}
|
478
|
|
479
|
// inherit access rules from metadata, if we don't have our own
|
480
|
if (remoteData) {
|
481
|
dataSysMeta.setAccessPolicy(sysMeta.getAccessPolicy());
|
482
|
// TODO: use access rules defined in EML, per data file
|
483
|
}
|
484
|
|
485
|
// update the values
|
486
|
HazelcastService.getInstance().getSystemMetadataMap().put(dataSysMeta.getIdentifier(), dataSysMeta);
|
487
|
|
488
|
|
489
|
} catch (McdbDocNotFoundException mdnf) {
|
490
|
mdnf.printStackTrace();
|
491
|
throw mdnf;
|
492
|
} catch (NumberFormatException nfe) {
|
493
|
nfe.printStackTrace();
|
494
|
throw nfe;
|
495
|
} catch (AccessionNumberException ane) {
|
496
|
ane.printStackTrace();
|
497
|
throw ane;
|
498
|
} catch (SQLException sqle) {
|
499
|
sqle.printStackTrace();
|
500
|
throw sqle;
|
501
|
} catch (NoSuchAlgorithmException nsae) {
|
502
|
nsae.printStackTrace();
|
503
|
throw nsae;
|
504
|
} catch (IOException ioe) {
|
505
|
ioe.printStackTrace();
|
506
|
throw ioe;
|
507
|
} catch (PropertyNotFoundException pnfe) {
|
508
|
pnfe.printStackTrace();
|
509
|
throw pnfe;
|
510
|
} catch (BaseException be) {
|
511
|
be.printStackTrace();
|
512
|
throw be;
|
513
|
}
|
514
|
|
515
|
}
|
516
|
|
517
|
// part of the ORE package
|
518
|
dataIds.add(dataGuid);
|
519
|
|
520
|
} // end if (EML package)
|
521
|
|
522
|
} // end for (data entities)
|
523
|
|
524
|
} // data entities not null
|
525
|
|
526
|
// ORE map
|
527
|
if (includeORE) {
|
528
|
// can we generate them?
|
529
|
if (!dataIds.isEmpty()) {
|
530
|
// it doesn't exist in the system?
|
531
|
if (!oreExistsFor(sysMeta.getIdentifier())) {
|
532
|
|
533
|
// generate the ORE map for this datapackage
|
534
|
Identifier resourceMapId = new Identifier();
|
535
|
// want to be able to run this over and over again for now
|
536
|
resourceMapId.setValue(resourceMapPrefix + sysMeta.getIdentifier().getValue());
|
537
|
idMap.put(sysMeta.getIdentifier(), dataIds);
|
538
|
ResourceMap rm = ResourceMapFactory.getInstance().createResourceMap(resourceMapId, idMap);
|
539
|
String resourceMapXML = ResourceMapFactory.getInstance().serializeResourceMap(rm);
|
540
|
// copy most of the same system metadata as the packaging metadata
|
541
|
SystemMetadata resourceMapSysMeta = new SystemMetadata();
|
542
|
BeanUtils.copyProperties(resourceMapSysMeta, sysMeta);
|
543
|
resourceMapSysMeta.setIdentifier(resourceMapId);
|
544
|
Checksum oreChecksum = ChecksumUtil.checksum(IOUtils.toInputStream(resourceMapXML, MetaCatServlet.DEFAULT_ENCODING), "MD5");
|
545
|
resourceMapSysMeta.setChecksum(oreChecksum);
|
546
|
ObjectFormatIdentifier formatId = ObjectFormatCache.getInstance().getFormat("http://www.openarchives.org/ore/terms").getFormatId();
|
547
|
resourceMapSysMeta.setFormatId(formatId);
|
548
|
resourceMapSysMeta.setSize(BigInteger.valueOf(sizeOfStream(IOUtils.toInputStream(resourceMapXML, MetaCatServlet.DEFAULT_ENCODING))));
|
549
|
|
550
|
// set the revision graph
|
551
|
resourceMapSysMeta.setObsoletes(null);
|
552
|
resourceMapSysMeta.setObsoletedBy(null);
|
553
|
// look up the resource map that this one obsoletes
|
554
|
if (sysMeta.getObsoletes() != null) {
|
555
|
Identifier resourceMapObsoletes = new Identifier();
|
556
|
resourceMapObsoletes.setValue(resourceMapPrefix + sysMeta.getObsoletes().getValue());
|
557
|
resourceMapSysMeta.setObsoletes(resourceMapObsoletes);
|
558
|
SystemMetadata resourceMapObsoletesSystemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(resourceMapObsoletes);
|
559
|
if (resourceMapObsoletesSystemMetadata != null) {
|
560
|
resourceMapObsoletesSystemMetadata.setObsoletedBy(resourceMapId);
|
561
|
HazelcastService.getInstance().getSystemMetadataMap().put(resourceMapObsoletes, resourceMapObsoletesSystemMetadata);
|
562
|
}
|
563
|
}
|
564
|
// look up the resource map that this one is obsoletedBy
|
565
|
if (sysMeta.getObsoletedBy() != null) {
|
566
|
Identifier resourceMapObsoletedBy = new Identifier();
|
567
|
resourceMapObsoletedBy.setValue(resourceMapPrefix + sysMeta.getObsoletedBy().getValue());
|
568
|
resourceMapSysMeta.setObsoletedBy(resourceMapObsoletedBy);
|
569
|
SystemMetadata resourceMapObsoletedBySystemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(resourceMapObsoletedBy);
|
570
|
if (resourceMapObsoletedBySystemMetadata != null) {
|
571
|
resourceMapObsoletedBySystemMetadata.setObsoletes(resourceMapId);
|
572
|
HazelcastService.getInstance().getSystemMetadataMap().put(resourceMapObsoletedBy, resourceMapObsoletedBySystemMetadata);
|
573
|
}
|
574
|
}
|
575
|
|
576
|
// save it locally, if it doesn't already exist
|
577
|
if (!IdentifierManager.getInstance().identifierExists(resourceMapId.getValue())) {
|
578
|
Session session = new Session();
|
579
|
session.setSubject(submitter);
|
580
|
MockHttpServletRequest request = new MockHttpServletRequest(null, null, null);
|
581
|
MNodeService.getInstance(request).insertDataObject(IOUtils.toInputStream(resourceMapXML, MetaCatServlet.DEFAULT_ENCODING), resourceMapId, session);
|
582
|
MNodeService.getInstance(request).insertSystemMetadata(resourceMapSysMeta);
|
583
|
logMetacat.info("Inserted ORE package: " + resourceMapId.getValue());
|
584
|
}
|
585
|
}
|
586
|
}
|
587
|
}
|
588
|
|
589
|
} catch (ParserConfigurationException pce) {
|
590
|
logMetacat.debug("There was a problem parsing the EML document. "
|
591
|
+ "The error message was: " + pce.getMessage());
|
592
|
|
593
|
} catch (SAXException saxe) {
|
594
|
logMetacat.debug("There was a problem traversing the EML document. "
|
595
|
+ "The error message was: " + saxe.getMessage());
|
596
|
|
597
|
} catch (XPathExpressionException xpee) {
|
598
|
logMetacat.debug("There was a problem searching the EML document. "
|
599
|
+ "The error message was: " + xpee.getMessage());
|
600
|
} catch (Exception e) {
|
601
|
logMetacat.debug("There was a problem creating System Metadata. "
|
602
|
+ "The error message was: " + e.getMessage());
|
603
|
e.printStackTrace();
|
604
|
} // end try()
|
605
|
|
606
|
} // end if()
|
607
|
|
608
|
return sysMeta;
|
609
|
}
|
610
|
|
611
|
/**
|
612
|
* Generate SystemMetadata for any object in the object store that does
|
613
|
* not already have it. SystemMetadata documents themselves, are, of course,
|
614
|
* exempt. This is a utility method for migration of existing object
|
615
|
* stores to DataONE where SystemMetadata is required for all objects.
|
616
|
* @param idList
|
617
|
* @param includeOre
|
618
|
* @param downloadData
|
619
|
* @throws ServiceFailure
|
620
|
* @throws McdbDocNotFoundException
|
621
|
* @throws PropertyNotFoundException
|
622
|
* @throws InvalidToken
|
623
|
* @throws NotAuthorized
|
624
|
* @throws NotFound
|
625
|
* @throws NotImplemented
|
626
|
* @throws InvalidRequest
|
627
|
* @throws NoSuchAlgorithmException
|
628
|
* @throws AccessionNumberException
|
629
|
* @throws SQLException
|
630
|
*/
|
631
|
public static void generateSystemMetadata(List<String> idList, boolean includeOre, boolean downloadData)
|
632
|
throws ServiceFailure, McdbDocNotFoundException, PropertyNotFoundException, InvalidToken, NotAuthorized,
|
633
|
NotFound, NotImplemented, InvalidRequest, NoSuchAlgorithmException, AccessionNumberException, SQLException
|
634
|
{
|
635
|
|
636
|
for (String localId : idList) {
|
637
|
//for each id, add a system metadata doc
|
638
|
try {
|
639
|
logMetacat.debug("generating system metadata for " + localId);
|
640
|
generateSystemMetadata(localId, includeOre, downloadData);
|
641
|
} catch (Exception e) {
|
642
|
logMetacat.error("Error generating system metadata for: " + localId, e);
|
643
|
}
|
644
|
}
|
645
|
logMetacat.info("done generating system metadata");
|
646
|
}
|
647
|
|
648
|
|
649
|
/**
|
650
|
* Generate SystemMetadata for a particular object with identifier localId.
|
651
|
* This is a utility method for migration of existing objects
|
652
|
* to DataONE where SystemMetadata is required for all objects.
|
653
|
* @param localId
|
654
|
* @param includeOre
|
655
|
* @param downloadData
|
656
|
* @throws ServiceFailure
|
657
|
* @throws McdbDocNotFoundException
|
658
|
* @throws PropertyNotFoundException
|
659
|
* @throws InvalidToken
|
660
|
* @throws NotAuthorized
|
661
|
* @throws NotFound
|
662
|
* @throws NotImplemented
|
663
|
* @throws InvalidRequest
|
664
|
* @throws NoSuchAlgorithmException
|
665
|
* @throws AccessionNumberException
|
666
|
* @throws SQLException
|
667
|
* @throws InvalidSystemMetadata
|
668
|
*/
|
669
|
protected static void generateSystemMetadata(String localId, boolean includeOre, boolean downloadData)
|
670
|
throws ServiceFailure, McdbDocNotFoundException, PropertyNotFoundException, InvalidToken, NotAuthorized,
|
671
|
NotFound, NotImplemented, InvalidRequest, NoSuchAlgorithmException, AccessionNumberException, SQLException, InvalidSystemMetadata
|
672
|
{
|
673
|
logMetacat.debug("generateSystemMetadata() called.");
|
674
|
logMetacat.debug("Creating SystemMetadata for localId " + localId);
|
675
|
SystemMetadata sm = null;
|
676
|
|
677
|
//generate required system metadata fields from the document
|
678
|
try {
|
679
|
sm = SystemMetadataFactory.createSystemMetadata(localId, includeOre, downloadData);
|
680
|
} catch (Exception e1) {
|
681
|
e1.printStackTrace();
|
682
|
ServiceFailure sf = new ServiceFailure("00","Exception in generateSystemMetadata: " +
|
683
|
e1.getMessage());
|
684
|
sf.setStackTrace(e1.getStackTrace());
|
685
|
throw sf;
|
686
|
}
|
687
|
|
688
|
//insert the systemmetadata object or just update it as needed
|
689
|
boolean exists = IdentifierManager.getInstance().systemMetadataExists(sm.getIdentifier().getValue());
|
690
|
if (!exists) {
|
691
|
IdentifierManager.getInstance().createSystemMetadata(sm);
|
692
|
logMetacat.info("Generated SystemMetadata for " + localId);
|
693
|
} else {
|
694
|
IdentifierManager.getInstance().updateSystemMetadata(sm);
|
695
|
logMetacat.info("Updated SystemMetadata for " + localId);
|
696
|
}
|
697
|
}
|
698
|
|
699
|
/**
|
700
|
* Determines if we already have registered an ORE map for this package
|
701
|
* @param guid of the EML/packaging object
|
702
|
* @return true if there is an ORE map for the given package
|
703
|
*/
|
704
|
private static boolean oreExistsFor(Identifier guid) {
|
705
|
// TODO: implement call to CN.search()
|
706
|
return false;
|
707
|
}
|
708
|
|
709
|
/**
|
710
|
* Find the size (in bytes) of a stream. Note: This needs to refactored out
|
711
|
* of MetacatHandler and into a utility when stream i/o in Metacat is
|
712
|
* evaluated.
|
713
|
*
|
714
|
* @param is The InputStream of bytes
|
715
|
*
|
716
|
* @return size The size in bytes of the input stream as a long
|
717
|
*
|
718
|
* @throws IOException
|
719
|
*/
|
720
|
private static long sizeOfStream(InputStream is) throws IOException {
|
721
|
|
722
|
long size = 0;
|
723
|
byte[] b = new byte[1024];
|
724
|
int numread = is.read(b, 0, 1024);
|
725
|
while (numread != -1) {
|
726
|
size += numread;
|
727
|
numread = is.read(b, 0, 1024);
|
728
|
}
|
729
|
return size;
|
730
|
|
731
|
}
|
732
|
}
|