Do not show empty brackets if no identifier is present for specimen
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / OccurrenceServiceImpl.java
index e4223005ca0f2654935c7bd4a000d3191b57b2bc..8a56608e9baf1c443a62aca83eca735e48688f67 100644 (file)
@@ -48,6 +48,7 @@ import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
 import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
 import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
+import eu.etaxonomy.cdm.api.service.dto.DNASampleDTO;
 import eu.etaxonomy.cdm.api.service.dto.DerivateDTO;
 import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO;
 import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO.ContigFile;
@@ -75,8 +76,6 @@ import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.CdmBaseType;
 import eu.etaxonomy.cdm.model.agent.AgentBase;
 import eu.etaxonomy.cdm.model.common.CdmBase;
-import eu.etaxonomy.cdm.model.common.DefinedTerm;
-import eu.etaxonomy.cdm.model.common.DefinedTermBase;
 import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.description.CategoricalData;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
@@ -87,6 +86,7 @@ import eu.etaxonomy.cdm.model.description.SpecimenDescription;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.location.Country;
 import eu.etaxonomy.cdm.model.location.NamedArea;
+import eu.etaxonomy.cdm.model.location.Point;
 import eu.etaxonomy.cdm.model.media.Media;
 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
@@ -108,9 +108,11 @@ import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
-import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
+import eu.etaxonomy.cdm.model.term.DefinedTerm;
+import eu.etaxonomy.cdm.model.term.DefinedTermBase;
 import eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer;
 import eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao;
+import eu.etaxonomy.cdm.persistence.dao.term.IDefinedTermDao;
 import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
 import eu.etaxonomy.cdm.persistence.query.AssignmentStatus;
@@ -154,6 +156,16 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
 
     private static final String SEPARATOR_STRING = ", ";
 
+    private static final List<String> DERIVED_UNIT_INIT_STRATEGY =  Arrays.asList(new String []{
+            "derivedFrom.derivatives",
+            "derivedFrom.originals",
+            "specimenTypeDesignations.*",
+            "specimenTypeDesignations.citation.*",
+            "specimenTypeDesignations.homotypicalGroup.*",
+            "specimenTypeDesignations.typifiedNames",
+            "collection.$"
+    });
+
     public OccurrenceServiceImpl() {
         logger.debug("Load OccurrenceService Bean");
     }
@@ -161,11 +173,11 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
 
     @Override
     @Transactional(readOnly = false)
