Project

General

Profile

« Previous | Next » 

Revision 9601

Added by Jing Tao over 8 years ago

Reformat the create and update method.

View differences:

src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
310 310
        UnsupportedType, InsufficientResources, NotFound, 
311 311
        InvalidSystemMetadata, NotImplemented, InvalidRequest {
312 312
        try {
313
        if(isReadOnlyMode()) {
314
            throw new ServiceFailure("1310", ReadOnlyChecker.DATAONEERROR);
315
        }
316

  
317
        //transform a sid to a pid if it is applicable
318
        String serviceFailureCode = "1310";
319
        Identifier sid = getPIDForSID(pid, serviceFailureCode);
320
        if(sid != null) {
321
            pid = sid;
322
        }
323
        
324
        String localId = null;
325
        boolean allowed = false;
326
        boolean isScienceMetadata = false;
327
        
328
        if (session == null) {
329
        	throw new InvalidToken("1210", "No session has been provided");
330
        }
331
        Subject subject = session.getSubject();
332

  
333
        // verify the pid is valid format
334
        if (!isValidIdentifier(pid)) {
335
        	throw new InvalidRequest("1202", "The provided identifier is invalid.");
336
        }
337
        
338
        // verify the new pid is valid format
339
        if (!isValidIdentifier(newPid)) {
340
            throw new InvalidRequest("1202", "The provided identifier is invalid.");
341
        }
342
        
343
        // make sure that the newPid doesn't exists
344
        boolean idExists = true;
345
        try {
346
            idExists = IdentifierManager.getInstance().identifierExists(newPid.getValue());
347
        } catch (SQLException e) {
348
            throw new ServiceFailure("1310", 
349
                                    "The requested identifier " + newPid.getValue() +
350
                                    " couldn't be determined if it is unique since : "+e.getMessage());
351
        }
352
        if (idExists) {
353
                throw new IdentifierNotUnique("1220", 
354
                          "The requested identifier " + newPid.getValue() +
355
                          " is already used by another object and" +
356
                          "therefore can not be used for this object. Clients should choose" +
357
                          "a new identifier that is unique and retry the operation or " +
358
                          "use CN.reserveIdentifier() to reserve one.");
313
            if(isReadOnlyMode()) {
314
                throw new ServiceFailure("1310", ReadOnlyChecker.DATAONEERROR);
315
            }
316
    
317
            //transform a sid to a pid if it is applicable
318
            String serviceFailureCode = "1310";
319
            Identifier sid = getPIDForSID(pid, serviceFailureCode);
320
            if(sid != null) {
321
                pid = sid;
322
            }
359 323
            
360
        }
361
        
362
       
363

  
364
        // check for the existing identifier
365
        try {
366
            localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
324
            String localId = null;
325
            boolean allowed = false;
326
            boolean isScienceMetadata = false;
367 327
            
368
        } catch (McdbDocNotFoundException e) {
369
            throw new InvalidRequest("1202", "The object with the provided " + 
370
                "identifier was not found.");
328
            if (session == null) {
329
            	throw new InvalidToken("1210", "No session has been provided");
330
            }
331
            Subject subject = session.getSubject();
332
    
333
            // verify the pid is valid format
334
            if (!isValidIdentifier(pid)) {
335
            	throw new InvalidRequest("1202", "The provided identifier is invalid.");
336
            }
371 337
            
372
        } catch (SQLException ee) {
373
            throw new ServiceFailure("1310", "The object with the provided " + 
374
                    "identifier "+pid.getValue()+" can't be identified since - "+ee.getMessage());
375
        }
376
        
377
        // set the originating node
378
        NodeReference originMemberNode = this.getCapabilities().getIdentifier();
379
        sysmeta.setOriginMemberNode(originMemberNode);
380
        
381
        // set the submitter to match the certificate
382
        sysmeta.setSubmitter(subject);
383
        // set the dates
384
        Date now = Calendar.getInstance().getTime();
385
        sysmeta.setDateSysMetadataModified(now);
386
        sysmeta.setDateUploaded(now);
387
        
388
        // make sure serial version is set to something
389
        BigInteger serialVersion = sysmeta.getSerialVersion();
390
        if (serialVersion == null) {
391
        	sysmeta.setSerialVersion(BigInteger.ZERO);
392
        }
393

  
394
        // does the subject have WRITE ( == update) priveleges on the pid?
395
        //allowed = isAuthorized(session, pid, Permission.WRITE);
396
        //CN having the permission is allowed; user with the write permission and calling on the authoritative node is allowed.
397
        allowed = allowUpdating(session, pid, Permission.WRITE);
398
        if (allowed) {
399
        	
400
        	// check quality of SM
401
        	if (sysmeta.getObsoletedBy() != null) {
402
        		throw new InvalidSystemMetadata("1300", "Cannot include obsoletedBy when updating object");
403
        	}
404
        	if (sysmeta.getObsoletes() != null && !sysmeta.getObsoletes().getValue().equals(pid.getValue())) {
405
        		throw new InvalidSystemMetadata("1300", "The identifier provided in obsoletes does not match old Identifier");
406
        	}
407

  
408
            // get the existing system metadata for the object
409
            SystemMetadata existingSysMeta = getSystemMetadata(session, pid);
410
            //System.out.println("the archive is "+existingSysMeta.getArchived());
411
            //Base on documentation, we can't update an archived object:
412
            //The update operation MUST fail with Exceptions.InvalidRequest on objects that have the Types.SystemMetadata.archived property set to true.
413
            if(existingSysMeta.getArchived() != null && existingSysMeta.getArchived()) {
414
                throw new InvalidRequest("1202","An archived object"+pid.getValue()+" can't be updated");
338
            // verify the new pid is valid format
339
            if (!isValidIdentifier(newPid)) {
340
                throw new InvalidRequest("1202", "The provided identifier is invalid.");
415 341
            }
416

  
417
            // check for previous update
418
            // see: https://redmine.dataone.org/issues/3336
419
            Identifier existingObsoletedBy = existingSysMeta.getObsoletedBy();
420
            if (existingObsoletedBy != null) {
421
            	throw new InvalidRequest("1202", 
422
            			"The previous identifier has already been made obsolete by: " + existingObsoletedBy.getValue());
342
            
343
            // make sure that the newPid doesn't exists
344
            boolean idExists = true;
345
            try {
346
                idExists = IdentifierManager.getInstance().identifierExists(newPid.getValue());
347
            } catch (SQLException e) {
348
                throw new ServiceFailure("1310", 
349
                                        "The requested identifier " + newPid.getValue() +
350
                                        " couldn't be determined if it is unique since : "+e.getMessage());
423 351
            }
424
            //check the sid in the system metadata. If it exists, it should be non-exist or match the old sid in the previous system metadata.
425
            Identifier sidInSys = sysmeta.getSeriesId();
426
            if(sidInSys != null) {
427
                if (!isValidIdentifier(sidInSys)) {
428
                    throw new InvalidSystemMetadata("1300", "The provided series id in the system metadata is invalid.");
352
            if (idExists) {
353
                    throw new IdentifierNotUnique("1220", 
354
                              "The requested identifier " + newPid.getValue() +
355
                              " is already used by another object and" +
356
                              "therefore can not be used for this object. Clients should choose" +
357
                              "a new identifier that is unique and retry the operation or " +
358
                              "use CN.reserveIdentifier() to reserve one.");
359
                
360
            }
361
            
362
           
363
    
364
            // check for the existing identifier
365
            try {
366
                localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
367
                
368
            } catch (McdbDocNotFoundException e) {
369
                throw new InvalidRequest("1202", "The object with the provided " + 
370
                    "identifier was not found.");
371
                
372
            } catch (SQLException ee) {
373
                throw new ServiceFailure("1310", "The object with the provided " + 
374
                        "identifier "+pid.getValue()+" can't be identified since - "+ee.getMessage());
375
            }
376
            
377
            // set the originating node
378
            NodeReference originMemberNode = this.getCapabilities().getIdentifier();
379
            sysmeta.setOriginMemberNode(originMemberNode);
380
            
381
            // set the submitter to match the certificate
382
            sysmeta.setSubmitter(subject);
383
            // set the dates
384
            Date now = Calendar.getInstance().getTime();
385
            sysmeta.setDateSysMetadataModified(now);
386
            sysmeta.setDateUploaded(now);
387
            
388
            // make sure serial version is set to something
389
            BigInteger serialVersion = sysmeta.getSerialVersion();
390
            if (serialVersion == null) {
391
            	sysmeta.setSerialVersion(BigInteger.ZERO);
392
            }
393
    
394
            // does the subject have WRITE ( == update) priveleges on the pid?
395
            //allowed = isAuthorized(session, pid, Permission.WRITE);
396
            //CN having the permission is allowed; user with the write permission and calling on the authoritative node is allowed.
397
            allowed = allowUpdating(session, pid, Permission.WRITE);
398
            if (allowed) {
399
            	
400
            	// check quality of SM
401
            	if (sysmeta.getObsoletedBy() != null) {
402
            		throw new InvalidSystemMetadata("1300", "Cannot include obsoletedBy when updating object");
403
            	}
404
            	if (sysmeta.getObsoletes() != null && !sysmeta.getObsoletes().getValue().equals(pid.getValue())) {
405
            		throw new InvalidSystemMetadata("1300", "The identifier provided in obsoletes does not match old Identifier");
406
            	}
407
    
408
                // get the existing system metadata for the object
409
                SystemMetadata existingSysMeta = getSystemMetadata(session, pid);
410
                //System.out.println("the archive is "+existingSysMeta.getArchived());
411
                //Base on documentation, we can't update an archived object:
412
                //The update operation MUST fail with Exceptions.InvalidRequest on objects that have the Types.SystemMetadata.archived property set to true.
413
                if(existingSysMeta.getArchived() != null && existingSysMeta.getArchived()) {
414
                    throw new InvalidRequest("1202","An archived object"+pid.getValue()+" can't be updated");
429 415
                }
430
                Identifier previousSid = existingSysMeta.getSeriesId();
431
                if(previousSid != null) {
432
                    // there is a previous sid, if the new sid doesn't match it, the new sid should be non-existing.
433
                    if(!sidInSys.getValue().equals(previousSid.getValue())) {
416
    
417
                // check for previous update
418
                // see: https://redmine.dataone.org/issues/3336
419
                Identifier existingObsoletedBy = existingSysMeta.getObsoletedBy();
420
                if (existingObsoletedBy != null) {
421
                	throw new InvalidRequest("1202", 
422
                			"The previous identifier has already been made obsolete by: " + existingObsoletedBy.getValue());
423
                }
424
                //check the sid in the system metadata. If it exists, it should be non-exist or match the old sid in the previous system metadata.
425
                Identifier sidInSys = sysmeta.getSeriesId();
426
                if(sidInSys != null) {
427
                    if (!isValidIdentifier(sidInSys)) {
428
                        throw new InvalidSystemMetadata("1300", "The provided series id in the system metadata is invalid.");
429
                    }
430
                    Identifier previousSid = existingSysMeta.getSeriesId();
431
                    if(previousSid != null) {
432
                        // there is a previous sid, if the new sid doesn't match it, the new sid should be non-existing.
433
                        if(!sidInSys.getValue().equals(previousSid.getValue())) {
434
                            try {
435
                                idExists = IdentifierManager.getInstance().identifierExists(sidInSys.getValue());
436
                            } catch (SQLException e) {
437
                                throw new ServiceFailure("1310", 
438
                                                        "The requested identifier " + sidInSys.getValue() +
439
                                                        " couldn't be determined if it is unique since : "+e.getMessage());
440
                            }
441
                            if(idExists) {
442
                                throw new InvalidSystemMetadata("1300", "The series id "+sidInSys.getValue()+" in the system metadata doesn't match the previous series id "
443
                                                                +previousSid.getValue()+", so it should NOT exist. However, it was used by another object.");
444
                            }
445
                        }
446
                    } else {
447
                        // there is no previous sid, the new sid should be non-existing.
434 448
                        try {
435 449
                            idExists = IdentifierManager.getInstance().identifierExists(sidInSys.getValue());
436 450
                        } catch (SQLException e) {
......
439 453
                                                    " couldn't be determined if it is unique since : "+e.getMessage());
440 454
                        }
441 455
                        if(idExists) {
442
                            throw new InvalidSystemMetadata("1300", "The series id "+sidInSys.getValue()+" in the system metadata doesn't match the previous series id "
443
                                                            +previousSid.getValue()+", so it should NOT exist. However, it was used by another object.");
456
                            throw new InvalidSystemMetadata("1300", "The series id "+sidInSys.getValue()+" in the system metadata should NOT exist since the previous series id is null."
457
                                                            +"However, it was used by another object.");
444 458
                        }
445 459
                    }
446
                } else {
447
                    // there is no previous sid, the new sid should be non-existing.
460
                    //the series id equals the pid (new pid hasn't been registered in the system, so IdentifierManager.getInstance().identifierExists method can't exclude this scenario)
461
                    if(sidInSys.getValue().equals(newPid.getValue())) {
462
                        throw new InvalidSystemMetadata("1300", "The series id "+sidInSys.getValue()+" in the system metadata shouldn't have the same value of the pid.");
463
                    }
464
                }
465
    
466
                isScienceMetadata = isScienceMetadata(sysmeta);
467
    
468
                // do we have XML metadata or a data object?
469
                if (isScienceMetadata) {
470
    
471
                    // update the science metadata XML document
472
                    // TODO: handle non-XML metadata/data documents (like netCDF)
473
                    // TODO: don't put objects into memory using stream to string
474
                    //String objectAsXML = "";
448 475
                    try {
449
                        idExists = IdentifierManager.getInstance().identifierExists(sidInSys.getValue());
450
                    } catch (SQLException e) {
451
                        throw new ServiceFailure("1310", 
452
                                                "The requested identifier " + sidInSys.getValue() +
453
                                                " couldn't be determined if it is unique since : "+e.getMessage());
476
                        //objectAsXML = IOUtils.toString(object, "UTF-8");
477
                    	String formatId = null;
478
                    	if(sysmeta.getFormatId() != null) {
479
                    	    formatId = sysmeta.getFormatId().getValue();
480
                    	}
481
                        localId = insertOrUpdateDocument(object, "UTF-8", pid, session, "update", formatId);
482
                        
483
                        // register the newPid and the generated localId
484
                        if (newPid != null) {
485
                            IdentifierManager.getInstance().createMapping(newPid.getValue(), localId);
486
                        }
487
    
488
                    } catch (IOException e) {
489
                        String msg = "The Node is unable to create the object. " + "There was a problem converting the object to XML";
490
                        logMetacat.info(msg);
491
                        throw new ServiceFailure("1310", msg + ": " + e.getMessage());
492
    
454 493
                    }
455
                    if(idExists) {
456
                        throw new InvalidSystemMetadata("1300", "The series id "+sidInSys.getValue()+" in the system metadata should NOT exist since the previous series id is null."
457
                                                        +"However, it was used by another object.");
458
                    }
494
    
495
                } else {
496
    
497
                    // update the data object
498
                    localId = insertDataObject(object, newPid, session);
499
    
459 500
                }
460
                //the series id equals the pid (new pid hasn't been registered in the system, so IdentifierManager.getInstance().identifierExists method can't exclude this scenario)
461
                if(sidInSys.getValue().equals(newPid.getValue())) {
462
                    throw new InvalidSystemMetadata("1300", "The series id "+sidInSys.getValue()+" in the system metadata shouldn't have the same value of the pid.");
463
                }
464
            }
465

  
466
            isScienceMetadata = isScienceMetadata(sysmeta);
467

  
468
            // do we have XML metadata or a data object?
469
            if (isScienceMetadata) {
470

  
471
                // update the science metadata XML document
472
                // TODO: handle non-XML metadata/data documents (like netCDF)
473
                // TODO: don't put objects into memory using stream to string
474
                //String objectAsXML = "";
501
                
502
                // add the newPid to the obsoletedBy list for the existing sysmeta
503
                existingSysMeta.setObsoletedBy(newPid);
504
                //increase version
505
                BigInteger current = existingSysMeta.getSerialVersion();
506
                //System.out.println("the current version is "+current);
507
                current = current.add(BigInteger.ONE);
508
                //System.out.println("the new current version is "+current);
509
                existingSysMeta.setSerialVersion(current);
510
                // then update the existing system metadata
511
                updateSystemMetadata(existingSysMeta);
512
    
513
                // prep the new system metadata, add pid to the affected lists
514
                sysmeta.setObsoletes(pid);
515
                //sysmeta.addDerivedFrom(pid);
516
    
517
                // and insert the new system metadata
518
                insertSystemMetadata(sysmeta);
519
    
520
                // log the update event
521
                EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), subject.getValue(), localId, Event.UPDATE.toString());
522
                
523
                // attempt to register the identifier - it checks if it is a doi
475 524
                try {
476
                    //objectAsXML = IOUtils.toString(object, "UTF-8");
477
                	String formatId = null;
478
                	if(sysmeta.getFormatId() != null) {
479
                	    formatId = sysmeta.getFormatId().getValue();
480
                	}
481
                    localId = insertOrUpdateDocument(object, "UTF-8", pid, session, "update", formatId);
482
                    
483
                    // register the newPid and the generated localId
484
                    if (newPid != null) {
485
                        IdentifierManager.getInstance().createMapping(newPid.getValue(), localId);
486
                    }
487

  
488
                } catch (IOException e) {
489
                    String msg = "The Node is unable to create the object. " + "There was a problem converting the object to XML";
490
                    logMetacat.info(msg);
491
                    throw new ServiceFailure("1310", msg + ": " + e.getMessage());
492

  
493
                }
494

  
525
        			DOIService.getInstance().registerDOI(sysmeta);
526
        		} catch (Exception e) {
527
                    throw new ServiceFailure("1190", "Could not register DOI: " + e.getMessage());
528
        		}
529
    
495 530
            } else {
496

  
497
                // update the data object
498
                localId = insertDataObject(object, newPid, session);
499

  
531
                throw new NotAuthorized("1200", "The provided identity does not have " + "permission to UPDATE the object identified by " + pid.getValue()
532
                        + " on the Member Node.");
500 533
            }
501
            
502
            // add the newPid to the obsoletedBy list for the existing sysmeta
503
            existingSysMeta.setObsoletedBy(newPid);
504
            //increase version
505
            BigInteger current = existingSysMeta.getSerialVersion();
506
            //System.out.println("the current version is "+current);
507
            current = current.add(BigInteger.ONE);
508
            //System.out.println("the new current version is "+current);
509
            existingSysMeta.setSerialVersion(current);
510
            // then update the existing system metadata
511
            updateSystemMetadata(existingSysMeta);
512

  
513
            // prep the new system metadata, add pid to the affected lists
514
            sysmeta.setObsoletes(pid);
515
            //sysmeta.addDerivedFrom(pid);
516

  
517
            // and insert the new system metadata
518
            insertSystemMetadata(sysmeta);
519

  
520
            // log the update event
521
            EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), subject.getValue(), localId, Event.UPDATE.toString());
522
            
523
            // attempt to register the identifier - it checks if it is a doi
524
            try {
525
    			DOIService.getInstance().registerDOI(sysmeta);
526
    		} catch (Exception e) {
527
                throw new ServiceFailure("1190", "Could not register DOI: " + e.getMessage());
528
    		}
529

  
530
        } else {
531
            throw new NotAuthorized("1200", "The provided identity does not have " + "permission to UPDATE the object identified by " + pid.getValue()
532
                    + " on the Member Node.");
533
        }
534 534
        } finally {
535 535
            IOUtils.closeQuietly(object);
536 536
        }
......
541 541
            IdentifierNotUnique, UnsupportedType, InsufficientResources, InvalidSystemMetadata, NotImplemented, InvalidRequest {
542 542
        Identifier resultPid = null;
543 543
        try {
544
        if(isReadOnlyMode()) {
545
            throw new ServiceFailure("1190", ReadOnlyChecker.DATAONEERROR);
546
        }
547
        // check for null session
548
        if (session == null) {
549
          throw new InvalidToken("1110", "Session is required to WRITE to the Node.");
550
        }
551
        // verify the pid is valid format
552
        if (!isValidIdentifier(pid)) {
553
            throw new InvalidRequest("1102", "The provided identifier is invalid.");
554
        }
555
        // set the submitter to match the certificate
556
        sysmeta.setSubmitter(session.getSubject());
557
        // set the originating node
558
        NodeReference originMemberNode = this.getCapabilities().getIdentifier();
559
        sysmeta.setOriginMemberNode(originMemberNode);
560
        
561
        // if no authoritative MN, set it to the same
562
        if (sysmeta.getAuthoritativeMemberNode() == null) {
563
        	sysmeta.setAuthoritativeMemberNode(originMemberNode);
564
        }
565
        
566
        sysmeta.setArchived(false);
567

  
568
        // set the dates
569
        Date now = Calendar.getInstance().getTime();
570
        sysmeta.setDateSysMetadataModified(now);
571
        sysmeta.setDateUploaded(now);
572
        
573
        // set the serial version
574
        sysmeta.setSerialVersion(BigInteger.ZERO);
575

  
576
        // check that we are not attempting to subvert versioning
577
        if (sysmeta.getObsoletes() != null && sysmeta.getObsoletes().getValue() != null) {
578
            throw new InvalidSystemMetadata("1180", 
579
              "The supplied system metadata is invalid. " +
580
              "The obsoletes field cannot have a value when creating entries.");
581
        }
582
        
583
        if (sysmeta.getObsoletedBy() != null && sysmeta.getObsoletedBy().getValue() != null) {
584
            throw new InvalidSystemMetadata("1180", 
585
              "The supplied system metadata is invalid. " +
586
              "The obsoletedBy field cannot have a value when creating entries.");
587
        }
588
        
589
        // verify the sid in the system metadata
590
        Identifier sid = sysmeta.getSeriesId();
591
        boolean idExists = false;
592
        if(sid != null) {
593
            if (!isValidIdentifier(sid)) {
594
                throw new InvalidSystemMetadata("1180", "The provided series id is invalid.");
544
            if(isReadOnlyMode()) {
545
                throw new ServiceFailure("1190", ReadOnlyChecker.DATAONEERROR);
595 546
            }
596
            try {
597
                idExists = IdentifierManager.getInstance().identifierExists(sid.getValue());
598
            } catch (SQLException e) {
599
                throw new ServiceFailure("1190", 
600
                                        "The series identifier " + sid.getValue() +
601
                                        " in the system metadata couldn't be determined if it is unique since : "+e.getMessage());
547
            // check for null session
548
            if (session == null) {
549
              throw new InvalidToken("1110", "Session is required to WRITE to the Node.");
602 550
            }
603
            if (idExists) {
604
                    throw new InvalidSystemMetadata("1180", 
605
                              "The series identifier " + sid.getValue() +
606
                              " is already used by another object and" +
607
                              "therefore can not be used for this object. Clients should choose" +
608
                              "a new identifier that is unique and retry the operation or " +
609
                              "use CN.reserveIdentifier() to reserve one.");
610
                
551
            // verify the pid is valid format
552
            if (!isValidIdentifier(pid)) {
553
                throw new InvalidRequest("1102", "The provided identifier is invalid.");
611 554
            }
612
            //the series id equals the pid (new pid hasn't been registered in the system, so IdentifierManager.getInstance().identifierExists method can't exclude this scenario )
613
            if(sid.getValue().equals(pid.getValue())) {
614
                throw new InvalidSystemMetadata("1180", "The series id "+sid.getValue()+" in the system metadata shouldn't have the same value of the pid.");
555
            // set the submitter to match the certificate
556
            sysmeta.setSubmitter(session.getSubject());
557
            // set the originating node
558
            NodeReference originMemberNode = this.getCapabilities().getIdentifier();
559
            sysmeta.setOriginMemberNode(originMemberNode);
560
            
561
            // if no authoritative MN, set it to the same
562
            if (sysmeta.getAuthoritativeMemberNode() == null) {
563
            	sysmeta.setAuthoritativeMemberNode(originMemberNode);
615 564
            }
616
        }
617

  
618
        // call the shared impl
619
        resultPid = super.create(session, pid, object, sysmeta);
620
        
621
        // attempt to register the identifier - it checks if it is a doi
622
        try {
623
			DOIService.getInstance().registerDOI(sysmeta);
624
		} catch (Exception e) {
625
			ServiceFailure sf = new ServiceFailure("1190", "Could not register DOI: " + e.getMessage());
626
			sf.initCause(e);
627
            throw sf;
628
		}
565
            
566
            sysmeta.setArchived(false);
567
    
568
            // set the dates
569
            Date now = Calendar.getInstance().getTime();
570
            sysmeta.setDateSysMetadataModified(now);
571
            sysmeta.setDateUploaded(now);
572
            
573
            // set the serial version
574
            sysmeta.setSerialVersion(BigInteger.ZERO);
575
    
576
            // check that we are not attempting to subvert versioning
577
            if (sysmeta.getObsoletes() != null && sysmeta.getObsoletes().getValue() != null) {
578
                throw new InvalidSystemMetadata("1180", 
579
                  "The supplied system metadata is invalid. " +
580
                  "The obsoletes field cannot have a value when creating entries.");
581
            }
582
            
583
            if (sysmeta.getObsoletedBy() != null && sysmeta.getObsoletedBy().getValue() != null) {
584
                throw new InvalidSystemMetadata("1180", 
585
                  "The supplied system metadata is invalid. " +
586
                  "The obsoletedBy field cannot have a value when creating entries.");
587
            }
588
            
589
            // verify the sid in the system metadata
590
            Identifier sid = sysmeta.getSeriesId();
591
            boolean idExists = false;
592
            if(sid != null) {
593
                if (!isValidIdentifier(sid)) {
594
                    throw new InvalidSystemMetadata("1180", "The provided series id is invalid.");
595
                }
596
                try {
597
                    idExists = IdentifierManager.getInstance().identifierExists(sid.getValue());
598
                } catch (SQLException e) {
599
                    throw new ServiceFailure("1190", 
600
                                            "The series identifier " + sid.getValue() +
601
                                            " in the system metadata couldn't be determined if it is unique since : "+e.getMessage());
602
                }
603
                if (idExists) {
604
                        throw new InvalidSystemMetadata("1180", 
605
                                  "The series identifier " + sid.getValue() +
606
                                  " is already used by another object and" +
607
                                  "therefore can not be used for this object. Clients should choose" +
608
                                  "a new identifier that is unique and retry the operation or " +
609
                                  "use CN.reserveIdentifier() to reserve one.");
610
                    
611
                }
612
                //the series id equals the pid (new pid hasn't been registered in the system, so IdentifierManager.getInstance().identifierExists method can't exclude this scenario )
613
                if(sid.getValue().equals(pid.getValue())) {
614
                    throw new InvalidSystemMetadata("1180", "The series id "+sid.getValue()+" in the system metadata shouldn't have the same value of the pid.");
615
                }
616
            }
617
    
618
            // call the shared impl
619
            resultPid = super.create(session, pid, object, sysmeta);
620
            
621
            // attempt to register the identifier - it checks if it is a doi
622
            try {
623
    			DOIService.getInstance().registerDOI(sysmeta);
624
    		} catch (Exception e) {
625
    			ServiceFailure sf = new ServiceFailure("1190", "Could not register DOI: " + e.getMessage());
626
    			sf.initCause(e);
627
                throw sf;
628
    		}
629 629
        } finally {
630 630
            IOUtils.closeQuietly(object);
631 631
        }

Also available in: Unified diff