Project

General

Profile

Revision 69019789

ID6901978919a5bc004c1265c97719ba2918d4b3fe
Parent 4a2b3c5b
Child 0420a928

Added by Katja Luther 11 months ago

ref #8782: add isDeletable to descriptionService

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/DescriptionServiceImpl.java
26 26
import org.springframework.transaction.annotation.Transactional;
27 27

  
28 28
import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
29
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
29 30
import eu.etaxonomy.cdm.api.service.pager.Pager;
30 31
import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
31 32
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
......
473 474
        List<Distribution> toDelete = new ArrayList<>();
474 475
        for(TaxonDistributionDTO obj : descriptionElements) {
475 476
            Iterator<TaxonDescription> iterator = obj.getDescriptionsWrapper().getDescriptions().iterator();
477
            List<DescriptionBase> list = new ArrayList(obj.getDescriptionsWrapper().getDescriptions());
478
          //  Map<UUID, DescriptionBase> map = dao.saveOrUpdateAll(list);
479
//            MergeResult<DescriptionBase> mergeResult = new MergeResult<DescriptionBase>(mergedEntity, newEntities)
480
//            mergedObjects.add(map.values());
476 481
            while (iterator.hasNext()){
477 482
                TaxonDescription desc = iterator.next();
478 483
                mergedObjects.add(dao.merge(desc, returnTransientEntity));
......
501 506
    @Override
502 507
    @Transactional(readOnly = false)
503 508
    public DeleteResult deleteDescription(DescriptionBase description) {
504
        DeleteResult deleteResult = new DeleteResult();
505
        description = load(description.getId(), Arrays.asList("descriptiveDataSets"));//avoid lazy init exception
506

  
507
    	if (description instanceof TaxonDescription){
508
    		TaxonDescription taxDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
509
    		Taxon tax = taxDescription.getTaxon();
510
    		tax.removeDescription(taxDescription, true);
511
            deleteResult.addUpdatedObject(tax);
512
    	}
513
    	else if (HibernateProxyHelper.isInstanceOf(description, SpecimenDescription.class)){
514
    	    SpecimenDescription specimenDescription = HibernateProxyHelper.deproxy(description, SpecimenDescription.class);
515
    	    SpecimenOrObservationBase<?> specimen = specimenDescription.getDescribedSpecimenOrObservation();
516
    	    specimen.removeDescription(specimenDescription);
517
    	    deleteResult.addUpdatedObject(specimen);
518
    	}
519 509

  
520
    	Set<DescriptiveDataSet> descriptiveDataSets = description.getDescriptiveDataSets();
521
    	for (Iterator<DescriptiveDataSet> iterator = descriptiveDataSets.iterator(); iterator.hasNext();) {
522
    	    iterator.next().removeDescription(description);
510
        DeleteResult deleteResult = new DeleteResult();
511
        if (description == null){
512
            return deleteResult;
523 513
        }
514
        description = load(description.getId(), Arrays.asList("descriptiveDataSets"));
515
        //avoid lazy init exception
516

  
517
        deleteResult = isDeletable(description.getUuid());
518
        if (deleteResult.isOk()){
519
        	if (description instanceof TaxonDescription){
520
        		TaxonDescription taxDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
521
        		Taxon tax = taxDescription.getTaxon();
522
        		tax.removeDescription(taxDescription, true);
523
                deleteResult.addUpdatedObject(tax);
524
        	}
525
        	else if (HibernateProxyHelper.isInstanceOf(description, SpecimenDescription.class)){
526
        	    SpecimenDescription specimenDescription = HibernateProxyHelper.deproxy(description, SpecimenDescription.class);
527
        	    SpecimenOrObservationBase<?> specimen = specimenDescription.getDescribedSpecimenOrObservation();
528
        	    specimen.removeDescription(specimenDescription);
529
        	    deleteResult.addUpdatedObject(specimen);
530
        	}
531

  
532
        	Set<DescriptiveDataSet> descriptiveDataSets = description.getDescriptiveDataSets();
533
        	for (Iterator<DescriptiveDataSet> iterator = descriptiveDataSets.iterator(); iterator.hasNext();) {
534
        	    iterator.next().removeDescription(description);
535
            }
524 536

  
525
    	dao.delete(description);
526
    	deleteResult.addDeletedObject(description);
527
    	deleteResult.setCdmEntity(description);
528

  
537
        	dao.delete(description);
538
        	deleteResult.addDeletedObject(description);
539
        	deleteResult.setCdmEntity(description);
540
        }else{
541
            logger.info(deleteResult.getExceptions().toString());
542
        }
529 543

  
530 544
        return deleteResult;
531 545
    }
......
542 556

  
543 557

  
544 558
    @Override
559
    public DeleteResult isDeletable(UUID descriptionUuid){
560
        DeleteResult result = new DeleteResult();
561
        DescriptionBase description = this.load(descriptionUuid);
562
        Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(description);
563

  
564
        if (references == null || references.isEmpty()){
565
            return result;
566
        }
567
        for (CdmBase ref: references){
568
            String message = null;
569
            if (description instanceof TaxonDescription && ref instanceof Taxon && ((TaxonDescription)description).getTaxon().equals(ref)){
570
                continue;
571
            } else if (description instanceof TaxonNameDescription && ref instanceof TaxonName && ((TaxonNameDescription)description).getTaxonName().equals(ref)){
572
                continue;
573
            } else if (description instanceof SpecimenDescription && ref instanceof SpecimenOrObservationBase && ((SpecimenDescription)description).getDescribedSpecimenOrObservation().equals(ref)){
574
                continue;
575
            } else if (ref instanceof DescriptionElementBase){
576
                continue;
577

  
578
            }else {
579
                message = "The description can't be completely deleted because it is referenced by " + ref.getUserFriendlyTypeName() ;
580
                result.setAbort();
581
            }
582
            if (message != null){
583
                result.addException(new ReferencedObjectUndeletableException(message));
584
                result.addRelatedObject(ref);
585

  
586
            }
587

  
588
        }
589

  
590
        return result;
591
    }
592

  
593
    @Override
545 594
    public TermVocabulary<Feature> getFeatureVocabulary(UUID uuid) {
546 595
        return vocabularyDao.findByUuid(uuid);
547 596
    }
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IDescriptionService.java
629 629
    UpdateResult moveDescriptionElementsToDescription(Set<UUID> descriptionElementUUIDs,
630 630
            DescriptionBase targetDescription, boolean isCopy, boolean setNameInSource);
631 631

  
632
    public DeleteResult isDeletable(UUID descriptionUuid);
633

  
632 634

  
633 635

  
634 636
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)