use of eu.etaxonomy.cdm.api.service.dto.EntityDTO 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;
}
Aggregations