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