Project

General

Profile

« Previous | Next » 

Revision 6099

-remove system metadata guid -> local id mapping (there is no document for system metadata now)
-include system metadata elements when replicating data objects (TODO: transfer all system metadata structures with the docinfo request).
TODO: remove docid+rev from the systemMetadata table definition

View differences:

IdentifierManager.java
45 45
import edu.ucsb.nceas.metacat.database.DBConnection;
46 46
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
47 47
import edu.ucsb.nceas.metacat.properties.PropertyService;
48
import edu.ucsb.nceas.metacat.shared.ServiceException;
49 48
import edu.ucsb.nceas.metacat.util.DocumentUtil;
50
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
51 49

  
52 50
/**
53 51
 * Manage the relationship between Metacat local identifiers (LocalIDs) that are
54
 * codified as the (docid, rev) pair with globally uniqe string identifiers
52
 * codified as the (docid, rev) pair with globally unique string identifiers
55 53
 * (GUIDs) that are opaque strings.  This class provides methods to manage these
56 54
 * identifiers, and to search for and look up LocalIDs based on their GUID and
57 55
 * vice versa. IdentifierManager is a singleton.
......
62 60
    
63 61
    public static final String TYPE_SYSTEM_METADATA = "systemmetadata";
64 62
    public static final String TYPE_IDENTIFIER = "identifier";
65
    public static String DATAONE_SM_DOCTYPE;
66 63
  
67 64
    /**
68 65
     * The single instance of the manager that is always returned.
69 66
     */
70 67
    private static IdentifierManager self = null;
71
    private Logger logMetacat = Logger.getLogger(EventLog.class);
68
    private Logger logMetacat = Logger.getLogger(IdentifierManager.class);
72 69

  
73 70
    /**
74 71
     * A private constructor that initializes the class when getInstance() is
75 72
     * called.
76 73
     */
77
    private IdentifierManager()
78
    {
79
        try
80
        {
81
            DATAONE_SM_DOCTYPE = PropertyService.getProperty("crudService.listObjects.ReturnDoctype");
82
        }
83
        catch(Exception e)
84
        {
85
            throw new RuntimeException("Error getting System Metadata doctype from " + 
86
                    "the properties file.  Please make sure crudService.listObjects.ReturnDoctype " +
87
                    "exists in metacat.properties.");
88
        }
89
    }
74
    private IdentifierManager() {}
90 75

  
91 76
    /**
92 77
     * Return the single instance of the manager after initializing it if it
......
220 205
    
221 206
    /**
222 207
     * return a hash of all of the info that is in the systemmetadata table
223
     * @param localId
208
     * @param guid
224 209
     * @return
225 210
     */
226
    public SystemMetadata getSystemMetadata(String localId)
211
    public SystemMetadata getSystemMetadata(String guid)
227 212
    	throws McdbDocNotFoundException
