Search in sources :

Example 1 with SpecimenOrObservationBaseDTO

use of eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO in project cdmlib by cybertaxonomy.

the class OccurrenceServiceImpl method listRootUnitDTOsByAssociatedTaxon.

@Override
@Transactional
public List<SpecimenOrObservationBaseDTO> listRootUnitDTOsByAssociatedTaxon(Set<TaxonRelationshipEdge> includedRelationships, UUID associatedTaxonUuid, List<String> propertyPaths) {
    Set<Taxon> taxa = new HashSet<>();
    Set<SpecimenOrObservationBaseDTO> rootUnitDTOs = new HashSet<>();
    boolean includeUnpublished = INCLUDE_UNPUBLISHED;
    Taxon associatedTaxon = (Taxon) taxonService.load(associatedTaxonUuid);
    if (includedRelationships != null) {
        taxa = taxonService.listRelatedTaxa(associatedTaxon, includedRelationships, null, includeUnpublished, null, null, null);
    }
    taxa.add(associatedTaxon);
    HashMap<UUID, SpecimenOrObservationBaseDTO> alreadyCollectedUnits = new HashMap<>();
    for (Taxon taxon : taxa) {
        // TODO there might be a good potential to speed up the whole processing by collecting all entities first
        // and to create the DTOs in a second step
        Set<SpecimenOrObservationBase> perTaxonOccurrences = dao.listByAssociatedTaxon(null, taxon, null, null, null, propertyPaths).stream().map(u -> HibernateProxyHelper.deproxy(u, SpecimenOrObservationBase.class)).collect(Collectors.toSet());
        for (SpecimenOrObservationBase<?> unit : perTaxonOccurrences) {
            unit = HibernateProxyHelper.deproxy(unit);
            if (unit instanceof DerivedUnit) {
                DerivedUnitDTO derivativeDTO;
                if (!alreadyCollectedUnits.containsKey(unit.getUuid())) {
                    DerivedUnit derivedUnit = (DerivedUnit) unit;
                    derivativeDTO = (DerivedUnitDTO) SpecimenOrObservationDTOFactory.fromEntity(derivedUnit, null);
                    if (unit instanceof DnaSample) {
                        derivativeDTO = DNASampleDTO.fromEntity((DnaSample) unit);
                    } else {
                        derivativeDTO = DerivedUnitDTO.fromEntity(derivedUnit, null, null);
                    }
                    alreadyCollectedUnits.put(derivativeDTO.getUuid(), derivativeDTO);
                    derivativeDTO.addAllDerivatives(getDerivedUnitDTOsFor(derivativeDTO, derivedUnit, alreadyCollectedUnits));
                }
                derivativeDTO = (DerivedUnitDTO) alreadyCollectedUnits.get(unit.getUuid());
                rootUnitDTOs.addAll(findRootUnitDTOs(derivativeDTO, alreadyCollectedUnits));
            } else {
                // only other option is FieldUnit
                rootUnitDTOs.add(FieldUnitDTO.fromEntity((FieldUnit) unit, 0, null));
            }
        }
    }
    List<SpecimenOrObservationBaseDTO> orderdDTOs = new ArrayList<>(rootUnitDTOs);
    // TODO order dtos by date can only be done by string comparison
    // the FieldUnitDTO.date needs to be a Partial object for sensible ordering
    Collections.sort(orderdDTOs, new Comparator<SpecimenOrObservationBaseDTO>() {

        @Override
        public int compare(SpecimenOrObservationBaseDTO o1, SpecimenOrObservationBaseDTO o2) {
            if (o1 instanceof FieldUnitDTO && o2 instanceof FieldUnitDTO) {
                FieldUnitDTO fu1 = (FieldUnitDTO) o1;
                FieldUnitDTO fu2 = (FieldUnitDTO) o2;
                // PartialComparator.INSTANCE_NULL_SMALLEST() here
                return PartialComparator.INSTANCE().compare(fu1.getDate(), fu2.getDate());
            }
            if (o1 instanceof DerivedUnitDTO && o2 instanceof DerivedUnitDTO) {
                SpecimenOrObservationBaseDTO du1 = o1;
                SpecimenOrObservationBaseDTO du2 = o2;
                return StringUtils.compare(du1.getLabel(), du2.getLabel());
            }
            if (o1 instanceof FieldUnitDTO && o2 instanceof DerivedUnitDTO) {
                return -1;
            } else {
                return 1;
            }
        }
    });
    return orderdDTOs;
}
Also used : Arrays(java.util.Arrays) Sequence(eu.etaxonomy.cdm.model.molecular.Sequence) Pager(eu.etaxonomy.cdm.api.service.pager.Pager) Rectangle(org.hibernate.search.spatial.impl.Rectangle) MediaDTO(eu.etaxonomy.cdm.api.service.dto.MediaDTO) IProgressMonitor(eu.etaxonomy.cdm.common.monitor.IProgressMonitor) PartialComparator(eu.etaxonomy.cdm.compare.common.PartialComparator) GatheringEvent(eu.etaxonomy.cdm.model.occurrence.GatheringEvent) Builder(org.apache.lucene.search.BooleanQuery.Builder) DefaultPagerImpl(eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl) Autowired(org.springframework.beans.factory.annotation.Autowired) DNASampleDTO(eu.etaxonomy.cdm.api.service.dto.DNASampleDTO) DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) StringUtils(org.apache.commons.lang3.StringUtils) LuceneParseException(eu.etaxonomy.cdm.api.service.search.LuceneParseException) SpecimenTypeDesignation(eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation) LuceneSearch(eu.etaxonomy.cdm.api.service.search.LuceneSearch) Logger(org.apache.log4j.Logger) TaxonBase(eu.etaxonomy.cdm.model.taxon.TaxonBase) SpecimenOrObservationType(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType) Map(java.util.Map) ILuceneIndexToolProvider(eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider) SortField(org.apache.lucene.search.SortField) CdmBaseType(eu.etaxonomy.cdm.model.CdmBaseType) EnumSet(java.util.EnumSet) ReferencedObjectUndeletableException(eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException) DeterminationEvent(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent) Occur(org.apache.lucene.search.BooleanClause.Occur) BytesRef(org.apache.lucene.util.BytesRef) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase) Collection(java.util.Collection) DerivedUnitDTO(eu.etaxonomy.cdm.api.service.dto.DerivedUnitDTO) Set(java.util.Set) FieldUnitDTO(eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SpecimenDeleteConfigurator(eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator) SpecimenOrObservationDTOFactory(eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationDTOFactory) TransientObjectException(org.hibernate.TransientObjectException) AmplificationResult(eu.etaxonomy.cdm.model.molecular.AmplificationResult) List(java.util.List) IIdentifiableEntityServiceConfigurator(eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator) TaxonRelationshipEdge(eu.etaxonomy.cdm.api.util.TaxonRelationshipEdge) DerivationEvent(eu.etaxonomy.cdm.model.occurrence.DerivationEvent) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) DescriptionBase(eu.etaxonomy.cdm.model.description.DescriptionBase) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) OrderHint(eu.etaxonomy.cdm.persistence.query.OrderHint) SpecimenDescription(eu.etaxonomy.cdm.model.description.SpecimenDescription) ParseException(org.apache.lucene.queryparser.classic.ParseException) AbstractBeanInitializer(eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer) IIdentifiableEntityCacheStrategy(eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy) DerivedUnitFacadeConfigurator(eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) HashMap(java.util.HashMap) Status(eu.etaxonomy.cdm.api.service.UpdateResult.Status) IndividualsAssociation(eu.etaxonomy.cdm.model.description.IndividualsAssociation) SingleRead(eu.etaxonomy.cdm.model.molecular.SingleRead) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) FindOccurrencesConfigurator(eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator) UuidAndTitleCache(eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache) Media(eu.etaxonomy.cdm.model.media.Media) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SpecimenNodeWrapper(eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper) DeleteConfiguratorBase(eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase) TopGroups(org.apache.lucene.search.grouping.TopGroups) Service(org.springframework.stereotype.Service) IOccurrenceDao(eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao) DnaSample(eu.etaxonomy.cdm.model.molecular.DnaSample) CdmBase(eu.etaxonomy.cdm.model.common.CdmBase) MediaSpecimen(eu.etaxonomy.cdm.model.occurrence.MediaSpecimen) Iterator(java.util.Iterator) ISequenceService(eu.etaxonomy.cdm.api.service.molecular.ISequenceService) ISearchResultBuilder(eu.etaxonomy.cdm.api.service.search.ISearchResultBuilder) HibernateProxyHelper(eu.etaxonomy.cdm.hibernate.HibernateProxyHelper) AbstractPagerImpl(eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl) IOException(java.io.IOException) SearchResult(eu.etaxonomy.cdm.api.service.search.SearchResult) DerivedUnitFacade(eu.etaxonomy.cdm.api.facade.DerivedUnitFacade) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) SpecimenOrObservationBaseDTO(eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO) Point(eu.etaxonomy.cdm.model.location.Point) FieldUnit(eu.etaxonomy.cdm.model.occurrence.FieldUnit) Language(eu.etaxonomy.cdm.model.common.Language) SearchResultBuilder(eu.etaxonomy.cdm.api.service.search.SearchResultBuilder) AssignmentStatus(eu.etaxonomy.cdm.persistence.query.AssignmentStatus) Comparator(java.util.Comparator) Collections(java.util.Collections) DerivedUnitFacadeNotSupportedException(eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException) Transactional(org.springframework.transaction.annotation.Transactional) QueryFactory(eu.etaxonomy.cdm.api.service.search.QueryFactory) FieldUnit(eu.etaxonomy.cdm.model.occurrence.FieldUnit) HashMap(java.util.HashMap) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) SpecimenOrObservationBaseDTO(eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO) ArrayList(java.util.ArrayList) DerivedUnitDTO(eu.etaxonomy.cdm.api.service.dto.DerivedUnitDTO) DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) FieldUnitDTO(eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO) DnaSample(eu.etaxonomy.cdm.model.molecular.DnaSample) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) UUID(java.util.UUID) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with SpecimenOrObservationBaseDTO