-    public void updateTitleCache(Class<? extends SpecimenOrObservationBase> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<SpecimenOrObservationBase> cacheStrategy, IProgressMonitor monitor) {
+    public UpdateResult updateCaches(Class<? extends SpecimenOrObservationBase> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<SpecimenOrObservationBase> cacheStrategy, IProgressMonitor monitor) {
         if (clazz == null) {
             clazz = SpecimenOrObservationBase.class;
         }
-        super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
+        return super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
     }
 
     /**
@@ -184,7 +196,7 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
      */
     @Override
     public List<Country> getCountryByName(String name) {
-        List<? extends DefinedTermBase> terms = this.definedTermDao.findByTitle(Country.class, name, null, null, null, null, null, null);
+        List<? extends DefinedTermBase> terms = this.definedTermDao.findByTitleWithRestrictions(Country.class, name, null, null, null, null, null, null);
         List<Country> countries = new ArrayList<>();
         for (int i = 0; i < terms.size(); i++) {
             countries.add((Country) terms.get(i));
@@ -311,18 +323,18 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
     }
 
     @Override
-    public DerivedUnitFacade getDerivedUnitFacade(DerivedUnit derivedUnit, List<String> propertyPaths) throws DerivedUnitFacadeNotSupportedException {
+    public DerivedUnitFacade getDerivedUnitFacade(DerivedUnit derivedUnit, List<String> derivedUnitFacadeInitStrategy) throws DerivedUnitFacadeNotSupportedException {
         derivedUnit = (DerivedUnit) dao.load(derivedUnit.getUuid(), null);
         DerivedUnitFacadeConfigurator config = DerivedUnitFacadeConfigurator.NewInstance();
         config.setThrowExceptionForNonSpecimenPreservationMethodRequest(false);
         DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(derivedUnit, config);
-        beanInitializer.initialize(derivedUnitFacade, propertyPaths);
+        beanInitializer.initialize(derivedUnitFacade, derivedUnitFacadeInitStrategy);
         return derivedUnitFacade;
     }
 
     @Override
     public List<DerivedUnitFacade> listDerivedUnitFacades(
-            DescriptionBase description, List<String> propertyPaths) {
+            DescriptionBase description, List<String> derivedUnitFacadeInitStrategy) {
 
         List<DerivedUnitFacade> derivedUnitFacadeList = new ArrayList<>();
         IndividualsAssociation tempIndividualsAssociation;
@@ -342,7 +354,7 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
             }
         }
 
-        beanInitializer.initializeAll(derivedUnitFacadeList, propertyPaths);
+        beanInitializer.initializeAll(derivedUnitFacadeList, derivedUnitFacadeInitStrategy);
 
         return derivedUnitFacadeList;
     }
@@ -379,7 +391,7 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
         Set<UUID> fieldUnitUuids = new HashSet<>();
         List<SpecimenOrObservationBase> records = listByAssociatedTaxon(null, includeRelationships, associatedTaxon, maxDepth, null, null, orderHints, propertyPaths);
         for (SpecimenOrObservationBase<?> specimen : records) {
-            for (FieldUnit fieldUnit : getFieldUnits(specimen.getUuid(), null)) {
+            for (FieldUnit fieldUnit : findFieldUnits(specimen.getUuid(), null)) {
                 fieldUnitUuids.add(fieldUnit.getUuid());
             }
         }
@@ -389,14 +401,13 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
     }
 
     @Override
-    public FieldUnitDTO assembleFieldUnitDTO(FieldUnit fieldUnit, UUID associatedTaxonUuid) {
+    public FieldUnitDTO assembleFieldUnitDTO(FieldUnit fieldUnit) {
 
         if (!getSession().contains(fieldUnit)) {
             fieldUnit = (FieldUnit) load(fieldUnit.getUuid());
         }
-        TaxonBase associatedTaxon = taxonService.load(associatedTaxonUuid);
 
-        FieldUnitDTO fieldUnitDTO = new FieldUnitDTO();
+        FieldUnitDTO fieldUnitDTO = new FieldUnitDTO(fieldUnit);
 
         if (fieldUnit.getGatheringEvent() != null) {
             GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
@@ -415,7 +426,7 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
                 collectionString += (fieldNumber != null ? fieldNumber : "");
                 collectionString.trim();
             }
-            fieldUnitDTO.setCollection(collectionString);
+            fieldUnitDTO.setCollectingString(collectionString);
             // Date
             Partial gatheringDate = gatheringEvent.getGatheringDate();
             String dateString = null;
@@ -428,9 +439,6 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
             fieldUnitDTO.setDate(dateString);
         }
 
-        // Taxon Name
-        fieldUnitDTO.setTaxonName(associatedTaxon.getName().getTitleCache());
-
         // Herbaria map
         Map<eu.etaxonomy.cdm.model.occurrence.Collection, Integer> collectionToCountMap = new HashMap<>();
         // List of accession numbers for citation
@@ -445,21 +453,27 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
                     continue;
                 }
                 // collect accession numbers for citation
-                String mostSignificantIdentifier = getMostSignificantIdentifier(derivedUnit);
-                if (mostSignificantIdentifier != null) {
-                    preservedSpecimenAccessionNumbers.add(mostSignificantIdentifier);
-                }
+                String identifier = getMostSignificantIdentifier(derivedUnit);
                 // collect collections for herbaria column
-                if (derivedUnit.getCollection() != null) {
-                    Integer herbariumCount = collectionToCountMap.get(derivedUnit.getCollection());
+                eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
+                if (collection != null) {
+                    //combine collection with identifier
+                    if (identifier != null) {
+                        if(collection.getCode()!=null){
+                            identifier = (collection.getCode()!=null?collection.getCode():"[no collection]")+" "+identifier;
+                        }
+                        preservedSpecimenAccessionNumbers.add(identifier);
+                    }
+
+                    Integer herbariumCount = collectionToCountMap.get(collection);
                     if (herbariumCount == null) {
                         herbariumCount = 0;
                     }
-                    collectionToCountMap.put(derivedUnit.getCollection(), herbariumCount + 1);
+                    collectionToCountMap.put(collection, herbariumCount + 1);
                 }
                 if (derivedUnit.getRecordBasis().equals(SpecimenOrObservationType.PreservedSpecimen)) {
                     PreservedSpecimenDTO preservedSpecimenDTO = assemblePreservedSpecimenDTO(derivedUnit, fieldUnitDTO);
-                    fieldUnitDTO.addPreservedSpecimenDTO(preservedSpecimenDTO);
+                    fieldUnitDTO.addDerivate(preservedSpecimenDTO);
                     fieldUnitDTO.setHasCharacterData(fieldUnitDTO.isHasCharacterData() || preservedSpecimenDTO.isHasCharacterData());
                     fieldUnitDTO.setHasDetailImage(fieldUnitDTO.isHasDetailImage() || preservedSpecimenDTO.isHasDetailImage());
                     fieldUnitDTO.setHasDna(fieldUnitDTO.isHasDna() || preservedSpecimenDTO.isHasDna());
@@ -502,7 +516,7 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
             herbariaString += SEPARATOR_STRING;
         }
         herbariaString = removeTail(herbariaString, SEPARATOR_STRING);
-        fieldUnitDTO.setHerbarium(herbariaString);
+        fieldUnitDTO.setCollection(herbariaString);
 
         return fieldUnitDTO;
     }
@@ -530,7 +544,7 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
         if (!getSession().contains(derivedUnit)) {
             derivedUnit = (DerivedUnit) load(derivedUnit.getUuid());
         }
-        PreservedSpecimenDTO preservedSpecimenDTO = new PreservedSpecimenDTO();
+        PreservedSpecimenDTO preservedSpecimenDTO = new PreservedSpecimenDTO(derivedUnit);
 
         //specimen identifier
         FormatKey collectionKey = FormatKey.COLLECTION_CODE;
@@ -538,14 +552,19 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
         if (CdmUtils.isBlank(specimenIdentifier)) {
             collectionKey = FormatKey.COLLECTION_NAME;
         }
-        specimenIdentifier = CdmFormatterFactory.format(derivedUnit, new FormatKey[] {
-                collectionKey, FormatKey.SPACE,
-                FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE });
+        if(CdmUtils.isNotBlank(derivedUnit.getMostSignificantIdentifier())){
+            specimenIdentifier = CdmFormatterFactory.format(derivedUnit, new FormatKey[] {
+                    collectionKey, FormatKey.SPACE, FormatKey.OPEN_BRACKET,
+                    FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.CLOSE_BRACKET });
+        }
+        if(CdmUtils.isBlank(specimenIdentifier)){
+            specimenIdentifier = derivedUnit.getTitleCache();
+        }
         if(CdmUtils.isBlank(specimenIdentifier)){
             specimenIdentifier = derivedUnit.getUuid().toString();
         }
         preservedSpecimenDTO.setAccessionNumber(specimenIdentifier);
-        preservedSpecimenDTO.setUuid(derivedUnit.getUuid().toString());
+
 
         //preferred stable URI
         preservedSpecimenDTO.setPreferredStableUri(derivedUnit.getPreferredStableUri());
@@ -589,15 +608,12 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
                 fieldUnitDTO.setHasType(true);
             }
             TypeDesignationStatusBase<?> typeStatus = specimenTypeDesignation.getTypeStatus();