228 213
    {
229
        try
230
        {
231
            AccessionNumber acc = new AccessionNumber(localId, "NONE");
232
            localId = acc.getDocid();
233
        }
234
        catch(Exception e)
235
        {
236
            //do nothing. just try the localId as it is
237
        }
214
        
238 215
        SystemMetadata sysMeta = new SystemMetadata();
239 216
        String sql = "select guid, date_uploaded, rights_holder, checksum, checksum_algorithm, " +
240 217
          "origin_member_node, authoritive_member_node, date_modified, submitter, object_format, size " +
241
          "from systemmetadata where docid = ?";
218
          "from systemmetadata where guid = ?";
242 219
        DBConnection dbConn = null;
243 220
        int serialNumber = -1;
244 221
        try 
......
249 226

  
250 227
            // Execute the statement
251 228
            PreparedStatement stmt = dbConn.prepareStatement(sql);
252
            stmt.setString(1, localId);
229
            stmt.setString(1, guid);
253 230
            ResultSet rs = stmt.executeQuery();
254 231
            if (rs.next()) 
255 232
            {
256
                String guid = rs.getString(1);
257 233
                Timestamp dateUploaded = rs.getTimestamp(2);
258 234
                String rightsHolder = rs.getString(3);
259 235
                String checksum = rs.getString(4);
......
295 271
            {
296 272
                stmt.close();
297 273
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
298
                throw new McdbDocNotFoundException("Could not find " + localId);
274
                throw new McdbDocNotFoundException("Could not find " + guid);
299 275
            }
300 276
            
301 277
        } 
302 278
        catch (SQLException e) 
303 279
        {
304 280
            e.printStackTrace();
305
            logMetacat.error("Error while getting system metadata for localid " + localId + " : "  
281
            logMetacat.error("Error while getting system metadata for guid " + guid + " : "  
306 282
                    + e.getMessage());
307 283
        } 
308 284
        finally 
......
430 406
            
431 407
            String sql2 = "select principal_name, permission, perm_type, perm_order from xml_access " +
432 408
            "where docid like '" + localId + "'";
433
            System.out.println("executing sql: " + sql2);
409
            logMetacat.debug("executing sql: " + sql2);
434 410
            PreparedStatement stmt2 = dbConn.prepareStatement(sql2);
435 411
            rs = stmt2.executeQuery();
436 412
            Vector accessVector = new Vector();
......
445 421
                accessHash.put("permission", permission);
446 422
                accessHash.put("permission_type", permissionType);
447 423
                accessHash.put("permission_order", permissionOrder);
448
                System.out.println("accessHash: " + accessHash.toString());
424
                logMetacat.debug("accessHash: " + accessHash.toString());
449 425
                accessVector.add(accessHash);
450 426
            }
451 427
            h.put("access", accessVector);
......
521 497
    
522 498
    /**
523 499
     * return all local ids in the object store that do not have associated
524
     * system metadata and are not themselves system metadata
500
     * system metadata
525 501
     */
526 502
    public List<String> getLocalIdsWithNoSystemMetadata()
527 503
    {
528 504
        Vector<String> ids = new Vector<String>();
529
        //String sql = "select docid from identifier where guid in (select guid from systemmetadata)"; 
530
        //String sql = "select docid from xml_documents where docid not " +
531
        //    "in (select docid from identifier where guid in (select guid from systemmetadata)) " +
532
        //    "and doctype not like '" + DATAONE_SM_DOCTYPE + "'";
533
        String sql = "select docid, rev from xml_documents where docid not in " +
534
            "(select docid from systemmetadata) and (docid not in " +
535
            "(select docid from identifier where guid in (select guid from systemmetadata)))";
505
        String sql = "select docid, rev from xml_documents " +
506
        		"where docid not in " +
507
        		"(select docid from identifier where guid in (select guid from systemmetadata)))";
536 508
        DBConnection dbConn = null;
537 509
        int serialNumber = -1;
538 510
        try 
......
549 521
                String localid = rs.getString(1);
550 522
                String rev = rs.getString(2);
551 523
                localid += "." + rev;
552
                System.out.println("id to add SM for: " + localid);
524
                logMetacat.debug("id to add SM for: " + localid);
553 525
                ids.add(localid);
554 526
            } 
555 527
            stmt.close();
......
608 580
        return ids;
609 581
    }
610 582
    
611
    /**
612
     * Lookup a GUID from the database, and return the associated LocalID. If
613
     * the identifier is not found, throw an exception.
614
     * 
615
     * @param guid the global identifier to look up
616
     * @return String containing the corresponding LocalId
617
     * @throws McdbDocNotFoundException if the identifier is not found
618
     */
619
    public String getLocalId(String guid) throws McdbDocNotFoundException
620
    {
621
        return this.getLocalId(guid, TYPE_IDENTIFIER);
622
    }
583

  
623 584
    
624 585
    /**
625 586
     * Determine if an identifier exists already, returning true if so.
......
642 603
    }
643 604
    
644 605
    /**
645
     * Create a mapping between two identifiers, one representing an 
646
     * unconstrained, globally unique string (guid), and one a localId 
647
     * in the Metacat docid format (scope.id.revision). 
648
     * This mapping allows one to find the global id (guid) from the localId.
649 606
     * 
650
     * @param guid the global string identifier to be mapped
651
     * @param localId the local identifier to be mapped
652
     */
653
    public void createMapping(String guid, String localId)
654
    {   
655
        createGenericMapping(guid, localId, TYPE_IDENTIFIER);
656
    }
657
    