use of eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO in project cdmlib by cybertaxonomy.

the class OccurrenceServiceImpl method findRootUnitDTOs.

@Override
public Collection<SpecimenOrObservationBaseDTO> findRootUnitDTOs(UUID unitUUID) {
    SpecimenOrObservationBase<?> entity = load(unitUUID);
    SpecimenOrObservationBaseDTO derivedUnitDTO = SpecimenOrObservationDTOFactory.fromEntity(entity);
    Collection<SpecimenOrObservationBaseDTO> rootUnitDTOs = new ArrayList<>();
    if (derivedUnitDTO != null) {
        if (derivedUnitDTO instanceof FieldUnitDTO) {
            rootUnitDTOs.add(derivedUnitDTO);
        } else {
            Map<UUID, SpecimenOrObservationBaseDTO> alreadyCollectedSpecimen = new HashMap<>();
            rootUnitDTOs = findRootUnitDTOs((DerivedUnitDTO) derivedUnitDTO, alreadyCollectedSpecimen);
        }
    }
    return rootUnitDTOs;
}
Also used : FieldUnitDTO(eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO) HashMap(java.util.HashMap) SpecimenOrObservationBaseDTO(eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO) ArrayList(java.util.ArrayList) DerivedUnitDTO(eu.etaxonomy.cdm.api.service.dto.DerivedUnitDTO) UUID(java.util.UUID)