-            if (typeStatus != null) {
-                List<String> typedTaxaNames = new ArrayList<>();
-                String label = typeStatus.getLabel();
-                Set<TaxonName> typifiedNames = specimenTypeDesignation.getTypifiedNames();
-                for (TaxonName taxonName : typifiedNames) {
-                    typedTaxaNames.add(taxonName.getNameCache());
-                }
-                preservedSpecimenDTO.addTypes(label, typedTaxaNames);
+            Set<TaxonName> typifiedNames = specimenTypeDesignation.getTypifiedNames();
+            List<String> typedTaxaNames = new ArrayList<>();
+            for (TaxonName taxonName : typifiedNames) {
+                typedTaxaNames.add(taxonName.getTitleCache());
             }
+            preservedSpecimenDTO.addTypes(typeStatus!=null?typeStatus.getLabel():"", typedTaxaNames);
         }
 
         // individuals associations
@@ -739,6 +755,48 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
         return derivedUnits;
     }
 
+    private Set<DerivateDTO> getDerivedUnitDTOsFor(DerivateDTO specimenDto, DerivedUnit specimen, HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen) {
+        Set<DerivateDTO> derivedUnits = new HashSet<>();
+//        load
+        for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
+            for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
+                if (!alreadyCollectedSpecimen.containsKey(specimenDto.getUuid())){
+                    PreservedSpecimenDTO dto;
+                    if (derivative instanceof DnaSample) {
+                        dto = new DNASampleDTO(derivative);
+                    } else {
+                        dto = new PreservedSpecimenDTO(derivative);
+                    }
+                    alreadyCollectedSpecimen.put(dto.getUuid(), dto);
+                    dto.addAllDerivates(getDerivedUnitDTOsFor(dto, derivative, alreadyCollectedSpecimen));
+                    derivedUnits.add(dto);
+                }
+            }
+        }
+        return derivedUnits;
+    }
+
+//    private Set<DerivateDTO> getDerivedUnitDTOsFor(DerivateDTO specimenDto, DerivedUnit specimen, HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen) {
+//        Set<DerivateDTO> derivedUnits = new HashSet<>();
+////        load
+//        for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
+//            for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
+//                if (!alreadyCollectedSpecimen.containsKey(specimenDto.getUuid())){
+//                    PreservedSpecimenDTO dto;
+//                    if (derivative instanceof DnaSample){
+//                        dto = DNASampleDTO.newInstance(derivative);
+//                    }else{
+//                        dto = PreservedSpecimenDTO.newInstance(derivative);
+//                    }
+//                    alreadyCollectedSpecimen.put(dto.getUuid(), dto);
+//                    dto.addAllDerivates(getDerivedUnitDTOsFor(dto, derivative, alreadyCollectedSpecimen));
+//                    derivedUnits.add(dto);
+//                }
+//            }
+//        }
+//        return derivedUnits;
+//    }
+
 
     @SuppressWarnings("unchecked")
     @Override
