Project

General

Profile

« Previous | Next » 

Revision 5976

In MetacatHandler I've removed updateSystemMetadata() in favor of additions to insertOrUpdateSystemMetadata(). Modified createSystemMetadata() to reflect the changes as well.

View differences:

src/edu/ucsb/nceas/metacat/MetacatHandler.java
3419 3419
            String dataDocUrl = distMetadata.url;
3420 3420
            String dataDocMimeType = distMetadata.mimeType;
3421 3421
            String dataDocLocalId = "";
3422
            String dataSysMetaId = "";
3422 3423
            logMetacat.debug("Data local ID: " + dataDocLocalId);
3423 3424
            logMetacat.debug("Data URL     : " + dataDocUrl);
3424 3425
            logMetacat.debug("Data mime    : " + dataDocMimeType);
......
3447 3448
                                 " to " + identifier.getValue());
3448 3449
                dataSysMeta.addDescribedBy(identifier);
3449 3450
                dataSysMeta.setObjectFormat(ObjectFormat.convert(dataDocMimeType));
3450
                this.updateSystemMetadata(dataSysMeta, username, groups);
3451
                dataSysMetaId = insertOrUpdateSystemMetadata(
3452
                  dataSysMeta, "update", username, groups);
3451 3453
                
3452 3454
              } catch ( McdbDocNotFoundException nf ) {
3453 3455
                // System metadata for data doesn't exist
......
3469 3471
                  
3470 3472
                  logMetacat.debug("Updating system metadata for " + 
3471 3473
                    dataDocId.getValue() + " to " + dataDocMimeType);
3472
                  this.updateSystemMetadata(dataSysMeta, username, groups);
3474
                  dataSysMetaId = insertOrUpdateSystemMetadata(
3475
                    dataSysMeta, "insert", username, groups);
3473 3476
                  
3474 3477
                } catch ( McdbDocNotFoundException mdnf) {
3475 3478
                  mdnf.printStackTrace();
......
3582 3585
     * needs to be refactored out of MetacatHandler and into a utitlity when 
3583 3586
     * stream i/o in Metacat is evaluated.
3584 3587
     *
3585
     * @param params  A hash table of parameters
3586
     * @param user  The username doing the document insert or update
3588
     * @param sysMeta The system metadata object to process
3589
     * @param action  The action to perform, insert or update
3590
     * @param user    The username doing the document insert or update
3587 3591
     * @param groups  The groups the user belongs in
3588 3592
     *
3589 3593
     * @return localId  The new docid of the inserted or updated document
3590 3594
     *
3591 3595
     * @throws SQLException
3592 3596
     */
3593
    private String insertOrUpdateSystemMetadata(
3594
      Hashtable<String, String[]> params, String user, String[] groups)
3595
      throws SQLException, McdbException, InsufficientKarmaException {
3597
    private String insertOrUpdateSystemMetadata(SystemMetadata sysMeta,
3598
      String action, String user, String[] groups) 
3599
      throws SQLException, InsufficientKarmaException, JiBXException,
3600
      PropertyNotFoundException, McdbException, McdbDocNotFoundException {
3596 3601
      
3602
      logMetacat.debug("MetacatHandler.insertOrUpdateSystemMetadata() called.");
3603
      
3604
      String metacatUrl = "";
3605
      action = action.toUpperCase();
3606
      
3607
      try {
3608
        String server  = PropertyService.getProperty("server.name");
3609
        String port    = PropertyService.getProperty("server.httpPort");
3610
        String context = PropertyService.getProperty("application.context");
3611
        metacatUrl     = "http://" + server + ":" + port + "/" + context;
3612
          
3613
      } catch ( PropertyNotFoundException pnfe ) {
3614
        logMetacat.error("Couldn't get the server url properties: " +
3615
          pnfe.getMessage());
3616
          throw pnfe;
3617
          
3618
      }
3619
      
3597 3620
      DocumentImplWrapper docImpl = new DocumentImplWrapper("", false);
3598 3621
      DBConnection dbConn         = null;
3599 3622
      int serialNumber            = -1;
3600 3623
      String qformat              = "xml";
3601
      String[] doctext            = null;
3602 3624
      String xml                  = null;
3603 3625
      String doc                  = null;
3604
      String[] action             = null;
3605
      String doAction             = null;
3606
      String[] docid              = null;
3607
      String accNumber            = null;
3626
      String localId              = null;
3608 3627
      String newAccNumber         = null;
3609 3628
      String pub                  = null;
3610 3629
      StringReader dtd            = null;
3611 3630
      
3612
      // ensure we have a an accession number
3613
      if ( params.get("docid") == null ) {
3614
        throw new McdbException("The docid parameter was not set.");
3631
      IdentifierManager im = IdentifierManager.getInstance();
3632
      Identifier sysMetaGuid = new Identifier();
3633
      sysMetaGuid.setValue(DocumentUtil.generateDocumentId(1));
3634
      sysMeta.setDateSysMetadataModified(new Date());
3635
      
3636
      // either generate or get a system metadata localId
3637
      try {
3638
        if ( action.equals("INSERT") ) {
3639
          localId = im.generateLocalId(sysMetaGuid.getValue(), 1, true);
3615 3640
        
3616
      } else {
3617
        accNumber = docid[0];
3641
        } else if ( action.equals("UPDATE") ) {
3642
          localId = im.getSystemMetadataLocalId(sysMetaGuid.getValue());
3643
          String docid = localId.substring(0, localId.lastIndexOf("."));
3644
          String revString = 
3645
            localId.substring(localId.lastIndexOf(".") + 1, localId.length());
3646
          int rev = new Integer(revString).intValue();
3647
          rev++;
3648
          docid = docid + "." + rev;
3649
          localId = docid;
3650
          logMetacat.debug("Incremented system metadata localId: " + localId);
3618 3651
          
3619
      }
3620
      
3621
      // ensure we have an action
3622
      if ( params.get("action") == null ) {
3623
        throw new McdbException("The action parameter was not set.");
3652
        }
3624 3653
        
3625
      } else {
3654
      } catch ( McdbDocNotFoundException mnfe ) {
3655
        throw mnfe;
3626 3656
        
3627
        if ( action[0].equals("insert") ) {
3628
            doAction = "INSERT";
3629

  
3630
        } else if (action[0].equals("update")) {
3631
            doAction = "UPDATE";
3632

  
3633
        }
3634
        
3635 3657
      }
3636 3658
      
3637
      // ensure we have the document text
3638
      if ( params.get("doctext") == null ) {
3639
        throw new McdbException("The doctext parameter was not set.");
3659
      // get the xml from the sytem metadata object
3660
      try {
3661
        xml = new String(serializeSystemMetadata(sysMeta).toByteArray());
3640 3662
        
3641
      } else {
3642
        doctext = params.get("doctext");
3643
        xml = doctext[0];
3663
      } catch ( JiBXException jxe ) {
3664
        throw jxe;
3644 3665
        
3645 3666
      }
3646 3667
      
3668
      logMetacat.debug("SystemMetadata to insert or update: " + xml);
3669
      
3647 3670
      // ensure user can insert or update      
3648 3671
      try {
3649 3672
        
......
3669 3692
        
3670 3693
        // write the document to the database and disk
3671 3694
        logMetacat.debug("MetacatHandler.insertOrUpdateSystemMetadata(): " +
3672
          "Begin writing XML to Metacat for " + doAction + 
3695
          "Begin writing XML to Metacat for " + action + 
3673 3696
          " operation.");
3674 3697
        
3675 3698
        // write the system metadata document
3676 3699
        newAccNumber = docImpl.write(dbConn, xml, pub, dtd, 
3677
                                     doAction, accNumber, user, groups);
3700
                                     action, localId, user, groups);
3678 3701
                                     
3679 3702
        logMetacat.debug("MetacatHandler.insertOrUpdateSystemMetadata(): " +
3680
          "Wrote XML to Metacat for " + doAction + 
3703
          "Wrote XML to Metacat for " + action + 
3681 3704
          " operation.");
3682
      
3705
        
3706
        if ( action.equals("INSERT") ) {
3707
          IdentifierManager.getInstance().createSystemMetadataMapping(
3708
                  sysMeta, sysMetaGuid.getValue());
3709
          setAccess(metacatUrl, user, newAccNumber, 
3710
                    "public", "4", "allow", "allowFirst");
3711
          
3712
        } else if ( action.equals("UPDATE") ) {
3713
          IdentifierManager.getInstance().updateSystemMetadataMapping(
3714
            sysMeta.getIdentifier().getValue(), localId);
3715
          IdentifierManager.getInstance().insertAdditionalSystemMetadataFields(
3716
            sysMeta.getDateUploaded().getTime(), 
3717
            sysMeta.getRightsHolder().getValue(),
3718
            sysMeta.getChecksum().getValue(), 
3719
            sysMeta.getChecksum().getAlgorithm().name(), 
3720
            sysMeta.getOriginMemberNode().getValue(), 
3721
            sysMeta.getAuthoritativeMemberNode().getValue(), 
3722
            sysMeta.getDateSysMetadataModified().getTime(), 
3723
            sysMeta.getSubmitter().getValue(), 
3724
            sysMeta.getIdentifier().getValue(), 
3725
            sysMeta.getObjectFormat().toString(), 
3726
            sysMeta.getSize());
3727
          
3728
        }
3683 3729
      // unfortunately DocumentImplWrapper only raises a general exception  
3684 3730
      } catch (Exception e ) {
3685 3731
          throw new McdbException(e.getMessage());
......
3762 3808
      }
3763 3809
    }
3764 3810
    
3765
    /*
3766
     * Update a system metadata document with new values
3767
     * 
3768
     * @param sysMeta  The new system metadata object
3769
     * @param user  The user submitting the system metadata document
3770
     * @param groups  The groups the user belongs to
3771
     */
3772
    private void updateSystemMetadata(SystemMetadata sysMeta,
3773
      String user, String[] groups) {
3774
      
3775
      logMetacat.debug("MetacatHandler.updateSystemMetadata() called.");
3776
      
3777
      String localId = null;
3778
      Hashtable<String, String[]> params = new Hashtable<String, String[]>();
3779
      params.put("action", new String[]{"update"});
3780
      
3781
      try {
3782
        String smId = IdentifierManager.getInstance().getSystemMetadataLocalId(
3783
          sysMeta.getIdentifier().getValue());
3784
        params.put("docid", new String[]{smId});
3785
        
3786
      } catch ( McdbDocNotFoundException mdnfe ) {
3787
        logMetacat.debug("There was a problem getting the system " +
3788
        "metadata local id. The error was: " + mdnfe.getMessage());
3789
        params.put("docid", null);
3790
        
3791
      }
3792
      
3793
      logMetacat.debug("Setting date modified to " + new Date());
3794
      // set the date_updated date
3795
      sysMeta.setDateSysMetadataModified(new Date());
3796
      
3797
      // update the XML document in Metacat
3798
      try {
3799
        String xml = new String(serializeSystemMetadata(sysMeta).toByteArray());
3800
        params.put("doctext", new String[]{xml});
3801
        
3802
      } catch ( JiBXException jxe ) {
3803
        logMetacat.debug("There was a problem serializing the system " +
3804
        "metadata document. The error was: " + jxe.getMessage());
3805
        params.put("doctext", null);
3806
        
3807
      }
3808
      
3809
      try {
3810
        localId = insertOrUpdateSystemMetadata(params, user, groups);
3811
        
3812
      } catch ( SQLException sqle ) {
3813
        logMetacat.debug("There was a problem writing the system metadata " +
3814
          "document to Metacat. The error message was: " + sqle.getMessage());
3815
        
3816
      } catch ( McdbException me ) {
3817
        logMetacat.debug("There was a problem writing the system metadata " +
3818
          "document to Metacat. The error message was: " + me.getMessage());
3819
        
3820
      } catch ( InsufficientKarmaException ike ) {
3821
        logMetacat.debug("There was a permission problem writing the system metadata " +
3822
          "document to Metacat. The error message was: " + ike.getMessage());
3823
        
3824
      }
3825
      
3826
      // update the identifier mapping
3827
      IdentifierManager.getInstance().updateSystemMetadataMapping(
3828
        sysMeta.getIdentifier().getValue(), localId);
3829
      
3830
      // update the system metadata table
3831
      IdentifierManager.getInstance().insertAdditionalSystemMetadataFields(
3832
        sysMeta.getDateUploaded().getTime(), 
3833
        sysMeta.getRightsHolder().getValue(),
3834
        sysMeta.getChecksum().getValue(), 
3835
        /*sysMeta.getChecksum().getAlgorithm().toString()*/
3836
        sysMeta.getChecksum().getAlgorithm().name(), 
3837
        sysMeta.getOriginMemberNode().getValue(), 
3838
        sysMeta.getAuthoritativeMemberNode().getValue(), 
3839
        sysMeta.getDateSysMetadataModified().getTime(), 
3840
        sysMeta.getSubmitter().getValue(), 
3841
        sysMeta.getIdentifier().getValue(), 
3842
        sysMeta.getObjectFormat().toString(), 
3843
        sysMeta.getSize());
3844
          
3845
    }
3846
    
3847 3811
    /**
3848 3812
     * deserialize a system metadata doc. Note: This needs to refactored out
3849 3813
     * of MetacatHandler and into a utitlity when stream i/o in Metacat is

Also available in: Unified diff