Project

General

Profile

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.math.BigInteger;
28
import java.sql.PreparedStatement;
29
import java.sql.ResultSet;
30
import java.sql.SQLException;
31
import java.sql.Timestamp;
32
import java.util.ArrayList;
33
import java.util.Date;
34
import java.util.Hashtable;
35
import java.util.List;
36
import java.util.Vector;
37

    
38
import org.apache.log4j.Logger;
39
import org.dataone.client.ObjectFormatCache;
40
import org.dataone.service.exceptions.NotFound;
41
import org.dataone.service.types.v1.AccessPolicy;
42
import org.dataone.service.types.v1.AccessRule;
43
import org.dataone.service.types.v1.Checksum;
44
import org.dataone.service.types.v1.Identifier;
45
import org.dataone.service.types.v1.NodeReference;
46
import org.dataone.service.types.v1.ObjectFormatIdentifier;
47
import org.dataone.service.types.v1.ObjectInfo;
48
import org.dataone.service.types.v1.ObjectList;
49
import org.dataone.service.types.v1.Permission;
50
import org.dataone.service.types.v1.Replica;
51
import org.dataone.service.types.v1.ReplicationPolicy;
52
import org.dataone.service.types.v1.ReplicationStatus;
53
import org.dataone.service.types.v1.Subject;
54
import org.dataone.service.types.v1.SystemMetadata;
55

    
56
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
57
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessAccess;
58
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
59
import edu.ucsb.nceas.metacat.database.DBConnection;
60
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
61
import edu.ucsb.nceas.metacat.properties.PropertyService;
62
import edu.ucsb.nceas.metacat.shared.AccessException;
63
import edu.ucsb.nceas.metacat.shared.ServiceException;
64
import edu.ucsb.nceas.metacat.util.DocumentUtil;
65
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
66

    
67
/**
68
 * Manage the relationship between Metacat local identifiers (LocalIDs) that are
69
 * codified as the (docid, rev) pair with globally unique string identifiers
70
 * (GUIDs) that are opaque strings.  This class provides methods to manage these
71
 * identifiers, and to search for and look up LocalIDs based on their GUID and
72
 * vice versa. IdentifierManager is a singleton.
73
 * 
74
 * @author Matthew Jones
75
 */
