Project

General

Profile

« Previous | Next » 

Revision 9219

Added by Jing Tao over 9 years ago

Add the code to roll back the saved system metadata if the object can't be saved.

View differences:

src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java
32 32
import java.io.OutputStream;
33 33
import java.io.OutputStreamWriter;
34 34
import java.io.Writer;
35
import java.math.BigInteger;
35 36
import java.sql.SQLException;
36 37
import java.util.ArrayList;
37 38
import java.util.Calendar;
......
393 394
      allowed = true;
394 395
    }
395 396
    
397
    if(!allowed) {
398
        throw new NotAuthorized("1100", "Provited Identity doesn't have the WRITE permission on the pid "+pid.getValue());
399
    }
396 400
    // verify checksum, only if we can reset the inputstream
397 401
    if (object.markSupported()) {
398 402
        logMetacat.debug("Checking checksum for: " + pid.getValue());
......
416 420
    }
417 421
    	
418 422
    // we have the go ahead
419
    if ( allowed ) {
423
    //if ( allowed ) {
424
         
425
        // save the sysmeta
426
        try {
427
            // lock and unlock of the pid happens in the subclass
428
            HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
429
           
430
        
431
        } catch (Exception e) {
432
            logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
433
            throw new ServiceFailure("1190", e.getMessage());
434
        }
420 435
      
421 436
        logMetacat.debug("Allowed to insert: " + pid.getValue());
422 437

  
......
433 448
	        //localId = im.getLocalId(pid.getValue());
434 449

  
435 450
        } catch (IOException e) {
451
            removeSystemMeta(pid);
436 452
        	String msg = "The Node is unable to create the object. " +
437 453
          "There was a problem converting the object to XML";
438 454
        	logMetacat.info(msg);
439 455
          throw new ServiceFailure("1190", msg + ": " + e.getMessage());
440 456

  
457
        } catch (ServiceFailure e) {
458
            removeSystemMeta(pid);
459
            throw e;
460
        } catch (Exception e) {
461
            removeSystemMeta(pid);
462
            throw new ServiceFailure("1190", "The node is unable to create the object: " + e.getMessage());
441 463
        }
442 464
                    
443 465
      } else {
444 466
	        
445 467
	      // DEFAULT CASE: DATA (needs to be checked and completed)
446
	      localId = insertDataObject(object, pid, session);
468
          try {
469
              localId = insertDataObject(object, pid, session);
470
          } catch (ServiceFailure e) {
471
              removeSystemMeta(pid);
472
              throw e;
473
          } catch (Exception e) {
474
              removeSystemMeta(pid);
475
              throw new ServiceFailure("1190", "The node is unable to create the object: " + e.getMessage());
476
          }
477
	      
447 478
      }   
448 479
    
449
    }
480
    //}
450 481

  
451 482
    logMetacat.debug("Done inserting new object: " + pid.getValue());
452 483
    
453
    // save the sysmeta
454
    try {
455
    	// lock and unlock of the pid happens in the subclass
456
    	HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
457
    	// submit for indexing
458
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
459
        
460
    } catch (Exception e) {
461
    	logMetacat.error("Problem creating system metadata: " + pid.getValue(), e);
462
        throw new ServiceFailure("1190", e.getMessage());
463
	}
464
    
465 484
    // setting the resulting identifier failed
466 485
    if (localId == null ) {
486
        removeSystemMeta(pid);
467 487
      throw new ServiceFailure("1190", "The Node is unable to create the object. ");
468 488
    }
489
    
490
    try {
491
        // submit for indexing
492
        MetacatSolrIndex.getInstance().submit(sysmeta.getIdentifier(), sysmeta, null, true);
493
    } catch (Exception e) {
494
        logMetacat.warn("Couldn't create solr index for object "+pid.getValue());
495
    }
469 496

  
470 497
    resultPid = pid;
471 498
    
......
473 500

  
474 501
    return resultPid;
475 502
  }
503
  
504
  /*
505
   * Roll-back method when inserting data object fails.
506
   */
507
  protected void removeSystemMeta(Identifier id){
508
      HazelcastService.getInstance().getSystemMetadataMap().remove(id);
509
  }
510
  
511
  /*
512
   * Roll-back method when inserting data object fails.
513
   */
514
  protected void removeSolrIndex(SystemMetadata sysMeta) {
515
      sysMeta.setSerialVersion(sysMeta.getSerialVersion().add(BigInteger.ONE));
516
      sysMeta.setArchived(true);
517
      sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
518
      try {
519
          MetacatSolrIndex.getInstance().submit(sysMeta.getIdentifier(), sysMeta, null, false);
520
      } catch (Exception e) {
521
          logMetacat.warn("Can't remove the solr index for pid "+sysMeta.getIdentifier().getValue());
522
      }
523
      
524
  }
476 525

  
477 526
  /**
478 527
   * Return the log records associated with a given event between the start and 

Also available in: Unified diff