cleanup
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / OccurrenceServiceImpl.java
index 92766fe840cf9fa59d0b1584bc36419cd88b725a..270fb3af85c01e2979bb300dde7cf366b20193e9 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;
@@ -155,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");
     }
@@ -442,17 +453,23 @@ 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);
@@ -499,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;
     }
@@ -538,6 +555,9 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
         specimenIdentifier = CdmFormatterFactory.format(derivedUnit, new FormatKey[] {
                 collectionKey, FormatKey.SPACE,
                 FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE });
+        if(CdmUtils.isBlank(specimenIdentifier)){
+            specimenIdentifier = derivedUnit.getTitleCache();
+        }
         if(CdmUtils.isBlank(specimenIdentifier)){
             specimenIdentifier = derivedUnit.getUuid().toString();
         }
@@ -586,15 +606,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
@@ -742,7 +759,12 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
         for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
             for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
                 if (!alreadyCollectedSpecimen.containsKey(specimenDto.getUuid())){
-                    PreservedSpecimenDTO dto = PreservedSpecimenDTO.newInstance(derivative, null);
+                    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);
@@ -846,11 +868,18 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
         taxa.add(associatedTaxon);
 
         for (Taxon taxon : taxa) {
-            List<SpecimenOrObservationBase> perTaxonOccurrences = dao.listByAssociatedTaxon(null,taxon, null, null, null, null);
+            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 = HibernateProxyHelper.deproxy(o, DerivedUnit.class);
-                    DerivateDTO derivedUnitDTO = PreservedSpecimenDTO.newInstance(derivedUnit, taxon.getName());
+                    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);
                     }
@@ -865,64 +894,8 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
 
     }
 
-//=======
-//<<<<<<< Updated upstream
-//=======
-//    @SuppressWarnings("unchecked")
-//    @Override
-//    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, null);
-//            for (SpecimenOrObservationBase<?> o : perTaxonOccurrences) {
-//                if (o.isInstanceOf(DerivedUnit.class)){
-//                    DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(o, DerivedUnit.class);
-//                    DerivateDTO derivedUnitDTO;
-//                    //DNASampleDTO dnaDto;
-//                    if (derivedUnit instanceof DnaSample){
-//                        derivedUnitDTO = DNASampleDTO.newInstance((DnaSample)derivedUnit);
-//                    }else{
-//                        derivedUnitDTO = PreservedSpecimenDTO.newInstance(derivedUnit);
-//                    }
-//                    if (alreadyCollectedSpecimen.get(derivedUnitDTO.getUuid()) == null){
-//                        alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
-//                    }
-//                    derivedUnitDTO.addAllDerivates(getDerivedUnitDTOsFor(derivedUnitDTO, derivedUnit, alreadyCollectedSpecimen));
-//                    FieldUnitDTO dto = this.findFieldUnitDTO(derivedUnitDTO, fieldUnitDTOs, alreadyCollectedSpecimen);
-//                    if (dto != null){
-//                        dto.addTaxonRelatedDerivedUnits(derivedUnitDTO);
-//                    }
-//                }
-//            }
-//
-//        }
-//
-//        return fieldUnitDTOs;
-//
-//    }
-//
-//>>>>>>> Stashed changes
-//>>>>>>> Stashed changes
+
+
     @Override
     public  List<DerivedUnit> findByAccessionNumber(
             String accessionNumberString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
@@ -1073,7 +1046,12 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
                 fieldUnitDto.addDerivate(derivedUnitDTO);
                 fieldUnits.add(fieldUnitDto);
             }else{
-                DerivateDTO originalDTO = PreservedSpecimenDTO.newInstance((DerivedUnit)specimen, null);
+                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);
             }
@@ -1089,64 +1067,6 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
     }
 
     @Override
-//=======
-//<<<<<<< Updated upstream
-//=======
-//
-//    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");
-//
-//        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 (specimen instanceof FieldUnit){
-//                return (FieldUnitDTO)alreadyCollectedSpecimen.get(specimen.getUuid());
-//            }else{
-//                fieldUnitDto = findFieldUnitDTO(alreadyCollectedSpecimen.get(specimen.getUuid()), fieldUnits, alreadyCollectedSpecimen);
-//            }
-//
-////            if ( alreadyCollectedSpecimen.get(specimen.getUuid()) instanceof FieldUnitDTO){
-////                ((FieldUnitDTO)alreadyCollectedSpecimen.get(specimen.getUuid())).getTaxonRelatedDerivedUnits().add(derivedUnitDTO.getUuid());
-////            }
-//        }else{
-//            if (specimen.isInstanceOf(FieldUnit.class)){
-//                fieldUnitDto = FieldUnitDTO.newInstance((FieldUnit)specimen);
-//
-//                fieldUnitDto.addPreservedSpecimenDTO((PreservedSpecimenDTO)derivedUnitDTO);
-//                fieldUnits.add(fieldUnitDto);
-//            }else{
-//                DerivateDTO originalDTO = PreservedSpecimenDTO.newInstance((DerivedUnit)specimen);
-//                originalDTO.addDerivate(derivedUnitDTO);
-//                fieldUnitDto = findFieldUnitDTO(originalDTO, fieldUnits, alreadyCollectedSpecimen);
-//            }
-//
-//        }
-//      //  }
-//        alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
-//
-//        return fieldUnitDto;
-//
-//    }
-//
-//    @Override
-//>>>>>>> Stashed changes
-//>>>>>>> Stashed changes
     @Transactional(readOnly = false)
     public UpdateResult moveSequence(DnaSample from, DnaSample to, Sequence sequence) {
         return moveSequence(from.getUuid(), to.getUuid(), sequence.getUuid());
@@ -1692,8 +1612,8 @@ public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObs
     }
 
     @Override
-    public Pager<SpecimenOrObservationBase> findByTitle(
-            IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config) {
+    public <S extends SpecimenOrObservationBase> Pager<S> findByTitle(
+            IIdentifiableEntityServiceConfigurator<S> config) {
         if (config instanceof FindOccurrencesConfigurator) {
             FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
             List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
@@ -1708,13 +1628,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);
     }
@@ -1725,9 +1646,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);