Project

General

Profile

1 5282 jones
/**
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 6384 cjones
import java.math.BigInteger;
28 5282 jones
import java.sql.PreparedStatement;
29
import java.sql.ResultSet;
30
import java.sql.SQLException;
31 5895 berkley
import java.sql.Timestamp;
32 6107 leinfelder
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 5282 jones
38
import org.apache.log4j.Logger;
39 6124 cjones
import org.dataone.client.ObjectFormatCache;
40
import org.dataone.service.exceptions.NotFound;
41 6366 leinfelder
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.ObjectFormat;
47
import org.dataone.service.types.v1.ObjectFormatIdentifier;
48
import org.dataone.service.types.v1.ObjectInfo;
49
import org.dataone.service.types.v1.ObjectList;
50
import org.dataone.service.types.v1.Permission;
51
import org.dataone.service.types.v1.Replica;
52
import org.dataone.service.types.v1.ReplicationPolicy;
53
import org.dataone.service.types.v1.ReplicationStatus;
54
import org.dataone.service.types.v1.Subject;
55
import org.dataone.service.types.v1.SystemMetadata;
56 5282 jones
57 6108 leinfelder
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
58
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessAccess;
59
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
60 5282 jones
import edu.ucsb.nceas.metacat.database.DBConnection;
61
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
62 5887 berkley
import edu.ucsb.nceas.metacat.properties.PropertyService;
63 6108 leinfelder
import edu.ucsb.nceas.metacat.shared.AccessException;
64 6299 leinfelder
import edu.ucsb.nceas.metacat.shared.ServiceException;
65 5286 jones
import edu.ucsb.nceas.metacat.util.DocumentUtil;
66 6299 leinfelder
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
67 5282 jones
68
/**
69
 * Manage the relationship between Metacat local identifiers (LocalIDs) that are
70 6099 leinfelder
 * codified as the (docid, rev) pair with globally unique string identifiers
71 5282 jones
 * (GUIDs) that are opaque strings.  This class provides methods to manage these
72
 * identifiers, and to search for and look up LocalIDs based on their GUID and
73
 * vice versa. IdentifierManager is a singleton.
74
 *
75
 * @author Matthew Jones
76
 */