76
public class IdentifierManager {
77
    
78
    public static final String TYPE_SYSTEM_METADATA = "systemmetadata";
79
    public static final String TYPE_IDENTIFIER = "identifier";
80
  
81
    /**
82
     * The single instance of the manager that is always returned.
83
     */
84
    private static IdentifierManager self = null;
85
    private Logger logMetacat = Logger.getLogger(IdentifierManager.class);
86

    
87
    /**
88
     * A private constructor that initializes the class when getInstance() is
89
     * called.
90
     */
91
    private IdentifierManager() {}
92

    
93
    /**
94
     * Return the single instance of the manager after initializing it if it
95
     * wasn't previously initialized.
96
     * 
97
     * @return the single IdentifierManager instance
98
     */
99
    public static IdentifierManager getInstance()
100
    {
101
        if (self == null) {
102
            self = new IdentifierManager();
103
        }
104
        return self;
105
    }
106
    
107
    public SystemMetadata asSystemMetadata(Date dateUploaded, String rightsHolder,
108
            String checksum, String checksumAlgorithm, String originMemberNode,
109
            String authoritativeMemberNode, Date dateModified, String submitter, 
110
            String guid, String fmtidStr, BigInteger size, BigInteger serialVersion) {
111
        SystemMetadata sysMeta = new SystemMetadata();
112

    
113
        Identifier sysMetaId = new Identifier();
114
        sysMetaId.setValue(guid);
115
        sysMeta.setIdentifier(sysMetaId);
116
        sysMeta.setDateUploaded(dateUploaded);
117
        Subject rightsHolderSubject = new Subject();
118
        rightsHolderSubject.setValue(rightsHolder);
119
        sysMeta.setRightsHolder(rightsHolderSubject);
120
        Checksum checksumObject = new Checksum();
121
        checksumObject.setValue(checksum);
122
        checksumObject.setAlgorithm(checksumAlgorithm);
123
        sysMeta.setChecksum(checksumObject);
124
        NodeReference omn = new NodeReference();
125
        omn.setValue(originMemberNode);
126
        sysMeta.setOriginMemberNode(omn);
127
        NodeReference amn = new NodeReference();
128
        amn.setValue(authoritativeMemberNode);
129
        sysMeta.setAuthoritativeMemberNode(amn);
130
        sysMeta.setDateSysMetadataModified(dateModified);
131
        Subject submitterSubject = new Subject();
132
        submitterSubject.setValue(submitter);
133
        sysMeta.setSubmitter(submitterSubject);
134
        try {
135
        	ObjectFormatIdentifier fmtid = 
136
        		ObjectFormatCache.getInstance().getFormat(fmtidStr).getFormatId();
137
        	sysMeta.setFormatId(fmtid);
138
        } catch (NotFound nfe) {
139
          logMetacat.error("The objectFormat " + fmtidStr +
140
          	" is not registered. Setting the default format id.");
141
          ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
142
          fmtid.setValue("application/octet-stream");
143
          sysMeta.setFormatId(fmtid);
144
        }
145
        sysMeta.setSize(size);
146
        sysMeta.setSerialVersion(serialVersion);
147
        
148
        return sysMeta;
149
    }
150
    
151
    /**
152
     * return a hash of all of the info that is in the systemmetadata table
153
     * @param localId
154
     * @return
155
     */
156
    public Hashtable<String, String> getSystemMetadataInfo(String localId)
157
    throws McdbDocNotFoundException
158
    {
159
        try
160
        {
161
            AccessionNumber acc = new AccessionNumber(localId, "NONE");
162
            localId = acc.getDocid();
163
        }
164
        catch(Exception e)
165
        {
166
            //do nothing. just try the localId as it is
167
        }
168
        Hashtable<String, String> h = new Hashtable<String, String>();
169
        String sql = "select guid, date_uploaded, rights_holder, checksum, checksum_algorithm, " +
170
          "origin_member_node, authoritive_member_node, date_modified, submitter, object_format, size " +
171
          "from systemmetadata where docid = ?";
172
        DBConnection dbConn = null;
173
        int serialNumber = -1;
174
        try 
175
        {
176
            // Get a database connection from the pool
177
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getDocumentInfo");
178
            serialNumber = dbConn.getCheckOutSerialNumber();
179

    
180
            // Execute the insert statement
181
            PreparedStatement stmt = dbConn.prepareStatement(sql);
182
            stmt.setString(1, localId);
183
            ResultSet rs = stmt.executeQuery();
184
            if (rs.next()) 
185
            {
186
                String guid = rs.getString(1);
187
                Timestamp dateUploaded = rs.getTimestamp(2);
188
                String rightsHolder = rs.getString(3);
189
                String checksum = rs.getString(4);
190
                String checksumAlgorithm = rs.getString(5);
191
                String originMemberNode = rs.getString(6);
192
                String authoritativeMemberNode = rs.getString(7);
193
                Timestamp dateModified = rs.getTimestamp(8);
194
                String submitter = rs.getString(9);
195
                String objectFormat = rs.getString(10);
196
                long size = new Long(rs.getString(11)).longValue();
197
                
198
                h.put("guid", guid);
199
                h.put("date_uploaded", new Long(dateUploaded.getTime()).toString());
200
                h.put("rights_holder", rightsHolder);
201
                h.put("checksum", checksum);
202
                h.put("checksum_algorithm", checksumAlgorithm);
203
                h.put("origin_member_node", originMemberNode);
204
                h.put("authoritative_member_node", authoritativeMemberNode);
205
                h.put("date_modified", new Long(dateModified.getTime()).toString());
206
                h.put("submitter", submitter);
207
                h.put("object_format", objectFormat);
208
                h.put("size", new Long(size).toString());
209
                
210
                stmt.close();
211
            } 
212
            else
213
            {
214
                stmt.close();
215
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
216
                throw new McdbDocNotFoundException("2Could not find document " + localId);
217
            }
218
            
219
        } 
220
        catch (SQLException e) 
221
        {
222
            e.printStackTrace();
223
            logMetacat.error("Error while getting system metadata info for localid " + localId + " : "  
224
                    + e.getMessage());
225
        } 
226
        finally 
227
        {
228
            // Return database connection to the pool
229
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
230
        }
231
        return h;
232
    }
233
    
234
    /**
235
     * return a hash of all of the info that is in the systemmetadata table
236
     * @param guid
237
     * @return
238
     * @throws McdbDocNotFoundException 
239
     */
240
    public SystemMetadata getSystemMetadata(String guid)
241
    	throws McdbDocNotFoundException
242
    {
243
        
244
        SystemMetadata sysMeta = new SystemMetadata();
245
        String sql = "select guid, date_uploaded, rights_holder, checksum, checksum_algorithm, " +
246
          "origin_member_node, authoritive_member_node, date_modified, submitter, object_format, size, " +
247
          "replication_allowed, number_replicas, obsoletes, obsoleted_by, serial_version " +
248
          "from systemmetadata where guid = ?";
249
        DBConnection dbConn = null;
250
        int serialNumber = -1;
251
        Boolean replicationAllowed = new Boolean(false);
252
        BigInteger numberOfReplicas = new BigInteger("-1");
253
        BigInteger serialVersion = new BigInteger("-1");
254

    
255
        try 
256
        {
257
            // Get a database connection from the pool
258
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getSystemMetadata");
259
            serialNumber = dbConn.getCheckOutSerialNumber();
260

    
261
            // Execute the statement
262
            PreparedStatement stmt = dbConn.prepareStatement(sql);
263
            stmt.setString(1, guid);
264
            ResultSet rs = stmt.executeQuery();
265
            if (rs.next()) 
266
            {
267
                Timestamp dateUploaded = rs.getTimestamp(2);
268
                String rightsHolder = rs.getString(3);
269
                String checksum = rs.getString(4);
270
                String checksumAlgorithm = rs.getString(5);
271
                String originMemberNode = rs.getString(6);
272
                String authoritativeMemberNode = rs.getString(7);
273
                Timestamp dateModified = rs.getTimestamp(8);
274
                String submitter = rs.getString(9);
275
                String fmtidStr = rs.getString(10);
276
                BigInteger size = new BigInteger(rs.getString(11));
277
                replicationAllowed = new Boolean(rs.getBoolean(12));
278
                numberOfReplicas = new BigInteger(rs.getString(13));
279
                String obsoletes = rs.getString(14);
280
                String obsoletedBy = rs.getString(15);
281
                serialVersion = new BigInteger(rs.getString(16));
282

    
283

    
284
                Identifier sysMetaId = new Identifier();
285
                sysMetaId.setValue(guid);
286
                sysMeta.setIdentifier(sysMetaId);
287
                sysMeta.setSerialVersion(serialVersion);
288
                sysMeta.setDateUploaded(dateUploaded);
289
                Subject rightsHolderSubject = new Subject();
290
                rightsHolderSubject.setValue(rightsHolder);
291
                sysMeta.setRightsHolder(rightsHolderSubject);
292
                Checksum checksumObject = new Checksum();
293
                checksumObject.setValue(checksum);
294
                checksumObject.setAlgorithm(checksumAlgorithm);
295
                sysMeta.setChecksum(checksumObject);
296
                if (originMemberNode != null) {
297
	                NodeReference omn = new NodeReference();
298
	                omn.setValue(originMemberNode);
299
	                sysMeta.setOriginMemberNode(omn);
300
                }
301
                if (authoritativeMemberNode != null) {
302
	                NodeReference amn = new NodeReference();
303
	                amn.setValue(authoritativeMemberNode);
304
	                sysMeta.setAuthoritativeMemberNode(amn);
305
                }
306
                sysMeta.setDateSysMetadataModified(dateModified);
307
                Subject submitterSubject = new Subject();
308
                submitterSubject.setValue(submitter);
309
                sysMeta.setSubmitter(submitterSubject);
310
                try {
311
                	ObjectFormatIdentifier fmtid = 
312
                		ObjectFormatCache.getInstance().getFormat(fmtidStr).getFormatId();
313
                	sysMeta.setFormatId(fmtid);
314
                } catch (NotFound nfe) {
315
                  logMetacat.error("The objectFormat " + fmtidStr +
316
                  	" is not registered. Setting the default format id.");
317
                  ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
318
                  fmtid.setValue("application/octet-stream");
319
                  sysMeta.setFormatId(fmtid);
320
                }
321
                sysMeta.setSize(size);
322
                if (obsoletes != null) {
323
	                Identifier obsoletesId = new Identifier();
324
	                obsoletesId.setValue(obsoletes);
325
	                sysMeta.setObsoletes(obsoletesId);
326
                }
327
                if (obsoletedBy != null) {
328
		            Identifier obsoletedById = new Identifier();
329
		            obsoletedById.setValue(obsoletedBy);
330
		            sysMeta.setObsoletedBy(obsoletedById);
331
                }
332
                stmt.close();
333
            } 
334
            else
335
            {
336
                stmt.close();
337
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
338
                throw new McdbDocNotFoundException("Could not find " + guid);
339
            }
340
            
341
        } 
342
        catch (SQLException e) 
343
        {
344
            e.printStackTrace();
345
            logMetacat.error("Error while getting system metadata for guid " + guid + " : "  
346
                    + e.getMessage());
347
        } 
348
        finally 
349
        {
350
            // Return database connection to the pool
351
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
352
        }
353

    
354
        // populate the replication policy
355
        ReplicationPolicy replicationPolicy = new ReplicationPolicy();
356
        if ( numberOfReplicas != null  && numberOfReplicas.intValue() != -1 ) {
357
            replicationPolicy.setNumberReplicas(numberOfReplicas.intValue());
358
            
359
        }
360
        
361
        if ( replicationAllowed != null ) {
362
            replicationPolicy.setReplicationAllowed(replicationAllowed);
363
            
364
        }
365
        replicationPolicy.setBlockedMemberNodeList(getReplicationPolicy(guid, "blocked"));
366
        replicationPolicy.setPreferredMemberNodeList(getReplicationPolicy(guid, "preferred"));
367
		    sysMeta.setReplicationPolicy(replicationPolicy);
368
		
369
		    // look up replication status
370
		    sysMeta.setReplicaList(getReplicationStatus(guid));
371
		
372
		    // look up access policy
373
		    try {
374
		    	sysMeta.setAccessPolicy(getAccessPolicy(guid));
375
		    } catch (AccessException e) {
376
		    	throw new McdbDocNotFoundException(e);
377
		    }
378
        
379
        return sysMeta;
380
    }
381
    
382
    
383
    private List<NodeReference> getReplicationPolicy(String guid, String policy)
384
		throws McdbDocNotFoundException {
385
		
386
		List<NodeReference> nodes = new ArrayList<NodeReference>();
387
		String sql = "select guid, policy, member_node " +
388
			"from systemMetadataReplicationPolicy where guid = ? and policy = ?";
389
	    DBConnection dbConn = null;
390
	    int serialNumber = -1;
391
	    try {
392
	        // Get a database connection from the pool
393
	        dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getReplicationPolicy");
394
	        serialNumber = dbConn.getCheckOutSerialNumber();
395
	
396
	        // Execute the statement
397
	        PreparedStatement stmt = dbConn.prepareStatement(sql);
398
	        stmt.setString(1, guid);
399
	        stmt.setString(2, policy);
400
	        ResultSet rs = stmt.executeQuery();
401
	        while (rs.next()) 
402
	        {
403
	            String memberNode = rs.getString(3);
404
	            NodeReference node = new NodeReference();
405
	            node.setValue(memberNode);
406
	            nodes.add(node);
407
	        
408
	        } 
409
	        stmt.close();
410
	        
411
	    } catch (SQLException e) {
412
	        logMetacat.error("Error while getting system metadata replication policy for guid " + guid, e);
413
	    } 
414
	    finally {
415
	        // Return database connection to the pool
416
	        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
417
	    }
418
	    
419
	    return nodes;
420
	}
421
    
422
    private List<Replica> getReplicationStatus(String guid) throws McdbDocNotFoundException {
423
		
424
		List<Replica> replicas = new ArrayList<Replica>();
425
		String sql = "select guid, member_node, status, date_verified " +
426
			"from systemMetadataReplicationStatus where guid = ?";
427
	    DBConnection dbConn = null;
428
	    int serialNumber = -1;
429
	    try {
430
	        // Get a database connection from the pool
431
	        dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getReplicas");
432
	        serialNumber = dbConn.getCheckOutSerialNumber();
433
	
434
	        // Execute the statement
435
	        PreparedStatement stmt = dbConn.prepareStatement(sql);
436
	        stmt.setString(1, guid);
437
	        ResultSet rs = stmt.executeQuery();
438
	        while (rs.next()) 
439
	        {
440
	            String memberNode = rs.getString(2);
441
	            String status = rs.getString(3);
442
	            java.sql.Date verified = rs.getDate(4);
443
	            
444
	            Replica replica = new Replica();	            
445
	            NodeReference node = new NodeReference();
446
	            node.setValue(memberNode);
447
	            replica.setReplicaMemberNode(node);
448
	            replica.setReplicationStatus(ReplicationStatus.valueOf(status));
449
	            replica.setReplicaVerified(new Date(verified.getTime()));
450
	            replicas.add(replica);
451
	        } 
452
	        stmt.close();
453
	        
454
	    } catch (SQLException e) {
455
	        logMetacat.error("Error while getting system metadata replication policy for guid " + guid, e);
456
	    } 
457
	    finally {
458
	        // Return database connection to the pool
459
	        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
460
	    }
461
	    
462
	    return replicas;
463
	}
464
    
465
    
466
    /**
467
     * return the newest rev for a given localId
468
     * @param localId
469
     * @return
470
     */
471
    public int getLatestRevForLocalId(String localId)
472
        throws McdbDocNotFoundException
473
    {
474
        try
475
        {
476
            AccessionNumber acc = new AccessionNumber(localId, "NONE");
477
            localId = acc.getDocid();
478
        }
479
        catch(Exception e)
480
        {
481
            //do nothing. just try the localId as it is
482
        }
483
        int rev = 0;
484
        String sql = "select rev from xml_documents where docid like ? ";
485
        DBConnection dbConn = null;
486
        int serialNumber = -1;
487
        try 
488
        {
489
            // Get a database connection from the pool
490
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getLatestRevForLocalId");
491
            serialNumber = dbConn.getCheckOutSerialNumber();
492

    
493
            // Execute the insert statement
494
            PreparedStatement stmt = dbConn.prepareStatement(sql);
495
            stmt.setString(1, localId);
496
            ResultSet rs = stmt.executeQuery();
497
            if (rs.next()) 
498
            {
499
                rev = rs.getInt(1);
500
                stmt.close();
501
            } 
502
            else
503
            {
504
                stmt.close();
505
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
506
                throw new McdbDocNotFoundException("While trying to get the latest rev, could not find document " + localId);
507
            }
508
        } 
509
        catch (SQLException e) 
510
        {
511
            logMetacat.error("Error while looking up the guid: " 
512
                    + e.getMessage());
513
        } 
514
        finally 
515
        {
516
            // Return database connection to the pool
517
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
518
        }
519
        return rev;
520
    }
521
    
522
    /**
523
     * return all local ids in the object store that do not have associated
524
     * system metadata
525
     */
526
    public List<String> getLocalIdsWithNoSystemMetadata(boolean includeRevisions)
527
    {
528
        Vector<String> ids = new Vector<String>();
529
        String sql = "select docid, rev from xml_documents " +
530
        		"where docid not in " +
531
        		"(select docid from identifier where guid in (select guid from systemmetadata))";
532
        
533
        String revisionSql = "select docid, rev from xml_revisions " +
534
				"where docid not in " +
535
				"(select docid from identifier where guid in (select guid from systemmetadata))";
536
        
537
        if (includeRevisions) {
538
        	sql = sql + " UNION ALL " + revisionSql;
539
        }
540
        
541
        DBConnection dbConn = null;
542
        int serialNumber = -1;
543
        try 
544
        {
545
            // Get a database connection from the pool
546
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getLocalIdsWithNoSystemMetadata");
547
            serialNumber = dbConn.getCheckOutSerialNumber();
548

    
549
            // Execute the insert statement
550
            PreparedStatement stmt = dbConn.prepareStatement(sql);
551
            ResultSet rs = stmt.executeQuery();
552
            while (rs.next()) 
553
            {
554
                String localid = rs.getString(1);
555
                String rev = rs.getString(2);
556
                localid += "." + rev;
557
                logMetacat.debug("id to add SM for: " + localid);
558
                ids.add(localid);
559
            } 
560
            stmt.close();
561
        } 
562
        catch (SQLException e) 
563
        {
564
            logMetacat.error("Error while looking up the guid: " 
565
                    + e.getMessage());
566
        } 
567
        finally 
568
        {
569
            // Return database connection to the pool
570
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
571
        }
572
        
573
        return ids;
574
    }
575
    
576
    /**
577
     * return a listing of all local ids in the object store
578
     * @return a list of all local ids in metacat
579
     */
580
    public List<String> getAllLocalIds()
581
    // seems to be an unnecessary and restrictive throw -rnahf 13-Sep-2011
582
    //    throws Exception
583
    {
584
        Vector<String> ids = new Vector<String>();
585
        String sql = "select docid from xml_documents";
586
        DBConnection dbConn = null;
587
        int serialNumber = -1;
588
        try 
589
        {
590
            // Get a database connection from the pool
591
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getAllLocalIds");
592
            serialNumber = dbConn.getCheckOutSerialNumber();
593

    
594
            // Execute the insert statement
595
            PreparedStatement stmt = dbConn.prepareStatement(sql);
596
            ResultSet rs = stmt.executeQuery();
597
            while (rs.next()) 
598
            {
599
                String localid = rs.getString(1);
600
                ids.add(localid);
601
            } 
602
            stmt.close();
603
        } 
604
        catch (SQLException e) 
605
        {
606
            logMetacat.error("Error while looking up the guid: " 
607
                    + e.getMessage());
608
        } 
609
        finally 
610
        {
611
            // Return database connection to the pool
612
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
613
        }
614
        return ids;
615
    }
616
    
617
    
618
    /**
619
     * return a listing of all guids in the object store
620
     * @return a list of all GUIDs in metacat
621
     */
622
    public List<String> getAllGUIDs()
623
    {
624
        Vector<String> guids = new Vector<String>();
625
        String sql = "select guid from identifier";
626
        DBConnection dbConn = null;
627
        int serialNumber = -1;
628
        try 
629
        {
630
            // Get a database connection from the pool
631
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getAllGUIDs");
632
            serialNumber = dbConn.getCheckOutSerialNumber();
633

    
634
            // Execute the insert statement
635
            PreparedStatement stmt = dbConn.prepareStatement(sql);
636
            ResultSet rs = stmt.executeQuery();
637
            while (rs.next()) 
638
            {
639
                String guid = rs.getString(1);
640
                guids.add(guid);
641
            } 
642
            stmt.close();
643
        } 
644
        catch (SQLException e) 
645
        {
646
            logMetacat.error("Error while retrieving the guid: " 
647
                    + e.getMessage());
648
        } 
649
        finally 
650
        {
651
            // Return database connection to the pool
652
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
653
        }
654
        return guids;
655
    }
656
    
657
    
658
    
659
    /**
660
     * returns a list of system metadata-only guids since the given date
661
     * @return a list of system ids in metacat that do not correspond to objects
662
     * TODO: need to check which server they are on
663
     */
664
    public List<String> getUpdatedSystemMetadataIds(Date since)
665
       throws Exception
666
    {
667
        List<String> ids = new Vector<String>();
668
        String sql = 
669
        	"select guid from " + TYPE_SYSTEM_METADATA +
670
        	" where guid not in " +
671
        	" (select guid from " + TYPE_IDENTIFIER + ") " +
672
        	" and date_modified > ?";
673
        DBConnection dbConn = null;
674
        int serialNumber = -1;
675
        try 
676
        {
677
            // Get a database connection from the pool
678
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getUpdatedSystemMetadataIds");
679
            serialNumber = dbConn.getCheckOutSerialNumber();
680

    
681
            // Execute the insert statement
682
            PreparedStatement stmt = dbConn.prepareStatement(sql);
683
            stmt.setDate(1, new java.sql.Date(since.getTime()));
684
            ResultSet rs = stmt.executeQuery();
685
            while (rs.next()) 
686
            {
687
                String guid = rs.getString(1);
688
                ids.add(guid);
689
            } 
690
            stmt.close();
691
        } 
692
        catch (SQLException e) 
693
        {
694
            logMetacat.error("Error while looking up the updated guids: " 
695
                    + e.getMessage());
696
        } 
697
        finally 
698
        {
699
            // Return database connection to the pool
700
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
701
        }
702
        return ids;
703
    }
704
    
705
    /**
706
     * returns a list of system metadata-only guids since the given date
707
     * @return a list of system ids in metacat that do not correspond to objects
708
     * TODO: need to check which server they are on
709
     */
710
    public Date getLastModifiedDate() throws Exception {
711
        Date maxDate = null;
712

    
713
        List<String> ids = new Vector<String>();
714
        String sql = 
715
        	"select max(date_modified) from " + TYPE_SYSTEM_METADATA;
716
        DBConnection dbConn = null;
717
        int serialNumber = -1;
718
        try 
719
        {
720
            // Get a database connection from the pool
721
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getLastModifiedDate");
722
            serialNumber = dbConn.getCheckOutSerialNumber();
723

    
724
            // Execute the insert statement
725
            PreparedStatement stmt = dbConn.prepareStatement(sql);
726
            ResultSet rs = stmt.executeQuery();
727
            if (rs.next()) {
728
            	maxDate = rs.getDate(1);
729
            } 
730
            stmt.close();
731
        } 
732
        catch (SQLException e) 
733
        {
734
            logMetacat.error("Error while looking up the latest update date: " 
735
                    + e.getMessage());
736
        } 
737
        finally 
738
        {
739
            // Return database connection to the pool
740
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
741
        }
742
        return maxDate;
743
    }
744

    
745
    
746
    /**
747
     * Determine if an identifier exists already, returning true if so.
748
     * 
749
     * @param guid the global identifier to look up
750
     * @return boolean true if the identifier exists
751
     */
752
    public boolean identifierExists(String guid)
753
    {
754
        boolean idExists = false;
755
        try {
756
            String id = getLocalId(guid);
757
            if (id != null) {
758
                idExists = true;
759
            }
760
        } catch (McdbDocNotFoundException e) {
761
        	// try system metadata only
762
        	try {
763
        		idExists = systemMetadataExists(guid);
764
            } catch (Exception e2) {
765
            	idExists = false;
766
            }
767
        }
768
        return idExists;
769
    }
770
    
771
    /**
772
     * 
773
     * @param guid
774
     * @param rev
775
     * @return
776
     */
777
    public String generateLocalId(String guid, int rev)
778
    {
779
        return generateLocalId(guid, rev, false);
780
    }
781

    
782
    /**
783
     * Given a global identifier (guid), create a suitable local identifier that
784
     * follows Metacat's docid semantics and format (scope.id.rev), and create
785
     * a mapping between these two identifiers.  This effectively reserves both
786
     * the global and the local identifier, as they will now be present in the
787
     * identifier mapping table.  If the incoming guid has the syntax of a
788
     * Metacat docid (scope.id.rev), then simply use it.
789
     * 
790
     * @param guid the global string identifier
791
     * @param rev the revision number to be used in the localId
792
     * @return String containing the localId to be used for Metacat operations
793
     */
794
    public String generateLocalId(String guid, int rev, boolean isSystemMetadata) 
795
    {
796
        String localId = "";
797
        boolean conformsToDocidFormat = false;
798
        
799
        // Check if the guid passed in is already in docid (scope.id.rev) format
800
        try {
801
            AccessionNumber acc = new AccessionNumber(guid, "NONE");
802
            if (new Integer(acc.getRev()).intValue() > 0) {
803
                conformsToDocidFormat = true;
804
            }
805
        } catch (NumberFormatException e) {
806
            // No action needed, simply detecting invalid AccessionNumbers
807
        } catch (AccessionNumberException e) {
808
            // No action needed, simply detecting invalid AccessionNumbers
809
        } catch (SQLException e) {
810
            // No action needed, simply detecting invalid AccessionNumbers
811
        }
812
        
813
        if (conformsToDocidFormat) {
814
            // if it conforms, use it for both guid and localId
815
            localId = guid;
816
        } else {
817
            // if not, then generate a new unique localId
818
            localId = DocumentUtil.generateDocumentId(rev);
819
        }
820
        
821
        // Register this new pair in the identifier mapping table
822
        logMetacat.debug("creating mapping in generateLocalId");
823
        if(!isSystemMetadata)
824
        { //don't do this if we're generating for system metadata
825
            createMapping(guid, localId);
826
        }
827
        
828
        return localId;
829
    }
830
    
831
    /**
832
     * given a local identifer, look up the guid.  Throw McdbDocNotFoundException
833
     * if the docid, rev is not found in the identifiers or systemmetadata tables
834
     *
835
     * @param docid the docid to look up
836
     * @param rev the revision of the docid to look up
837
     * @return String containing the mapped guid
838
     * @throws McdbDocNotFoundException if the docid, rev is not found
839
     */
840
    public String getGUID(String docid, int rev)
841
      throws McdbDocNotFoundException
842
    {
843
        logMetacat.debug("getting guid for " + docid);
844
        String query = "select guid from identifier where docid = ? and rev = ?";
845
        String guid = null;
846
        
847
        DBConnection dbConn = null;
848
        int serialNumber = -1;
849
        try {
850
            // Get a database connection from the pool
851
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getGUID");
852
            serialNumber = dbConn.getCheckOutSerialNumber();
853
            
854
            // Execute the insert statement
855
            PreparedStatement stmt = dbConn.prepareStatement(query);
856
            stmt.setString(1, docid);
857
            stmt.setInt(2, rev);
858
            ResultSet rs = stmt.executeQuery();
859
            if (rs.next()) 
860
            {
861
                guid = rs.getString(1);
862
            } 
863
            else
864
            {
865
            	throw new McdbDocNotFoundException("No guid registered for docid " + docid + "." + rev);
866
            }
867
            
868
        } catch (SQLException e) {
869
            logMetacat.error("Error while looking up the guid: " 
870
                    + e.getMessage());
871
        } finally {
872
            // Return database connection to the pool
873
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
874
        }
875
        
876
        return guid;
877
    }
878
    
879
    public boolean systemMetadataExists(String guid) {
880
		logMetacat.debug("looking up system metadata for guid " + guid);
881
		boolean exists = false;
882
		String query = "select guid from systemmetadata where guid = ?";
883

    
884
		DBConnection dbConn = null;
885
		int serialNumber = -1;
886
		try {
887
			// Get a database connection from the pool
888
			dbConn = DBConnectionPool.getDBConnection("IdentifierManager.systemMetadataExisits");
889
			serialNumber = dbConn.getCheckOutSerialNumber();
890

    
891
			// Execute the insert statement
892
			PreparedStatement stmt = dbConn.prepareStatement(query);
893
			stmt.setString(1, guid);
894
			ResultSet rs = stmt.executeQuery();
895
			if (rs.next()) {
896
				exists = true;
897
			}
898

    
899
		} catch (SQLException e) {
900
			logMetacat.error("Error while looking up the system metadata: "
901
					+ e.getMessage());
902
		} finally {
903
			// Return database connection to the pool
904
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
905
		}
906

    
907
		return exists;
908
	}
909
    
910
    /**
911
     * creates a system metadata mapping and adds additional fields from sysmeta
912
     * to the table for quick searching.
913
     * 
914
     * @param guid the id to insert
915
     * @param localId the systemMetadata object to get the local id for
916
     * @throws McdbDocNotFoundException 
917
     */
918
    public void createSystemMetadata(SystemMetadata sysmeta) throws McdbDocNotFoundException
919
    {
920
    	String guid = sysmeta.getIdentifier().getValue();
921
    	// insert the record
922
        insertSystemMetadata(guid);
923
        // update with the values
924
        updateSystemMetadata(sysmeta);
925
    }
926
        
927
    
928
    /**
929
     * update a mapping
930
     * @param guid
931
     * @param localId
932
     */
933
    public void updateMapping(String guid, String localId)
934
    {
935
    	
936
        logMetacat.debug("$$$$$$$$$$$$$$ updating mapping table");
937
        int serialNumber = -1;
938
        DBConnection dbConn = null;
939
        try {
940
            // Parse the localId into scope and rev parts
941
            AccessionNumber acc = new AccessionNumber(localId, "NOACTION");
942
            String docid = acc.getDocid();
943
            int rev = 1;
944
            if(acc.getRev() != null)
945
            {
946
              rev = (new Integer(acc.getRev()).intValue());
947
            }
948

    
949
            // Get a database connection from the pool
950
            dbConn = 
951
                DBConnectionPool.getDBConnection("IdentifierManager.updateMapping");
952
            serialNumber = dbConn.getCheckOutSerialNumber();
953

    
954
            // Execute the update statement
955
            String query = "update " + TYPE_IDENTIFIER + " set (docid, rev) = (?, ?) where guid = ?";
956
            PreparedStatement stmt = dbConn.prepareStatement(query);
957
            stmt.setString(1, docid);
958
            stmt.setInt(2, rev);
959
            stmt.setString(3, guid);
960
            int rows = stmt.executeUpdate();
961

    
962
            stmt.close();
963
        } catch (SQLException e) {
964
            e.printStackTrace();
965
            logMetacat.error("SQL error while updating a mapping identifier: " 
966
                    + e.getMessage());
967
        } catch (NumberFormatException e) {
968
            e.printStackTrace();
969
            logMetacat.error("NumberFormat error while updating a mapping identifier: " 
970
                    + e.getMessage());
971
        } catch (AccessionNumberException e) {
972
            e.printStackTrace();
973
            logMetacat.error("AccessionNumber error while updating a mapping identifier: " 
974
                    + e.getMessage());
975
        } finally {
976
            // Return database connection to the pool
977
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
978
        }
979
        logMetacat.debug("done updating mapping");
980
    }
981
        
982
    private void updateSystemMetadataFields(long dateUploaded, String rightsHolder,
983
            String checksum, String checksumAlgorithm, String originMemberNode,
984
            String authoritativeMemberNode, long modifiedDate, String submitter, 
985
            String guid, String objectFormat, BigInteger size, boolean archived, boolean replicationAllowed,
986
            int numberReplicas, String obsoletes, String obsoletedBy, BigInteger serialVersion)
987
    {
988
        DBConnection dbConn = null;
989
        int serialNumber = -1;
990
        
991
        try {
992
            // Get a database connection from the pool
993
            dbConn = 
994
                DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
995
            serialNumber = dbConn.getCheckOutSerialNumber();
996

    
997
            // Execute the insert statement
998
            String query = "update " + TYPE_SYSTEM_METADATA + 
999
                " set (date_uploaded, rights_holder, checksum, checksum_algorithm, " +
1000
                "origin_member_node, authoritive_member_node, date_modified, " +
1001
                "submitter, object_format, size, archived, replication_allowed, number_replicas, " +
1002
                "obsoletes, obsoleted_by, serial_version) " +
1003
                "= (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) where guid = ?";
1004
            PreparedStatement stmt = dbConn.prepareStatement(query);
1005
            
1006
            //data values
1007
            stmt.setTimestamp(1, new java.sql.Timestamp(dateUploaded));
1008
            stmt.setString(2, rightsHolder);
1009
            stmt.setString(3, checksum);
1010
            stmt.setString(4, checksumAlgorithm);
1011
            stmt.setString(5, originMemberNode);
1012
            stmt.setString(6, authoritativeMemberNode);
1013
            stmt.setTimestamp(7, new java.sql.Timestamp(modifiedDate));
1014
            stmt.setString(8, submitter);
1015
            stmt.setString(9, objectFormat);
1016
            stmt.setString(10, size.toString());
1017
            stmt.setBoolean(11, archived);
1018
            stmt.setBoolean(12, replicationAllowed);
1019
            stmt.setInt(13, numberReplicas);
1020
            stmt.setString(14, obsoletes);
1021
            stmt.setString(15, obsoletedBy);
1022
            stmt.setString(16, serialVersion.toString());
1023

    
1024
            //where clause
1025
            stmt.setString(17, guid);
1026
            logMetacat.debug("stmt: " + stmt.toString());
1027
            //execute
1028
            int rows = stmt.executeUpdate();
1029

    
1030
            stmt.close();
1031
        } catch (SQLException e) {
1032
            e.printStackTrace();
1033
            logMetacat.error("updateSystemMetadataFields: SQL error while updating system metadata: " 
1034
                    + e.getMessage());
1035
        } catch (NumberFormatException e) {
1036
            e.printStackTrace();
1037
            logMetacat.error("updateSystemMetadataFields: NumberFormat error while updating system metadata: " 
1038
                    + e.getMessage());
1039
        } finally {
1040
            // Return database connection to the pool
1041
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1042
        }
1043
    }
1044
    
1045
    private void insertReplicationPolicy(String guid, String policy, List<String> memberNodes)
1046
    {
1047
        DBConnection dbConn = null;
1048
        int serialNumber = -1;
1049
        
1050
        try {
1051
            // Get a database connection from the pool
1052
            dbConn = 
1053
                DBConnectionPool.getDBConnection("IdentifierManager.insertReplicationPolicy");
1054
            serialNumber = dbConn.getCheckOutSerialNumber();
1055

    
1056
            // remove existing values first
1057
            String delete = "delete from systemMetadataReplicationPolicy " + 
1058
            "where guid = ? and policy = ?";
1059
	        PreparedStatement stmt = dbConn.prepareStatement(delete);
1060
	        //data values
1061
	        stmt.setString(1, guid);
1062
	        stmt.setString(2, policy);
1063
	        //execute
1064
	        int deletedCount = stmt.executeUpdate();
1065
	        stmt.close();
1066
            
1067
            for (String memberNode: memberNodes) {
1068
	            // Execute the insert statement
1069
	            String insert = "insert into systemMetadataReplicationPolicy " + 
1070
	                "(guid, policy, member_node) " +
1071
	                "values (?, ?, ?)";
1072
	            PreparedStatement insertStatement = dbConn.prepareStatement(insert);
1073
	            
1074
	            //data values
1075
	            insertStatement.setString(1, guid);
1076
	            insertStatement.setString(2, policy);
1077
	            insertStatement.setString(3, memberNode);
1078
	            //execute
1079
	            int rows = insertStatement.executeUpdate();
1080
	            insertStatement.close();
1081
            }
1082
        } catch (SQLException e) {
1083
            logMetacat.error("SQL error while adding systemMetadataReplicationPolicy for: " + guid, e); 
1084
        } finally {
1085
            // Return database connection to the pool
1086
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1087
        }
1088
    }
1089
    
1090
    private void insertReplicationStatus(String guid, List<Replica> replicas) {
1091
        DBConnection dbConn = null;
1092
        int serialNumber = -1;
1093
        
1094
        try {
1095
            // Get a database connection from the pool
1096
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.insertReplicas");
1097
            serialNumber = dbConn.getCheckOutSerialNumber();
1098

    
1099
            // remove existing values first
1100
            String delete = "delete from systemMetadataReplicationStatus " + 
1101
            "where guid = ?";
1102
	        PreparedStatement stmt = dbConn.prepareStatement(delete);
1103
	        //data values
1104
	        stmt.setString(1, guid);
1105
	        //execute
1106
	        int deletedCount = stmt.executeUpdate();
1107
	        stmt.close();
1108
            
1109
	        if (replicas != null) {
1110
	            for (Replica replica: replicas) {
1111
		            // Execute the insert statement
1112
		            String insert = "insert into systemMetadataReplicationStatus " + 
1113
		                "(guid, member_node, status, date_verified) " +
1114
		                "values (?, ?, ?, ?)";
1115
		            PreparedStatement insertStatement = dbConn.prepareStatement(insert);
1116
		            
1117
		            //data values
1118
		            String memberNode = replica.getReplicaMemberNode().getValue();
1119
		            String status = replica.getReplicationStatus().toString();
1120
		            java.sql.Date sqlDate = new java.sql.Date(replica.getReplicaVerified().getTime());
1121
		            insertStatement.setString(1, guid);
1122
		            insertStatement.setString(2, memberNode);
1123
		            insertStatement.setString(3, status);
1124
		            insertStatement.setDate(4, sqlDate);
1125
	
1126
		            //execute
1127
		            int rows = insertStatement.executeUpdate();
1128
		            insertStatement.close();
1129
	            }
1130
	        }
1131
        } catch (SQLException e) {
1132
            logMetacat.error("SQL error while adding systemMetadataReplicationStatus for: " + guid, e); 
1133
        } finally {
1134
            // Return database connection to the pool
1135
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1136
        }
1137
    }
1138
    
1139
    /**
1140
     * Insert the system metadata fields into the db
1141
     * @param sm
1142
     * @throws McdbDocNotFoundException 
1143
     */
1144
    public void updateSystemMetadata(SystemMetadata sm) throws McdbDocNotFoundException {
1145
    	
1146
        Boolean replicationAllowed = false;
1147
		Integer numberReplicas = -1;
1148
    	ReplicationPolicy replicationPolicy = sm.getReplicationPolicy();
1149
    	if (replicationPolicy != null) {
1150
    		replicationAllowed = replicationPolicy.getReplicationAllowed();
1151
    		numberReplicas = replicationPolicy.getNumberReplicas();
1152
    		replicationAllowed = replicationAllowed == null ? false: replicationAllowed;
1153
    		numberReplicas = numberReplicas == null ? -1: numberReplicas;
1154
    	}
1155

    
1156
    	// the main systemMetadata fields
1157
		updateSystemMetadataFields(
1158
				sm.getDateUploaded() == null ? null: sm.getDateUploaded().getTime(),
1159
				sm.getRightsHolder() == null ? null: sm.getRightsHolder().getValue(), 
1160
				sm.getChecksum() == null ? null: sm.getChecksum().getValue(), 
1161
				sm.getChecksum() == null ? null: sm.getChecksum().getAlgorithm(), 
1162
				sm.getOriginMemberNode() == null ? null: sm.getOriginMemberNode().getValue(),
1163
				sm.getAuthoritativeMemberNode() == null ? null: sm.getAuthoritativeMemberNode().getValue(), 
1164
				sm.getDateSysMetadataModified() == null ? null: sm.getDateSysMetadataModified().getTime(),
1165
				sm.getSubmitter() == null ? null: sm.getSubmitter().getValue(), 
1166
		        sm.getIdentifier().getValue(),
1167
		        sm.getFormatId() == null ? null: sm.getFormatId().getValue(),
1168
		        sm.getSize(),
1169
		        sm.getArchived() == null ? false: sm.getArchived(),
1170
		        replicationAllowed, 
1171
		        numberReplicas,
1172
		        sm.getObsoletes() == null ? null:sm.getObsoletes().getValue(),
1173
		        sm.getObsoletedBy() == null ? null: sm.getObsoletedBy().getValue(),
1174
		        sm.getSerialVersion()
1175
        );
1176
        
1177
        String guid = sm.getIdentifier().getValue();
1178
        
1179
        // save replication policies
1180
        if (replicationPolicy != null) {
1181
		    List<String> nodes = null;
1182
		    String policy = null;
1183
		    
1184
		    // check for null 
1185
		    if (replicationPolicy.getBlockedMemberNodeList() != null) {
1186
			    nodes = new ArrayList<String>();
1187
			    policy = "blocked";
1188
			    for (NodeReference node: replicationPolicy.getBlockedMemberNodeList()) {
1189
			    	nodes.add(node.getValue());
1190
			    }
1191
			    this.insertReplicationPolicy(guid, policy, nodes);
1192
		    }
1193
		    
1194
		    if (replicationPolicy.getPreferredMemberNodeList() != null) {
1195
			    nodes = new ArrayList<String>();
1196
			    policy = "preferred";
1197
			    for (NodeReference node: replicationPolicy.getPreferredMemberNodeList()) {
1198
			    	nodes.add(node.getValue());
1199
			    }
1200
		        this.insertReplicationPolicy(guid, policy, nodes);
1201
		    }
1202
        }
1203
        
1204
        // save replica information
1205
        this.insertReplicationStatus(guid, sm.getReplicaList());
1206
        
1207
        // save access policy
1208
        AccessPolicy accessPolicy = sm.getAccessPolicy();
1209
        if (accessPolicy != null) {
1210
        	try {
1211
				this.insertAccessPolicy(guid, accessPolicy);
1212
			} catch (AccessException e) {
1213
				throw new McdbDocNotFoundException(e);
1214
			}
1215
        }
1216
    }
1217
    
1218
    /**
1219
     * Creates Metacat access rules and inserts them
1220
     * @param accessPolicy
1221
     * @throws McdbDocNotFoundException
1222
     * @throws AccessException
1223
     */
1224
    private void insertAccessPolicy(String guid, AccessPolicy accessPolicy) throws McdbDocNotFoundException, AccessException {
1225
    	
1226
    	// check for the existing permOrder so that we remain compatible with it (DataONE does not care)
1227
        XMLAccessAccess accessController  = new XMLAccessAccess();
1228
		String existingPermOrder = AccessControlInterface.ALLOWFIRST;
1229
        Vector<XMLAccessDAO> existingAccess = accessController.getXMLAccessForDoc(guid);
1230
        if (existingAccess != null && existingAccess.size() > 0) {
1231
        	existingPermOrder = existingAccess.get(0).getPermOrder();
1232
        }
1233
        
1234
    	List<XMLAccessDAO> accessDAOs = new ArrayList<XMLAccessDAO>();
1235
        for (AccessRule accessRule: accessPolicy.getAllowList()) {
1236
        	List<Subject> subjects = accessRule.getSubjectList();
1237
        	List<Permission> permissions = accessRule.getPermissionList();
1238
        	for (Subject subject: subjects) {
1239
    			XMLAccessDAO accessDAO = new XMLAccessDAO();
1240
        		accessDAO.setPrincipalName(subject.getValue());
1241
    			accessDAO.setGuid(guid);
1242
    			accessDAO.setPermType(AccessControlInterface.ALLOW);
1243
				accessDAO.setPermOrder(existingPermOrder);
1244
    			if (permissions != null) {
1245
	    			for (Permission permission: permissions) {
1246
	    				Long metacatPermission = new Long(convertPermission(permission));
1247
	        			accessDAO.addPermission(metacatPermission);
1248
	    			}
1249
    			}
1250
    			accessDAOs.add(accessDAO);
1251
        	}
1252
        }
1253
        
1254
        
1255
        // remove all existing allow records
1256
        accessController.deleteXMLAccessForDoc(guid, AccessControlInterface.ALLOW);
1257
        // add the ones we can for this guid
1258
        accessController.insertAccess(guid, accessDAOs);
1259
        
1260
        
1261
    }
1262
    
1263
    /**
1264
     * Lookup access policy from Metacat
1265
     * @param guid
1266
     * @return
1267
     * @throws McdbDocNotFoundException
1268
     * @throws AccessException
1269
     */
1270
    public AccessPolicy getAccessPolicy(String guid) throws McdbDocNotFoundException, AccessException {
1271
        AccessPolicy accessPolicy = new AccessPolicy();
1272

    
1273
    	// use GUID to look up the access
1274
        XMLAccessAccess accessController  = new XMLAccessAccess();
1275
        List<XMLAccessDAO> accessDAOs = accessController.getXMLAccessForDoc(guid);
1276
        
1277
        for (XMLAccessDAO accessDAO: accessDAOs) {
1278
        	// only add allow rule
1279
        	if (accessDAO.getPermType().equals(AccessControlInterface.ALLOW)) {
1280
	        	AccessRule accessRule = new AccessRule();    	
1281
	        	List <Permission> permissions = convertPermission(accessDAO.getPermission().intValue());
1282
	        	accessRule.setPermissionList(permissions);
1283
	        	Subject subject = new Subject();
1284
	        	subject.setValue(accessDAO.getPrincipalName());
1285
	        	accessRule.addSubject(subject);
1286
	            accessPolicy.addAllow(accessRule);
1287
        	}
1288
        }
1289
        return accessPolicy;
1290
    }
1291
    
1292
    public int convertPermission(Permission permission) {
1293
    	if (permission.equals(Permission.READ)) {
1294
    		return AccessControlInterface.READ;
1295
    	}
1296
    	if (permission.equals(Permission.WRITE)) {
1297
    		return AccessControlInterface.WRITE;
1298
    	}
1299
    	if (permission.equals(Permission.CHANGE_PERMISSION)) {
1300
    		return AccessControlInterface.CHMOD;
1301
    	}
1302
		return -1;
1303
    }
1304
    
1305
    public List<Permission> convertPermission(int permission) {
1306
    	
1307
    	List<Permission> permissions = new ArrayList<Permission>();
1308
    	if (permission == AccessControlInterface.ALL) {
1309
    		permissions.add(Permission.READ);
1310
    		permissions.add(Permission.WRITE);
1311
    		permissions.add(Permission.CHANGE_PERMISSION);
1312
    		return permissions;
1313
    	}
1314
    	
1315
    	if ((permission & AccessControlInterface.CHMOD) == AccessControlInterface.CHMOD) {
1316
    		permissions.add(Permission.CHANGE_PERMISSION);
1317
    	}
1318
    	if ((permission & AccessControlInterface.READ) == AccessControlInterface.READ) {
1319
    		permissions.add(Permission.READ);
1320
    	}
1321
    	if ((permission & AccessControlInterface.WRITE) == AccessControlInterface.WRITE) {
1322
    		permissions.add(Permission.WRITE);
1323
    	}
1324
    	
1325
		return permissions;
1326
    }
1327
    
1328
    /**
1329
     * Lookup a localId given the GUID. If
1330
     * the identifier is not found, throw an exception.
1331
     * 
1332
     * @param guid the global identifier to look up
1333
     * @return String containing the corresponding LocalId
1334
     * @throws McdbDocNotFoundException if the identifier is not found
1335
     */
1336
    public String getLocalId(String guid) throws McdbDocNotFoundException {
1337
      
1338
      String db_guid = "";
1339
      String docid = "";
1340
      int rev = 0;
1341
      
1342
      String query = "select guid, docid, rev from " + TYPE_IDENTIFIER + " where guid = ?";
1343
      
1344
      DBConnection dbConn = null;
1345
      int serialNumber = -1;
1346
      try {
1347
          // Get a database connection from the pool
1348
          dbConn = DBConnectionPool.getDBConnection("Identifier.getLocalId");
1349
          serialNumber = dbConn.getCheckOutSerialNumber();
1350
          
1351
          // Execute the insert statement
1352
          PreparedStatement stmt = dbConn.prepareStatement(query);
1353
          stmt.setString(1, guid);
1354
          ResultSet rs = stmt.executeQuery();
1355
          if (rs.next()) {
1356
              db_guid = rs.getString(1);
1357
              docid = rs.getString(2);
1358
              rev = rs.getInt(3);
1359
              assert(db_guid.equals(guid));
1360
          } else {
1361
              throw new McdbDocNotFoundException("Document not found:" + guid);
1362
          }
1363
          stmt.close();
1364
      } catch (SQLException e) {
1365
          logMetacat.error("Error while looking up the local identifier: " 
1366
                  + e.getMessage());
1367
      } finally {
1368
          // Return database connection to the pool
1369
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1370
      }
1371
      return docid + "." + rev;
1372
    }
1373
    
1374
    /**
1375
     * query the systemmetadata table based on the given parameters
1376
     * @param startTime
1377
     * @param endTime
1378
     * @param objectFormat
1379
     * @param replicaStatus
1380
     * @param start
1381
     * @param count
1382
     * @return ObjectList
1383
     * @throws SQLException 
1384
     * @throws ServiceException 
1385
     * @throws PropertyNotFoundException 
1386
     */
1387
    public ObjectList querySystemMetadata(Date startTime, Date endTime,
1388
        ObjectFormatIdentifier objectFormatId, boolean replicaStatus,
1389
        int start, int count) 
1390
        throws SQLException, PropertyNotFoundException, ServiceException {
1391
        ObjectList ol = new ObjectList();
1392
        int total = 0;
1393
        DBConnection dbConn = null;
1394
        int serialNumber = -1;
1395

    
1396
        try {
1397
            String sql = "select guid, date_uploaded, rights_holder, checksum, "
1398
                    + "checksum_algorithm, origin_member_node, authoritive_member_node, "
1399
                    + "date_modified, submitter, object_format, size from systemmetadata";
1400

    
1401
            boolean f1 = false;
1402
            boolean f2 = false;
1403
            boolean f3 = false;
1404

    
1405
            if (startTime != null) {
1406
                sql += " where systemmetadata.date_modified >= ?";
1407
                f1 = true;
1408
            }
1409

    
1410
            if (endTime != null) {
1411
                if (!f1) {
1412
                    sql += " where systemmetadata.date_modified < ?";
1413
                } else {
1414
                    sql += " and systemmetadata.date_modified < ?";
1415
                }
1416
                f2 = true;
1417
            }
1418

    
1419
            if (objectFormatId != null) {
1420
                if (!f1 && !f2) {
1421
                    sql += " where object_format = ?";
1422
                } else {
1423
                    sql += " and object_format = ?";
1424
                }
1425
                f3 = true;
1426
            }
1427

    
1428
            if (replicaStatus) {
1429
                String currentNodeId = PropertyService.getInstance().getProperty("dataone.memberNodeId");
1430
                if (!f1 && !f2 && !f3) {
1431
                    sql += " where authoritive_member_node != '" +
1432
                        currentNodeId.trim() + "'";
1433
                } else {
1434
                    sql += " and authoritive_member_node != '" +
1435
                        currentNodeId.trim() + "'";
1436
                }
1437
            }
1438

    
1439
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.querySystemMetadata");
1440
            serialNumber = dbConn.getCheckOutSerialNumber();
1441
            PreparedStatement stmt = dbConn.prepareStatement(sql);
1442

    
1443
            if (f1 && f2 && f3) {
1444
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1445
                stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
1446
                stmt.setString(3, objectFormatId.getValue());
1447
            } else if (f1 && f2 && !f3) {
1448
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1449
                stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
1450
            } else if (f1 && !f2 && f3) {
1451
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1452
                stmt.setString(2, objectFormatId.getValue());
1453
            } else if (f1 && !f2 && !f3) {
1454
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1455
            } else if (!f1 && f2 && f3) {
1456
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
1457
                stmt.setString(2, objectFormatId.getValue());
1458
            } else if (!f1 && !f2 && f3) {
1459
                stmt.setString(1, objectFormatId.getValue());
1460
            } else if (!f1 && f2 && !f3) {
1461
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
1462
            }
1463

    
1464
            // logMetacat.debug("LISTOBJECTS QUERY: " + stmt.toString());
1465

    
1466
            ResultSet rs = stmt.executeQuery();
1467
            for (int i = 0; i < start; i++) {
1468
                if (rs.next()) {
1469
                    total++;
1470
                } else {
1471
                    break;
1472
                }
1473
            }
1474

    
1475
            int countIndex = 0;
1476

    
1477
            while (rs.next()) {                
1478
                total++;
1479
                if (countIndex >= count) {
1480
                    // allow unlimited (negative number for count)
1481
                    if (count > 0) {
1482
                        break;
1483
                    }
1484
                }
1485

    
1486
                String guid = rs.getString(1);
1487
                // logMetacat.debug("query found doc with guid " + guid);
1488
                // Timestamp dateUploaded = rs.getTimestamp(2);
1489
                // String rightsHolder = rs.getString(3);
1490
                String checksum = rs.getString(4);
1491
                String checksumAlgorithm = rs.getString(5);
1492
                // String originMemberNode = rs.getString(6);
1493
                // String authoritiveMemberNode = rs.getString(7);
1494
                Timestamp dateModified = rs.getTimestamp(8);
1495
                // String submitter = rs.getString(9);
1496
                String fmtidStr = rs.getString(10);
1497
                String sz = rs.getString(11);
1498
                BigInteger size = new BigInteger("0");
1499

    
1500
                if (sz != null && !sz.trim().equals("")) {
1501
                    size = new BigInteger(rs.getString(11));
1502
                }
1503

    
1504
                ObjectInfo oi = new ObjectInfo();
1505

    
1506
                Identifier id = new Identifier();
1507
                id.setValue(guid);
1508
                oi.setIdentifier(id);
1509

    
1510
                if (dateModified != null) {
1511
                    oi.setDateSysMetadataModified(new Date(dateModified.getTime()));
1512
                }
1513

    
1514
                Checksum cs = new Checksum();
1515
                cs.setValue(checksum);
1516
                try {
1517
                    // cs.setAlgorithm(ChecksumAlgorithm.valueOf(checksumAlgorithm));
1518
                    cs.setAlgorithm(checksumAlgorithm);
1519
                } catch (Exception e) {
1520
                    logMetacat.error("could not parse checksum algorithm", e);
1521
                    continue;
1522
                }
1523
                oi.setChecksum(cs);
1524

    
1525
                // set the format type
1526
                ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
1527
                fmtid.setValue(fmtidStr);
1528
                oi.setFormatId(fmtid);
1529

    
1530
                oi.setSize(size);
1531

    
1532
                // when requested count == 0, return an empty object list
1533
                if (count != 0) {
1534
                    ol.addObjectInfo(oi);                    
1535
                }
1536
                countIndex++;
1537
            }
1538

    
1539
            // expend the resultset to get the total count of possible rows
1540
            while (rs.next()) {
1541
                total++;
1542
            }
1543
        }
1544

    
1545
        finally {
1546
            // Return database connection to the pool
1547
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1548
        }
1549

    
1550
        ol.setStart(start);
1551
        ol.setCount(ol.sizeObjectInfoList());
1552
        ol.setTotal(total);
1553

    
1554
        return ol;
1555
    }
1556
    
1557
    /**
1558
     * create a mapping in the identifier table
1559
     * @param guid
1560
     * @param localId
1561
     */
1562
    public void createMapping(String guid, String localId)
1563
    {        
1564
        
1565
        int serialNumber = -1;
1566
        DBConnection dbConn = null;
1567
        try {
1568

    
1569
            // Parse the localId into scope and rev parts
1570
            AccessionNumber acc = new AccessionNumber(localId, "NOACTION");
1571
            String docid = acc.getDocid();
1572
            int rev = 1;
1573
            if (acc.getRev() != null) {
1574
              rev = (new Integer(acc.getRev()).intValue());
1575
            }
1576

    
1577
            // Get a database connection from the pool
1578
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
1579
            serialNumber = dbConn.getCheckOutSerialNumber();
1580

    
1581
            // Execute the insert statement
1582
            String query = "insert into " + TYPE_IDENTIFIER + " (guid, docid, rev) values (?, ?, ?)";
1583
            PreparedStatement stmt = dbConn.prepareStatement(query);
1584
            stmt.setString(1, guid);
1585
            stmt.setString(2, docid);
1586
            stmt.setInt(3, rev);
1587
            logMetacat.debug("mapping query: " + stmt.toString());
1588
            int rows = stmt.executeUpdate();
1589

    
1590
            stmt.close();
1591
        } catch (SQLException e) {
1592
            e.printStackTrace();
1593
            logMetacat.error("createGenericMapping: SQL error while creating a mapping to the " + TYPE_IDENTIFIER + " identifier: " 
1594
                    + e.getMessage());
1595
        } catch (NumberFormatException e) {
1596
            e.printStackTrace();
1597
            logMetacat.error("createGenericMapping: NumberFormat error while creating a mapping to the " + TYPE_IDENTIFIER + " identifier: " 
1598
                    + e.getMessage());
1599
        } catch (AccessionNumberException e) {
1600
            e.printStackTrace();
1601
            logMetacat.error("createGenericMapping: AccessionNumber error while creating a mapping to the " + TYPE_IDENTIFIER + " identifier: " 
1602
                    + e.getMessage());
1603
        } finally {
1604
            // Return database connection to the pool
1605
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1606
        }
1607
    }
1608
    
1609
    /**
1610
     * create the systemmetadata record
1611
     * @param guid
1612
     */
1613
    private void insertSystemMetadata(String guid)
1614
    {        
1615
        
1616
        int serialNumber = -1;
1617
        DBConnection dbConn = null;
1618
        try {
1619

    
1620
            // Get a database connection from the pool
1621
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.insertSystemMetadata");
1622
            serialNumber = dbConn.getCheckOutSerialNumber();
1623

    
1624
            // Execute the insert statement
1625
            String query = "insert into " + TYPE_SYSTEM_METADATA + " (guid) values (?)";
1626
            PreparedStatement stmt = dbConn.prepareStatement(query);
1627
            stmt.setString(1, guid);
1628
            logMetacat.debug("system metadata query: " + stmt.toString());
1629
            int rows = stmt.executeUpdate();
1630

    
1631
            stmt.close();
1632
        } catch (Exception e) {
1633
            e.printStackTrace();
1634
            logMetacat.error("Error while creating " + TYPE_SYSTEM_METADATA + " record: " + guid, e );
1635
        } finally {
1636
            // Return database connection to the pool
1637
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1638
        }
1639
    }
1640
    
1641
    public void deleteSystemMetadata(String guid)
1642
    {        
1643
        
1644
        int serialNumber = -1;
1645
        DBConnection dbConn = null;
1646
        String query = null;
1647
        PreparedStatement stmt = null;
1648
        int rows = 0;
1649
        try {
1650

    
1651
            // Get a database connection from the pool
1652
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.deleteSystemMetadata");
1653
            serialNumber = dbConn.getCheckOutSerialNumber();
1654

    
1655
            // remove main system metadata entry
1656
            query = "delete from " + TYPE_SYSTEM_METADATA + " where guid = ? ";
1657
            stmt = dbConn.prepareStatement(query);
1658
            stmt.setString(1, guid);
1659
            logMetacat.debug("delete system metadata: " + stmt.toString());
1660
            rows = stmt.executeUpdate();
1661
            stmt.close();
1662
            
1663
            // remove the systemMetadataReplicationPolicy
1664
            query = "delete from systemMetadataReplicationPolicy " + 
1665
            "where guid = ?";
1666
            stmt = dbConn.prepareStatement(query);
1667
            stmt.setString(1, guid);
1668
            logMetacat.debug("delete systemMetadataReplicationPolicy: " + stmt.toString());
1669
            rows = stmt.executeUpdate();
1670
            stmt.close();
1671
            
1672
            // remove the systemMetadataReplicationStatus
1673
            query = "delete from systemMetadataReplicationStatus " + 
1674
            "where guid = ?";
1675
            stmt = dbConn.prepareStatement(query);
1676
            stmt.setString(1, guid);
1677
            logMetacat.debug("delete systemMetadataReplicationStatus: " + stmt.toString());
1678
            rows = stmt.executeUpdate();
1679
            stmt.close();
1680
            
1681
            // TODO: remove the access?
1682
            // Metacat keeps "deleted" documents so we should not remove access rules.
1683
            
1684
        } catch (Exception e) {
1685
            e.printStackTrace();
1686
            logMetacat.error("Error while deleting " + TYPE_SYSTEM_METADATA + " record: " + guid, e );
1687
            try {
1688
				dbConn.rollback();
1689
			} catch (SQLException sqle) {
1690
	            logMetacat.error("Error while rolling back delete for record: " + guid, sqle );
1691
			}
1692
        } finally {
1693
            // Return database connection to the pool
1694
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1695
        }
1696
    }
1697
    
1698
    public void updateAuthoritativeMemberNodeId(String existingMemberNodeId, String newMemberNodeId)
1699
    {
1700
        DBConnection dbConn = null;
1701
        int serialNumber = -1;
1702
        
1703
        try {
1704
            // Get a database connection from the pool
1705
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.updateAuthoritativeMemberNodeId");
1706
            serialNumber = dbConn.getCheckOutSerialNumber();
1707

    
1708
            // Execute the insert statement
1709
            String query = "update " + TYPE_SYSTEM_METADATA + 
1710
                " set authoritive_member_node = ? " +
1711
                " where authoritive_member_node = ?";
1712
            PreparedStatement stmt = dbConn.prepareStatement(query);
1713
            
1714
            //data values
1715
            stmt.setString(1, newMemberNodeId);
1716
            stmt.setString(2, existingMemberNodeId);
1717

    
1718
            logMetacat.debug("stmt: " + stmt.toString());
1719
            //execute
1720
            int rows = stmt.executeUpdate();
1721

    
1722
            stmt.close();
1723
        } catch (SQLException e) {
1724
            e.printStackTrace();
1725
            logMetacat.error("updateSystemMetadataFields: SQL error while updating system metadata: " 
1726
                    + e.getMessage());
1727
        } catch (NumberFormatException e) {
1728
            e.printStackTrace();
1729
            logMetacat.error("updateSystemMetadataFields: NumberFormat error while updating system metadata: " 
1730
                    + e.getMessage());
1731
        } finally {
1732
            // Return database connection to the pool
1733
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1734
        }
1735
    }
1736
}
1737

    
(37-37/64)