Project

General

Profile

« Previous | Next » 

Revision 6105

rework SystemMetadata creation when inserting documents via the Metacat servlet api (in which case there was no client-supplued system metadata)

View differences:

src/edu/ucsb/nceas/metacat/MetacatHandler.java
1852 1852
              // calls to handleInsertOrUpdateAction()
1853 1853
              if ( !(ipAddress.contains("/d1")) ) {
1854 1854
                
1855
                SystemMetadata sysMeta = new SystemMetadata();  
1856
                IdentifierManager im = IdentifierManager.getInstance();
1857
                Hashtable<String, Object> docInfo = 
1858
                  im.getDocumentInfo(newdocid);
1859
                int rev = im.getLatestRevForLocalId(newdocid);
1860
                Identifier identifier = new Identifier();
1861
                String sysMetaLocalId = "";
1862
                
1863
                // it's possible that system metadata exists although
1855
                SystemMetadata sysMeta = null;
1856
				// it's possible that system metadata exists although
1864 1857
                // older clients don't support it. Try updates first.
1865 1858
                try {
1866
                  // handle updates
1867
                  identifier.setValue(im.getGUID(newdocid, rev));
1868
                  sysMeta = getSystemMetadata(identifier);
1869
                  IdentifierManager.getInstance().updateSystemMetadata(sysMeta);
1859
                	// get the docid parts
1860
                	String docidWithoutRev = DocumentUtil.getSmartDocId(newdocid);
1861
                    int rev = IdentifierManager.getInstance().getLatestRevForLocalId(newdocid);
1862
                	String guid = IdentifierManager.getInstance().getGUID(docidWithoutRev, rev);
1863
                	sysMeta  = IdentifierManager.getInstance().getSystemMetadata(guid);
1864
                	// TODO: need to update? we just looked it up
1865
                	//IdentifierManager.getInstance().updateSystemMetadata(sysMeta);
1870 1866
                  
1871 1867
                } catch ( McdbDocNotFoundException mnfe) {
1872 1868
                  
1873 1869
                  // handle inserts
1874 1870
                  try {
1875 1871
                    sysMeta = createSystemMetadata(newdocid, user, groups);
1872
                    IdentifierManager.getInstance().createSystemMetadata(sysMeta);
1876 1873
                    IdentifierManager.getInstance().updateSystemMetadata(sysMeta);
1877 1874
                    
1878 1875
                  } catch ( McdbDocNotFoundException dnfe ) {
......
3528 3525
      logMetacat.debug("provided username: " + username);
3529 3526
      
3530 3527
      // create system metadata for the document
3531
      IdentifierManager im = IdentifierManager.getInstance();
3532
      Hashtable<String, Object> docInfo;
3533
      docInfo = im.getDocumentInfo(localId);
3534 3528
      SystemMetadata sysMeta = new SystemMetadata();
3535
      InputStream inputStream;
3536
      int rev = im.getLatestRevForLocalId(localId);
3529
      int rev = IdentifierManager.getInstance().getLatestRevForLocalId(localId);
3537 3530
      AccessionNumber accNum = new AccessionNumber(localId, "NONE");
3538
      Identifier identifier = new Identifier();
3539
      
3531
      String guid = null;
3540 3532
      try {
3541
        // get the identifier if it exists
3542
        identifier.setValue(im.getGUID(accNum.getDocid(), rev));
3543
        
3533
    	  // get the guid if it exists
3534
    	  guid = IdentifierManager.getInstance().getGUID(accNum.getDocid(), rev);
3544 3535
      } catch ( McdbDocNotFoundException dnfe ) {
3545 3536
        // otherwise create the mapping
3546 3537
        logMetacat.debug("There was a problem getting the guid from " +
3547 3538
          "the given localId (docid and revision). The error message was: " + dnfe.getMessage());
3548 3539
        logMetacat.debug("No guid in the identifier table.  adding it for " + localId);
3549
        im.createMapping(localId, localId);
3540
        IdentifierManager.getInstance().createMapping(localId, localId);
3550 3541
        logMetacat.debug("Mapping created for " + localId);
3551 3542
        logMetacat.debug("accessionNumber: " + accNum);
3552
        identifier.setValue(im.getGUID(accNum.getDocid(), rev));
3553
        
3543
  	  	guid = IdentifierManager.getInstance().getGUID(accNum.getDocid(), rev);
3554 3544
      }
3545
      Identifier identifier = new Identifier();
3546
      identifier.setValue(guid);
3555 3547
      
3548
      //set the id
3549
      sysMeta.setIdentifier(identifier);
3550
      
3556 3551
      // get the data or metadata object
3552
      InputStream inputStream;
3557 3553
      try {
3558 3554
          inputStream = this.read(localId);
3559
          
3560 3555
      } catch ( ParseLSIDException ple ) {
3561 3556
        logMetacat.debug("There was a problem parsing the LSID from " +
3562 3557
          localId + ". The error message was: " + ple.getMessage());
......
3588 3583
        throw ioe;
3589 3584
        
3590 3585
      } // end try()
3591
    
3592
      //set the id
3593
      sysMeta.setIdentifier(identifier);
3594 3586
      
3587
      // get additional docinfo
3588
      Hashtable<String, Object> docInfo = IdentifierManager.getInstance().getDocumentInfo(localId);
3595 3589
      //set the default object format
3596 3590
      String doctype = (String) docInfo.get("doctype");
3597 3591
      ObjectFormat format = ObjectFormat.convert(doctype);
......
3603 3597
          }
3604 3598
      }
3605 3599
      sysMeta.setObjectFormat(format);
3606
      logMetacat.debug("The ObjectFormat for " + localId + 
3607
                       " is " + format.toString());
3600
      logMetacat.debug("The ObjectFormat for " + localId + " is " + format.toString());
3608 3601
      
3609 3602
      // further parse EML documents to get data object format,
3610 3603
      // describes and describedBy information
3611 3604
      if ( format == ObjectFormat.EML_2_0_0 ||
3612 3605
           format == ObjectFormat.EML_2_0_1 ||
3613
           format == ObjectFormat.EML_2_1_0 ) {
3606
           format == ObjectFormat.EML_2_1_0 ||
3607
           format == ObjectFormat.EML_2_1_1 ) {
3614 3608
        
3615 3609
        try {
3616 3610
          DataoneEMLParser emlParser = DataoneEMLParser.getInstance();
......
3633 3627
                dataDocMimeType = distMetadata.mimeType;
3634 3628
            }
3635 3629
            String dataDocLocalId = "";
3636
            String dataSysMetaId = "";
3637 3630
            logMetacat.debug("Data local ID: " + dataDocLocalId);
3638 3631
            logMetacat.debug("Data URL     : " + dataDocUrl);
3639 3632
            logMetacat.debug("Data mime    : " + dataDocMimeType);
3640 3633
            
3641 3634
            //we only handle ecogrid urls right now
3642
            if ( dataDocUrl.trim().startsWith("ecogrid://knb/") ) {
3635
            String ecogridPrefix = "ecogrid://knb/";
3636
            if ( dataDocUrl.trim().startsWith(ecogridPrefix) ) {
3643 3637
              dataDocLocalId = 
3644
                dataDocUrl.substring(dataDocUrl.indexOf("ecogrid://knb/") + 
3645
                "ecogrid://knb/".length(), dataDocUrl.length());
3638
                dataDocUrl.substring(dataDocUrl.indexOf(ecogridPrefix) + ecogridPrefix.length());
3646 3639
              
3647
              //set the id
3648
              Identifier dataDocId = new Identifier();
3649
              dataDocId.setValue(dataDocLocalId);
3640
              // look up the guid for the data
3641
              String dataDocid = DocumentUtil.getSmartDocId(dataDocLocalId);
3642
              int dataRev = DocumentUtil.getRevisionFromAccessionNumber(dataDocLocalId);
3650 3643
              
3651
              // add describes into EML system metadata
3652
              sysMeta.addDescribe(dataDocId);
3653
              
3654
              SystemMetadata dataSysMeta = new SystemMetadata();
3644
              SystemMetadata dataSysMeta = null;
3655 3645
              // check if data system metadata exists
3646
              String dataGuidString = null;
3656 3647
              try {
3657
                logMetacat.debug("Checking for existing system metadata for " + 
3658
                  dataDocId.getValue());
3659
                dataSysMeta = this.getSystemMetadata(dataDocId);
3660
                // add describedBy sysmeta
3661
                logMetacat.debug("Setting describedBy for " + dataDocId.getValue() +
3662
                                 " to " + identifier.getValue());
3663
                dataSysMeta.addDescribedBy(identifier);
3664
                ObjectFormat f = ObjectFormat.convert(dataDocMimeType);
3665
                if (f == null) {
3666
                    f = ObjectFormat.OCTET_STREAM;
3667
                }
3668
                dataSysMeta.setObjectFormat(f);
3669
                IdentifierManager.getInstance().updateSystemMetadata(dataSysMeta);
3670
                
3648
            	  dataGuidString = IdentifierManager.getInstance().getGUID(dataDocid, dataRev);
3649
            	  dataSysMeta = IdentifierManager.getInstance().getSystemMetadata(dataGuidString);
3671 3650
              } catch ( McdbDocNotFoundException nf ) {
3672
                // System metadata for data doesn't exist
3673
                logMetacat.debug("There was not an existing system metadata " + 
3674
                  "document for " + dataDocId.getValue());
3675
                try {
3676
                  logMetacat.debug("Creating a system metadata " + 
3677
                    "document for " + dataDocId.getValue());
3678
                  dataSysMeta = 
3679
                    this.createSystemMetadata(dataDocLocalId, username, groups);
3651
            	  // System metadata for data doesn't exist yet, so create it
3652
            	  logMetacat.debug("There was not an existing system metadata " + "document for " + dataDocLocalId);
3653
            	  try {
3654
            		  logMetacat.debug("Creating a system metadata " + "document for " + dataDocLocalId);
3655
            		  dataSysMeta = this.createSystemMetadata(dataDocLocalId, username, groups);
3656
            		  
3657
            		  // now look it up again
3658
                	  dataGuidString = IdentifierManager.getInstance().getGUID(dataDocid, dataRev);
3659

  
3660
                	  //set the guid
3661
                      Identifier dataGuid = new Identifier();
3662
                      dataGuid.setValue(dataGuidString);
3663
                      
3664
                      // add describes into EML system metadata
3665
                      sysMeta.addDescribe(dataGuid);
3666

  
3667
                      // add describedBy sysmeta
3668
                      logMetacat.debug("Setting describedBy for " + dataGuid.getValue() + " to " + identifier.getValue());
3669
                      dataSysMeta.addDescribedBy(identifier);
3670
                      
3671
                      // set object format
3672
                      logMetacat.debug("Updating system metadata for " + dataGuid.getValue() + " to " + dataDocMimeType);
3673
                      ObjectFormat f = ObjectFormat.convert(dataDocMimeType);
3674
                      if (f == null) {
3675
                    	  f = ObjectFormat.OCTET_STREAM;
3676
                      }
3677
                      dataSysMeta.setObjectFormat(f);
3680 3678
                  
3681
                  logMetacat.debug("Setting describedBy for " + 
3682
                    dataDocId.getValue() + " to " + identifier.getValue());
3683
                  dataSysMeta.addDescribedBy(identifier);
3679
            		  // update the values
3680
            		  IdentifierManager.getInstance().updateSystemMetadata(dataSysMeta);
3684 3681
                  
3685
                  logMetacat.debug("Setting mimeType for " + 
3686
                    dataDocId.getValue() + " to " + dataDocMimeType);
3687
                  ObjectFormat f = ObjectFormat.convert(dataDocMimeType);
3688
                  if (f == null) {
3689
                      f = ObjectFormat.OCTET_STREAM;
3690
                  }
3691
                  dataSysMeta.setObjectFormat(f);
3692
                  
3693
                  logMetacat.debug("Updating system metadata for " + 
3694
                    dataDocId.getValue() + " to " + dataDocMimeType);
3695
                  IdentifierManager.getInstance().updateSystemMetadata(dataSysMeta);
3696
                  
3697 3682
                } catch ( McdbDocNotFoundException mdnf) {
3698 3683
                  mdnf.printStackTrace();
3699 3684
                  throw mdnf;
......
3718 3703
                } catch ( BaseException be) {
3719 3704
                  be.printStackTrace();
3720 3705
                  throw be;
3721
                  
3722 3706
                }
3723

  
3724
              } catch ( McdbException me ) {
3725
                logMetacat.debug("There was a Metacat problem. " +
3726
                  "The error message was: " + me.getMessage());
3727
                throw me;
3728

  
3729
              } // end try()
3707
              }
3730 3708
              
3731 3709
            } // end if()
3732 3710
            
......
3789 3767
      
3790 3768
      return sysMeta;
3791 3769
    }
3792
    
3793
    /**
3794
     * Get the system metadata for a document with a specified guid
3795
     *
3796
     * @param guid  The identifier used to look up system metadata
3797
     *
3798
     * @return sysMeta  The desired SystemMetadata object
3799
     */
3800
    private SystemMetadata getSystemMetadata(Identifier guid) 
3801
      throws McdbDocNotFoundException {
3802
      
3803
      logMetacat.debug("MetacatHandler.getSystemMetadata() called.");
3804
      
3805
      SystemMetadata sysMeta = null;
3806
      
3807
      try {
3808
        sysMeta = IdentifierManager.getInstance().getSystemMetadata(guid.getValue());
3809
      } catch ( McdbDocNotFoundException dnfe ) {
3810
        logMetacat.debug("There was a problem getting the system " +
3811
        "metadata. The error was: " + dnfe.getMessage());
3812
        throw dnfe;
3813
      }
3814
      return sysMeta;
3815

  
3816
    }
3817 3770
       
3818 3771
    /*
3819 3772
     * Find the size (in bytes) of a stream. Note: This needs to refactored out

Also available in: Unified diff