Example 3 with SpecimenOrObservationBaseDTO

use of eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO in project cdmlib by cybertaxonomy.

the class OccurrenceListController method doGetByGeneticAccessionNumber.

@RequestMapping(method = RequestMethod.GET, value = "byGeneticAccessionNumber")
public SpecimenOrObservationBaseDTO doGetByGeneticAccessionNumber(@RequestParam(value = "accessionNumber", required = true) String accessionNumber, HttpServletRequest request, HttpServletResponse response) throws IOException {
    logger.info("doGetByGeneticAccessionNumber() - " + requestPathAndQuery(request));
    SpecimenOrObservationBaseDTO sobDto = service.findByGeneticAccessionNumber(accessionNumber, null);
    if (sobDto == null) {
        response.setHeader("Failure", "No DNA available for accession number ");
        HttpStatusMessage.create("No DNA available for accession number " + accessionNumber, 400).send(response);
        return null;
    }
    return sobDto;
}
Also used : SpecimenOrObservationBaseDTO(eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with SpecimenOrObservationBaseDTO

use of eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO in project cdmlib by cybertaxonomy.

the class OccurrenceServiceImpl method originalDTOs.

/**
 * @param originalDTO
 * @return
 */
private Set<SpecimenOrObservationBaseDTO> originalDTOs(UUID derivativeUuid) {
    Set<SpecimenOrObservationBaseDTO> dtos = new HashSet<>();
    List<SpecimenOrObservationBase> specimens = dao.findOriginalsForDerivedUnit(derivativeUuid, null);
    for (SpecimenOrObservationBase sob : specimens) {
        if (sob instanceof FieldUnit) {
            dtos.add(FieldUnitDTO.fromEntity((FieldUnit) sob));
        } else {
            dtos.add(DerivedUnitDTO.fromEntity((DerivedUnit) sob));
        }
    }
    return dtos;
}
Also used : DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) FieldUnit(eu.etaxonomy.cdm.model.occurrence.FieldUnit) SpecimenOrObservationBaseDTO(eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO) SpecimenOrObservationBase(eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase) HashSet(java.util.HashSet)

Example 5 with SpecimenOrObservationBaseDTO

use of eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO in project cdmlib by cybertaxonomy.

