Search in sources :

Example 1 with TaxonStatus

use of eu.etaxonomy.cdm.persistence.dto.TaxonStatus in project cdmlib by cybertaxonomy.

the class ClassificationServiceImpl method getTaxonInContext.

@Override
public TaxonInContextDTO getTaxonInContext(UUID classificationUuid, UUID taxonBaseUuid, Boolean doChildren, Boolean doSynonyms, boolean includeUnpublished, List<UUID> ancestorMarkers, TaxonNodeSortMode sortMode) {
    TaxonInContextDTO result = new TaxonInContextDTO();
    TaxonBase<?> taxonBase = taxonDao.load(taxonBaseUuid);
    if (taxonBase == null) {
        throw new EntityNotFoundException("Taxon with uuid " + taxonBaseUuid + " not found in datasource");
    }
    boolean isSynonym = false;
    Taxon acceptedTaxon;
    if (taxonBase.isInstanceOf(Synonym.class)) {
        isSynonym = true;
        Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
        acceptedTaxon = synonym.getAcceptedTaxon();
        if (acceptedTaxon == null) {
            throw new EntityNotFoundException("Accepted taxon not found for synonym");
        }
        TaxonStatus taxonStatus = TaxonStatus.Synonym;
        if (synonym.getName() != null && acceptedTaxon.getName() != null && synonym.getName().getHomotypicalGroup().equals(acceptedTaxon.getName().getHomotypicalGroup())) {
            taxonStatus = TaxonStatus.SynonymObjective;
        }
        result.setTaxonStatus(taxonStatus);
    } else {
        acceptedTaxon = CdmBase.deproxy(taxonBase, Taxon.class);
        result.setTaxonStatus(TaxonStatus.Accepted);
    }
    UUID acceptedTaxonUuid = acceptedTaxon.getUuid();
    UUID taxonNodeUuid = getTaxonNodeUuidByTaxonUuid(classificationUuid, acceptedTaxonUuid);
    if (taxonNodeUuid == null) {
        throw new EntityNotFoundException("Taxon not found in classficiation with uuid " + classificationUuid + ". Either classification does not exist or does not contain taxon/synonym with uuid " + taxonBaseUuid);
    }
    result.setTaxonNodeUuid(taxonNodeUuid);
    // TODO make it a dao call
    Taxon parentTaxon = getParentTaxon(classificationUuid, acceptedTaxon);
    if (parentTaxon != null) {
        result.setParentTaxonUuid(parentTaxon.getUuid());
        result.setParentTaxonLabel(parentTaxon.getTitleCache());
        if (parentTaxon.getName() != null) {
            result.setParentNameLabel(parentTaxon.getName().getTitleCache());
        }
    }
    result.setTaxonUuid(taxonBaseUuid);
    result.setClassificationUuid(classificationUuid);
    if (taxonBase.getSec() != null) {
        result.setSecundumUuid(taxonBase.getSec().getUuid());
        result.setSecundumLabel(taxonBase.getSec().getTitleCache());
    }
    result.setTaxonLabel(taxonBase.getTitleCache());
    TaxonName name = taxonBase.getName();
    result.setNameUuid(name.getUuid());
    result.setNameLabel(name.getTitleCache());
    result.setNameWithoutAuthor(name.getNameCache());
    result.setGenusOrUninomial(name.getGenusOrUninomial());
    result.setInfraGenericEpithet(name.getInfraGenericEpithet());
    result.setSpeciesEpithet(name.getSpecificEpithet());
    result.setInfraSpecificEpithet(name.getInfraSpecificEpithet());
    result.setAuthorship(name.getAuthorshipCache());
    Rank rank = name.getRank();
    if (rank != null) {
        result.setRankUuid(rank.getUuid());
        String rankLabel = rank.getAbbreviation();
        if (StringUtils.isBlank(rankLabel)) {
            rankLabel = rank.getLabel();
        }
        result.setRankLabel(rankLabel);
    }
    boolean recursive = false;
    Integer pageSize = null;
    Integer pageIndex = null;
    Pager<TaxonNodeDto> children = taxonNodeService.pageChildNodesDTOs(taxonNodeUuid, recursive, includeUnpublished, doSynonyms, sortMode, pageSize, pageIndex);
    // children
    if (!isSynonym) {
        for (TaxonNodeDto childDto : children.getRecords()) {
            if (doChildren && childDto.getTaxonStatus().equals(TaxonStatus.Accepted)) {
                EntityDTO<Taxon> child = new EntityDTO<Taxon>(childDto.getTaxonUuid(), childDto.getTitleCache());
                result.addChild(child);
            } else if (doSynonyms && childDto.getTaxonStatus().isSynonym()) {
                EntityDTO<Synonym> child = new EntityDTO<>(childDto.getTaxonUuid(), childDto.getTitleCache());
                result.addSynonym(child);
            }
        }
    } else {
        result.setAcceptedTaxonUuid(acceptedTaxonUuid);
        String nameTitel = acceptedTaxon.getName() == null ? null : acceptedTaxon.getName().getTitleCache();
        result.setAcceptedTaxonLabel(acceptedTaxon.getTitleCache());
        result.setAcceptedNameLabel(nameTitel);
    }
    // marked ancestors
    if (ancestorMarkers != null && !ancestorMarkers.isEmpty()) {
        @SuppressWarnings("rawtypes") List<DefinedTermBase> markerTypesTerms = termDao.list(ancestorMarkers, pageSize, null, null, null);
        List<MarkerType> markerTypes = new ArrayList<>();
        for (DefinedTermBase<?> term : markerTypesTerms) {
            if (term.isInstanceOf(MarkerType.class)) {
                markerTypes.add(CdmBase.deproxy(term, MarkerType.class));
            }
        }
        if (!markerTypes.isEmpty()) {
            TaxonNode node = taxonNodeDao.findByUuid(taxonNodeUuid);
            handleAncestorsForMarkersRecursive(result, markerTypes, node);
        }
    }
    return result;
}
Also used : TaxonNode(eu.etaxonomy.cdm.model.taxon.TaxonNode) ArrayList(java.util.ArrayList) TaxonStatus(eu.etaxonomy.cdm.persistence.dto.TaxonStatus) MarkedEntityDTO(eu.etaxonomy.cdm.api.service.dto.MarkedEntityDTO) EntityDTO(eu.etaxonomy.cdm.api.service.dto.EntityDTO) DefinedTermBase(eu.etaxonomy.cdm.model.term.DefinedTermBase) TaxonName(eu.etaxonomy.cdm.model.name.TaxonName) TaxonInContextDTO(eu.etaxonomy.cdm.api.service.dto.TaxonInContextDTO) UUID(java.util.UUID) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) Rank(eu.etaxonomy.cdm.model.name.Rank) EntityNotFoundException(javax.persistence.EntityNotFoundException) MarkerType(eu.etaxonomy.cdm.model.common.MarkerType) Synonym(eu.etaxonomy.cdm.model.taxon.Synonym) TaxonNodeDto(eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto)

Aggregations

EntityDTO (eu.etaxonomy.cdm.api.service.dto.EntityDTO)1 MarkedEntityDTO (eu.etaxonomy.cdm.api.service.dto.MarkedEntityDTO)1 TaxonInContextDTO (eu.etaxonomy.cdm.api.service.dto.TaxonInContextDTO)1 MarkerType (eu.etaxonomy.cdm.model.common.MarkerType)1 Rank (eu.etaxonomy.cdm.model.name.Rank)1 TaxonName (eu.etaxonomy.cdm.model.name.TaxonName)1 Synonym (eu.etaxonomy.cdm.model.taxon.Synonym)1 Taxon (eu.etaxonomy.cdm.model.taxon.Taxon)1 TaxonNode (eu.etaxonomy.cdm.model.taxon.TaxonNode)1 DefinedTermBase (eu.etaxonomy.cdm.model.term.DefinedTermBase)1 TaxonNodeDto (eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto)1 TaxonStatus (eu.etaxonomy.cdm.persistence.dto.TaxonStatus)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1