@@ -769,9 +827,9 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
                 occurrenceIds.add(o.getId());
             }
         }
-        occurrences = (List<T>) dao.loadList(occurrenceIds, propertyPaths);
+        occurrences = (List<T>) dao.loadList(occurrenceIds, null, propertyPaths);
 
-        return new DefaultPagerImpl<T>(pageNumber, Long.valueOf(occurrenceIds.size()), pageSize, occurrences);
+        return new DefaultPagerImpl<T>(pageNumber, Long.valueOf(occurrences.size()), pageSize, occurrences);
 
     }
 
@@ -788,14 +846,77 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
     }
 
     @Override
-    public  List<DerivedUnit> findByAccessionNumber(
-            String accessionNumberString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
+    public List<FieldUnitDTO> findFieldUnitDTOByAssociatedTaxon(Set<TaxonRelationshipEdge> includedRelationships,
+            UUID associatedTaxonUuid) {
+
+        Set<Taxon> taxa = new HashSet<>();
+        Set<Integer> occurrenceIds = new HashSet<>();
+        List<FieldUnitDTO> fieldUnitDTOs = new ArrayList<>();
+        HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen = new HashMap<>();
+        List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
+        boolean includeUnpublished = INCLUDE_UNPUBLISHED;
+
+        // Integer limit = PagerUtils.limitFor(pageSize);
+        // Integer start = PagerUtils.startFor(pageSize, pageNumber);
+
+        Taxon associatedTaxon = (Taxon) taxonService.load(associatedTaxonUuid);
+
+
+        if (includedRelationships != null) {
+            taxa = taxonService.listRelatedTaxa(associatedTaxon, includedRelationships, null, includeUnpublished, null, null, null);
+        }
+
+        taxa.add(associatedTaxon);
+
+        for (Taxon taxon : taxa) {
+            List<SpecimenOrObservationBase> perTaxonOccurrences = dao.listByAssociatedTaxon(null,taxon, null, null, null, DERIVED_UNIT_INIT_STRATEGY);
+            for (SpecimenOrObservationBase<?> o : perTaxonOccurrences) {
+                if (o.isInstanceOf(DerivedUnit.class)){
+                    DerivedUnit derivedUnit;
+                    DerivateDTO derivedUnitDTO;
+                    if (o.isInstanceOf(DnaSample.class)) {
+                         derivedUnit = HibernateProxyHelper.deproxy(o, DnaSample.class);
+                        derivedUnitDTO = new DNASampleDTO(derivedUnit);
+                    } else {
+                        derivedUnit = HibernateProxyHelper.deproxy(o, DerivedUnit.class);
+                        derivedUnitDTO = new PreservedSpecimenDTO(derivedUnit);
+                    }
+                    if (alreadyCollectedSpecimen.get(derivedUnitDTO.getUuid()) == null){
+                        alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
+                    }
+                    derivedUnitDTO.addAllDerivates(getDerivedUnitDTOsFor(derivedUnitDTO, derivedUnit, alreadyCollectedSpecimen));
+                    this.findFieldUnitDTO(derivedUnitDTO, fieldUnitDTOs,
+                            alreadyCollectedSpecimen);
+                }
+            }
+
+        }
+
+        return fieldUnitDTOs;
+
+    }
+
+
+
+    @Override
+    public  FieldUnitDTO findByAccessionNumber(
+            String accessionNumberString, List<OrderHint> orderHints,
             List<String> propertyPaths)  {
 
-        List<DerivedUnit> records = new ArrayList<>();
-        records = dao.getByGeneticAccessionNumber(accessionNumberString, propertyPaths);
+        DnaSample dnaSample = dao.findByGeneticAccessionNumber(accessionNumberString, propertyPaths);
+        DerivateDTO derivedUnitDTO;
+        HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen = new HashMap<>();
+        List<FieldUnitDTO> fieldUnitDTOs = new ArrayList<>();
+        if (dnaSample != null){
+            derivedUnitDTO = new DNASampleDTO(dnaSample);
+            alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
+            derivedUnitDTO.addAllDerivates(getDerivedUnitDTOsFor(derivedUnitDTO, dnaSample, alreadyCollectedSpecimen));
+            FieldUnitDTO fieldUnit = this.findFieldUnitDTO(derivedUnitDTO, fieldUnitDTOs,
+                    alreadyCollectedSpecimen);
 
-        return records;
+            return fieldUnit;
+        }
+        return null;
 
     }
 
@@ -867,15 +988,18 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
 
 
     @Override
-    public Collection<FieldUnit> getFieldUnits(UUID derivedUnitUuid, List<String> propertyPaths) {
+    public Collection<FieldUnit> findFieldUnits(UUID derivedUnitUuid, List<String> propertyPaths) {
         //It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
         //from which this DerivedUnit was derived until all FieldUnits are found.
 
         // FIXME: use HQL queries to increase performance
+
         SpecimenOrObservationBase<?> specimen = load(derivedUnitUuid, propertyPaths);
 //        specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
         Collection<FieldUnit> fieldUnits = new ArrayList<>();
-
+        if (specimen == null){
+            return null;
+        }
         if (specimen.isInstanceOf(FieldUnit.class)) {
             fieldUnits.add(HibernateProxyHelper.deproxy(specimen, FieldUnit.class));
         }
@@ -901,6 +1025,150 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
         return fieldUnits;
     }
 
+
+
+    @Override
+    @Transactional(readOnly=true)
+    public FieldUnitDTO findFieldUnitDTO(DerivateDTO derivedUnitDTO, Collection<FieldUnitDTO> fieldUnits,
+            HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen) {
+        //It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
+        //from which this DerivedUnit was derived until all FieldUnits are found.
+        List<SpecimenOrObservationBase> specimens = new ArrayList<>();
+        List<String> propertyPaths = new ArrayList<>();
+
+        propertyPaths.add("descriptions.elements.media.title");
+        propertyPaths.add("kindOfUnit");
+        propertyPaths.add("derivedFrom");
+
+        specimens = dao.findOriginalsForDerivedUnit(derivedUnitDTO.getUuid(), propertyPaths);
+
+        if (specimens.size() > 1){
+            logger.debug("The derived unit with uuid " + derivedUnitDTO.getUuid() + "has more than one orginal");
+        }
+      //  for (SpecimenOrObservationBase specimen: specimens){
+        SpecimenOrObservationBase specimen = null;
+        if (specimens.size() > 0){
+            specimen = specimens.get(0);
+        }else{
+            return null;
+        }
+        FieldUnitDTO fieldUnitDto = null;
+        if (alreadyCollectedSpecimen.get(specimen.getUuid()) != null){
+            alreadyCollectedSpecimen.get(specimen.getUuid()).addDerivate(derivedUnitDTO);
+//            if ( alreadyCollectedSpecimen.get(specimen.getUuid()) instanceof FieldUnitDTO){
+//                ((FieldUnitDTO)alreadyCollectedSpecimen.get(specimen.getUuid())).getTaxonRelatedDerivedUnits().add(derivedUnitDTO.getUuid());
+//            }
+        }else{
+            if (specimen.isInstanceOf(FieldUnit.class)){
+                fieldUnitDto = new FieldUnitDTO((FieldUnit)specimen);
+                fieldUnitDto.addDerivate(derivedUnitDTO);
+                fieldUnits.add(fieldUnitDto);
+
+            }else{
+                DerivateDTO originalDTO;
+                if (specimen instanceof DnaSample){
+                    originalDTO = new DNASampleDTO((DnaSample)specimen);
+                } else {
+                    originalDTO = new PreservedSpecimenDTO((DerivedUnit)specimen);
+                }
+                originalDTO.addDerivate(derivedUnitDTO);
+                fieldUnitDto = findFieldUnitDTO(originalDTO, fieldUnits,
+                        alreadyCollectedSpecimen);
+            }
+
+        }
+      //  }
+        alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
+//        if (fieldUnitDto != null){
+//            fieldUnitDto.addTaxonRelatedDerivedUnits(derivedUnitDTO);
+//        }
+        return fieldUnitDto;
+
+    }
+
+    @Override
+    @Transactional(readOnly=true)
+    public FieldUnitDTO loadFieldUnitDTO(UUID derivedUnitUuid) {
+
+        FieldUnitDTO fieldUnitDTO = null;
+        DerivateDTO derivedUnitDTO = null;
+
+        Map<UUID, DerivateDTO> cycleDetectionMap = new HashMap<>();
+        SpecimenOrObservationBase derivative = dao.load(derivedUnitUuid);
+        if(derivative != null){
+            derivedUnitDTO = DerivateDTO.newInstance(derivative);
+            while(true){
+                Set<DerivateDTO> originals = originalDTOs(derivedUnitUuid);
+
+                if(originals.isEmpty()){
+                    break;
+                }
+                if (originals.size() > 1){
+                    logger.debug("The derived unit with uuid " + derivedUnitUuid + "has more than one orginal, ignoring all but the first one.");
+                }
+
+                DerivateDTO originalDTO = originals.iterator().next();
+
+                // cycle detection and handling
+                if(cycleDetectionMap.containsKey(originalDTO.getUuid())){
+                    // cycle detected!!!
+                    try {
+                        throw new Exception();
+                    } catch(Exception e){
+                        logger.error("Cycle in derivate graph detected at DerivedUnit with uuid=" + originalDTO.getUuid() , e);
+                    }
+                    // to solve the situation for the output we remove the derivate from the more distant graph node
+                    // by removing it from the derivatives of its original
+                    // but let the derivate to be added to the original which is closer to the FieldUnit (below at originalDTO.addDerivate(derivedUnitDTO);)
+                    for(DerivateDTO seenOriginal: cycleDetectionMap.values()){
+                        for(DerivateDTO derivateDTO : seenOriginal.getDerivates()){
+                            if(derivateDTO.equals(originalDTO)){
+                                seenOriginal.getDerivates().remove(originalDTO);
+                            }
+                        }
+                    }
+                } else {
+                    cycleDetectionMap.put(originalDTO.getUuid(), originalDTO);
+                }
+
+
+                if (originalDTO instanceof FieldUnitDTO){
+                    fieldUnitDTO = (FieldUnitDTO)originalDTO;
+                    if(derivedUnitDTO != null){
+                        fieldUnitDTO.addDerivate(derivedUnitDTO);
+                    }
+                    break;
+                }else{
+                    if (derivedUnitDTO == null){
+                        derivedUnitDTO = originalDTO;
+                    } else {
+                        originalDTO.addDerivate(derivedUnitDTO);
+                        derivedUnitDTO = originalDTO;
+                    }
+                }
+            }
+        }
+        return fieldUnitDTO;
+
+    }
+
+    /**
+     * @param originalDTO
+     * @return
+     */
+    private Set<DerivateDTO> originalDTOs(UUID derivativeUuid) {
+
+        Set<DerivateDTO> dtos = new HashSet<>();
+
+        List<SpecimenOrObservationBase> specimens = dao.findOriginalsForDerivedUnit(derivativeUuid, null);
+        for(SpecimenOrObservationBase sob : specimens){
+            dtos.add(DerivateDTO.newInstance(sob));
+        }
+
+        return dtos;
+    }
+
+
     @Override
     @Transactional(readOnly = false)
     public UpdateResult moveSequence(DnaSample from, DnaSample to, Sequence sequence) {
@@ -1447,8 +1715,32 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
     }
 
     @Override
-    public Pager<SpecimenOrObservationBase> findByTitle(
-            IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config) {
+    public List<PreservedSpecimenDTO> findByTitlePreservedSpecimenDTO(FindOccurrencesConfigurator config) {
+        Taxon taxon = null;
+        if(config.getAssociatedTaxonUuid()!=null){
+            TaxonBase<?> taxonBase = taxonService.load(config.getAssociatedTaxonUuid());
+            if(taxonBase.isInstanceOf(Taxon.class)){
+                taxon = CdmBase.deproxy(taxonBase, Taxon.class);
+            }
+        }
+        TaxonName taxonName = null;
+        if(config.getAssociatedTaxonNameUuid()!=null){
+            taxonName = nameService.load(config.getAssociatedTaxonNameUuid());
+        }
+        List<DerivedUnit> occurrences = new ArrayList<>();
+        occurrences.addAll(dao.findOccurrences(DerivedUnit.class,
+                config.getTitleSearchString(), config.getSignificantIdentifier(),
+                config.getSpecimenType(), taxon, taxonName, config.getMatchMode(), null, null,
+                config.getOrderHints(), null));
+
+        List<PreservedSpecimenDTO> dtos = new ArrayList<>();
+        occurrences.forEach(derivedUnit->dtos.add(assemblePreservedSpecimenDTO(derivedUnit)));
+        return dtos;
+    }
+
+    @Override
+    public <S extends SpecimenOrObservationBase> Pager<S> findByTitle(
+            IIdentifiableEntityServiceConfigurator<S> config) {
         if (config instanceof FindOccurrencesConfigurator) {
             FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
             List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
@@ -1463,13 +1755,14 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
             if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
                 taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
             }
-            occurrences.addAll(dao.findOccurrences(occurrenceConfig.getClazz(),
+            List<? extends SpecimenOrObservationBase> foundOccurrences = dao.findOccurrences(occurrenceConfig.getClazz(),
                     occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
                     occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
-                    occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
+                    occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths());
+            occurrences.addAll(foundOccurrences);
             occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
 
-            return new DefaultPagerImpl<>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
+            return new DefaultPagerImpl<>(config.getPageNumber(), occurrences.size(), config.getPageSize(), (List<S>)occurrences);
         }
         return super.findByTitle(config);
     }
@@ -1480,9 +1773,9 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
         //filter out (un-)assigned specimens
         if(taxon==null && taxonName==null){
             AssignmentStatus assignmentStatus = occurrenceConfig.getAssignmentStatus();
-            List<SpecimenOrObservationBase<?>> specimenWithAssociations = new ArrayList<>();
+            List<SpecimenOrObservationBase> specimenWithAssociations = new ArrayList<>();
             if(!assignmentStatus.equals(AssignmentStatus.ALL_SPECIMENS)){
-                for (SpecimenOrObservationBase<?> specimenOrObservationBase : occurrences) {
+                for (SpecimenOrObservationBase specimenOrObservationBase : occurrences) {
                     boolean includeUnpublished = true;  //TODO not sure if this is correct, maybe we have to propagate publish flag to higher methods.
                     Collection<TaxonBase<?>> associatedTaxa = listAssociatedTaxa(specimenOrObservationBase,
                             includeUnpublished, null, null, null, null);
@@ -1517,7 +1810,7 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
     @Override
     public List<SpecimenOrObservationBase<?>> getAllHierarchyDerivatives(SpecimenOrObservationBase<?> specimen){
         List<SpecimenOrObservationBase<?>> allHierarchyDerivatives = new ArrayList<>();
-        Collection<FieldUnit> fieldUnits = getFieldUnits(specimen.getUuid(), null);
+        Collection<FieldUnit> fieldUnits = findFieldUnits(specimen.getUuid(), null);
         if(fieldUnits.isEmpty()){
             allHierarchyDerivatives.add(specimen);
             allHierarchyDerivatives.addAll(getAllChildDerivatives(specimen));
@@ -1562,7 +1855,17 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
      * {@inheritDoc}
      */
     @Override
-    public List<FieldUnit> getFieldUnitsForGatheringEvent(UUID gatheringEventUuid) {
-        return dao.getFieldUnitsForGatheringEvent(gatheringEventUuid, null, null, null, null);
+    public List<FieldUnit> findFieldUnitsForGatheringEvent(UUID gatheringEventUuid) {
+        return dao.findFieldUnitsForGatheringEvent(gatheringEventUuid, null, null, null, null);
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public List<Point> findPointsForFieldUnitList(List<UUID> fieldUnitUuids) {
+
+        return dao.findPointsForFieldUnitList(fieldUnitUuids);
     }
 }