the class OccurrenceServiceImpl method loadFieldUnitDTO.

@Override
@Transactional(readOnly = true)
public FieldUnitDTO loadFieldUnitDTO(UUID derivedUnitUuid) {
    FieldUnitDTO fieldUnitDTO = null;
    DerivedUnitDTO derivedUnitDTO = null;
    Map<UUID, SpecimenOrObservationBaseDTO> cycleDetectionMap = new HashMap<>();
    SpecimenOrObservationBase<?> derivative = dao.load(derivedUnitUuid);
    if (derivative != null) {
        if (derivative instanceof FieldUnit) {
            fieldUnitDTO = FieldUnitDTO.fromEntity((FieldUnit) derivative);
            return fieldUnitDTO;
        } else {
            // must be a DerivedUnit otherwise
            derivedUnitDTO = DerivedUnitDTO.fromEntity((DerivedUnit) derivative);
            while (true) {
                Set<SpecimenOrObservationBaseDTO> originals = originalDTOs(derivedUnitDTO.getUuid());
                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.");
                }
                SpecimenOrObservationBaseDTO 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);
                    }
                    // but let the derivate to be added to the original which is closer to the FieldUnit (below at originalDTO.addDerivate(derivedUnitDTO);)
                    for (SpecimenOrObservationBaseDTO seenOriginal : cycleDetectionMap.values()) {
                        for (SpecimenOrObservationBaseDTO derivateDTO : seenOriginal.getDerivatives()) {
                            if (derivateDTO.equals(originalDTO)) {
                                seenOriginal.getDerivatives().remove(originalDTO);
                            }
                        }
                    }
                } else {
                    cycleDetectionMap.put(originalDTO.getUuid(), originalDTO);
                }
                if (originalDTO instanceof FieldUnitDTO) {
                    fieldUnitDTO = (FieldUnitDTO) originalDTO;
                    break;
                } else {
                    // So this must be a DerivedUnitDTO
                    if (derivedUnitDTO == null) {
                        derivedUnitDTO = (DerivedUnitDTO) originalDTO;
                    } else {
                        derivedUnitDTO = (DerivedUnitDTO) originalDTO;
                    }
                }
            }
        }
    }
    return fieldUnitDTO;
}
Also used : DerivedUnit(eu.etaxonomy.cdm.model.occurrence.DerivedUnit) FieldUnit(eu.etaxonomy.cdm.model.occurrence.FieldUnit) FieldUnitDTO(eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO) HashMap(java.util.HashMap) SpecimenOrObservationBaseDTO(eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO) DerivedUnitDTO(eu.etaxonomy.cdm.api.service.dto.DerivedUnitDTO) UUID(java.util.UUID) LuceneParseException(eu.etaxonomy.cdm.api.service.search.LuceneParseException) ReferencedObjectUndeletableException(eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException) TransientObjectException(org.hibernate.TransientObjectException) ParseException(org.apache.lucene.queryparser.classic.ParseException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) IOException(java.io.IOException) DerivedUnitFacadeNotSupportedException(eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SpecimenOrObservationBaseDTO (eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO)9 DerivedUnitDTO (eu.etaxonomy.cdm.api.service.dto.DerivedUnitDTO)6 DerivedUnit (eu.etaxonomy.cdm.model.occurrence.DerivedUnit)5 UUID (java.util.UUID)5 FieldUnitDTO (eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO)4 FieldUnit (eu.etaxonomy.cdm.model.occurrence.FieldUnit)4 HashMap (java.util.HashMap)4 DerivedUnitFacadeNotSupportedException (eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException)3 FindOccurrencesConfigurator (eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator)3 SpecimenDeleteConfigurator (eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator)3 DNASampleDTO (eu.etaxonomy.cdm.api.service.dto.DNASampleDTO)3 ReferencedObjectUndeletableException (eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException)3 ISequenceService (eu.etaxonomy.cdm.api.service.molecular.ISequenceService)3 LuceneParseException (eu.etaxonomy.cdm.api.service.search.LuceneParseException)3 HibernateProxyHelper (eu.etaxonomy.cdm.hibernate.HibernateProxyHelper)3 CdmBase (eu.etaxonomy.cdm.model.common.CdmBase)3 DescriptionElementBase (eu.etaxonomy.cdm.model.description.DescriptionElementBase)3 IndividualsAssociation (eu.etaxonomy.cdm.model.description.IndividualsAssociation)3 SpecimenDescription (eu.etaxonomy.cdm.model.description.SpecimenDescription)3 DnaSample (eu.etaxonomy.cdm.model.molecular.DnaSample)3