658
    /**
659
     * 
660 607
     * @param guid
661 608
     * @param rev
662 609
     * @return
......
706 653
        }
707 654
        
708 655
        // Register this new pair in the identifier mapping table
709
        System.out.println("creating mapping in generateLocalId");
656
        logMetacat.debug("creating mapping in generateLocalId");
710 657
        if(!isSystemMetadata)
711 658
        { //don't do this if we're generating for system metadata
712 659
            createMapping(guid, localId);
......
727 674
    public String getGUID(String docid, int rev)
728 675
      throws McdbDocNotFoundException
729 676
    {
730
        System.out.println("getting guid for " + docid);
677
        logMetacat.debug("getting guid for " + docid);
731 678
        String query = "select guid from identifier where docid = ? and rev = ?";
732 679
        String guid = null;
733
        boolean found = false;
734 680
        
735 681
        DBConnection dbConn = null;
736 682
        int serialNumber = -1;
......
777 723
    }
778 724
    
779 725
    /**
780
     * creates a mapping for a system metadata document, but DOES NOT add any
781
     * of the extra system metadata information to the table.  You must call 
782
     * insertAdditionalSystemMetadataFields to complete the entry
783
     * 
784
     * @param guid the id to insert
785
     * @param localId the systemMetadata object to get the local id for
786
     */
787
    public void createSystemMetadataMapping(String guid, String localId)
788
    {
789
        createGenericMapping(guid, localId, TYPE_SYSTEM_METADATA);
790
    }
791
    
792
    /**
793 726
     * creates a system metadata mapping and adds additional fields from sysmeta
794 727
     * to the table for quick searching.
795 728
     * 
796 729
     * @param guid the id to insert
797 730
     * @param localId the systemMetadata object to get the local id for
798 731
     */
799
    public void createSystemMetadataMapping(SystemMetadata sysmeta, String localId)
732
    public void createSystemMetadata(SystemMetadata sysmeta)
800 733
    {
801
        createGenericMapping(sysmeta.getIdentifier().getValue(), localId, TYPE_SYSTEM_METADATA);
802
        insertAdditionalSystemMetadataFields(sysmeta);
734
        insertSystemMetadata(sysmeta.getIdentifier().getValue());
735
        updateSystemMetadata(sysmeta);
803 736
    }
737
        
804 738
    
805 739
    /**
806
     * update a system metadata mapping
807
     * @param guid
808
     * @param localId
809
     */
810
    public void updateSystemMetadataMapping(String guid, String localId)
811
    {
812
    	updateMapping(guid, localId, TYPE_SYSTEM_METADATA);
813
    }
814
    
815
    /**
816 740
     * update a mapping
817 741
     * @param guid
818 742
     * @param localId
819 743
     */
820 744
    public void updateMapping(String guid, String localId)
821 745
    {
822
    	updateMapping(guid, localId, TYPE_IDENTIFIER);
823
    }
824
    
825
    /**
826
     * update a mapping
827
     * @param guid
828
     * @param localId
829
     */
830
    private void updateMapping(String guid, String localId, String type)
831
    {
832
    	if (!type.equals(TYPE_IDENTIFIER) && !type.equals(TYPE_SYSTEM_METADATA)) {
833
    		throw new RuntimeException("Cannot create mapping for type " + type +
834
    		            ".  Please choose 'identifier' or 'systemmetadata'.");
835
    	}
836 746
    	
837
        System.out.println("$$$$$$$$$$$$$$ updating mapping table");
747
        logMetacat.debug("$$$$$$$$$$$$$$ updating mapping table");
838 748
        int serialNumber = -1;
839 749
        DBConnection dbConn = null;
840 750
        try {
......
852 762
                DBConnectionPool.getDBConnection("IdentifierManager.updateMapping");
853 763
            serialNumber = dbConn.getCheckOutSerialNumber();
854 764

  
855
            // Execute the insert statement
856
            String query = "update " + type + " set (docid, rev) = (?, ?) where guid='" + guid + "'";
857
            //System.out.println("query: " + query + " for params: (guid:" + guid + ", docid=" + docid + ", rev=" + rev + ")");
765
            // Execute the update statement
766
            String query = "update " + TYPE_IDENTIFIER + " set (docid, rev) = (?, ?) where guid='" + guid + "'";
858 767
            PreparedStatement stmt = dbConn.prepareStatement(query);
859 768
            stmt.setString(1, docid);
860 769
            stmt.setInt(2, rev);
......
877 786
            // Return database connection to the pool
878 787
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
879 788
        }
880
        System.out.println("done updating mapping");
789
        logMetacat.debug("done updating mapping");
881 790
    }
882
    
883
    /**
884
     * return the localId of a system metadata document based on its guid
885
     */
886
    public String getSystemMetadataLocalId(String guid)
887
      throws McdbDocNotFoundException
888
    {
889
      return this.getLocalId(guid, TYPE_SYSTEM_METADATA);
890
    }
891
    
