Project

General

Profile

Revision bba9e555

IDbba9e5555fb03fefa3fdfdd599dd5b334bfddc63
Parent 88b4ec59
Child 79107c62

Added by Patrick Plitzner almost 6 years ago

  • fixed deep delete recursion bug
    • fixed tests

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/OccurrenceServiceImpl.java
947 947
            //check for children and parents (derivation events)
948 948
            else if(cdmBase.isInstanceOf(DerivationEvent.class)){
949 949
                DerivationEvent derivationEvent = HibernateProxyHelper.deproxy(cdmBase, DerivationEvent.class);
950
                //check if derivation event is derivedFrom event (parent -> child)
951
                if(derivationEvent.getDerivatives().contains(specimen)){
952
                    //if it is then the specimen is still deletable
953
                    continue;
954
                }
955
                else if(!specimenDeleteConfigurator.isDeleteChildren()){
956
                    //if not and children should not be deleted then it is undeletable
957
                    deleteResult.setAbort();
958
                    deleteResult.addException(new ReferencedObjectUndeletableException("Derivate still has child derivates."));
959
                    break;
960
                }
961
                else{
962
                    //check all children if they can be deleted
963
                    Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
964
                    DeleteResult childResult = new DeleteResult();
965
                    for (DerivedUnit derivedUnit : derivatives) {
966
                        childResult.includeResult(isDeletable(derivedUnit, specimenDeleteConfigurator));
950
                //check if derivation event is empty
951
                if(!derivationEvent.getDerivatives().isEmpty()){
952
                    if(derivationEvent.getDerivatives().size()==1 && derivationEvent.getDerivatives().contains(specimen)){
953
                        //if it is the parent event with only one derivate then the specimen is still deletable
954
                        continue;
967 955
                    }
968
                    if(!childResult.isOk()){
956
                    else if(!specimenDeleteConfigurator.isDeleteChildren()){
957
                        //if not and children should not be deleted then it is undeletable
969 958
                        deleteResult.setAbort();
970
                        deleteResult.includeResult(childResult);
959
                        deleteResult.addException(new ReferencedObjectUndeletableException("Derivate still has child derivates."));
971 960
                        break;
972 961
                    }
962
                    else{
963
                        //check all children if they can be deleted
964
                        Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
965
                        DeleteResult childResult = new DeleteResult();
966
                        for (DerivedUnit derivedUnit : derivatives) {
967
                            childResult.includeResult(isDeletable(derivedUnit, specimenDeleteConfigurator));
968
                        }
969
                        if(!childResult.isOk()){
970
                            deleteResult.setAbort();
971
                            deleteResult.includeResult(childResult);
972
                            break;
973
                        }
974
                    }
973 975
                }
974 976
            }
975 977
            //check for amplification
......
995 997
    public DeleteResult delete(SpecimenOrObservationBase<?> specimen, SpecimenDeleteConfigurator config) {
996 998
        specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
997 999

  
1000
        if(config.isDeleteChildren()){
1001
            Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
1002
            for (DerivationEvent derivationEvent : derivationEvents) {
1003
                Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1004
                for (DerivedUnit derivedUnit : derivatives) {
1005
                    delete(derivedUnit, config);
1006
                }
1007
            }
1008
        }
1009

  
998 1010
        DeleteResult deleteResult = isDeletable(specimen, config);
999 1011
        if(!deleteResult.isOk()){
1000 1012
            return deleteResult;
......
1002 1014

  
1003 1015
        //check related objects
1004 1016
        Set<CdmBase> relatedObjects = deleteResult.getRelatedObjects();
1017

  
1005 1018
        for (CdmBase relatedObject : relatedObjects) {
1006 1019
            //check for TypeDesignations
1007 1020
            if(relatedObject.isInstanceOf(SpecimenTypeDesignation.class)){
......
1058 1071
                        }
1059 1072
                    }
1060 1073
                }
1061
                //child derivation events
1062 1074
                else{
1063
                    deleteResult.includeResult(deepDelete(specimen, config));
1075
                    //child derivation events should not occur since we delete the hierarchy from bottom to top
1064 1076
                }
1065 1077
            }
1066 1078
        }
......
1096 1108
            deleteResult.setStatus(DeleteStatus.OK);
1097 1109
        }
1098 1110
        else if(from.isInstanceOf(SpecimenOrObservationBase.class))  {
1099
            deleteResult = deepDelete(HibernateProxyHelper.deproxy(from, SpecimenOrObservationBase.class), config);
1111
            deleteResult = delete(HibernateProxyHelper.deproxy(from, SpecimenOrObservationBase.class), config);
1100 1112
        }
1101 1113
        return deleteResult;
1102 1114
    }
