1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2010 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* '$Author: jones $'
|
7
|
* '$Date: 2010-02-03 17:58:12 -0900 (Wed, 03 Feb 2010) $'
|
8
|
* '$Revision: 5211 $'
|
9
|
*
|
10
|
* This program is free software; you can redistribute it and/or modify
|
11
|
* it under the terms of the GNU General Public License as published by
|
12
|
* the Free Software Foundation; either version 2 of the License, or
|
13
|
* (at your option) any later version.
|
14
|
*
|
15
|
* This program is distributed in the hope that it will be useful,
|
16
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
* GNU General Public License for more details.
|
19
|
*
|
20
|
* You should have received a copy of the GNU General Public License
|
21
|
* along with this program; if not, write to the Free Software
|
22
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
*/
|
24
|
|
25
|
package edu.ucsb.nceas.metacat;
|
26
|
|
27
|
import java.util.*;
|
28
|
|
29
|
import java.sql.PreparedStatement;
|
30
|
import java.sql.ResultSet;
|
31
|
import java.sql.SQLException;
|
32
|
import java.sql.Timestamp;
|
33
|
|
34
|
import org.apache.log4j.Logger;
|
35
|
import org.dataone.service.types.Checksum;
|
36
|
import org.dataone.service.types.ChecksumAlgorithm;
|
37
|
import org.dataone.service.types.ObjectFormat;
|
38
|
import org.dataone.service.types.ObjectInfo;
|
39
|
import org.dataone.service.types.ObjectList;
|
40
|
import org.dataone.service.types.SystemMetadata;
|
41
|
import org.dataone.service.types.Identifier;
|
42
|
|
43
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
44
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
45
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
46
|
import edu.ucsb.nceas.metacat.service.ObjectFormatService;
|
47
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
48
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
49
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
50
|
|
51
|
/**
|
52
|
* Manage the relationship between Metacat local identifiers (LocalIDs) that are
|
53
|
* codified as the (docid, rev) pair with globally uniqe string identifiers
|
54
|
* (GUIDs) that are opaque strings. This class provides methods to manage these
|
55
|
* identifiers, and to search for and look up LocalIDs based on their GUID and
|
56
|
* vice versa. IdentifierManager is a singleton.
|
57
|
*
|
58
|
* @author Matthew Jones
|
59
|
*/
|
60
|
public class IdentifierManager {
|
61
|
|
62
|
public static final String TYPE_SYSTEM_METADATA = "systemmetadata";
|
63
|
public static final String TYPE_IDENTIFIER = "identifier";
|
64
|
public static String DATAONE_SM_DOCTYPE;
|
65
|
|
66
|
/**
|
67
|
* The single instance of the manager that is always returned.
|
68
|
*/
|
69
|
private static IdentifierManager self = null;
|
70
|
private Logger logMetacat = Logger.getLogger(EventLog.class);
|
71
|
|
72
|
/**
|
73
|
* A private constructor that initializes the class when getInstance() is
|
74
|
* called.
|
75
|
*/
|
76
|
private IdentifierManager()
|
77
|
{
|
78
|
try
|
79
|
{
|
80
|
DATAONE_SM_DOCTYPE = PropertyService.getProperty("crudService.listObjects.ReturnDoctype");
|
81
|
}
|
82
|
catch(Exception e)
|
83
|
{
|
84
|
throw new RuntimeException("Error getting System Metadata doctype from " +
|
85
|
"the properties file. Please make sure crudService.listObjects.ReturnDoctype " +
|
86
|
"exists in metacat.properties.");
|
87
|
}
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Return the single instance of the manager after initializing it if it
|
92
|
* wasn't previously initialized.
|
93
|
*
|
94
|
* @return the single IdentifierManager instance
|
95
|
*/
|
96
|
public static IdentifierManager getInstance()
|
97
|
{
|
98
|
if (self == null) {
|
99
|
self = new IdentifierManager();
|
100
|
}
|
101
|
return self;
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* return a hash of all of the info that is in the systemmetadata table
|
106
|
* @param localId
|
107
|
* @return
|
108
|
*/
|
109
|
public Hashtable<String, String> getSystemMetadataInfo(String localId)
|
110
|
throws McdbDocNotFoundException
|
111
|
{
|
112
|
try
|
113
|
{
|
114
|
AccessionNumber acc = new AccessionNumber(localId, "NONE");
|
115
|
localId = acc.getDocid();
|
116
|
}
|
117
|
catch(Exception e)
|
118
|
{
|
119
|
//do nothing. just try the localId as it is
|
120
|
}
|
121
|
Hashtable<String, String> h = new Hashtable<String, String>();
|
122
|
String sql = "select guid, date_uploaded, rights_holder, checksum, checksum_algorithm, " +
|
123
|
"origin_member_node, authoritive_member_node, date_modified, submitter, object_format, size " +
|
124
|
"from systemmetadata where docid = ?";
|
125
|
DBConnection dbConn = null;
|
126
|
int serialNumber = -1;
|
127
|
try
|
128
|
{
|
129
|
// Get a database connection from the pool
|
130
|
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getDocumentInfo");
|
131
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
132
|
|
133
|
// Execute the insert statement
|
134
|
PreparedStatement stmt = dbConn.prepareStatement(sql);
|
135
|
stmt.setString(1, localId);
|
136
|
ResultSet rs = stmt.executeQuery();
|
137
|
if (rs.next())
|
138
|
{
|
139
|
String guid = rs.getString(1);
|
140
|
Timestamp dateUploaded = rs.getTimestamp(2);
|
141
|
String rightsHolder = rs.getString(3);
|
142
|
String checksum = rs.getString(4);
|
143
|
String checksumAlgorithm = rs.getString(5);
|
144
|
String originMemberNode = rs.getString(6);
|
145
|
String authoritativeMemberNode = rs.getString(7);
|
146
|
Timestamp dateModified = rs.getTimestamp(8);
|
147
|
String submitter = rs.getString(9);
|
148
|
String objectFormat = rs.getString(10);
|
149
|
long size = new Long(rs.getString(11)).longValue();
|
150
|
|
151
|
h.put("guid", guid);
|
152
|
h.put("date_uploaded", new Long(dateUploaded.getTime()).toString());
|
153
|
h.put("rights_holder", rightsHolder);
|
154
|
h.put("checksum", checksum);
|
155
|
h.put("checksum_algorithm", checksumAlgorithm);
|
156
|
h.put("origin_member_node", originMemberNode);
|
157
|
h.put("authoritative_member_node", authoritativeMemberNode);
|
158
|
h.put("date_modified", new Long(dateModified.getTime()).toString());
|
159
|
h.put("submitter", submitter);
|
160
|
h.put("object_format", objectFormat);
|
161
|
h.put("size", new Long(size).toString());
|
162
|
|
163
|
stmt.close();
|
164
|
}
|
165
|
else
|
166
|
{
|
167
|
stmt.close();
|
168
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
169
|
throw new McdbDocNotFoundException("2Could not find document " + localId);
|
170
|
}
|
171
|
|
172
|
}
|
173
|
catch (SQLException e)
|
174
|
{
|
175
|
e.printStackTrace();
|
176
|
logMetacat.error("Error while getting system metadata info for localid " + localId + " : "
|
177
|
+ e.getMessage());
|
178
|
}
|
179
|
finally
|
180
|
{
|
181
|
// Return database connection to the pool
|
182
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
183
|
}
|
184
|
return h;
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* return information on the document with localId. These are the fields
|
189
|
* from the xml_documents table. They can be used to contstruct metadata
|
190
|
* about the object that is stored.
|
191
|
* @param localId
|
192
|
* @return
|
193
|
* @throws McdbDocNotFoundException
|
194
|
*/
|
195
|
public Hashtable<String, Object> getDocumentInfo(String localId)
|
196
|
throws McdbDocNotFoundException
|
197
|
{
|
198
|
try
|
199
|
{
|
200
|
AccessionNumber acc = new AccessionNumber(localId, "NONE");
|
201
|
localId = acc.getDocid();
|
202
|
}
|
203
|
catch(Exception e)
|
204
|
{
|
205
|
//do nothing. just try the localId as it is
|
206
|
}
|
207
|
Hashtable<String, Object> h = new Hashtable<String, Object>();
|
208
|
String sql = "select docname, doctype, user_owner, user_updated, " +
|
209
|
"server_location, rev, date_created, date_updated from " +
|
210
|
"xml_documents where docid like '" + localId + "'";
|
211
|
DBConnection dbConn = null;
|
212
|
int serialNumber = -1;
|
213
|
try
|
214
|
{
|
215
|
// Get a database connection from the pool
|
216
|
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getDocumentInfo");
|
217
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
218
|
|
219
|
// Execute the insert statement
|
220
|
PreparedStatement stmt = dbConn.prepareStatement(sql);
|
221
|
ResultSet rs = stmt.executeQuery();
|
222
|
if (rs.next())
|
223
|
{
|
224
|
String docname = rs.getString(1);
|
225
|
String doctype = rs.getString(2);
|
226
|
String user_owner = rs.getString(3);
|
227
|
String user_updated = rs.getString(4);
|
228
|
String server_location = rs.getString(5);
|
229
|
int rev = rs.getInt(6);
|
230
|
String date_created = rs.getString(7);
|
231
|
String date_updated = rs.getString(8);
|
232
|
h.put("docid", localId);
|
233
|
h.put("docname", docname);
|
234
|
h.put("doctype", doctype);
|
235
|
h.put("user_owner", user_owner);
|
236
|
h.put("user_updated", user_updated);
|
237
|
h.put("server_location", server_location);
|
238
|
h.put("rev", new Integer(rev).toString());
|
239
|
h.put("date_created", date_created);
|
240
|
h.put("date_updated", date_updated);
|
241
|
|
242
|
stmt.close();
|
243
|
}
|
244
|
else
|
245
|
{
|
246
|
stmt.close();
|
247
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
248
|
throw new McdbDocNotFoundException("2Could not find document " + localId);
|
249
|
}
|
250
|
|
251
|
String sql2 = "select principal_name, permission, perm_type, perm_order from xml_access " +
|
252
|
"where docid like '" + localId + "'";
|
253
|
System.out.println("executing sql: " + sql2);
|
254
|
PreparedStatement stmt2 = dbConn.prepareStatement(sql2);
|
255
|
rs = stmt2.executeQuery();
|
256
|
Vector accessVector = new Vector();
|
257
|
while(rs.next())
|
258
|
{
|
259
|
Hashtable accessHash = new Hashtable();
|
260
|
String principal_name = rs.getString(1);
|
261
|
String permission = rs.getString(2);
|
262
|
String permissionType = rs.getString(3);
|
263
|
String permissionOrder = rs.getString(4);
|
264
|
accessHash.put("principal_name", principal_name);
|
265
|
accessHash.put("permission", permission);
|
266
|
accessHash.put("permission_type", permissionType);
|
267
|
accessHash.put("permission_order", permissionOrder);
|
268
|
System.out.println("accessHash: " + accessHash.toString());
|
269
|
accessVector.add(accessHash);
|
270
|
}
|
271
|
h.put("access", accessVector);
|
272
|
}
|
273
|
catch (SQLException e)
|
274
|
{
|
275
|
e.printStackTrace();
|
276
|
logMetacat.error("Error while getting document info for localid " + localId + " : "
|
277
|
+ e.getMessage());
|
278
|
}
|
279
|
finally
|
280
|
{
|
281
|
// Return database connection to the pool
|
282
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
283
|
}
|
284
|
return h;
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* return the newest rev for a given localId
|
289
|
* @param localId
|
290
|
* @return
|
291
|
*/
|
292
|
public int getLatestRevForLocalId(String localId)
|
293
|
throws McdbDocNotFoundException
|
294
|
{
|
295
|
try
|
296
|
{
|
297
|
AccessionNumber acc = new AccessionNumber(localId, "NONE");
|
298
|
localId = acc.getDocid();
|
299
|
}
|
300
|
catch(Exception e)
|
301
|
{
|
302
|
//do nothing. just try the localId as it is
|
303
|
}
|
304
|
int rev = 0;
|
305
|
String sql = "select rev from xml_documents where docid like '" + localId + "'";
|
306
|
DBConnection dbConn = null;
|
307
|
int serialNumber = -1;
|
308
|
try
|
309
|
{
|
310
|
// Get a database connection from the pool
|
311
|
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getLatestRevForLocalId");
|
312
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
313
|
|
314
|
// Execute the insert statement
|
315
|
PreparedStatement stmt = dbConn.prepareStatement(sql);
|
316
|
ResultSet rs = stmt.executeQuery();
|
317
|
if (rs.next())
|
318
|
{
|
319
|
rev = rs.getInt(1);
|
320
|
stmt.close();
|
321
|
}
|
322
|
else
|
323
|
{
|
324
|
stmt.close();
|
325
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
326
|
throw new McdbDocNotFoundException("While trying to get the latest rev, could not find document " + localId);
|
327
|
}
|
328
|
}
|
329
|
catch (SQLException e)
|
330
|
{
|
331
|
logMetacat.error("Error while looking up the guid: "
|
332
|
+ e.getMessage());
|
333
|
}
|
334
|
finally
|
335
|
{
|
336
|
// Return database connection to the pool
|
337
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
338
|
}
|
339
|
return rev;
|
340
|
}
|
341
|
|
342
|
/**
|
343
|
* return all local ids in the object store that do not have associated
|
344
|
* system metadata and are not themselves system metadata
|
345
|
*/
|
346
|
public List<String> getLocalIdsWithNoSystemMetadata()
|
347
|
{
|
348
|
Vector<String> ids = new Vector<String>();
|
349
|
//String sql = "select docid from identifier where guid in (select guid from systemmetadata)";
|
350
|
//String sql = "select docid from xml_documents where docid not " +
|
351
|
// "in (select docid from identifier where guid in (select guid from systemmetadata)) " +
|
352
|
// "and doctype not like '" + DATAONE_SM_DOCTYPE + "'";
|
353
|
String sql = "select docid, rev from xml_documents where docid not in " +
|
354
|
"(select docid from systemmetadata) and (docid not in " +
|
355
|
"(select docid from identifier where guid in (select guid from systemmetadata)))";
|
356
|
DBConnection dbConn = null;
|
357
|
int serialNumber = -1;
|
358
|
try
|
359
|
{
|
360
|
// Get a database connection from the pool
|
361
|
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getLocalIdsWithNoSystemMetadata");
|
362
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
363
|
|
364
|
// Execute the insert statement
|
365
|
PreparedStatement stmt = dbConn.prepareStatement(sql);
|
366
|
ResultSet rs = stmt.executeQuery();
|
367
|
while (rs.next())
|
368
|
{
|
369
|
String localid = rs.getString(1);
|
370
|
String rev = rs.getString(2);
|
371
|
localid += "." + rev;
|
372
|
System.out.println("id to add SM for: " + localid);
|
373
|
ids.add(localid);
|
374
|
}
|
375
|
stmt.close();
|
376
|
}
|
377
|
catch (SQLException e)
|
378
|
{
|
379
|
logMetacat.error("Error while looking up the guid: "
|
380
|
+ e.getMessage());
|
381
|
}
|
382
|
finally
|
383
|
{
|
384
|
// Return database connection to the pool
|
385
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
386
|
}
|
387
|
|
388
|
return ids;
|
389
|
}
|
390
|
|
391
|
/**
|
392
|
* return a listing of all local ids in the object store
|
393
|
* @return a list of all local ids in metacat
|
394
|
*/
|
395
|
public List<String> getAllLocalIds()
|
396
|
throws Exception
|
397
|
{
|
398
|
Vector<String> ids = new Vector<String>();
|
399
|
String sql = "select docid from xml_documents";
|
400
|
DBConnection dbConn = null;
|
401
|
int serialNumber = -1;
|
402
|
try
|
403
|
{
|
404
|
// Get a database connection from the pool
|
405
|
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getAllLocalIds");
|
406
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
407
|
|
408
|
// Execute the insert statement
|
409
|
PreparedStatement stmt = dbConn.prepareStatement(sql);
|
410
|
ResultSet rs = stmt.executeQuery();
|
411
|
while (rs.next())
|
412
|
{
|
413
|
String localid = rs.getString(1);
|
414
|
ids.add(localid);
|
415
|
}
|
416
|
stmt.close();
|
417
|
}
|
418
|
catch (SQLException e)
|
419
|
{
|
420
|
logMetacat.error("Error while looking up the guid: "
|
421
|
+ e.getMessage());
|
422
|
}
|
423
|
finally
|
424
|
{
|
425
|
// Return database connection to the pool
|
426
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
427
|
}
|
428
|
return ids;
|
429
|
}
|
430
|
|
431
|
/**
|
432
|
* Lookup a GUID from the database, and return the associated LocalID. If
|
433
|
* the identifier is not found, throw an exception.
|
434
|
*
|
435
|
* @param guid the global identifier to look up
|
436
|
* @return String containing the corresponding LocalId
|
437
|
* @throws McdbDocNotFoundException if the identifier is not found
|
438
|
*/
|
439
|
public String getLocalId(String guid) throws McdbDocNotFoundException
|
440
|
{
|
441
|
return this.getLocalId(guid, TYPE_IDENTIFIER);
|
442
|
}
|
443
|
|
444
|
/**
|
445
|
* Determine if an identifier exists already, returning true if so.
|
446
|
*
|
447
|
* @param guid the global identifier to look up
|
448
|
* @return boolean true if the identifier exists
|
449
|
*/
|
450
|
public boolean identifierExists(String guid)
|
451
|
{
|
452
|
boolean idExists = false;
|
453
|
try {
|
454
|
String id = getLocalId(guid);
|
455
|
if (id != null) {
|
456
|
idExists = true;
|
457
|
}
|
458
|
} catch (McdbDocNotFoundException e) {
|
459
|
idExists = false;
|
460
|
}
|
461
|
return idExists;
|
462
|
}
|
463
|
|
464
|
/**
|
465
|
* Create a mapping between two identifiers, one representing an
|
466
|
* unconstrained, globally unique string (guid), and one a localId
|
467
|
* in the Metacat docid format (scope.id.revision).
|
468
|
* This mapping allows one to find the global id (guid) from the localId.
|
469
|
*
|
470
|
* @param guid the global string identifier to be mapped
|
471
|
* @param localId the local identifier to be mapped
|
472
|
*/
|
473
|
public void createMapping(String guid, String localId)
|
474
|
{
|
475
|
createGenericMapping(guid, localId, TYPE_IDENTIFIER);
|
476
|
}
|
477
|
|
478
|
/**
|
479
|
*
|
480
|
* @param guid
|
481
|
* @param rev
|
482
|
* @return
|
483
|
*/
|
484
|
public String generateLocalId(String guid, int rev)
|
485
|
{
|
486
|
return generateLocalId(guid, rev, false);
|
487
|
}
|
488
|
|
489
|
/**
|
490
|
* Given a global identifier (guid), create a suitable local identifier that
|
491
|
* follows Metacat's docid semantics and format (scope.id.rev), and create
|
492
|
* a mapping between these two identifiers. This effectively reserves both
|
493
|
* the global and the local identifier, as they will now be present in the
|
494
|
* identifier mapping table. If the incoming guid has the syntax of a
|
495
|
* Metacat docid (scope.id.rev), then simply use it.
|
496
|
*
|
497
|
* @param guid the global string identifier
|
498
|
* @param rev the revision number to be used in the localId
|
499
|
* @return String containing the localId to be used for Metacat operations
|
500
|
*/
|
501
|
public String generateLocalId(String guid, int rev, boolean isSystemMetadata)
|
502
|
{
|
503
|
String localId = "";
|
504
|
boolean conformsToDocidFormat = false;
|
505
|
|
506
|
// Check if the guid passed in is already in docid (scope.id.rev) format
|
507
|
try {
|
508
|
AccessionNumber acc = new AccessionNumber(guid, "NONE");
|
509
|
if (new Integer(acc.getRev()).intValue() > 0) {
|
510
|
conformsToDocidFormat = true;
|
511
|
}
|
512
|
} catch (NumberFormatException e) {
|
513
|
// No action needed, simply detecting invalid AccessionNumbers
|
514
|
} catch (AccessionNumberException e) {
|
515
|
// No action needed, simply detecting invalid AccessionNumbers
|
516
|
} catch (SQLException e) {
|
517
|
// No action needed, simply detecting invalid AccessionNumbers
|
518
|
}
|
519
|
|
520
|
if (conformsToDocidFormat) {
|
521
|
// if it conforms, use it for both guid and localId
|
522
|
localId = guid;
|
523
|
} else {
|
524
|
// if not, then generate a new unique localId
|
525
|
localId = DocumentUtil.generateDocumentId(rev);
|
526
|
}
|
527
|
|
528
|
// Register this new pair in the identifier mapping table
|
529
|
System.out.println("creating mapping in generateLocalId");
|
530
|
if(!isSystemMetadata)
|
531
|
{ //don't do this if we're generating for system metadata
|
532
|
createMapping(guid, localId);
|
533
|
}
|
534
|
|
535
|
return localId;
|
536
|
}
|
537
|
|
538
|
/**
|
539
|
* given a local identifer, look up the guid. Throw McdbDocNotFoundException
|
540
|
* if the docid, rev is not found in the identifiers or systemmetadata tables
|
541
|
*
|
542
|
* @param docid the docid to look up
|
543
|
* @param rev the revision of the docid to look up
|
544
|
* @return String containing the mapped guid
|
545
|
* @throws McdbDocNotFoundException if the docid, rev is not found
|
546
|
*/
|
547
|
public String getGUID(String docid, int rev)
|
548
|
throws McdbDocNotFoundException
|
549
|
{
|
550
|
System.out.println("getting guid for " + docid);
|
551
|
String query = "select guid from identifier where docid = ? and rev = ?";
|
552
|
String guid = null;
|
553
|
boolean found = false;
|
554
|
|
555
|
DBConnection dbConn = null;
|
556
|
int serialNumber = -1;
|
557
|
try {
|
558
|
// Get a database connection from the pool
|
559
|
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getGUID");
|
560
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
561
|
|
562
|
// Execute the insert statement
|
563
|
PreparedStatement stmt = dbConn.prepareStatement(query);
|
564
|
stmt.setString(1, docid);
|
565
|
stmt.setInt(2, rev);
|
566
|
ResultSet rs = stmt.executeQuery();
|
567
|
if (rs.next())
|
568
|
{
|
569
|
guid = rs.getString(1);
|
570
|
}
|
571
|
else
|
572
|
{
|
573
|
query = "select guid from systemmetadata where docid = ? and rev = ?";
|
574
|
stmt = dbConn.prepareStatement(query);
|
575
|
stmt.setString(1, docid);
|
576
|
stmt.setInt(2, rev);
|
577
|
rs = stmt.executeQuery();
|
578
|
if(rs.next())
|
579
|
{
|
580
|
guid = rs.getString(1);
|
581
|
}
|
582
|
else
|
583
|
{
|
584
|
throw new McdbDocNotFoundException("No guid registered for docid " + docid + "." + rev);
|
585
|
}
|
586
|
}
|
587
|
|
588
|
} catch (SQLException e) {
|
589
|
logMetacat.error("Error while looking up the guid: "
|
590
|
+ e.getMessage());
|
591
|
} finally {
|
592
|
// Return database connection to the pool
|
593
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
594
|
}
|
595
|
|
596
|
return guid;
|
597
|
}
|
598
|
|
599
|
/**
|
600
|
* creates a mapping for a system metadata document, but DOES NOT add any
|
601
|
* of the extra system metadata information to the table. You must call
|
602
|
* insertAdditionalSystemMetadataFields to complete the entry
|
603
|
*
|
604
|
* @param guid the id to insert
|
605
|
* @param localId the systemMetadata object to get the local id for
|
606
|
*/
|
607
|
public void createSystemMetadataMapping(String guid, String localId)
|
608
|
{
|
609
|
createGenericMapping(guid, localId, TYPE_SYSTEM_METADATA);
|
610
|
}
|
611
|
|
612
|
/**
|
613
|
* creates a system metadata mapping and adds additional fields from sysmeta
|
614
|
* to the table for quick searching.
|
615
|
*
|
616
|
* @param guid the id to insert
|
617
|
* @param localId the systemMetadata object to get the local id for
|
618
|
*/
|
619
|
public void createSystemMetadataMapping(SystemMetadata sysmeta, String localId)
|
620
|
{
|
621
|
createGenericMapping(sysmeta.getIdentifier().getValue(), localId, TYPE_SYSTEM_METADATA);
|
622
|
insertAdditionalSystemMetadataFields(sysmeta);
|
623
|
}
|
624
|
|
625
|
/**
|
626
|
* update a system metadata mapping
|
627
|
* @param guid
|
628
|
* @param localId
|
629
|
*/
|
630
|
public void updateSystemMetadataMapping(String guid, String localId)
|
631
|
{
|
632
|
updateMapping(guid, localId, TYPE_SYSTEM_METADATA);
|
633
|
}
|
634
|
|
635
|
/**
|
636
|
* update a mapping
|
637
|
* @param guid
|
638
|
* @param localId
|
639
|
*/
|
640
|
public void updateMapping(String guid, String localId)
|
641
|
{
|
642
|
updateMapping(guid, localId, TYPE_IDENTIFIER);
|
643
|
}
|
644
|
|
645
|
/**
|
646
|
* update a mapping
|
647
|
* @param guid
|
648
|
* @param localId
|
649
|
*/
|
650
|
private void updateMapping(String guid, String localId, String type)
|
651
|
{
|
652
|
if (!type.equals(TYPE_IDENTIFIER) && !type.equals(TYPE_SYSTEM_METADATA)) {
|
653
|
throw new RuntimeException("Cannot create mapping for type " + type +
|
654
|
". Please choose 'identifier' or 'systemmetadata'.");
|
655
|
}
|
656
|
|
657
|
System.out.println("$$$$$$$$$$$$$$ updating mapping table");
|
658
|
int serialNumber = -1;
|
659
|
DBConnection dbConn = null;
|
660
|
try {
|
661
|
// Parse the localId into scope and rev parts
|
662
|
AccessionNumber acc = new AccessionNumber(localId, "NOACTION");
|
663
|
String docid = acc.getDocid();
|
664
|
int rev = 1;
|
665
|
if(acc.getRev() != null)
|
666
|
{
|
667
|
rev = (new Integer(acc.getRev()).intValue());
|
668
|
}
|
669
|
|
670
|
// Get a database connection from the pool
|
671
|
dbConn =
|
672
|
DBConnectionPool.getDBConnection("IdentifierManager.updateMapping");
|
673
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
674
|
|
675
|
// Execute the insert statement
|
676
|
String query = "update " + type + " set (docid, rev) = (?, ?) where guid='" + guid + "'";
|
677
|
//System.out.println("query: " + query + " for params: (guid:" + guid + ", docid=" + docid + ", rev=" + rev + ")");
|
678
|
PreparedStatement stmt = dbConn.prepareStatement(query);
|
679
|
stmt.setString(1, docid);
|
680
|
stmt.setInt(2, rev);
|
681
|
int rows = stmt.executeUpdate();
|
682
|
|
683
|
stmt.close();
|
684
|
} catch (SQLException e) {
|
685
|
e.printStackTrace();
|
686
|
logMetacat.error("SQL error while updating a mapping identifier: "
|
687
|
+ e.getMessage());
|
688
|
} catch (NumberFormatException e) {
|
689
|
e.printStackTrace();
|
690
|
logMetacat.error("NumberFormat error while updating a mapping identifier: "
|
691
|
+ e.getMessage());
|
692
|
} catch (AccessionNumberException e) {
|
693
|
e.printStackTrace();
|
694
|
logMetacat.error("AccessionNumber error while updating a mapping identifier: "
|
695
|
+ e.getMessage());
|
696
|
} finally {
|
697
|
// Return database connection to the pool
|
698
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
699
|
}
|
700
|
System.out.println("done updating mapping");
|
701
|
}
|
702
|
|
703
|
/**
|
704
|
* return the localId of a system metadata document based on its guid
|
705
|
*/
|
706
|
public String getSystemMetadataLocalId(String guid)
|
707
|
throws McdbDocNotFoundException
|
708
|
{
|
709
|
return this.getLocalId(guid, TYPE_SYSTEM_METADATA);
|
710
|
}
|
711
|
|
712
|
public void insertAdditionalSystemMetadataFields(long dateUploaded, String rightsHolder,
|
713
|
String checksum, String checksumAlgorithm, String originMemberNode,
|
714
|
String authoritativeMemberNode, long modifiedDate, String submitter,
|
715
|
String guid, String objectFormat, long size)
|
716
|
{
|
717
|
DBConnection dbConn = null;
|
718
|
int serialNumber = -1;
|
719
|
|
720
|
try {
|
721
|
// Get a database connection from the pool
|
722
|
dbConn =
|
723
|
DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
|
724
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
725
|
|
726
|
// Execute the insert statement
|
727
|
String query = "update " + TYPE_SYSTEM_METADATA +
|
728
|
" set (date_uploaded, rights_holder, checksum, checksum_algorithm, " +
|
729
|
"origin_member_node, authoritive_member_node, date_modified, " +
|
730
|
"submitter, object_format, size) " +
|
731
|
"= (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) where guid = ?";
|
732
|
PreparedStatement stmt = dbConn.prepareStatement(query);
|
733
|
|
734
|
//data values
|
735
|
stmt.setTimestamp(1, new java.sql.Timestamp(dateUploaded));
|
736
|
stmt.setString(2, rightsHolder);
|
737
|
stmt.setString(3, checksum);
|
738
|
stmt.setString(4, checksumAlgorithm);
|
739
|
stmt.setString(5, originMemberNode);
|
740
|
stmt.setString(6, authoritativeMemberNode);
|
741
|
stmt.setTimestamp(7, new java.sql.Timestamp(modifiedDate));
|
742
|
stmt.setString(8, submitter);
|
743
|
stmt.setString(9, objectFormat);
|
744
|
stmt.setString(10, new Long(size).toString());
|
745
|
//where clause
|
746
|
stmt.setString(11, guid);
|
747
|
System.out.println("stmt: " + stmt.toString());
|
748
|
//execute
|
749
|
int rows = stmt.executeUpdate();
|
750
|
|
751
|
stmt.close();
|
752
|
} catch (SQLException e) {
|
753
|
e.printStackTrace();
|
754
|
logMetacat.error("insertAdditionalSystemMetadataFields: SQL error while creating a mapping to the system metadata identifier: "
|
755
|
+ e.getMessage());
|
756
|
} catch (NumberFormatException e) {
|
757
|
e.printStackTrace();
|
758
|
logMetacat.error("insertAdditionalSystemMetadataFields: NumberFormat error while creating a mapping to the system metadata identifier: "
|
759
|
+ e.getMessage());
|
760
|
} finally {
|
761
|
// Return database connection to the pool
|
762
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
763
|
}
|
764
|
}
|
765
|
|
766
|
/**
|
767
|
* Insert the system metadata fields into the db
|
768
|
* @param sm
|
769
|
*/
|
770
|
public void insertAdditionalSystemMetadataFields(SystemMetadata sm)
|
771
|
{
|
772
|
insertAdditionalSystemMetadataFields(
|
773
|
sm.getDateUploaded().getTime(),
|
774
|
sm.getRightsHolder().getValue(),
|
775
|
sm.getChecksum().getValue(),
|
776
|
sm.getChecksum().getAlgorithm().name(),
|
777
|
sm.getOriginMemberNode().getValue(),
|
778
|
sm.getAuthoritativeMemberNode().getValue(),
|
779
|
sm.getDateSysMetadataModified().getTime(),
|
780
|
sm.getSubmitter().getValue(),
|
781
|
sm.getIdentifier().getValue(),
|
782
|
sm.getObjectFormat().toString(),
|
783
|
sm.getSize());
|
784
|
}
|
785
|
|
786
|
/**
|
787
|
* return a localId based on a guid. The type can either be 'identifier'
|
788
|
* to get an id from the identifier table or 'systemmetadata' to get
|
789
|
* the identifier from the systemidentifier table
|
790
|
*/
|
791
|
private String getLocalId(String guid, String type)
|
792
|
throws McdbDocNotFoundException
|
793
|
{
|
794
|
if(!type.equals(TYPE_IDENTIFIER) &&
|
795
|
!type.equals(TYPE_SYSTEM_METADATA))
|
796
|
{
|
797
|
throw new RuntimeException("cannot lookup the identifier for type " + type +
|
798
|
". Please choose 'identifier' or 'systemmetadata'.");
|
799
|
}
|
800
|
|
801
|
String db_guid = "";
|
802
|
String docid = "";
|
803
|
int rev = 0;
|
804
|
|
805
|
String query = "select guid, docid, rev from " + type + " where guid = ?";
|
806
|
//System.out.println("query: " + query + " for params: (" + guid + ")");
|
807
|
|
808
|
DBConnection dbConn = null;
|
809
|
int serialNumber = -1;
|
810
|
try {
|
811
|
// Get a database connection from the pool
|
812
|
dbConn = DBConnectionPool.getDBConnection("Identifier.getLocalId");
|
813
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
814
|
|
815
|
// Execute the insert statement
|
816
|
PreparedStatement stmt = dbConn.prepareStatement(query);
|
817
|
stmt.setString(1, guid);
|
818
|
ResultSet rs = stmt.executeQuery();
|
819
|
if (rs.next()) {
|
820
|
db_guid = rs.getString(1);
|
821
|
docid = rs.getString(2);
|
822
|
rev = rs.getInt(3);
|
823
|
assert(db_guid.equals(guid));
|
824
|
} else {
|
825
|
throw new McdbDocNotFoundException("Document not found:" + guid);
|
826
|
}
|
827
|
stmt.close();
|
828
|
} catch (SQLException e) {
|
829
|
logMetacat.error("Error while looking up the local identifier: "
|
830
|
+ e.getMessage());
|
831
|
} finally {
|
832
|
// Return database connection to the pool
|
833
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
834
|
}
|
835
|
return docid + "." + rev;
|
836
|
}
|
837
|
|
838
|
/**
|
839
|
* query the systemmetadata table based on the given parameters
|
840
|
* @param startTime
|
841
|
* @param endTime
|
842
|
* @param objectFormat
|
843
|
* @param replicaStatus
|
844
|
* @param start
|
845
|
* @param count
|
846
|
* @return ObjectList
|
847
|
*/
|
848
|
public ObjectList querySystemMetadata(Date startTime, Date endTime,
|
849
|
ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
|
850
|
{
|
851
|
ObjectList ol = new ObjectList();
|
852
|
int total = 0;
|
853
|
DBConnection dbConn = null;
|
854
|
int serialNumber = -1;
|
855
|
|
856
|
try
|
857
|
{
|
858
|
String sql = "select guid, date_uploaded, rights_holder, checksum, " +
|
859
|
"checksum_algorithm, origin_member_node, authoritive_member_node, " +
|
860
|
"date_modified, submitter, object_format, size from systemmetadata";
|
861
|
|
862
|
boolean f1 = false;
|
863
|
boolean f2 = false;
|
864
|
boolean f3 = false;
|
865
|
|
866
|
if(startTime != null)
|
867
|
{
|
868
|
sql += " where systemmetadata.date_modified > ?";
|
869
|
f1 = true;
|
870
|
}
|
871
|
|
872
|
if(endTime != null)
|
873
|
{
|
874
|
if(!f1)
|
875
|
{
|
876
|
sql += " where systemmetadata.date_modified < ?";
|
877
|
}
|
878
|
else
|
879
|
{
|
880
|
sql += " and systemmetadata.date_modified < ?";
|
881
|
}
|
882
|
f2 = true;
|
883
|
}
|
884
|
|
885
|
if(objectFormat != null)
|
886
|
{
|
887
|
if(!f1 && !f2)
|
888
|
{
|
889
|
sql += " where object_format = ?";
|
890
|
}
|
891
|
else
|
892
|
{
|
893
|
sql += " and object_format = ?";
|
894
|
}
|
895
|
f3 = true;
|
896
|
}
|
897
|
|
898
|
if(replicaStatus)
|
899
|
{
|
900
|
String currentNodeId = PropertyService.getInstance().getProperty("dataone.memberNodeId");
|
901
|
if(!f1 && !f2 && !f3)
|
902
|
{
|
903
|
sql += " where authoritive_member_node != '" + currentNodeId.trim() + "'";
|
904
|
}
|
905
|
else
|
906
|
{
|
907
|
sql += " and authoritive_member_node != '" + currentNodeId.trim() + "'";
|
908
|
}
|
909
|
}
|
910
|
|
911
|
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.querySystemMetadata");
|
912
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
913
|
PreparedStatement stmt = dbConn.prepareStatement(sql);
|
914
|
|
915
|
if(f1 && f2 && f3)
|
916
|
{
|
917
|
stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
|
918
|
stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
|
919
|
stmt.setString(3, objectFormat.toString());
|
920
|
}
|
921
|
else if(f1 && f2 && !f3)
|
922
|
{
|
923
|
stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
|
924
|
stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
|
925
|
}
|
926
|
else if(f1 && !f2 && f3)
|
927
|
{
|
928
|
stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
|
929
|
stmt.setString(2, objectFormat.toString());
|
930
|
}
|
931
|
else if(f1 && !f2 && !f3)
|
932
|
{
|
933
|
stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
|
934
|
}
|
935
|
else if(!f1 && f2 && f3)
|
936
|
{
|
937
|
stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
|
938
|
stmt.setString(2, objectFormat.toString());
|
939
|
}
|
940
|
else if(!f1 && !f2 && f3)
|
941
|
{
|
942
|
stmt.setString(1, objectFormat.toString());
|
943
|
}
|
944
|
else if(!f1 && f2 && !f3)
|
945
|
{
|
946
|
stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
|
947
|
}
|
948
|
|
949
|
//System.out.println("LISTOBJECTS QUERY: " + stmt.toString());
|
950
|
|
951
|
ResultSet rs = stmt.executeQuery();
|
952
|
for(int i=0; i<start; i++)
|
953
|
{
|
954
|
if(rs.next())
|
955
|
{
|
956
|
total++;
|
957
|
}
|
958
|
else
|
959
|
{
|
960
|
break;
|
961
|
}
|
962
|
}
|
963
|
|
964
|
int countIndex = 0;
|
965
|
|
966
|
while(rs.next())
|
967
|
{
|
968
|
total++;
|
969
|
if(countIndex >= count)
|
970
|
{
|
971
|
break;
|
972
|
}
|
973
|
String guid = rs.getString(1);
|
974
|
//System.out.println("query found doc with guid " + guid);
|
975
|
//Timestamp dateUploaded = rs.getTimestamp(2);
|
976
|
//String rightsHolder = rs.getString(3);
|
977
|
String checksum = rs.getString(4);
|
978
|
String checksumAlgorithm = rs.getString(5);
|
979
|
//String originMemberNode = rs.getString(6);
|
980
|
String authoritiveMemberNode = rs.getString(7);
|
981
|
Timestamp dateModified = rs.getTimestamp(8);
|
982
|
//String submitter = rs.getString(9);
|
983
|
String format = rs.getString(10);
|
984
|
String sz = rs.getString(11);
|
985
|
long size = 0;
|
986
|
if(sz != null && !sz.trim().equals(""))
|
987
|
{
|
988
|
size = new Long(rs.getString(11)).longValue();
|
989
|
}
|
990
|
|
991
|
ObjectInfo oi = new ObjectInfo();
|
992
|
|
993
|
Identifier id = new Identifier();
|
994
|
id.setValue(guid);
|
995
|
oi.setIdentifier(id);
|
996
|
|
997
|
oi.setDateSysMetadataModified(new Date(dateModified.getTime()));
|
998
|
|
999
|
Checksum cs = new Checksum();
|
1000
|
cs.setValue(checksum);
|
1001
|
cs.setAlgorithm(ChecksumAlgorithm.valueOf(checksumAlgorithm));
|
1002
|
//cs.setAlgorithm(ChecksumAlgorithm.convert(checksumAlgorithm));
|
1003
|
oi.setChecksum(cs);
|
1004
|
|
1005
|
ObjectFormat oFormat = ObjectFormatService.getFormat(format);
|
1006
|
if(oFormat != null)
|
1007
|
{
|
1008
|
oi.setObjectFormat(oFormat);
|
1009
|
}
|
1010
|
else
|
1011
|
{ //if there is no object format, just default to text/plain
|
1012
|
oi.setObjectFormat(ObjectFormatService.getFormat("application/octet-stream"));
|
1013
|
}
|
1014
|
|
1015
|
oi.setSize(size);
|
1016
|
|
1017
|
ol.addObjectInfo(oi);
|
1018
|
countIndex++;
|
1019
|
}
|
1020
|
|
1021
|
while(rs.next())
|
1022
|
{ //expend the resultset to get the total count of possible rows
|
1023
|
total++;
|
1024
|
}
|
1025
|
}
|
1026
|
catch(Exception e)
|
1027
|
{
|
1028
|
e.printStackTrace();
|
1029
|
System.out.println("Error querying system metadata: " + e.getMessage());
|
1030
|
}
|
1031
|
finally
|
1032
|
{
|
1033
|
// Return database connection to the pool
|
1034
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1035
|
}
|
1036
|
|
1037
|
ol.setStart(start);
|
1038
|
ol.setCount(ol.sizeObjectInfoList());
|
1039
|
ol.setTotal(total);
|
1040
|
|
1041
|
return ol;
|
1042
|
}
|
1043
|
|
1044
|
/**
|
1045
|
* create a mapping in the systemmetadata or identifier table
|
1046
|
* @param guid
|
1047
|
* @param localId
|
1048
|
* @param type
|
1049
|
*/
|
1050
|
private void createGenericMapping(String guid, String localId, String type)
|
1051
|
{
|
1052
|
if(!type.equals(TYPE_IDENTIFIER) &&
|
1053
|
!type.equals(TYPE_SYSTEM_METADATA))
|
1054
|
{
|
1055
|
throw new RuntimeException("Cannot create mapping for type " + type +
|
1056
|
". Please choose 'identifier' or 'systemmetadata'.");
|
1057
|
}
|
1058
|
|
1059
|
int serialNumber = -1;
|
1060
|
DBConnection dbConn = null;
|
1061
|
try {
|
1062
|
|
1063
|
// Parse the localId into scope and rev parts
|
1064
|
AccessionNumber acc = new AccessionNumber(localId, "NOACTION");
|
1065
|
String docid = acc.getDocid();
|
1066
|
int rev = 1;
|
1067
|
if(acc.getRev() != null)
|
1068
|
{
|
1069
|
rev = (new Integer(acc.getRev()).intValue());
|
1070
|
}
|
1071
|
|
1072
|
// Get a database connection from the pool
|
1073
|
dbConn =
|
1074
|
DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
|
1075
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
1076
|
|
1077
|
// Execute the insert statement
|
1078
|
String query = "insert into " + type + " (guid, docid, rev) values (?, ?, ?)";
|
1079
|
//System.out.println("query: " + query + " for params: (" + guid + ", " + docid + ", " + rev + ")");
|
1080
|
PreparedStatement stmt = dbConn.prepareStatement(query);
|
1081
|
stmt.setString(1, guid);
|
1082
|
stmt.setString(2, docid);
|
1083
|
stmt.setInt(3, rev);
|
1084
|
System.out.println("generic mapping query: " + stmt.toString());
|
1085
|
int rows = stmt.executeUpdate();
|
1086
|
|
1087
|
stmt.close();
|
1088
|
} catch (SQLException e) {
|
1089
|
e.printStackTrace();
|
1090
|
logMetacat.error("createGenericMapping: SQL error while creating a mapping to the " + type + " identifier: "
|
1091
|
+ e.getMessage());
|
1092
|
} catch (NumberFormatException e) {
|
1093
|
e.printStackTrace();
|
1094
|
logMetacat.error("createGenericMapping: NumberFormat error while creating a mapping to the " + type + " identifier: "
|
1095
|
+ e.getMessage());
|
1096
|
} catch (AccessionNumberException e) {
|
1097
|
e.printStackTrace();
|
1098
|
logMetacat.error("createGenericMapping: AccessionNumber error while creating a mapping to the " + type + " identifier: "
|
1099
|
+ e.getMessage());
|
1100
|
} finally {
|
1101
|
// Return database connection to the pool
|
1102
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1103
|
}
|
1104
|
}
|
1105
|
}
|
1106
|
|