Project

General

Profile

« Previous | Next » 

Revision 5961

Modified MetacatHandler, added three methods:
getSystemMetadata() - returns a SystemMetadata object from the systemmetadata table using the given GUID. Stub only.
updateSystemMetadata() - updates the systemmetadata table using the given SystemMetadata object.
insertOrUpdateSystemMetadata() - Inserts or updates system metadata documents in Metacat. Note: This needs to be refactored out of MetacatHandler and into a utitlity when stream i/o in Metacat is evaluated.

View differences:

src/edu/ucsb/nceas/metacat/MetacatHandler.java
3280 3280
      return sysmetaOut;
3281 3281
    }
3282 3282
    
3283
    /*
3284
     * Inserts or updates system metadata documents in Metacat. Note: This
3285
     * needs to be refactored out of MetacatHandler and into a utitlity when 
3286
     * stream i/o in Metacat is evaluated.
3287
     *
3288
     * @param params  A hash table of parameters
3289
     * @param user  The username doing the document insert or update
3290
     * @param groups  The groups the user belongs in
3291
     *
3292
     * @return localId  The new docid of the inserted or updated document
3293
     *
3294
     * @throws SQLException
3295
     */
3296
    private String insertOrUpdateSystemMetadata(
3297
      Hashtable<String, String[]> params, String user, String[] groups)
3298
      throws SQLException, McdbException, InsufficientKarmaException {
3299
      
3300
      DocumentImplWrapper docImpl = new DocumentImplWrapper("", false);
3301
      DBConnection dbConn         = null;
3302
      int serialNumber            = -1;
3303
      String qformat              = "xml";
3304
      String[] doctext            = null;
3305
      String xml                  = null;
3306
      String doc                  = null;
3307
      String[] action             = null;
3308
      String doAction             = null;
3309
      String[] docid              = null;
3310
      String accNumber            = null;
3311
      String newAccNumber         = null;
3312
      String pub                  = null;
3313
      StringReader dtd            = null;
3314
      
3315
      // ensure we have a an accession number
3316
      if ( params.get("docid") == null ) {
3317
        throw new McdbException("The docid parameter was not set.");
3318
        
3319
      } else {
3320
        accNumber = docid[0];
3321
          
3322
      }
3323
      
3324
      // ensure we have an action
3325
      if ( params.get("action") == null ) {
3326
        throw new McdbException("The action parameter was not set.");
3327
        
3328
      } else {
3329
        
3330
        if ( action[0].equals("insert") ) {
3331
            doAction = "INSERT";
3332

  
3333
        } else if (action[0].equals("update")) {
3334
            doAction = "UPDATE";
3335

  
3336
        }
3337
        
3338
      }
3339
      
3340
      // ensure we have the document text
3341
      if ( params.get("doctext") == null ) {
3342
        throw new McdbException("The doctext parameter was not set.");
3343
        
3344
      } else {
3345
        doctext = params.get("doctext");
3346
        xml = doctext[0];
3347
        
3348
      }
3349
      
3350
      // ensure user can insert or update      
3351
      try {
3352
        
3353
        if ( !(AuthUtil.canInsertOrUpdate(user, groups)) ) {
3354
          throw new InsufficientKarmaException("The user " + user +
3355
            "does not have permission to insert or update system metadata.");
3356
            
3357
        }
3358
        
3359
      } catch (MetacatUtilException ue) {
3360
        throw new InsufficientKarmaException("Couldn't determine if user " + user +
3361
          "has permission to insert or update system metadata.");
3362
         
3363
      }
3364
      
3365
      try {
3366
        
3367
        // get a database connection from the pool
3368
        dbConn = 
3369
        DBConnectionPool.getDBConnection(
3370
          "MetacatHandler.insertOrUpdateSystemMetadata");
3371
        serialNumber = dbConn.getCheckOutSerialNumber();
3372
        
3373
        // write the document to the database and disk
3374
        logMetacat.debug("MetacatHandler.insertOrUpdateSystemMetadata(): " +
3375
          "Begin writing XML to Metacat for " + doAction + 
3376
          " operation.");
3377
        
3378
        // write the system metadata document
3379
        newAccNumber = docImpl.write(dbConn, xml, pub, dtd, 
3380
                                     doAction, accNumber, user, groups);
3381
                                     
3382
        logMetacat.debug("MetacatHandler.insertOrUpdateSystemMetadata(): " +
3383
          "Wrote XML to Metacat for " + doAction + 
3384
          " operation.");
3385
      
3386
      // unfortunately DocumentImplWrapper only raises a general exception  
3387
      } catch (Exception e ) {
3388
          throw new McdbException(e.getMessage());
3389
          
3390
      } finally {
3391
        // Return db connection
3392
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
3393
        
3394
      }
3395
      
3396
      return newAccNumber;
3397
    }