1103 1115

  
1104
    private DeleteResult deepDelete(SpecimenOrObservationBase<?> entity, SpecimenDeleteConfigurator config){
1105
        DeleteResult deleteResult = isDeletable(entity, config);
1106
        if(!deleteResult.isOk()){
1107
            return deleteResult;
1108
        }
1109
        Set<DerivationEvent> derivationEvents = entity.getDerivationEvents();
1110
        for (DerivationEvent derivationEvent : derivationEvents) {
1111
            Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1112
            for (DerivedUnit derivedUnit : derivatives) {
1113
                deleteResult.includeResult(deepDelete(derivedUnit, config));
1114
            }
1115
        }
1116
        deleteResult.includeResult(delete(entity, config));
1117
        return deleteResult;
1118
    }
1116
//    private DeleteResult deepDelete(SpecimenOrObservationBase<?> entity, SpecimenDeleteConfigurator config){
1117
//        Set<DerivationEvent> derivationEvents = entity.getDerivationEvents();
1118
//        for (DerivationEvent derivationEvent : derivationEvents) {
1119
//            Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1120
//            for (DerivedUnit derivedUnit : derivatives) {
1121
//                DeleteResult deleteResult = deepDelete(derivedUnit, config);
1122
//                if(!deleteResult.isOk()){
1123
//                    return deleteResult;
1124
//                }
1125
//            }
1126
//        }
1127
//        return delete(entity, config);
1128
//    }
1119 1129

  
1120 1130
    /* (non-Javadoc)
1121 1131
     * @see eu.etaxonomy.cdm.api.service.IOccurrenceService#listAssociatedTaxa(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase)
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/OccurrenceServiceDeepDeleteTest.java
48 48
    private final UUID DERIVED_UNIT_UUID = UUID.fromString("448be6e7-f19c-4a10-9a0a-97aa005f817d");
49 49
    private final UUID DNA_SAMPLE_UUID = UUID.fromString("bee4212b-aff1-484e-845f-065c7d6216af");
50 50
    private final UUID SEQUENCE_UUID = UUID.fromString("0b867369-de8c-4837-a708-5b7d9f6091be");
51
    private final UUID TAXON_UUID = UUID.fromString("e93b3840-326c-446e-89f1-a7611f17c0e8");
52 51

  
53 52
    @SuppressWarnings("unused")
54 53
    private static final Logger logger = Logger.getLogger(OccurrenceServiceDeepDeleteTest.class);
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/OccurrenceServiceTest.java
646 646
        UUID derivedUnitUuid = UUID.fromString("f7fd1dc1-3c93-42a7-8279-cde5bfe37ea0");
647 647
        UUID botanicalNameUuid = UUID.fromString("7396430c-c932-4dd3-a45a-40c2808b132e");
648 648
//        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
649
////        derivedUnit.setUuid(derivedUnitUuid);
649
//        derivedUnit.setUuid(derivedUnitUuid);
650 650
//
651 651
//        occurrenceService.save(derivedUnit);
652 652
//
653 653
//        //create name with type specimen
654 654
//        BotanicalName name = BotanicalName.PARSED_NAME("Campanula patual sec L.");
655
////        name.setUuid(botanicalNameUuid);
655
//        name.setUuid(botanicalNameUuid);
656 656
//        SpecimenTypeDesignation typeDesignation = SpecimenTypeDesignation.NewInstance();
657 657
//        typeDesignation.setTypeSpecimen(derivedUnit);
658 658
//        //add type designation to name
......
722 722
        UUID dnaSampleUuid = UUID.fromString("7efd1d66-ac7f-4202-acdf-a72cbb9c3a21");
723 723
//        //how the XML was generated
724 724
//        FieldUnit fieldUnit = FieldUnit.NewInstance();
725
////        fieldUnit.setUuid(fieldUnitUuid);
725
//        fieldUnit.setUuid(fieldUnitUuid);
726 726
//        //sub derivates (DerivedUnit, DnaSample)
727 727
//        DerivedUnit derivedUnit = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
728
////        derivedUnit.setUuid(derivedUnitUuid);
728
//        derivedUnit.setUuid(derivedUnitUuid);
729 729
//        DnaSample dnaSample = DnaSample.NewInstance();
730
////        dnaSample.setUuid(dnaSampleUuid);
730
//        dnaSample.setUuid(dnaSampleUuid);
731 731
//
732 732
//        //derivation events
733 733
//        DerivationEvent.NewSimpleInstance(fieldUnit, derivedUnit, DerivationEventType.ACCESSIONING());
......
767 767
        //sub derivates (DerivedUnit, DnaSample)
768 768
        DerivedUnit derivedUnit = (DerivedUnit) occurrenceService.load(derivedUnitUuid);
769 769
        DnaSample dnaSample = (DnaSample) occurrenceService.load(dnaSampleUuid);
770
//        Sequence sequence = dnaSample.getSequences().iterator().next();
771 770

  
772 771
        //derivation events
773 772
        DerivationEvent fieldUnitToDerivedUnitEvent = fieldUnit.getDerivationEvents().iterator().next();
......
785 784
        assertFalse(deleteResult.toString(), deleteResult.isOk());
786 785
        assertTrue(deleteResult.toString(), deleteResult.getRelatedObjects().contains(derivedUnitToDnaSampleEvent));
787 786

  
788
        //delete DnaSample
789
        occurrenceService.deleteDerivateHierarchy(dnaSample, config);
790

  
791
        deleteResult = occurrenceService.isDeletable(derivedUnit, config);
792
        assertTrue(deleteResult.toString(), deleteResult.isOk());
793

  
794 787
        //check deletion of fieldUnit
795 788
        deleteResult = occurrenceService.isDeletable(fieldUnit, config);
796 789
        assertFalse(deleteResult.toString(), deleteResult.isOk());
797 790
        assertTrue(deleteResult.toString(), deleteResult.getRelatedObjects().contains(fieldUnitToDerivedUnitEvent));
798

  
799
        //delete DerivedUnit
800
        occurrenceService.deleteDerivateHierarchy(derivedUnit, config);
801

  
802
        deleteResult = occurrenceService.isDeletable(fieldUnit, config);
803
        assertTrue(deleteResult.toString(), deleteResult.isOk());
804 791
    }
805 792

  
806 793
    /* (non-Javadoc)
cdmlib-services/src/test/resources/eu/etaxonomy/cdm/api/service/OccurrenceServiceTest.testIsDeletableWithChildren.xml
1 1
<?xml version='1.0' encoding='UTF-8'?>
2 2
<dataset>
3
  <SPECIMENOROBSERVATIONBASE DTYPE="FieldUnit" ID="5000" CREATED="2014-12-16 09:26:39.0" UUID="92ada058-4c14-4131-8ecd-b82dc1dd2882" UPDATED="[null]" RECORDBASIS="FU" LSID_AUTHORITY="[null]" LSID_LSID="[null]" LSID_NAMESPACE="[null]" LSID_OBJECT="[null]" LSID_REVISION="[null]" PROTECTEDTITLECACHE="false" TITLECACHE="-title cache generation not implemented-" PUBLISH="true" INDIVIDUALCOUNT="[null]" ACCESSIONNUMBER="[null]" BARCODE="[null]" CATALOGNUMBER="[null]" COLLECTORSNUMBER="[null]" EXSICCATUM="[null]" ORIGINALLABELINFO="[null]" FIELDNOTES="[null]" FIELDNUMBER="[null]" CREATEDBY_ID="[null]" UPDATEDBY_ID="[null]" LIFESTAGE_ID="[null]" SEX_ID="[null]" KINDOFUNIT_ID="[null]" COLLECTION_ID="[null]" DERIVEDFROM_ID="[null]" STOREDUNDER_ID="[null]" PRESERVATION_ID="[null]" GATHERINGEVENT_ID="[null]" PRIMARYCOLLECTOR_ID="[null]" MEDIASPECIMEN_ID="[null]" DNAQUALITY_ID="[null]"/>
4
  <SPECIMENOROBSERVATIONBASE DTYPE="DerivedUnit" ID="5001" CREATED="2014-12-16 09:26:39.0" UUID="896dffdc-6809-4914-8950-5501fee1c0fd" UPDATED="2014-12-16 09:26:39.682" RECORDBASIS="FS" LSID_AUTHORITY="[null]" LSID_LSID="[null]" LSID_NAMESPACE="[null]" LSID_OBJECT="[null]" LSID_REVISION="[null]" PROTECTEDTITLECACHE="false" TITLECACHE="class eu.etaxonomy.cdm.model.occurrence.DerivedUnit: 896dffdc-6809-4914-8950-5501fee1c0fd" PUBLISH="true" INDIVIDUALCOUNT="[null]" ACCESSIONNUMBER="[null]" BARCODE="[null]" CATALOGNUMBER="[null]" COLLECTORSNUMBER="[null]" EXSICCATUM="[null]" ORIGINALLABELINFO="[null]" FIELDNOTES="[null]" FIELDNUMBER="[null]" CREATEDBY_ID="[null]" UPDATEDBY_ID="[null]" LIFESTAGE_ID="[null]" SEX_ID="[null]" KINDOFUNIT_ID="[null]" COLLECTION_ID="[null]" DERIVEDFROM_ID="5000" STOREDUNDER_ID="[null]" PRESERVATION_ID="[null]" GATHERINGEVENT_ID="[null]" PRIMARYCOLLECTOR_ID="[null]" MEDIASPECIMEN_ID="[null]" DNAQUALITY_ID="[null]"/>
5
  <SPECIMENOROBSERVATIONBASE DTYPE="DnaSample" ID="5002" CREATED="2014-12-16 09:26:39.0" UUID="7efd1d66-ac7f-4202-acdf-a72cbb9c3a21" UPDATED="2014-12-16 09:26:39.683" RECORDBASIS="DS" LSID_AUTHORITY="[null]" LSID_LSID="[null]" LSID_NAMESPACE="[null]" LSID_OBJECT="[null]" LSID_REVISION="[null]" PROTECTEDTITLECACHE="false" TITLECACHE="-title cache generation not implemented-" PUBLISH="true" INDIVIDUALCOUNT="[null]" ACCESSIONNUMBER="[null]" BARCODE="[null]" CATALOGNUMBER="[null]" COLLECTORSNUMBER="[null]" EXSICCATUM="[null]" ORIGINALLABELINFO="[null]" FIELDNOTES="[null]" FIELDNUMBER="[null]" CREATEDBY_ID="[null]" UPDATEDBY_ID="[null]" LIFESTAGE_ID="[null]" SEX_ID="[null]" KINDOFUNIT_ID="[null]" COLLECTION_ID="[null]" DERIVEDFROM_ID="5001" STOREDUNDER_ID="[null]" PRESERVATION_ID="[null]" GATHERINGEVENT_ID="[null]" PRIMARYCOLLECTOR_ID="[null]" MEDIASPECIMEN_ID="[null]" DNAQUALITY_ID="[null]"/>
3
  <SPECIMENOROBSERVATIONBASE DTYPE="FieldUnit" ID="5000" CREATED="2014-12-16 12:59:25.0" UUID="92ada058-4c14-4131-8ecd-b82dc1dd2882" UPDATED="[null]" RECORDBASIS="FU" LSID_AUTHORITY="[null]" LSID_LSID="[null]" LSID_NAMESPACE="[null]" LSID_OBJECT="[null]" LSID_REVISION="[null]" PROTECTEDTITLECACHE="false" TITLECACHE="-title cache generation not implemented-" PUBLISH="true" INDIVIDUALCOUNT="[null]" ACCESSIONNUMBER="[null]" BARCODE="[null]" CATALOGNUMBER="[null]" COLLECTORSNUMBER="[null]" EXSICCATUM="[null]" ORIGINALLABELINFO="[null]" FIELDNOTES="[null]" FIELDNUMBER="[null]" CREATEDBY_ID="[null]" UPDATEDBY_ID="[null]" LIFESTAGE_ID="[null]" SEX_ID="[null]" KINDOFUNIT_ID="[null]" COLLECTION_ID="[null]" DERIVEDFROM_ID="[null]" STOREDUNDER_ID="[null]" PRESERVATION_ID="[null]" GATHERINGEVENT_ID="[null]" PRIMARYCOLLECTOR_ID="[null]" MEDIASPECIMEN_ID="[null]" DNAQUALITY_ID="[null]"/>
4
  <SPECIMENOROBSERVATIONBASE DTYPE="DerivedUnit" ID="5001" CREATED="2014-12-16 12:59:25.0" UUID="896dffdc-6809-4914-8950-5501fee1c0fd" UPDATED="2014-12-16 12:59:25.228" RECORDBASIS="FS" LSID_AUTHORITY="[null]" LSID_LSID="[null]" LSID_NAMESPACE="[null]" LSID_OBJECT="[null]" LSID_REVISION="[null]" PROTECTEDTITLECACHE="false" TITLECACHE="class eu.etaxonomy.cdm.model.occurrence.DerivedUnit: 896dffdc-6809-4914-8950-5501fee1c0fd" PUBLISH="true" INDIVIDUALCOUNT="[null]" ACCESSIONNUMBER="[null]" BARCODE="[null]" CATALOGNUMBER="[null]" COLLECTORSNUMBER="[null]" EXSICCATUM="[null]" ORIGINALLABELINFO="[null]" FIELDNOTES="[null]" FIELDNUMBER="[null]" CREATEDBY_ID="[null]" UPDATEDBY_ID="[null]" LIFESTAGE_ID="[null]" SEX_ID="[null]" KINDOFUNIT_ID="[null]" COLLECTION_ID="[null]" DERIVEDFROM_ID="5000" STOREDUNDER_ID="[null]" PRESERVATION_ID="[null]" GATHERINGEVENT_ID="[null]" PRIMARYCOLLECTOR_ID="[null]" MEDIASPECIMEN_ID="[null]" DNAQUALITY_ID="[null]"/>
5
  <SPECIMENOROBSERVATIONBASE DTYPE="DnaSample" ID="5002" CREATED="2014-12-16 12:59:25.0" UUID="7efd1d66-ac7f-4202-acdf-a72cbb9c3a21" UPDATED="2014-12-16 12:59:25.228" RECORDBASIS="DS" LSID_AUTHORITY="[null]" LSID_LSID="[null]" LSID_NAMESPACE="[null]" LSID_OBJECT="[null]" LSID_REVISION="[null]" PROTECTEDTITLECACHE="false" TITLECACHE="-title cache generation not implemented-" PUBLISH="true" INDIVIDUALCOUNT="[null]" ACCESSIONNUMBER="[null]" BARCODE="[null]" CATALOGNUMBER="[null]" COLLECTORSNUMBER="[null]" EXSICCATUM="[null]" ORIGINALLABELINFO="[null]" FIELDNOTES="[null]" FIELDNUMBER="[null]" CREATEDBY_ID="[null]" UPDATEDBY_ID="[null]" LIFESTAGE_ID="[null]" SEX_ID="[null]" KINDOFUNIT_ID="[null]" COLLECTION_ID="[null]" DERIVEDFROM_ID="5001" STOREDUNDER_ID="[null]" PRESERVATION_ID="[null]" GATHERINGEVENT_ID="[null]" PRIMARYCOLLECTOR_ID="[null]" MEDIASPECIMEN_ID="[null]" DNAQUALITY_ID="[null]"/>
6 6
  <SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT ORIGINALS_ID="5000" DERIVATIONEVENTS_ID="5000"/>
7 7
  <SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT ORIGINALS_ID="5001" DERIVATIONEVENTS_ID="5001"/>
8
  <DERIVATIONEVENT ID="5000" CREATED="2014-12-16 09:26:39.0" UUID="9a17a0e5-54fe-459c-9d5a-15f1712c2322" UPDATED="2014-12-16 09:26:39.681" DESCRIPTION="[null]" TIMEPERIOD_END="[null]" TIMEPERIOD_FREETEXT="[null]" TIMEPERIOD_START="[null]" CREATEDBY_ID="[null]" UPDATEDBY_ID="[null]" ACTOR_ID="[null]" INSTITUTION_ID="[null]" TYPE_ID="2004"/>
9
  <DERIVATIONEVENT ID="5001" CREATED="2014-12-16 09:26:39.0" UUID="7635f0fa-0da2-4e51-997c-c696f915327f" UPDATED="2014-12-16 09:26:39.682" DESCRIPTION="[null]" TIMEPERIOD_END="[null]" TIMEPERIOD_FREETEXT="[null]" TIMEPERIOD_START="[null]" CREATEDBY_ID="[null]" UPDATEDBY_ID="[null]" ACTOR_ID="[null]" INSTITUTION_ID="[null]" TYPE_ID="2001"/>
8
  <DERIVATIONEVENT ID="5000" CREATED="2014-12-16 12:59:25.0" UUID="fb3c15c7-85ba-4685-a5d9-5e04bfe56bff" UPDATED="2014-12-16 12:59:25.227" DESCRIPTION="[null]" TIMEPERIOD_END="[null]" TIMEPERIOD_FREETEXT="[null]" TIMEPERIOD_START="[null]" CREATEDBY_ID="[null]" UPDATEDBY_ID="[null]" ACTOR_ID="[null]" INSTITUTION_ID="[null]" TYPE_ID="2004"/>
9
  <DERIVATIONEVENT ID="5001" CREATED="2014-12-16 12:59:25.0" UUID="7e0953ed-7d40-49c1-a920-9a29c0f4f778" UPDATED="2014-12-16 12:59:25.228" DESCRIPTION="[null]" TIMEPERIOD_END="[null]" TIMEPERIOD_FREETEXT="[null]" TIMEPERIOD_START="[null]" CREATEDBY_ID="[null]" UPDATEDBY_ID="[null]" ACTOR_ID="[null]" INSTITUTION_ID="[null]" TYPE_ID="2001"/>
10
  <AMPLIFICATIONRESULT/>
10 11
</dataset>

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)