892
    private void insertAdditionalSystemMetadataFields(long dateUploaded, String rightsHolder,
791
        
792
    private void updateSystemMetadataFields(long dateUploaded, String rightsHolder,
893 793
            String checksum, String checksumAlgorithm, String originMemberNode,
894 794
            String authoritativeMemberNode, long modifiedDate, String submitter, 
895 795
            String guid, String objectFormat, long size)
......
924 824
            stmt.setString(10, new Long(size).toString());
925 825
            //where clause
926 826
            stmt.setString(11, guid);
927
            System.out.println("stmt: " + stmt.toString());
827
            logMetacat.debug("stmt: " + stmt.toString());
928 828
            //execute
929 829
            int rows = stmt.executeUpdate();
930 830

  
......
992 892
     * Insert the system metadata fields into the db
993 893
     * @param sm
994 894
     */
995
    public void insertAdditionalSystemMetadataFields(SystemMetadata sm)
895
    public void updateSystemMetadata(SystemMetadata sm)
996 896
    {
997
        insertAdditionalSystemMetadataFields(
897
        updateSystemMetadataFields(
998 898
                sm.getDateUploaded().getTime(),
999 899
                sm.getRightsHolder().getValue(), 
1000 900
                sm.getChecksum().getValue(), 
......
1050 950
    }
1051 951
    
1052 952
    /**
1053
     * return a localId based on a guid.  The type can either be 'identifier' 
1054
     * to get an id from the identifier table or 'systemmetadata' to get
1055
     * the identifier from the systemidentifier table
953
     * Lookup a localId given the GUID. If
954
     * the identifier is not found, throw an exception.
955
     * 
956
     * @param guid the global identifier to look up
957
     * @return String containing the corresponding LocalId
958
     * @throws McdbDocNotFoundException if the identifier is not found
1056 959
     */
1057
    private String getLocalId(String guid, String type)
1058
      throws McdbDocNotFoundException
1059
    {
1060
      if(!type.equals(TYPE_IDENTIFIER) &&
1061
        !type.equals(TYPE_SYSTEM_METADATA))
1062
      {
1063
        throw new RuntimeException("cannot lookup the identifier for type " + type +
1064
          ".  Please choose 'identifier' or 'systemmetadata'.");
1065
      }
960
    public String getLocalId(String guid) throws McdbDocNotFoundException {
1066 961
      
1067 962
      String db_guid = "";
1068 963
      String docid = "";
1069 964
      int rev = 0;
1070 965
      
1071
      String query = "select guid, docid, rev from " + type + " where guid = ?";
1072
      //System.out.println("query: " + query + " for params: (" + guid + ")");
966
      String query = "select guid, docid, rev from " + TYPE_IDENTIFIER + " where guid = ?";
1073 967
      
1074 968
      DBConnection dbConn = null;
1075 969
      int serialNumber = -1;
......
1212 1106
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
1213 1107
            }
1214 1108
            
1215
            //System.out.println("LISTOBJECTS QUERY: " + stmt.toString());
1109
            //logMetacat.debug("LISTOBJECTS QUERY: " + stmt.toString());
1216 1110
            
1217 1111
            ResultSet rs = stmt.executeQuery();
1218 1112
            for(int i=0; i<start; i++)
......
1237 1131
                    break;
1238 1132
                }
1239 1133
                String guid = rs.getString(1);
1240
                //System.out.println("query found doc with guid " + guid);
1134
                //logMetacat.debug("query found doc with guid " + guid);
1241 1135
                //Timestamp dateUploaded = rs.getTimestamp(2);
1242 1136
                //String rightsHolder = rs.getString(3);
1243 1137
                String checksum = rs.getString(4);
......
1292 1186
        catch(Exception e)
1293 1187
        {
1294 1188
           e.printStackTrace();
1295
           System.out.println("Error querying system metadata: " + e.getMessage());
1189
           logMetacat.error("Error querying system metadata: " + e.getMessage());
1296 1190
        }
1297 1191
        finally 
1298 1192
        {
......
1308 1202
    }
1309 1203
    
1310 1204
    /**
1311
     * create a mapping in the systemmetadata or identifier table
1205
     * create a mapping in the identifier table
1312 1206
     * @param guid
1313 1207
     * @param localId
1314
     * @param type
1315 1208
     */
1316
    private void createGenericMapping(String guid, String localId, String type)
1209
    public void createMapping(String guid, String localId)
1317 1210
    {        
1318
        if(!type.equals(TYPE_IDENTIFIER) &&
1319
        !type.equals(TYPE_SYSTEM_METADATA))
1320
        {
1321
          throw new RuntimeException("Cannot create mapping for type " + type +
1322
            ".  Please choose 'identifier' or 'systemmetadata'.");
1323
        }
1324 1211
        
1325 1212
        int serialNumber = -1;
1326 1213
        DBConnection dbConn = null;
......
1330 1217
            AccessionNumber acc = new AccessionNumber(localId, "NOACTION");
1331 1218
            String docid = acc.getDocid();
1332 1219
            int rev = 1;
1333
            if(acc.getRev() != null)
1334
            {
1220
            if (acc.getRev() != null) {
1335 1221
              rev = (new Integer(acc.getRev()).intValue());
1336 1222
            }
1337 1223

  
1338 1224
            // Get a database connection from the pool
1339
            dbConn = 
1340
                DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
1225
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
1341 1226
            serialNumber = dbConn.getCheckOutSerialNumber();
1342 1227

  
1343 1228
            // Execute the insert statement
1344
            String query = "insert into " + type + " (guid, docid, rev) values (?, ?, ?)";
1345
            //System.out.println("query: " + query + " for params: (" + guid + ", " + docid + ", " + rev + ")");
1229
            String query = "insert into " + TYPE_IDENTIFIER + " (guid, docid, rev) values (?, ?, ?)";
1346 1230
            PreparedStatement stmt = dbConn.prepareStatement(query);
1347 1231
            stmt.setString(1, guid);
1348 1232
            stmt.setString(2, docid);
1349 1233
            stmt.setInt(3, rev);
1350
            System.out.println("generic mapping query: " + stmt.toString());
1234
            logMetacat.debug("mapping query: " + stmt.toString());
1351 1235
            int rows = stmt.executeUpdate();
1352 1236

  
1353 1237
            stmt.close();
1354 1238
        } catch (SQLException e) {
1355 1239
            e.printStackTrace();
1356
            logMetacat.error("createGenericMapping: SQL error while creating a mapping to the " + type + " identifier: " 
1240
            logMetacat.error("createGenericMapping: SQL error while creating a mapping to the " + TYPE_IDENTIFIER + " identifier: " 
1357 1241
                    + e.getMessage());
1358 1242
        } catch (NumberFormatException e) {
1359 1243
            e.printStackTrace();
1360
            logMetacat.error("createGenericMapping: NumberFormat error while creating a mapping to the " + type + " identifier: " 
1244
            logMetacat.error("createGenericMapping: NumberFormat error while creating a mapping to the " + TYPE_IDENTIFIER + " identifier: " 
1361 1245
                    + e.getMessage());
1362 1246
        } catch (AccessionNumberException e) {
1363 1247
            e.printStackTrace();
1364
            logMetacat.error("createGenericMapping: AccessionNumber error while creating a mapping to the " + type + " identifier: " 
1248
            logMetacat.error("createGenericMapping: AccessionNumber error while creating a mapping to the " + TYPE_IDENTIFIER + " identifier: " 
1365 1249
                    + e.getMessage());
1366 1250
        } finally {
1367 1251
            // Return database connection to the pool
1368 1252
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1369 1253
        }
1370 1254
    }
1255
    
1256
    /**
1257
     * create the systemmetadata record
1258
     * @param guid
1259
     */
1260
    public void insertSystemMetadata(String guid)
1261
    {        
1262
        
1263
        int serialNumber = -1;
1264
        DBConnection dbConn = null;
1265
        try {
1266

  
1267
            // Get a database connection from the pool
1268
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.insertSystemMetadata");
1269
            serialNumber = dbConn.getCheckOutSerialNumber();
1270

  
1271
            // Execute the insert statement
1272
            String query = "insert into " + TYPE_SYSTEM_METADATA + " (guid) values (?)";
1273
            PreparedStatement stmt = dbConn.prepareStatement(query);
1274
            stmt.setString(1, guid);
1275
            logMetacat.debug("system metadata query: " + stmt.toString());
1276
            int rows = stmt.executeUpdate();
1277

  
1278
            stmt.close();
1279
        } catch (Exception e) {
1280
            e.printStackTrace();
1281
            logMetacat.error("Error while creating " + TYPE_SYSTEM_METADATA + " record: " 
1282
                    + e.getMessage());
1283
        } finally {
1284
            // Return database connection to the pool
1285
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1286
        }
1287
    }
1371 1288
}
1372 1289

  

Also available in: Unified diff