77
public class IdentifierManager {
78
79 5334 berkley
    public static final String TYPE_SYSTEM_METADATA = "systemmetadata";
80
    public static final String TYPE_IDENTIFIER = "identifier";
81
82 5282 jones
    /**
83
     * The single instance of the manager that is always returned.
84
     */
85
    private static IdentifierManager self = null;
86 6099 leinfelder
    private Logger logMetacat = Logger.getLogger(IdentifierManager.class);
87 5282 jones
88
    /**
89
     * A private constructor that initializes the class when getInstance() is
90
     * called.
91
     */
92 6099 leinfelder
    private IdentifierManager() {}
93 5282 jones
94
    /**
95
     * Return the single instance of the manager after initializing it if it
96
     * wasn't previously initialized.
97
     *
98
     * @return the single IdentifierManager instance
99
     */
100
    public static IdentifierManager getInstance()
101
    {
102
        if (self == null) {
103
            self = new IdentifierManager();
104
        }
105
        return self;
106
    }
107
108 6097 leinfelder
    public SystemMetadata asSystemMetadata(Date dateUploaded, String rightsHolder,
109
            String checksum, String checksumAlgorithm, String originMemberNode,
110
            String authoritativeMemberNode, Date dateModified, String submitter,
111 6561 leinfelder
            String guid, String fmtidStr, BigInteger size, BigInteger serialVersion) {
112 6097 leinfelder
        SystemMetadata sysMeta = new SystemMetadata();
113
114
        Identifier sysMetaId = new Identifier();
115
        sysMetaId.setValue(guid);
116
        sysMeta.setIdentifier(sysMetaId);
117
        sysMeta.setDateUploaded(dateUploaded);
118
        Subject rightsHolderSubject = new Subject();
119
        rightsHolderSubject.setValue(rightsHolder);
120
        sysMeta.setRightsHolder(rightsHolderSubject);
121
        Checksum checksumObject = new Checksum();
122
        checksumObject.setValue(checksum);
123 6397 leinfelder
        checksumObject.setAlgorithm(checksumAlgorithm);
124 6097 leinfelder
        sysMeta.setChecksum(checksumObject);
125
        NodeReference omn = new NodeReference();
126
        omn.setValue(originMemberNode);
127
        sysMeta.setOriginMemberNode(omn);
128
        NodeReference amn = new NodeReference();
129
        amn.setValue(authoritativeMemberNode);
130
        sysMeta.setAuthoritativeMemberNode(amn);
131
        sysMeta.setDateSysMetadataModified(dateModified);
132
        Subject submitterSubject = new Subject();
133
        submitterSubject.setValue(submitter);
134
        sysMeta.setSubmitter(submitterSubject);
135 6124 cjones
        try {
136 6384 cjones
        	ObjectFormatIdentifier fmtid =
137 6561 leinfelder
        		ObjectFormatCache.getInstance().getFormat(fmtidStr).getFormatId();
138
        	sysMeta.setFormatId(fmtid);
139 6124 cjones
        } catch (NotFound nfe) {
140 6296 cjones
          logMetacat.error("The objectFormat " + fmtidStr +
141 6384 cjones
          	" is not registered. Setting the default format id.");
142 6124 cjones
          ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
143
          fmtid.setValue("application/octet-stream");
144 6561 leinfelder
          sysMeta.setFormatId(fmtid);
145 6124 cjones
        }
146 6097 leinfelder
        sysMeta.setSize(size);
147 6561 leinfelder
        sysMeta.setSerialVersion(serialVersion);
148 6097 leinfelder
149
        return sysMeta;
150
    }
151
152 5282 jones
    /**
153 5895 berkley
     * return a hash of all of the info that is in the systemmetadata table
154
     * @param localId
155
     * @return
156
     */
157
    public Hashtable<String, String> getSystemMetadataInfo(String localId)
158
    throws McdbDocNotFoundException
159
    {
160
        try
161
        {
162
            AccessionNumber acc = new AccessionNumber(localId, "NONE");
163
            localId = acc.getDocid();
164
        }
165
        catch(Exception e)
166
        {
167
            //do nothing. just try the localId as it is
168
        }
169
        Hashtable<String, String> h = new Hashtable<String, String>();
170
        String sql = "select guid, date_uploaded, rights_holder, checksum, checksum_algorithm, " +
171 5944 berkley
          "origin_member_node, authoritive_member_node, date_modified, submitter, object_format, size " +
172 5895 berkley
          "from systemmetadata where docid = ?";
173
        DBConnection dbConn = null;
174
        int serialNumber = -1;
175
        try
176
        {
177
            // Get a database connection from the pool
178
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getDocumentInfo");
179
            serialNumber = dbConn.getCheckOutSerialNumber();
180
181
            // Execute the insert statement
182
            PreparedStatement stmt = dbConn.prepareStatement(sql);
183
            stmt.setString(1, localId);
184
            ResultSet rs = stmt.executeQuery();
185
            if (rs.next())
186
            {
187
                String guid = rs.getString(1);
188
                Timestamp dateUploaded = rs.getTimestamp(2);
189
                String rightsHolder = rs.getString(3);
190
                String checksum = rs.getString(4);
191
                String checksumAlgorithm = rs.getString(5);
192
                String originMemberNode = rs.getString(6);
193
                String authoritativeMemberNode = rs.getString(7);
194
                Timestamp dateModified = rs.getTimestamp(8);
195
                String submitter = rs.getString(9);
196 5917 berkley
                String objectFormat = rs.getString(10);
197
                long size = new Long(rs.getString(11)).longValue();
198 5895 berkley
199
                h.put("guid", guid);
200
                h.put("date_uploaded", new Long(dateUploaded.getTime()).toString());
201
                h.put("rights_holder", rightsHolder);
202
                h.put("checksum", checksum);
203
                h.put("checksum_algorithm", checksumAlgorithm);
204
                h.put("origin_member_node", originMemberNode);
205
                h.put("authoritative_member_node", authoritativeMemberNode);
206
                h.put("date_modified", new Long(dateModified.getTime()).toString());
207
                h.put("submitter", submitter);
208 5917 berkley
                h.put("object_format", objectFormat);
209
                h.put("size", new Long(size).toString());
210 5895 berkley
211
                stmt.close();
212
            }
213
            else
214
            {
215
                stmt.close();
216
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
217
                throw new McdbDocNotFoundException("2Could not find document " + localId);
218
            }
219
220
        }
221
        catch (SQLException e)
222
        {
223
            e.printStackTrace();
224
            logMetacat.error("Error while getting system metadata info for localid " + localId + " : "
225
                    + e.getMessage());
226
        }
227
        finally
228
        {
229
            // Return database connection to the pool
230
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
231
        }
232
        return h;
233
    }
234
235
    /**
236 6097 leinfelder
     * return a hash of all of the info that is in the systemmetadata table
237 6099 leinfelder
     * @param guid
238 6097 leinfelder
     * @return
239 6108 leinfelder
     * @throws McdbDocNotFoundException
240 6097 leinfelder
     */
241 6099 leinfelder
    public SystemMetadata getSystemMetadata(String guid)
242 6097 leinfelder
    	throws McdbDocNotFoundException
243
    {
244 6099 leinfelder
245 6097 leinfelder
        SystemMetadata sysMeta = new SystemMetadata();
246
        String sql = "select guid, date_uploaded, rights_holder, checksum, checksum_algorithm, " +
247 6375 leinfelder
          "origin_member_node, authoritive_member_node, date_modified, submitter, object_format, size, " +
248 6561 leinfelder
          "replication_allowed, number_replicas, obsoletes, obsoleted_by, serial_version " +
249 6099 leinfelder
          "from systemmetadata where guid = ?";
250 6097 leinfelder
        DBConnection dbConn = null;
251
        int serialNumber = -1;
252 6533 cjones
        Boolean replicationAllowed = new Boolean(false);
253
        BigInteger numberOfReplicas = new BigInteger("-1");
254 6561 leinfelder
        BigInteger serialVersion = new BigInteger("-1");
255
256 6097 leinfelder
        try
257
        {
258
            // Get a database connection from the pool
259
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getSystemMetadata");
260
            serialNumber = dbConn.getCheckOutSerialNumber();
261
262
            // Execute the statement
263
            PreparedStatement stmt = dbConn.prepareStatement(sql);
264 6099 leinfelder
            stmt.setString(1, guid);
265 6097 leinfelder
            ResultSet rs = stmt.executeQuery();
266
            if (rs.next())
267
            {
268
                Timestamp dateUploaded = rs.getTimestamp(2);
269
                String rightsHolder = rs.getString(3);
270
                String checksum = rs.getString(4);
271
                String checksumAlgorithm = rs.getString(5);
272
                String originMemberNode = rs.getString(6);
273
                String authoritativeMemberNode = rs.getString(7);
274
                Timestamp dateModified = rs.getTimestamp(8);
275
                String submitter = rs.getString(9);
276 6296 cjones
                String fmtidStr = rs.getString(10);
277 6384 cjones
                BigInteger size = new BigInteger(rs.getString(11));
278 6535 cjones
                replicationAllowed = new Boolean(rs.getBoolean(12));
279 6533 cjones
                numberOfReplicas = new BigInteger(rs.getString(13));
280
                String obsoletes = rs.getString(14);
281
                String obsoletedBy = rs.getString(15);
282 6561 leinfelder
                serialVersion = new BigInteger(rs.getString(16));
283 6375 leinfelder
284 6561 leinfelder
285 6097 leinfelder
                Identifier sysMetaId = new Identifier();
286
                sysMetaId.setValue(guid);
287
                sysMeta.setIdentifier(sysMetaId);
288 6561 leinfelder
                sysMeta.setSerialVersion(serialVersion);
289 6097 leinfelder
                sysMeta.setDateUploaded(dateUploaded);
290
                Subject rightsHolderSubject = new Subject();
291
                rightsHolderSubject.setValue(rightsHolder);
292
                sysMeta.setRightsHolder(rightsHolderSubject);
293
                Checksum checksumObject = new Checksum();
294
                checksumObject.setValue(checksum);
295 6397 leinfelder
                checksumObject.setAlgorithm(checksumAlgorithm);
296 6097 leinfelder
                sysMeta.setChecksum(checksumObject);
297 6519 leinfelder
                if (originMemberNode != null) {
298
	                NodeReference omn = new NodeReference();
299
	                omn.setValue(originMemberNode);
300
	                sysMeta.setOriginMemberNode(omn);
301
                }
302
                if (authoritativeMemberNode != null) {
303
	                NodeReference amn = new NodeReference();
304
	                amn.setValue(authoritativeMemberNode);
305
	                sysMeta.setAuthoritativeMemberNode(amn);
306
                }
307 6097 leinfelder
                sysMeta.setDateSysMetadataModified(dateModified);
308
                Subject submitterSubject = new Subject();
309
                submitterSubject.setValue(submitter);
310
                sysMeta.setSubmitter(submitterSubject);
311 6124 cjones
                try {
312 6384 cjones
                	ObjectFormatIdentifier fmtid =
313 6561 leinfelder
                		ObjectFormatCache.getInstance().getFormat(fmtidStr).getFormatId();
314
                	sysMeta.setFormatId(fmtid);
315 6124 cjones
                } catch (NotFound nfe) {
316 6296 cjones
                  logMetacat.error("The objectFormat " + fmtidStr +
317 6384 cjones
                  	" is not registered. Setting the default format id.");
318 6124 cjones
                  ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
319
                  fmtid.setValue("application/octet-stream");
320 6561 leinfelder
                  sysMeta.setFormatId(fmtid);
321 6124 cjones
                }
322 6097 leinfelder
                sysMeta.setSize(size);
323 6423 leinfelder
                if (obsoletes != null) {
324
	                Identifier obsoletesId = new Identifier();
325
	                obsoletesId.setValue(obsoletes);
326
	                sysMeta.setObsoletes(obsoletesId);
327
                }
328
                if (obsoletedBy != null) {
329
		            Identifier obsoletedById = new Identifier();
330
		            obsoletedById.setValue(obsoletedBy);
331
		            sysMeta.setObsoletedBy(obsoletedById);
332
                }
333 6097 leinfelder
                stmt.close();
334
            }
335
            else
336
            {
337
                stmt.close();
338
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
339 6099 leinfelder
                throw new McdbDocNotFoundException("Could not find " + guid);
340 6097 leinfelder
            }
341
342
        }
343
        catch (SQLException e)
344
        {
345
            e.printStackTrace();
346 6099 leinfelder
            logMetacat.error("Error while getting system metadata for guid " + guid + " : "
347 6097 leinfelder
                    + e.getMessage());
348
        }
349
        finally
350
        {
351
            // Return database connection to the pool
352
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
353
        }
354 6377 leinfelder
355 6533 cjones
        // populate the replication policy
356 6107 leinfelder
        ReplicationPolicy replicationPolicy = new ReplicationPolicy();
357 6533 cjones
        if ( numberOfReplicas != null  && numberOfReplicas.intValue() != -1 ) {
358
            replicationPolicy.setNumberReplicas(numberOfReplicas.intValue());
359
360
        }
361
362
        if ( replicationAllowed != null ) {
363
            replicationPolicy.setReplicationAllowed(replicationAllowed);
364
365
        }
366 6107 leinfelder
        replicationPolicy.setBlockedMemberNodeList(getReplicationPolicy(guid, "blocked"));
367
        replicationPolicy.setPreferredMemberNodeList(getReplicationPolicy(guid, "preferred"));
368 6533 cjones
		    sysMeta.setReplicationPolicy(replicationPolicy);
369 6107 leinfelder
370 6533 cjones
		    // look up replication status
371
		    sysMeta.setReplicaList(getReplicationStatus(guid));
372 6108 leinfelder
373 6533 cjones
		    // look up access policy
374
		    try {
375
		    	sysMeta.setAccessPolicy(getAccessPolicy(guid));
376
		    } catch (AccessException e) {
377
		    	throw new McdbDocNotFoundException(e);
378
		    }
379 6107 leinfelder
380 6097 leinfelder
        return sysMeta;
381
    }
382
383
384 6107 leinfelder
    private List<NodeReference> getReplicationPolicy(String guid, String policy)
385
		throws McdbDocNotFoundException {
386
387
		List<NodeReference> nodes = new ArrayList<NodeReference>();
388
		String sql = "select guid, policy, member_node " +
389
			"from systemMetadataReplicationPolicy where guid = ? and policy = ?";
390
	    DBConnection dbConn = null;
391
	    int serialNumber = -1;
392
	    try {
393
	        // Get a database connection from the pool
394
	        dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getReplicationPolicy");
395
	        serialNumber = dbConn.getCheckOutSerialNumber();
396
397
	        // Execute the statement
398
	        PreparedStatement stmt = dbConn.prepareStatement(sql);
399
	        stmt.setString(1, guid);
400
	        stmt.setString(2, policy);
401
	        ResultSet rs = stmt.executeQuery();
402
	        while (rs.next())
403
	        {
404
	            String memberNode = rs.getString(3);
405
	            NodeReference node = new NodeReference();
406
	            node.setValue(memberNode);
407
	            nodes.add(node);
408
409
	        }
410
	        stmt.close();
411
412
	    } catch (SQLException e) {
413
	        logMetacat.error("Error while getting system metadata replication policy for guid " + guid, e);
414
	    }
415
	    finally {
416
	        // Return database connection to the pool
417
	        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
418
	    }
419
420
	    return nodes;
421
	}
422 6097 leinfelder
423 6107 leinfelder
    private List<Replica> getReplicationStatus(String guid) throws McdbDocNotFoundException {
424
425
		List<Replica> replicas = new ArrayList<Replica>();
426
		String sql = "select guid, member_node, status, date_verified " +
427
			"from systemMetadataReplicationStatus where guid = ?";
428
	    DBConnection dbConn = null;
429
	    int serialNumber = -1;
430
	    try {
431
	        // Get a database connection from the pool
432
	        dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getReplicas");
433
	        serialNumber = dbConn.getCheckOutSerialNumber();
434
435
	        // Execute the statement
436
	        PreparedStatement stmt = dbConn.prepareStatement(sql);
437
	        stmt.setString(1, guid);
438
	        ResultSet rs = stmt.executeQuery();
439
	        while (rs.next())
440
	        {
441
	            String memberNode = rs.getString(2);
442
	            String status = rs.getString(3);
443
	            java.sql.Date verified = rs.getDate(4);
444
445
	            Replica replica = new Replica();
446
	            NodeReference node = new NodeReference();
447
	            node.setValue(memberNode);
448
	            replica.setReplicaMemberNode(node);
449 6454 leinfelder
	            replica.setReplicationStatus(ReplicationStatus.valueOf(status));
450 6107 leinfelder
	            replica.setReplicaVerified(new Date(verified.getTime()));
451
	            replicas.add(replica);
452
	        }
453
	        stmt.close();
454
455
	    } catch (SQLException e) {
456
	        logMetacat.error("Error while getting system metadata replication policy for guid " + guid, e);
457
	    }
458
	    finally {
459
	        // Return database connection to the pool
460
	        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
461
	    }
462
463
	    return replicas;
464
	}
465
466 6097 leinfelder
    /**
467 5378 berkley
     * return information on the document with localId.  These are the fields
468
     * from the xml_documents table.  They can be used to contstruct metadata
469
     * about the object that is stored.
470
     * @param localId
471
     * @return
472
     * @throws McdbDocNotFoundException
473
     */
474 5441 berkley
    public Hashtable<String, Object> getDocumentInfo(String localId)
475 5378 berkley
        throws McdbDocNotFoundException
476
    {
477 5798 berkley
        try
478
        {
479
            AccessionNumber acc = new AccessionNumber(localId, "NONE");
480
            localId = acc.getDocid();
481
        }
482
        catch(Exception e)
483
        {
484
            //do nothing. just try the localId as it is
485
        }
486 5441 berkley
        Hashtable<String, Object> h = new Hashtable<String, Object>();
487 5378 berkley
        String sql = "select docname, doctype, user_owner, user_updated, " +
488
            "server_location, rev, date_created, date_updated from " +
489 6595 leinfelder
            "xml_documents where docid like ?";
490 5378 berkley
        DBConnection dbConn = null;
491
        int serialNumber = -1;
492
        try
493
        {
494
            // Get a database connection from the pool
495
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getDocumentInfo");
496
            serialNumber = dbConn.getCheckOutSerialNumber();
497
498
            // Execute the insert statement
499
            PreparedStatement stmt = dbConn.prepareStatement(sql);
500 6595 leinfelder
            stmt.setString(1, localId);
501 5378 berkley
            ResultSet rs = stmt.executeQuery();
502
            if (rs.next())
503
            {
504
                String docname = rs.getString(1);
505
                String doctype = rs.getString(2);
506
                String user_owner = rs.getString(3);
507
                String user_updated = rs.getString(4);
508
                String server_location = rs.getString(5);
509
                int rev = rs.getInt(6);
510
                String date_created = rs.getString(7);
511
                String date_updated = rs.getString(8);
512 6042 cjones
                h.put("docid", localId);
513 5378 berkley
                h.put("docname", docname);
514
                h.put("doctype", doctype);
515
                h.put("user_owner", user_owner);
516
                h.put("user_updated", user_updated);
517
                h.put("server_location", server_location);
518
                h.put("rev", new Integer(rev).toString());
519
                h.put("date_created", date_created);
520
                h.put("date_updated", date_updated);
521
522
                stmt.close();
523
            }
524
            else
525
            {
526
                stmt.close();
527
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
528 5798 berkley
                throw new McdbDocNotFoundException("2Could not find document " + localId);
529 5378 berkley
            }
530 5441 berkley
531 5444 berkley
            String sql2 = "select principal_name, permission, perm_type, perm_order from xml_access " +
532 5441 berkley
            "where docid like '" + localId + "'";
533 6099 leinfelder
            logMetacat.debug("executing sql: " + sql2);
534 5441 berkley
            PreparedStatement stmt2 = dbConn.prepareStatement(sql2);
535 5442 berkley
            rs = stmt2.executeQuery();
536 5441 berkley
            Vector accessVector = new Vector();
537 5445 berkley
            while(rs.next())
538 5441 berkley
            {
539
                Hashtable accessHash = new Hashtable();
540
                String principal_name = rs.getString(1);
541
                String permission = rs.getString(2);
542
                String permissionType = rs.getString(3);
543
                String permissionOrder = rs.getString(4);
544
                accessHash.put("principal_name", principal_name);
545
                accessHash.put("permission", permission);
546
                accessHash.put("permission_type", permissionType);
547
                accessHash.put("permission_order", permissionOrder);
548 6099 leinfelder
                logMetacat.debug("accessHash: " + accessHash.toString());
549 5441 berkley
                accessVector.add(accessHash);
550
            }
551
            h.put("access", accessVector);
552 5378 berkley
        }
553
        catch (SQLException e)
554
        {
555 5444 berkley
            e.printStackTrace();
556 5378 berkley
            logMetacat.error("Error while getting document info for localid " + localId + " : "
557
                    + e.getMessage());
558
        }
559
        finally
560
        {
561
            // Return database connection to the pool
562
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
563
        }
564
        return h;
565
    }
566
567
    /**
568
     * return the newest rev for a given localId
569
     * @param localId
570
     * @return
571
     */
572
    public int getLatestRevForLocalId(String localId)
573
        throws McdbDocNotFoundException
574
    {
575 5798 berkley
        try
576
        {
577
            AccessionNumber acc = new AccessionNumber(localId, "NONE");
578
            localId = acc.getDocid();
579
        }
580
        catch(Exception e)
581
        {
582
            //do nothing. just try the localId as it is
583
        }
584 5378 berkley
        int rev = 0;
585 6595 leinfelder
        String sql = "select rev from xml_documents where docid like ? ";
586 5378 berkley
        DBConnection dbConn = null;
587
        int serialNumber = -1;
588
        try
589
        {
590
            // Get a database connection from the pool
591
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getLatestRevForLocalId");
592
            serialNumber = dbConn.getCheckOutSerialNumber();
593
594
            // Execute the insert statement
595
            PreparedStatement stmt = dbConn.prepareStatement(sql);
596 6595 leinfelder
            stmt.setString(1, localId);
597 5378 berkley
            ResultSet rs = stmt.executeQuery();
598
            if (rs.next())
599
            {
600
                rev = rs.getInt(1);
601
                stmt.close();
602
            }
603
            else
604
            {
605
                stmt.close();
606
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
607 5798 berkley
                throw new McdbDocNotFoundException("While trying to get the latest rev, could not find document " + localId);
608 5378 berkley
            }
609
        }
610
        catch (SQLException e)
611
        {
612
            logMetacat.error("Error while looking up the guid: "
613
                    + e.getMessage());
614
        }
615
        finally
616
        {
617
            // Return database connection to the pool
618
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
619
        }
620
        return rev;
621
    }
622
623
    /**
624 5377 berkley
     * return all local ids in the object store that do not have associated
625 6099 leinfelder
     * system metadata
626 5377 berkley
     */
627
    public List<String> getLocalIdsWithNoSystemMetadata()
628
    {
629
        Vector<String> ids = new Vector<String>();
630 6099 leinfelder
        String sql = "select docid, rev from xml_documents " +
631
        		"where docid not in " +
632
        		"(select docid from identifier where guid in (select guid from systemmetadata)))";
633 5377 berkley
        DBConnection dbConn = null;
634
        int serialNumber = -1;
635
        try
636
        {
637
            // Get a database connection from the pool
638
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getLocalIdsWithNoSystemMetadata");
639
            serialNumber = dbConn.getCheckOutSerialNumber();
640
641
            // Execute the insert statement
642
            PreparedStatement stmt = dbConn.prepareStatement(sql);
643
            ResultSet rs = stmt.executeQuery();
644
            while (rs.next())
645
            {
646
                String localid = rs.getString(1);
647 5798 berkley
                String rev = rs.getString(2);
648
                localid += "." + rev;
649 6099 leinfelder
                logMetacat.debug("id to add SM for: " + localid);
650 5377 berkley
                ids.add(localid);
651
            }
652
            stmt.close();
653
        }
654
        catch (SQLException e)
655
        {
656
            logMetacat.error("Error while looking up the guid: "
657
                    + e.getMessage());
658
        }
659
        finally
660
        {
661
            // Return database connection to the pool
662
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
663
        }
664
665
        return ids;
666
    }
667
668
    /**
669
     * return a listing of all local ids in the object store
670
     * @return a list of all local ids in metacat
671
     */
672
    public List<String> getAllLocalIds()
673 6416 rnahf
    // seems to be an unnecessary and restrictive throw -rnahf 13-Sep-2011
674
    //    throws Exception
675 5377 berkley
    {
676
        Vector<String> ids = new Vector<String>();
677
        String sql = "select docid from xml_documents";
678
        DBConnection dbConn = null;
679
        int serialNumber = -1;
680
        try
681
        {
682
            // Get a database connection from the pool
683
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getAllLocalIds");
684
            serialNumber = dbConn.getCheckOutSerialNumber();
685
686
            // Execute the insert statement
687
            PreparedStatement stmt = dbConn.prepareStatement(sql);
688
            ResultSet rs = stmt.executeQuery();
689
            while (rs.next())
690
            {
691
                String localid = rs.getString(1);
692
                ids.add(localid);
693
            }
694
            stmt.close();
695
        }
696
        catch (SQLException e)
697
        {
698
            logMetacat.error("Error while looking up the guid: "
699
                    + e.getMessage());
700
        }
701
        finally
702
        {
703
            // Return database connection to the pool
704
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
705
        }
706
        return ids;
707
    }
708
709 6416 rnahf
710 6118 leinfelder
    /**
711 6416 rnahf
     * return a listing of all guids in the object store
712
     * @return a list of all GUIDs in metacat
713
     */
714
    public List<String> getAllGUIDs()
715
    {
716
        Vector<String> guids = new Vector<String>();
717
        String sql = "select guid from identifier";
718
        DBConnection dbConn = null;
719
        int serialNumber = -1;
720
        try
721
        {
722
            // Get a database connection from the pool
723
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getAllGUIDs");
724
            serialNumber = dbConn.getCheckOutSerialNumber();
725
726
            // Execute the insert statement
727
            PreparedStatement stmt = dbConn.prepareStatement(sql);
728
            ResultSet rs = stmt.executeQuery();
729
            while (rs.next())
730
            {
731
                String guid = rs.getString(1);
732
                guids.add(guid);
733
            }
734
            stmt.close();
735
        }
736
        catch (SQLException e)
737
        {
738
            logMetacat.error("Error while retrieving the guid: "
739
                    + e.getMessage());
740
        }
741
        finally
742
        {
743
            // Return database connection to the pool
744
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
745
        }
746
        return guids;
747
    }
748
749
750
751
    /**
752 6118 leinfelder
     * returns a list of system metadata-only guids since the given date
753
     * @return a list of system ids in metacat that do not correspond to objects
754
     * TODO: need to check which server they are on
755
     */
756
    public List<String> getUpdatedSystemMetadataIds(Date since)
757
       throws Exception
758
    {
759
        List<String> ids = new Vector<String>();
760
        String sql =
761
        	"select guid from " + TYPE_SYSTEM_METADATA +
762
        	" where guid not in " +
763
        	" (select guid from " + TYPE_IDENTIFIER + ") " +
764
        	" and date_modified > ?";
765
        DBConnection dbConn = null;
766
        int serialNumber = -1;
767
        try
768
        {
769
            // Get a database connection from the pool
770
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getUpdatedSystemMetadataIds");
771
            serialNumber = dbConn.getCheckOutSerialNumber();
772 6099 leinfelder
773 6118 leinfelder
            // Execute the insert statement
774
            PreparedStatement stmt = dbConn.prepareStatement(sql);
775
            stmt.setDate(1, new java.sql.Date(since.getTime()));
776
            ResultSet rs = stmt.executeQuery();
777
            while (rs.next())
778
            {
779
                String guid = rs.getString(1);
780
                ids.add(guid);
781
            }
782
            stmt.close();
783
        }
784
        catch (SQLException e)
785
        {
786
            logMetacat.error("Error while looking up the updated guids: "
787
                    + e.getMessage());
788
        }
789
        finally
790
        {
791
            // Return database connection to the pool
792
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
793
        }
794
        return ids;
795
    }
796 5282 jones
797 6459 leinfelder
    /**
798
     * returns a list of system metadata-only guids since the given date
799
     * @return a list of system ids in metacat that do not correspond to objects
800
     * TODO: need to check which server they are on
801
     */
802
    public Date getLastModifiedDate() throws Exception {
803
        Date maxDate = null;
804 6118 leinfelder
805 6459 leinfelder
        List<String> ids = new Vector<String>();
806
        String sql =
807
        	"select max(date_modified) from " + TYPE_SYSTEM_METADATA;
808
        DBConnection dbConn = null;
809
        int serialNumber = -1;
810
        try
811
        {
812
            // Get a database connection from the pool
813
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getLastModifiedDate");
814
            serialNumber = dbConn.getCheckOutSerialNumber();
815
816
            // Execute the insert statement
817
            PreparedStatement stmt = dbConn.prepareStatement(sql);
818
            ResultSet rs = stmt.executeQuery();
819
            if (rs.next()) {
820
            	maxDate = rs.getDate(1);
821
            }
822
            stmt.close();
823
        }
824
        catch (SQLException e)
825
        {
826
            logMetacat.error("Error while looking up the latest update date: "
827
                    + e.getMessage());
828
        }
829
        finally
830
        {
831
            // Return database connection to the pool
832
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
833
        }
834
        return maxDate;
835
    }
836
837 6118 leinfelder
838 5282 jones
    /**
839
     * Determine if an identifier exists already, returning true if so.
840
     *
841
     * @param guid the global identifier to look up
842
     * @return boolean true if the identifier exists
843
     */
844
    public boolean identifierExists(String guid)
845
    {
846
        boolean idExists = false;
847
        try {
848
            String id = getLocalId(guid);
849
            if (id != null) {
850
                idExists = true;
851
            }
852
        } catch (McdbDocNotFoundException e) {
853 6123 leinfelder
        	// try system metadata only
854
        	try {
855 6416 rnahf
        		idExists = systemMetadataExists(guid);
856 6454 leinfelder
            } catch (Exception e2) {
857 6123 leinfelder
            	idExists = false;
858
            }
859 5282 jones
        }
860
        return idExists;
861
    }
862
863
    /**
864
     *
865 5453 berkley
     * @param guid
866
     * @param rev
867
     * @return
868
     */
869
    public String generateLocalId(String guid, int rev)
870
    {
871
        return generateLocalId(guid, rev, false);
872
    }
873 5286 jones
874
    /**
875
     * Given a global identifier (guid), create a suitable local identifier that
876
     * follows Metacat's docid semantics and format (scope.id.rev), and create
877
     * a mapping between these two identifiers.  This effectively reserves both
878
     * the global and the local identifier, as they will now be present in the
879
     * identifier mapping table.  If the incoming guid has the syntax of a
880
     * Metacat docid (scope.id.rev), then simply use it.
881
     *
882
     * @param guid the global string identifier
883
     * @param rev the revision number to be used in the localId
884
     * @return String containing the localId to be used for Metacat operations
885
     */
886 5453 berkley
    public String generateLocalId(String guid, int rev, boolean isSystemMetadata)
887 5286 jones
    {
888
        String localId = "";
889
        boolean conformsToDocidFormat = false;
890
891
        // Check if the guid passed in is already in docid (scope.id.rev) format
892
        try {
893
            AccessionNumber acc = new AccessionNumber(guid, "NONE");
894
            if (new Integer(acc.getRev()).intValue() > 0) {
895
                conformsToDocidFormat = true;
896
            }
897
        } catch (NumberFormatException e) {
898
            // No action needed, simply detecting invalid AccessionNumbers
899
        } catch (AccessionNumberException e) {
900
            // No action needed, simply detecting invalid AccessionNumbers
901
        } catch (SQLException e) {
902
            // No action needed, simply detecting invalid AccessionNumbers
903
        }
904
905
        if (conformsToDocidFormat) {
906
            // if it conforms, use it for both guid and localId
907
            localId = guid;
908
        } else {
909
            // if not, then generate a new unique localId
910
            localId = DocumentUtil.generateDocumentId(rev);
911
        }
912
913
        // Register this new pair in the identifier mapping table
914 6099 leinfelder
        logMetacat.debug("creating mapping in generateLocalId");
915 5453 berkley
        if(!isSystemMetadata)
916
        { //don't do this if we're generating for system metadata
917
            createMapping(guid, localId);
918
        }
919 5286 jones
920
        return localId;
921
    }
922 5322 berkley
923
    /**
924
     * given a local identifer, look up the guid.  Throw McdbDocNotFoundException
925 5452 berkley
     * if the docid, rev is not found in the identifiers or systemmetadata tables
926 5322 berkley
     *
927
     * @param docid the docid to look up
928
     * @param rev the revision of the docid to look up
929
     * @return String containing the mapped guid
930
     * @throws McdbDocNotFoundException if the docid, rev is not found
931
     */
932
    public String getGUID(String docid, int rev)
933
      throws McdbDocNotFoundException
934
    {
935 6099 leinfelder
        logMetacat.debug("getting guid for " + docid);
936 5322 berkley
        String query = "select guid from identifier where docid = ? and rev = ?";
937
        String guid = null;
938
939
        DBConnection dbConn = null;
940
        int serialNumber = -1;
941
        try {
942
            // Get a database connection from the pool
943 5377 berkley
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getGUID");
944 5322 berkley
            serialNumber = dbConn.getCheckOutSerialNumber();
945
946
            // Execute the insert statement
947
            PreparedStatement stmt = dbConn.prepareStatement(query);
948
            stmt.setString(1, docid);
949
            stmt.setInt(2, rev);
950
            ResultSet rs = stmt.executeQuery();
951 5451 berkley
            if (rs.next())
952
            {
953 5322 berkley
                guid = rs.getString(1);
954 5451 berkley
            }
955
            else
956
            {
957 6104 leinfelder
            	throw new McdbDocNotFoundException("No guid registered for docid " + docid + "." + rev);
958 5322 berkley
            }
959 5451 berkley
960 5322 berkley
        } catch (SQLException e) {
961
            logMetacat.error("Error while looking up the guid: "
962
                    + e.getMessage());
963
        } finally {
964
            // Return database connection to the pool
965
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
966
        }
967
968
        return guid;
969
    }
970 5333 berkley
971 6454 leinfelder
    public boolean systemMetadataExists(String guid) {
972 6123 leinfelder
		logMetacat.debug("looking up system metadata for guid " + guid);
973
		boolean exists = false;
974 6337 leinfelder
		String query = "select guid from systemmetadata where guid = ?";
975 6123 leinfelder
976
		DBConnection dbConn = null;
977
		int serialNumber = -1;
978
		try {
979
			// Get a database connection from the pool
980
			dbConn = DBConnectionPool.getDBConnection("IdentifierManager.systemMetadataExisits");
981
			serialNumber = dbConn.getCheckOutSerialNumber();
982
983
			// Execute the insert statement
984
			PreparedStatement stmt = dbConn.prepareStatement(query);
985
			stmt.setString(1, guid);
986
			ResultSet rs = stmt.executeQuery();
987
			if (rs.next()) {
988
				exists = true;
989
			}
990
991
		} catch (SQLException e) {
992
			logMetacat.error("Error while looking up the system metadata: "
993
					+ e.getMessage());
994
		} finally {
995
			// Return database connection to the pool
996
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
997
		}
998
999
		return exists;
1000
	}
1001
1002 5333 berkley
    /**
1003 5887 berkley
     * creates a system metadata mapping and adds additional fields from sysmeta
1004
     * to the table for quick searching.
1005
     *
1006
     * @param guid the id to insert
1007
     * @param localId the systemMetadata object to get the local id for
1008 6108 leinfelder
     * @throws McdbDocNotFoundException
1009 5887 berkley
     */
1010 6108 leinfelder
    public void createSystemMetadata(SystemMetadata sysmeta) throws McdbDocNotFoundException
1011 5887 berkley
    {
1012 6277 leinfelder
    	String guid = sysmeta.getIdentifier().getValue();
1013 6337 leinfelder
    	// insert the record
1014
        insertSystemMetadata(guid);
1015
        // update with the values
1016 6099 leinfelder
        updateSystemMetadata(sysmeta);
1017 5887 berkley
    }
1018 6099 leinfelder
1019 5887 berkley
1020
    /**
1021 6079 leinfelder
     * update a mapping
1022
     * @param guid
1023
     * @param localId
1024
     */
1025
    public void updateMapping(String guid, String localId)
1026
    {
1027
1028 6099 leinfelder
        logMetacat.debug("$$$$$$$$$$$$$$ updating mapping table");
1029 5350 berkley
        int serialNumber = -1;
1030
        DBConnection dbConn = null;
1031
        try {
1032
            // Parse the localId into scope and rev parts
1033
            AccessionNumber acc = new AccessionNumber(localId, "NOACTION");
1034
            String docid = acc.getDocid();
1035
            int rev = 1;
1036
            if(acc.getRev() != null)
1037
            {
1038
              rev = (new Integer(acc.getRev()).intValue());
1039
            }
1040
1041
            // Get a database connection from the pool
1042
            dbConn =
1043 6079 leinfelder
                DBConnectionPool.getDBConnection("IdentifierManager.updateMapping");
1044 5350 berkley
            serialNumber = dbConn.getCheckOutSerialNumber();
1045
1046 6099 leinfelder
            // Execute the update statement
1047 6595 leinfelder
            String query = "update " + TYPE_IDENTIFIER + " set (docid, rev) = (?, ?) where guid = ?";
1048 5350 berkley
            PreparedStatement stmt = dbConn.prepareStatement(query);
1049
            stmt.setString(1, docid);
1050
            stmt.setInt(2, rev);
1051 6595 leinfelder
            stmt.setString(3, guid);
1052 5350 berkley
            int rows = stmt.executeUpdate();
1053
1054
            stmt.close();
1055
        } catch (SQLException e) {
1056
            e.printStackTrace();
1057 6079 leinfelder
            logMetacat.error("SQL error while updating a mapping identifier: "
1058 5350 berkley
                    + e.getMessage());
1059
        } catch (NumberFormatException e) {
1060
            e.printStackTrace();
1061 6079 leinfelder
            logMetacat.error("NumberFormat error while updating a mapping identifier: "
1062 5350 berkley
                    + e.getMessage());
1063
        } catch (AccessionNumberException e) {
1064
            e.printStackTrace();
1065 6079 leinfelder
            logMetacat.error("AccessionNumber error while updating a mapping identifier: "
1066 5350 berkley
                    + e.getMessage());
1067
        } finally {
1068
            // Return database connection to the pool
1069
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1070
        }
1071 6099 leinfelder
        logMetacat.debug("done updating mapping");
1072 5350 berkley
    }
1073 6099 leinfelder
1074
    private void updateSystemMetadataFields(long dateUploaded, String rightsHolder,
1075 5887 berkley
            String checksum, String checksumAlgorithm, String originMemberNode,
1076 5917 berkley
            String authoritativeMemberNode, long modifiedDate, String submitter,
1077 6384 cjones
            String guid, String objectFormat, BigInteger size, boolean replicationAllowed,
1078 6561 leinfelder
            int numberReplicas, String obsoletes, String obsoletedBy, BigInteger serialVersion)
1079 5887 berkley
    {
1080
        DBConnection dbConn = null;
1081
        int serialNumber = -1;
1082 5957 berkley
1083 5887 berkley
        try {
1084
            // Get a database connection from the pool
1085
            dbConn =
1086
                DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
1087
            serialNumber = dbConn.getCheckOutSerialNumber();
1088
1089
            // Execute the insert statement
1090
            String query = "update " + TYPE_SYSTEM_METADATA +
1091
                " set (date_uploaded, rights_holder, checksum, checksum_algorithm, " +
1092 5917 berkley
                "origin_member_node, authoritive_member_node, date_modified, " +
1093 6561 leinfelder
                "submitter, object_format, size, replication_allowed, number_replicas, " +
1094
                "obsoletes, obsoleted_by, serial_version) " +
1095
                "= (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) where guid = ?";
1096 5887 berkley
            PreparedStatement stmt = dbConn.prepareStatement(query);
1097
1098
            //data values
1099
            stmt.setTimestamp(1, new java.sql.Timestamp(dateUploaded));
1100
            stmt.setString(2, rightsHolder);
1101
            stmt.setString(3, checksum);
1102
            stmt.setString(4, checksumAlgorithm);
1103
            stmt.setString(5, originMemberNode);
1104
            stmt.setString(6, authoritativeMemberNode);
1105
            stmt.setTimestamp(7, new java.sql.Timestamp(modifiedDate));
1106
            stmt.setString(8, submitter);
1107 5917 berkley
            stmt.setString(9, objectFormat);
1108 6384 cjones
            stmt.setString(10, size.toString());
1109 6107 leinfelder
            stmt.setBoolean(11, replicationAllowed);
1110
            stmt.setInt(12, numberReplicas);
1111 6375 leinfelder
            stmt.setString(13, obsoletes);
1112
            stmt.setString(14, obsoletedBy);
1113 6561 leinfelder
            stmt.setString(15, serialVersion.toString());
1114 6375 leinfelder
1115 5887 berkley
            //where clause
1116 6561 leinfelder
            stmt.setString(16, guid);
1117 6099 leinfelder
            logMetacat.debug("stmt: " + stmt.toString());
1118 5887 berkley
            //execute
1119
            int rows = stmt.executeUpdate();
1120
1121
            stmt.close();
1122
        } catch (SQLException e) {
1123
            e.printStackTrace();
1124 5950 berkley
            logMetacat.error("insertAdditionalSystemMetadataFields: SQL error while creating a mapping to the system metadata identifier: "
1125 5887 berkley
                    + e.getMessage());
1126
        } catch (NumberFormatException e) {
1127
            e.printStackTrace();
1128 5950 berkley
            logMetacat.error("insertAdditionalSystemMetadataFields: NumberFormat error while creating a mapping to the system metadata identifier: "
1129 5887 berkley
                    + e.getMessage());
1130
        } finally {
1131
            // Return database connection to the pool
1132
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1133
        }
1134
    }
1135
1136 6107 leinfelder
    private void insertReplicationPolicy(String guid, String policy, List<String> memberNodes)
1137
    {
1138
        DBConnection dbConn = null;
1139
        int serialNumber = -1;
1140
1141
        try {
1142
            // Get a database connection from the pool
1143
            dbConn =
1144
                DBConnectionPool.getDBConnection("IdentifierManager.insertReplicationPolicy");
1145
            serialNumber = dbConn.getCheckOutSerialNumber();
1146
1147
            // remove existing values first
1148
            String delete = "delete from systemMetadataReplicationPolicy " +
1149
            "where guid = ? and policy = ?";
1150
	        PreparedStatement stmt = dbConn.prepareStatement(delete);
1151
	        //data values
1152
	        stmt.setString(1, guid);
1153
	        stmt.setString(2, policy);
1154
	        //execute
1155
	        int deletedCount = stmt.executeUpdate();
1156
	        stmt.close();
1157
1158
            for (String memberNode: memberNodes) {
1159
	            // Execute the insert statement
1160
	            String insert = "insert into systemMetadataReplicationPolicy " +
1161
	                "(guid, policy, member_node) " +
1162
	                "values (?, ?, ?)";
1163
	            PreparedStatement insertStatement = dbConn.prepareStatement(insert);
1164
1165
	            //data values
1166
	            insertStatement.setString(1, guid);
1167
	            insertStatement.setString(2, policy);
1168
	            insertStatement.setString(3, memberNode);
1169
	            //execute
1170
	            int rows = insertStatement.executeUpdate();
1171
	            insertStatement.close();
1172
            }
1173
        } catch (SQLException e) {
1174
            logMetacat.error("SQL error while adding systemMetadataReplicationPolicy for: " + guid, e);
1175
        } finally {
1176
            // Return database connection to the pool
1177
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1178
        }
1179
    }
1180
1181
    private void insertReplicationStatus(String guid, List<Replica> replicas) {
1182
        DBConnection dbConn = null;
1183
        int serialNumber = -1;
1184
1185
        try {
1186
            // Get a database connection from the pool
1187
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.insertReplicas");
1188
            serialNumber = dbConn.getCheckOutSerialNumber();
1189
1190
            // remove existing values first
1191
            String delete = "delete from systemMetadataReplicationStatus " +
1192
            "where guid = ?";
1193
	        PreparedStatement stmt = dbConn.prepareStatement(delete);
1194
	        //data values
1195
	        stmt.setString(1, guid);
1196
	        //execute
1197
	        int deletedCount = stmt.executeUpdate();
1198
	        stmt.close();
1199
1200 6443 leinfelder
	        if (replicas != null) {
1201
	            for (Replica replica: replicas) {
1202
		            // Execute the insert statement
1203
		            String insert = "insert into systemMetadataReplicationStatus " +
1204
		                "(guid, member_node, status, date_verified) " +
1205
		                "values (?, ?, ?, ?)";
1206
		            PreparedStatement insertStatement = dbConn.prepareStatement(insert);
1207
1208
		            //data values
1209
		            String memberNode = replica.getReplicaMemberNode().getValue();
1210
		            String status = replica.getReplicationStatus().toString();
1211
		            java.sql.Date sqlDate = new java.sql.Date(replica.getReplicaVerified().getTime());
1212
		            insertStatement.setString(1, guid);
1213
		            insertStatement.setString(2, memberNode);
1214
		            insertStatement.setString(3, status);
1215
		            insertStatement.setDate(4, sqlDate);
1216
1217
		            //execute
1218
		            int rows = insertStatement.executeUpdate();
1219
		            insertStatement.close();
1220
	            }
1221
	        }
1222 6107 leinfelder
        } catch (SQLException e) {
1223
            logMetacat.error("SQL error while adding systemMetadataReplicationStatus for: " + guid, e);
1224
        } finally {
1225
            // Return database connection to the pool
1226
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1227
        }
1228
    }
1229
1230 5334 berkley
    /**
1231 5887 berkley
     * Insert the system metadata fields into the db
1232
     * @param sm
1233 6108 leinfelder
     * @throws McdbDocNotFoundException
1234 5887 berkley
     */
1235 6108 leinfelder
    public void updateSystemMetadata(SystemMetadata sm) throws McdbDocNotFoundException {
1236 6107 leinfelder
1237
        Boolean replicationAllowed = false;
1238
		Integer numberReplicas = -1;
1239
    	ReplicationPolicy replicationPolicy = sm.getReplicationPolicy();
1240
    	if (replicationPolicy != null) {
1241
    		replicationAllowed = replicationPolicy.getReplicationAllowed();
1242
    		numberReplicas = replicationPolicy.getNumberReplicas();
1243
    		replicationAllowed = replicationAllowed == null ? false: replicationAllowed;
1244
    		numberReplicas = numberReplicas == null ? -1: numberReplicas;
1245
    	}
1246
1247
    	// the main systemMetadata fields
1248
		updateSystemMetadataFields(
1249 6311 leinfelder
				sm.getDateUploaded() == null ? null: sm.getDateUploaded().getTime(),
1250
				sm.getRightsHolder() == null ? null: sm.getRightsHolder().getValue(),
1251
				sm.getChecksum() == null ? null: sm.getChecksum().getValue(),
1252 6397 leinfelder
				sm.getChecksum() == null ? null: sm.getChecksum().getAlgorithm(),
1253 6311 leinfelder
				sm.getOriginMemberNode() == null ? null: sm.getOriginMemberNode().getValue(),
1254
				sm.getAuthoritativeMemberNode() == null ? null: sm.getAuthoritativeMemberNode().getValue(),
1255
				sm.getDateSysMetadataModified() == null ? null: sm.getDateSysMetadataModified().getTime(),
1256
				sm.getSubmitter() == null ? null: sm.getSubmitter().getValue(),
1257 6384 cjones
        sm.getIdentifier().getValue(),
1258 6561 leinfelder
        sm.getFormatId() == null ? null: sm.getFormatId().getValue(),
1259 6384 cjones
        sm.getSize(),
1260
        replicationAllowed,
1261
        numberReplicas,
1262
        sm.getObsoletes() == null ? null:sm.getObsoletes().getValue(),
1263 6561 leinfelder
        sm.getObsoletedBy() == null ? null: sm.getObsoletedBy().getValue(),
1264
        sm.getSerialVersion()
1265
        );
1266 6097 leinfelder
1267
        String guid = sm.getIdentifier().getValue();
1268
1269 6107 leinfelder
        // save replication policies
1270
        if (replicationPolicy != null) {
1271
		    List<String> nodes = null;
1272
		    String policy = null;
1273
1274 6494 leinfelder
		    // check for null
1275
		    if (replicationPolicy.getBlockedMemberNodeList() != null) {
1276
			    nodes = new ArrayList<String>();
1277
			    policy = "blocked";
1278
			    for (NodeReference node: replicationPolicy.getBlockedMemberNodeList()) {
1279
			    	nodes.add(node.getValue());
1280
			    }
1281
			    this.insertReplicationPolicy(guid, policy, nodes);
1282 6107 leinfelder
		    }
1283
1284 6494 leinfelder
		    if (replicationPolicy.getPreferredMemberNodeList() != null) {
1285
			    nodes = new ArrayList<String>();
1286
			    policy = "preferred";
1287
			    for (NodeReference node: replicationPolicy.getPreferredMemberNodeList()) {
1288
			    	nodes.add(node.getValue());
1289
			    }
1290
		        this.insertReplicationPolicy(guid, policy, nodes);
1291 6107 leinfelder
		    }
1292
        }
1293 6097 leinfelder
1294 6107 leinfelder
        // save replica information
1295
        this.insertReplicationStatus(guid, sm.getReplicaList());
1296
1297 6108 leinfelder
        // save access policy
1298
        AccessPolicy accessPolicy = sm.getAccessPolicy();
1299
        if (accessPolicy != null) {
1300
        	try {
1301
				this.insertAccessPolicy(guid, accessPolicy);
1302
			} catch (AccessException e) {
1303
				throw new McdbDocNotFoundException(e);
1304
			}
1305
        }
1306 5887 berkley
    }
1307
1308
    /**
1309 6108 leinfelder
     * Creates Metacat access rules and inserts them
1310
     * @param accessPolicy
1311
     * @throws McdbDocNotFoundException
1312
     * @throws AccessException
1313
     */
1314
    private void insertAccessPolicy(String guid, AccessPolicy accessPolicy) throws McdbDocNotFoundException, AccessException {
1315
1316
    	List<XMLAccessDAO> accessDAOs = new ArrayList<XMLAccessDAO>();
1317
        for (AccessRule accessRule: accessPolicy.getAllowList()) {
1318
        	List<Subject> subjects = accessRule.getSubjectList();
1319
        	List<Permission> permissions = accessRule.getPermissionList();
1320
        	for (Subject subject: subjects) {
1321 6122 leinfelder
    			XMLAccessDAO accessDAO = new XMLAccessDAO();
1322
        		accessDAO.setPrincipalName(subject.getValue());
1323
    			accessDAO.setGuid(guid);
1324
    			accessDAO.setPermType(AccessControlInterface.ALLOW);
1325
    			accessDAO.setPermOrder(AccessControlInterface.DENYFIRST);
1326
    			for (Permission permission: permissions) {
1327
    				Long metacatPermission = new Long(convertPermission(permission));
1328
        			accessDAO.addPermission(metacatPermission);
1329
    			}
1330
    			accessDAOs.add(accessDAO);
1331 6108 leinfelder
        	}
1332
        }
1333
1334 6122 leinfelder
        // use GUID to update
1335
        XMLAccessAccess accessController  = new XMLAccessAccess(true);
1336
        accessController.replaceAccess(guid, accessDAOs);
1337 6108 leinfelder
1338
1339
    }
1340
1341
    /**
1342
     * Lookup access policy from Metacat
1343
     * @param guid
1344
     * @return
1345
     * @throws McdbDocNotFoundException
1346
     * @throws AccessException
1347
     */
1348
    private AccessPolicy getAccessPolicy(String guid) throws McdbDocNotFoundException, AccessException {
1349 6450 leinfelder
        AccessPolicy accessPolicy = new AccessPolicy();
1350
1351 6122 leinfelder
    	// use GUID to look up the access
1352
        XMLAccessAccess accessController  = new XMLAccessAccess(true);
1353
    	List<XMLAccessDAO> accessDAOs = accessController.getXMLAccessForDoc(guid);
1354 6108 leinfelder
        for (XMLAccessDAO accessDAO: accessDAOs) {
1355 6450 leinfelder
        	AccessRule accessRule = new AccessRule();
1356 6108 leinfelder
        	Permission permission = convertPermission(accessDAO.getPermission().intValue());
1357
        	accessRule.addPermission(permission);
1358
        	Subject subject = new Subject();
1359
        	subject.setValue(accessDAO.getPrincipalName());
1360
        	accessRule.addSubject(subject);
1361 6450 leinfelder
            accessPolicy.addAllow(accessRule);
1362 6108 leinfelder
        }
1363
        return accessPolicy;
1364
    }
1365
1366
    public int convertPermission(Permission permission) {
1367
    	if (permission.equals(Permission.READ)) {
1368
    		return AccessControlInterface.READ;
1369
    	}
1370
    	if (permission.equals(Permission.WRITE)) {
1371
    		return AccessControlInterface.WRITE;
1372
    	}
1373
    	if (permission.equals(Permission.CHANGE_PERMISSION)) {
1374
    		return AccessControlInterface.CHMOD;
1375
    	}
1376
		return -1;
1377
    }
1378
1379
    public Permission convertPermission(int permission) {
1380
    	if (permission == AccessControlInterface.READ) {
1381
    		return Permission.READ;
1382
    	}
1383
    	if (permission == AccessControlInterface.WRITE) {
1384
    		return Permission.WRITE;
1385
    	}
1386
    	if (permission == AccessControlInterface.CHMOD) {
1387
    		return Permission.CHANGE_PERMISSION;
1388
    	}
1389
		return null;
1390
    }
1391
1392
    /**
1393 6099 leinfelder
     * Lookup a localId given the GUID. If
1394
     * the identifier is not found, throw an exception.
1395
     *
1396
     * @param guid the global identifier to look up
1397
     * @return String containing the corresponding LocalId
1398
     * @throws McdbDocNotFoundException if the identifier is not found
1399 5334 berkley
     */
1400 6099 leinfelder
    public String getLocalId(String guid) throws McdbDocNotFoundException {
1401 5334 berkley
1402
      String db_guid = "";
1403
      String docid = "";
1404
      int rev = 0;
1405
1406 6099 leinfelder
      String query = "select guid, docid, rev from " + TYPE_IDENTIFIER + " where guid = ?";
1407 5334 berkley
1408
      DBConnection dbConn = null;
1409 5333 berkley
      int serialNumber = -1;
1410 5334 berkley
      try {
1411
          // Get a database connection from the pool
1412
          dbConn = DBConnectionPool.getDBConnection("Identifier.getLocalId");
1413
          serialNumber = dbConn.getCheckOutSerialNumber();
1414
1415
          // Execute the insert statement
1416
          PreparedStatement stmt = dbConn.prepareStatement(query);
1417
          stmt.setString(1, guid);
1418
          ResultSet rs = stmt.executeQuery();
1419
          if (rs.next()) {
1420
              db_guid = rs.getString(1);
1421
              docid = rs.getString(2);
1422
              rev = rs.getInt(3);
1423
              assert(db_guid.equals(guid));
1424
          } else {
1425
              throw new McdbDocNotFoundException("Document not found:" + guid);
1426
          }
1427
          stmt.close();
1428
      } catch (SQLException e) {
1429
          logMetacat.error("Error while looking up the local identifier: "
1430
                  + e.getMessage());
1431
      } finally {
1432
          // Return database connection to the pool
1433
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1434
      }
1435
      return docid + "." + rev;
1436
    }
1437
1438 5377 berkley
    /**
1439 5895 berkley
     * query the systemmetadata table based on the given parameters
1440
     * @param startTime
1441
     * @param endTime
1442
     * @param objectFormat
1443
     * @param replicaStatus
1444
     * @param start
1445
     * @param count
1446
     * @return ObjectList
1447 6299 leinfelder
     * @throws SQLException
1448
     * @throws ServiceException
1449
     * @throws PropertyNotFoundException
1450 5895 berkley
     */
1451
    public ObjectList querySystemMetadata(Date startTime, Date endTime,
1452 6337 leinfelder
            ObjectFormatIdentifier objectFormatId, boolean replicaStatus, int start, int count) throws SQLException, PropertyNotFoundException, ServiceException
1453 5895 berkley
    {
1454
        ObjectList ol = new ObjectList();
1455
        int total = 0;
1456 5943 berkley
        DBConnection dbConn = null;
1457
        int serialNumber = -1;
1458 5895 berkley
1459 6299 leinfelder
        try {
1460 5895 berkley
            String sql = "select guid, date_uploaded, rights_holder, checksum, " +
1461
                "checksum_algorithm, origin_member_node, authoritive_member_node, " +
1462 5917 berkley
                "date_modified, submitter, object_format, size from systemmetadata";
1463 5895 berkley
1464
            boolean f1 = false;
1465
            boolean f2 = false;
1466
            boolean f3 = false;
1467
1468 6299 leinfelder
            if (startTime != null) {
1469 5917 berkley
                sql += " where systemmetadata.date_modified > ?";
1470 5895 berkley
                f1 = true;
1471
            }
1472
1473 6299 leinfelder
            if (endTime != null) {
1474
                if (!f1) {
1475 5917 berkley
                    sql += " where systemmetadata.date_modified < ?";
1476
                }
1477 6299 leinfelder
                else {
1478 5917 berkley
                    sql += " and systemmetadata.date_modified < ?";
1479
                }
1480 5895 berkley
                f2 = true;
1481
            }
1482
1483 6337 leinfelder
            if (objectFormatId != null) {
1484 6299 leinfelder
                if (!f1 && !f2) {
1485 5917 berkley
                    sql += " where object_format = ?";
1486
                }
1487 6299 leinfelder
                else {
1488 5917 berkley
                    sql += " and object_format = ?";
1489
                }
1490 5895 berkley
                f3 = true;
1491
            }
1492
1493 6299 leinfelder
            if (replicaStatus) {
1494 5895 berkley
                String currentNodeId = PropertyService.getInstance().getProperty("dataone.memberNodeId");
1495 6299 leinfelder
                if (!f1 && !f2 && !f3) {
1496 5917 berkley
                    sql += " where authoritive_member_node != '" + currentNodeId.trim() + "'";
1497
                }
1498 6299 leinfelder
                else {
1499 5917 berkley
                    sql += " and authoritive_member_node != '" + currentNodeId.trim() + "'";
1500
                }
1501 5895 berkley
            }
1502
1503 5943 berkley
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.querySystemMetadata");
1504
            serialNumber = dbConn.getCheckOutSerialNumber();
1505 5895 berkley
            PreparedStatement stmt = dbConn.prepareStatement(sql);
1506
1507 6299 leinfelder
            if (f1 && f2 && f3) {
1508 5895 berkley
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1509
                stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
1510 6337 leinfelder
                stmt.setString(3, objectFormatId.getValue());
1511 5895 berkley
            }
1512 6299 leinfelder
            else if (f1 && f2 && !f3) {
1513 5895 berkley
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1514
                stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
1515
            }
1516 6299 leinfelder
            else if (f1 && !f2 && f3) {
1517 5895 berkley
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1518 6337 leinfelder
                stmt.setString(2, objectFormatId.getValue());
1519 5895 berkley
            }
1520 6299 leinfelder
            else if (f1 && !f2 && !f3) {
1521 5895 berkley
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
1522
            }
1523 6299 leinfelder
            else if (!f1 && f2 && f3) {
1524 5895 berkley
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
1525 6337 leinfelder
                stmt.setString(2, objectFormatId.getValue());
1526 5895 berkley
            }
1527 6299 leinfelder
            else if (!f1 && !f2 && f3) {
1528 6337 leinfelder
                stmt.setString(1, objectFormatId.getValue());
1529 5895 berkley
            }
1530 6299 leinfelder
            else if (!f1 && f2 && !f3) {
1531 5895 berkley
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
1532
            }
1533
1534 6099 leinfelder
            //logMetacat.debug("LISTOBJECTS QUERY: " + stmt.toString());
1535 5895 berkley
1536
            ResultSet rs = stmt.executeQuery();
1537 6299 leinfelder
            for (int i=0; i<start; i++) {
1538
                if (rs.next()) {
1539 5917 berkley
                    total++;
1540 6299 leinfelder
                } else {
1541 5917 berkley
                    break;
1542
                }
1543 5895 berkley
            }
1544 5917 berkley
1545 5895 berkley
            int countIndex = 0;
1546
1547 6299 leinfelder
            while (rs.next()) {
1548 5895 berkley
                total++;
1549 6299 leinfelder
                if (countIndex >= count) {
1550
                	// allow unlimited (negative number for count)
1551
                	if (count > 0) {
1552
                		break;
1553
                	}
1554 5895 berkley
                }
1555 6318 cjones
1556 5895 berkley
                String guid = rs.getString(1);
1557 6099 leinfelder
                //logMetacat.debug("query found doc with guid " + guid);
1558 5895 berkley
                //Timestamp dateUploaded = rs.getTimestamp(2);
1559
                //String rightsHolder = rs.getString(3);
1560 6318 cjones
                String checksum = rs.getString(4);
1561
                String checksumAlgorithm = rs.getString(5);
1562 5895 berkley
                //String originMemberNode = rs.getString(6);
1563 6318 cjones
                //String authoritiveMemberNode = rs.getString(7);
1564
                Timestamp dateModified = rs.getTimestamp(8);
1565 5895 berkley
                //String submitter = rs.getString(9);
1566 6318 cjones
                String fmtidStr = rs.getString(10);
1567
                String sz = rs.getString(11);
1568 6384 cjones
                BigInteger size = new BigInteger("0");
1569 6299 leinfelder
1570
                if (sz != null && !sz.trim().equals("")) {
1571 6384 cjones
                    size = new BigInteger(rs.getString(11));
1572 5917 berkley
                }
1573 5895 berkley
1574
                ObjectInfo oi = new ObjectInfo();
1575
1576
                Identifier id = new Identifier();
1577
                id.setValue(guid);
1578
                oi.setIdentifier(id);
1579
1580 6299 leinfelder
                if (dateModified != null) {
1581
                	oi.setDateSysMetadataModified(new Date(dateModified.getTime()));
1582
                }
1583 5895 berkley
1584
                Checksum cs = new Checksum();
1585
                cs.setValue(checksum);
1586 6299 leinfelder
                try {
1587 6368 leinfelder
                	//cs.setAlgorithm(ChecksumAlgorithm.valueOf(checksumAlgorithm));
1588 6397 leinfelder
                    cs.setAlgorithm(checksumAlgorithm);
1589 6299 leinfelder
                } catch (Exception e) {
1590 6516 leinfelder
					logMetacat.error("could not parse checksum algorithm", e);
1591
					continue;
1592 6299 leinfelder
				}
1593 5895 berkley
                oi.setChecksum(cs);
1594
1595 6519 leinfelder
                // set the format type
1596
                ObjectFormatIdentifier fmtid = new ObjectFormatIdentifier();
1597
            	fmtid.setValue(fmtidStr);
1598 6561 leinfelder
                oi.setFormatId(fmtid);
1599 6519 leinfelder
1600
//                try {
1601
//	                oi.setFmtid(ObjectFormatCache.getInstance().getFormat(fmtidStr).getFmtid());
1602
//                } catch (NotFound e) {
1603
//                	logMetacat.error("could not find object format: " + fmtidStr, e);
1604
//                	// skip this one
1605
//                	continue;
1606
//				}
1607 5917 berkley
1608
                oi.setSize(size);
1609 5895 berkley
1610
                ol.addObjectInfo(oi);
1611
                countIndex++;
1612
            }
1613
1614 6299 leinfelder
            // expend the resultset to get the total count of possible rows
1615
            while (rs.next()) {
1616 5895 berkley
                total++;
1617
            }
1618
        }
1619 6299 leinfelder
1620
        finally {
1621 5943 berkley
            // Return database connection to the pool
1622
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1623
        }
1624 5895 berkley
1625
        ol.setStart(start);
1626 5922 berkley
        ol.setCount(ol.sizeObjectInfoList());
1627 5895 berkley
        ol.setTotal(total);
1628
1629
        return ol;
1630
    }
1631
1632
    /**
1633 6099 leinfelder
     * create a mapping in the identifier table
1634 5377 berkley
     * @param guid
1635
     * @param localId
1636
     */
1637 6099 leinfelder
    public void createMapping(String guid, String localId)
1638 5453 berkley
    {
1639 5334 berkley
1640
        int serialNumber = -1;
1641 5333 berkley
        DBConnection dbConn = null;
1642
        try {
1643
1644
            // Parse the localId into scope and rev parts
1645
            AccessionNumber acc = new AccessionNumber(localId, "NOACTION");
1646
            String docid = acc.getDocid();
1647 5344 berkley
            int rev = 1;
1648 6099 leinfelder
            if (acc.getRev() != null) {
1649 5344 berkley
              rev = (new Integer(acc.getRev()).intValue());
1650
            }
1651 5333 berkley
1652
            // Get a database connection from the pool
1653 6099 leinfelder
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
1654 5333 berkley
            serialNumber = dbConn.getCheckOutSerialNumber();
1655
1656
            // Execute the insert statement
1657 6099 leinfelder
            String query = "insert into " + TYPE_IDENTIFIER + " (guid, docid, rev) values (?, ?, ?)";
1658 5333 berkley
            PreparedStatement stmt = dbConn.prepareStatement(query);
1659
            stmt.setString(1, guid);
1660
            stmt.setString(2, docid);
1661
            stmt.setInt(3, rev);
1662 6099 leinfelder
            logMetacat.debug("mapping query: " + stmt.toString());
1663 5333 berkley
            int rows = stmt.executeUpdate();
1664
1665
            stmt.close();
1666
        } catch (SQLException e) {
1667 5344 berkley
            e.printStackTrace();
1668 6099 leinfelder
            logMetacat.error("createGenericMapping: SQL error while creating a mapping to the " + TYPE_IDENTIFIER + " identifier: "
1669 5333 berkley
                    + e.getMessage());
1670
        } catch (NumberFormatException e) {
1671 5344 berkley
            e.printStackTrace();
1672 6099 leinfelder
            logMetacat.error("createGenericMapping: NumberFormat error while creating a mapping to the " + TYPE_IDENTIFIER + " identifier: "
1673 5333 berkley
                    + e.getMessage());
1674
        } catch (AccessionNumberException e) {
1675 5344 berkley
            e.printStackTrace();
1676 6099 leinfelder
            logMetacat.error("createGenericMapping: AccessionNumber error while creating a mapping to the " + TYPE_IDENTIFIER + " identifier: "
1677 5333 berkley
                    + e.getMessage());
1678
        } finally {
1679
            // Return database connection to the pool
1680
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1681
        }
1682
    }
1683 6099 leinfelder
1684
    /**
1685
     * create the systemmetadata record
1686
     * @param guid
1687
     */
1688 6337 leinfelder
    private void insertSystemMetadata(String guid)
1689 6099 leinfelder
    {
1690
1691
        int serialNumber = -1;
1692
        DBConnection dbConn = null;
1693
        try {
1694
1695
            // Get a database connection from the pool
1696
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.insertSystemMetadata");
1697
            serialNumber = dbConn.getCheckOutSerialNumber();
1698
1699
            // Execute the insert statement
1700 6337 leinfelder
            String query = "insert into " + TYPE_SYSTEM_METADATA + " (guid) values (?)";
1701 6099 leinfelder
            PreparedStatement stmt = dbConn.prepareStatement(query);
1702
            stmt.setString(1, guid);
1703
            logMetacat.debug("system metadata query: " + stmt.toString());
1704
            int rows = stmt.executeUpdate();
1705
1706
            stmt.close();
1707
        } catch (Exception e) {
1708
            e.printStackTrace();
1709 6277 leinfelder
            logMetacat.error("Error while creating " + TYPE_SYSTEM_METADATA + " record: " + guid, e );
1710 6099 leinfelder
        } finally {
1711
            // Return database connection to the pool
1712
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1713
        }
1714
    }
1715 6277 leinfelder
1716 6648 leinfelder
    public void deleteSystemMetadata(String guid)
1717 6277 leinfelder
    {
1718
1719
        int serialNumber = -1;
1720
        DBConnection dbConn = null;
1721 6648 leinfelder
        String query = null;
1722
        PreparedStatement stmt = null;
1723
        int rows = 0;
1724 6277 leinfelder
        try {
1725
1726
            // Get a database connection from the pool
1727 6648 leinfelder
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.deleteSystemMetadata");
1728 6277 leinfelder
            serialNumber = dbConn.getCheckOutSerialNumber();
1729
1730 6648 leinfelder
            // remove main system metadata entry
1731
            query = "delete from " + TYPE_SYSTEM_METADATA + " where guid = ? ";
1732
            stmt = dbConn.prepareStatement(query);
1733 6277 leinfelder
            stmt.setString(1, guid);
1734
            logMetacat.debug("delete system metadata: " + stmt.toString());
1735 6648 leinfelder
            rows = stmt.executeUpdate();
1736 6277 leinfelder
            stmt.close();
1737 6648 leinfelder
1738
            // remove the systemMetadataReplicationPolicy
1739
            query = "delete from systemMetadataReplicationPolicy " +
1740
            "where guid = ?";
1741
            stmt = dbConn.prepareStatement(query);
1742
            stmt.setString(1, guid);
1743
            logMetacat.debug("delete systemMetadataReplicationPolicy: " + stmt.toString());
1744
            rows = stmt.executeUpdate();
1745
            stmt.close();
1746
1747
            // remove the systemMetadataReplicationStatus
1748
            query = "delete from systemMetadataReplicationStatus " +
1749
            "where guid = ?";
1750
            stmt = dbConn.prepareStatement(query);
1751
            stmt.setString(1, guid);
1752
            logMetacat.debug("delete systemMetadataReplicationStatus: " + stmt.toString());
1753
            rows = stmt.executeUpdate();
1754
            stmt.close();
1755
1756
            // TODO: remove the xml_access?
1757
            // Metacat keeps "deleted" documents so we should not remove access rules.
1758
1759 6277 leinfelder
        } catch (Exception e) {
1760
            e.printStackTrace();
1761
            logMetacat.error("Error while deleting " + TYPE_SYSTEM_METADATA + " record: " + guid, e );
1762 6648 leinfelder
            try {
1763
				dbConn.rollback();
1764
			} catch (SQLException sqle) {
1765
	            logMetacat.error("Error while rolling back delete for record: " + guid, sqle );
1766
			}
1767 6277 leinfelder
        } finally {
1768
            // Return database connection to the pool
1769
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1770
        }
1771
    }
1772 5282 jones
}