3398
    
3283 3399
    /**
3400
     * Get the system metadata for a document with a specified guid
3401
     *
3402
     * @param guid  The identifier used to look up system metadata
3403
     *
3404
     * @return sysMeta  The desired SystemMetadata object
3405
     */
3406
     public SystemMetadata getSystemMetadata(Identifier guid) 
3407
      throws NotFound {
3408
      SystemMetadata sysMeta = new SystemMetadata();
3409
      
3410
      return sysMeta;
3411
    }
3412
    
3413
    /*
3414
     * Update a system metadata document with new values
3415
     * 
3416
     * @param sysMeta  The new system metadata object
3417
     * @param user  The user submitting the system metadata document
3418
     * @param groups  The groups the user belongs to
3419
     */
3420
    private void updateSystemMetadata(SystemMetadata sysMeta,
3421
      String user, String[] groups) {
3422
      
3423
      logMetacat.debug("MetacatHandler.updateSystemMetadata() called.");
3424
      
3425
      String localId = null;
3426
      Hashtable<String, String[]> params = new Hashtable<String, String[]>();
3427
      params.put("action", new String[]{"update"});
3428
      
3429
      try {
3430
        String smId = IdentifierManager.getInstance().getSystemMetadataLocalId(
3431
          sysMeta.getIdentifier().getValue());
3432
        params.put("docid", new String[]{smId});
3433
        
3434
      } catch ( McdbDocNotFoundException mdnfe ) {
3435
        logMetacat.debug("There was a problem getting the system " +
3436
        "metadata local id. The error was: " + mdnfe.getMessage());
3437
        params.put("docid", null);
3438
        
3439
      }
3440
      
3441
      logMetacat.debug("Setting date modified to " + new Date());
3442
      // set the date_updated date
3443
      sysMeta.setDateSysMetadataModified(new Date());
3444
      
3445
      // update the XML document in Metacat
3446
      try {
3447
        String xml = new String(serializeSystemMetadata(sysMeta).toByteArray());
3448
        params.put("doctext", new String[]{xml});
3449
        
3450
      } catch ( JiBXException jxe ) {
3451
        logMetacat.debug("There was a problem serializing the system " +
3452
        "metadata document. The error was: " + jxe.getMessage());
3453
        params.put("doctext", null);
3454
        
3455
      }
3456
      
3457
      try {
3458
        localId = insertOrUpdateSystemMetadata(params, user, groups);
3459
        
3460
      } catch ( SQLException sqle ) {
3461
        logMetacat.debug("There was a problem writing the system metadata " +
3462
          "document to Metacat. The error message was: " + sqle.getMessage());
3463
        
3464
      } catch ( McdbException me ) {
3465
        logMetacat.debug("There was a problem writing the system metadata " +
3466
          "document to Metacat. The error message was: " + me.getMessage());
3467
        
3468
      } catch ( InsufficientKarmaException ike ) {
3469
        logMetacat.debug("There was a permission problem writing the system metadata " +
3470
          "document to Metacat. The error message was: " + ike.getMessage());
3471
        
3472
      }
3473
      
3474
      // update the identifier mapping
3475
      IdentifierManager.getInstance().updateSystemMetadataMapping(
3476
        sysMeta.getIdentifier().getValue(), localId);
3477
      
3478
      // update the system metadata table
3479
      IdentifierManager.getInstance().insertAdditionalSystemMetadataFields(
3480
        sysMeta.getDateUploaded().getTime(), 
3481
        sysMeta.getRightsHolder().getValue(),
3482
        sysMeta.getChecksum().getValue(), 
3483
        /*sysMeta.getChecksum().getAlgorithm().toString()*/
3484
        sysMeta.getChecksum().getAlgorithm().name(), 
3485
        sysMeta.getOriginMemberNode().getValue(), 
3486
        sysMeta.getAuthoritativeMemberNode().getValue(), 
3487
        sysMeta.getDateSysMetadataModified().getTime(), 
3488
        sysMeta.getSubmitter().getValue(), 
3489
        sysMeta.getIdentifier().getValue(), 
3490
        sysMeta.getObjectFormat().toString(), 
3491
        sysMeta.getSize());
3492
          
3493
    }
3494
    
3495
    /**
3284 3496
     * deserialize a system metadata doc. Note: This needs to refactored out
3285 3497
     * of MetacatHandler and into a utitlity when stream i/o in Metacat is
3286 3498
     * evaluated.

Also available in: Unified diff