ref #6668 Include deleted derivatives recursively into DeleteResult
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / OccurrenceServiceImpl.java
index bba69f768a3e33520ed69d905971994728c9d914..e2f1411fc00f2ccea3d6a2f049c854d953b82cc0 100644 (file)
@@ -138,6 +138,9 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
     @Autowired
     private INameService nameService;
 
+    @Autowired
+    private IEventBaseService eventService;
+
     @Autowired
     private ITaxonService taxonService;
 
@@ -301,8 +304,8 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
     }
 
     @Override
-    public List<UuidAndTitleCache<DerivedUnit>> getDerivedUnitUuidAndTitleCache() {
-        return dao.getDerivedUnitUuidAndTitleCache();
+    public List<UuidAndTitleCache<DerivedUnit>> getDerivedUnitUuidAndTitleCache(Integer limit, String pattern) {
+        return dao.getDerivedUnitUuidAndTitleCache(limit, pattern);
     }
 
     @Override
@@ -528,7 +531,7 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
 
         //specimen identifier
         FormatKey collectionKey = FormatKey.COLLECTION_CODE;
-        String specimenIdentifier = CdmFormatterFactory.format(derivedUnit, new FormatKey[] { FormatKey.COLLECTION_CODE });
+        String specimenIdentifier = CdmFormatterFactory.format(derivedUnit, collectionKey);
         if (CdmUtils.isBlank(specimenIdentifier)) {
             collectionKey = FormatKey.COLLECTION_NAME;
         }
@@ -1203,6 +1206,11 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
     public DeleteResult delete(SpecimenOrObservationBase<?> specimen, SpecimenDeleteConfigurator config) {
         specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
 
+        DeleteResult deleteResult = isDeletable(specimen.getUuid(), config);
+        if (!deleteResult.isOk()) {
+            return deleteResult;
+        }
+
         if (config.isDeleteChildren()) {
             Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
             //clone to avoid concurrent modification
@@ -1217,17 +1225,13 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
                     derivativesToDelete.add(unit);
                 }
                 for (DerivedUnit unit:derivativesToDelete){
-                    delete(unit, config);
+                    deleteResult.includeResult(delete(unit, config));
                 }
             }
         }
 
 
 
-        DeleteResult deleteResult = isDeletable(specimen.getUuid(), config);
-        if (!deleteResult.isOk()) {
-            return deleteResult;
-        }
 
         // check related objects
         Set<CdmBase> relatedObjects = deleteResult.getRelatedObjects();
@@ -1289,6 +1293,8 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
                             specimenOrObservationBase.removeDerivationEvent(derivationEvent);
                             deleteResult.addUpdatedObject(specimenOrObservationBase);
                         }
+                        // if derivationEvent has no derivates anymore, delete it
+                        eventService.delete(derivationEvent);
                     }
                 }
                 else{
@@ -1300,7 +1306,12 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
             FieldUnit fieldUnit = HibernateProxyHelper.deproxy(specimen, FieldUnit.class);
             GatheringEvent event = fieldUnit.getGatheringEvent();
             fieldUnit.setGatheringEvent(null);
-
+            if (event != null){
+                DeleteResult result = eventService.isDeletable(event.getUuid(), null);
+                if (result.isOk()){
+                    eventService.delete(event);
+                }
+            }
 
         }
         deleteResult.includeResult(delete(specimen));