Search in sources :

Example 21 with TermNode

use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.

the class TermNodeServiceImpl method saveCharacterNodeDtoList.

@Override
public UpdateResult saveCharacterNodeDtoList(List<CharacterNodeDto> dtos) {
    MergeResult<TermNode> mergeResult;
    UpdateResult result = new UpdateResult();
    List<UUID> nodeUuids = new ArrayList<>();
    dtos.stream().forEach(dto -> nodeUuids.add(dto.getUuid()));
    List<TermNode> nodes = dao.list(nodeUuids, null, 0, null, null);
    // check all attributes for changes and adapt
    for (TermNode<Character> node : nodes) {
        for (CharacterNodeDto dto : dtos) {
            // TermNodeDto dto = dtoIterator.next();
            if (dto.getUuid().equals(node.getUuid())) {
                updateFeatureStates(node, dto, true);
                updateFeatureStates(node, dto, false);
                // if (!dto.getInapplicableIf().equals(node.getInapplicableIf())){
                // node.getInapplicableIf().clear();
                // node.getInapplicableIf().addAll(dto.getInapplicableIf());
                // }
                // if (!dto.getOnlyApplicableIf().equals(node.getOnlyApplicableIf())){
                // node.getOnlyApplicableIf().clear();
                // node.getOnlyApplicableIf().addAll(dto.getOnlyApplicableIf());
                // }
                Character character = null;
                CharacterDto characterDto = (CharacterDto) dto.getTerm();
                character = HibernateProxyHelper.deproxy(node.getTerm(), Character.class);
                if (characterDto.getRatioTo() != null) {
                    TermNode ratioToStructure = this.load(characterDto.getRatioTo().getUuid());
                    character.setRatioToStructure(ratioToStructure);
                } else {
                    character.setRatioToStructure(null);
                }
                // supportsXXX
                // TODO add all other supportsXXX (6 are missing)
                character.setSupportsCategoricalData(characterDto.isSupportsCategoricalData());
                character.setSupportsQuantitativeData(characterDto.isSupportsQuantitativeData());
                // availableForXXX
                character.setAvailableForTaxon(characterDto.isAvailableForTaxon());
                character.setAvailableForOccurrence(characterDto.isAvailableForOccurrence());
                character.setAvailableForTaxonName(characterDto.isAvailableForTaxonName());
                // representations
                for (Representation rep : dto.getTerm().getRepresentations()) {
                    Representation oldRep = character.getRepresentation(rep.getLanguage());
                    if (oldRep == null) {
                        oldRep = new Representation();
                        oldRep.setLanguage(rep.getLanguage());
                        character.addRepresentation(oldRep);
                    }
                    oldRep.setLabel(rep.getLabel());
                    oldRep.setAbbreviatedLabel(rep.getAbbreviatedLabel());
                    oldRep.setText(rep.getText());
                    oldRep.setPlural(rep.getPlural());
                }
                Set<Representation> deleteRepresentations = new HashSet<>();
                if (character.getRepresentations().size() > dto.getTerm().getRepresentations().size()) {
                    for (Representation rep : character.getRepresentations()) {
                        if (dto.getTerm().getRepresentation(rep.getLanguage()) == null) {
                            deleteRepresentations.add(rep);
                        }
                    }
                }
                if (!deleteRepresentations.isEmpty()) {
                    for (Representation rep : deleteRepresentations) {
                        character.removeRepresentation(rep);
                    }
                }
                // structural modifier
                if (characterDto.getStructureModifier() != null) {
                    DefinedTerm structureModifier = (DefinedTerm) termService.load(characterDto.getStructureModifier().getUuid());
                    character.setStructureModifier(structureModifier);
                } else {
                    character.setStructureModifier(null);
                }
                // recommended measurement units
                character.getRecommendedMeasurementUnits().clear();
                List<UUID> uuids = new ArrayList<>();
                for (TermDto termDto : characterDto.getRecommendedMeasurementUnits()) {
                    uuids.add(termDto.getUuid());
                }
                List<DefinedTermBase> terms;
                if (!uuids.isEmpty()) {
                    terms = termService.load(uuids, null);
                    Set<MeasurementUnit> measurementUnits = new HashSet<>();
                    for (DefinedTermBase term : terms) {
                        if (term instanceof MeasurementUnit) {
                            measurementUnits.add((MeasurementUnit) term);
                        }
                    }
                    character.getRecommendedMeasurementUnits().addAll(measurementUnits);
                }
                // statistical measures
                character.getRecommendedStatisticalMeasures().clear();
                uuids = new ArrayList<>();
                for (TermDto termDto : characterDto.getRecommendedStatisticalMeasures()) {
                    uuids.add(termDto.getUuid());
                }
                if (!uuids.isEmpty()) {
                    terms = termService.load(uuids, null);
                    Set<StatisticalMeasure> statisticalMeasures = new HashSet<>();
                    for (DefinedTermBase<?> term : terms) {
                        if (term instanceof StatisticalMeasure) {
                            statisticalMeasures.add((StatisticalMeasure) term);
                        }
                    }
                    character.getRecommendedStatisticalMeasures().addAll(statisticalMeasures);
                }
                // recommended mod. vocabularies
                character.getRecommendedModifierEnumeration().clear();
                uuids = new ArrayList<>();
                for (TermVocabularyDto termDto : characterDto.getRecommendedModifierEnumeration()) {
                    uuids.add(termDto.getUuid());
                }
                List<TermVocabulary> termVocs;
                if (!uuids.isEmpty()) {
                    termVocs = vocabularyService.load(uuids, null);
                    for (TermVocabulary voc : termVocs) {
                        character.addRecommendedModifierEnumeration(voc);
                    }
                }
                // supported state vocabularies
                character.getSupportedCategoricalEnumerations().clear();
                uuids = new ArrayList<>();
                for (TermVocabularyDto termDto : characterDto.getSupportedCategoricalEnumerations()) {
                    uuids.add(termDto.getUuid());
                }
                if (!uuids.isEmpty()) {
                    termVocs = vocabularyService.load(uuids, null);
                    for (TermVocabulary voc : termVocs) {
                        character.addSupportedCategoricalEnumeration(voc);
                    }
                }
                node.setTerm(character);
                mergeResult = dao.merge(node, true);
                result.addUpdatedObject(mergeResult.getMergedEntity());
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) TermNode(eu.etaxonomy.cdm.model.term.TermNode) StatisticalMeasure(eu.etaxonomy.cdm.model.description.StatisticalMeasure) DefinedTermBase(eu.etaxonomy.cdm.model.term.DefinedTermBase) TermVocabularyDto(eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto) CharacterDto(eu.etaxonomy.cdm.persistence.dto.CharacterDto) UUID(java.util.UUID) HashSet(java.util.HashSet) MeasurementUnit(eu.etaxonomy.cdm.model.description.MeasurementUnit) Character(eu.etaxonomy.cdm.model.description.Character) TermVocabulary(eu.etaxonomy.cdm.model.term.TermVocabulary) Representation(eu.etaxonomy.cdm.model.term.Representation) TermDto(eu.etaxonomy.cdm.persistence.dto.TermDto) CharacterNodeDto(eu.etaxonomy.cdm.persistence.dto.CharacterNodeDto) DefinedTerm(eu.etaxonomy.cdm.model.term.DefinedTerm)

Example 22 with TermNode

use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.

the class TermNodeServiceImpl method moveNode.

@Override
public UpdateResult moveNode(UUID movedNodeUuid, UUID targetNodeUuid, int position) {
    UpdateResult result = new UpdateResult();
    List<String> propertyPaths = new ArrayList<>();
    propertyPaths.add("parent");
    propertyPaths.add("parent.children");
    propertyPaths.add("children");
    TermNode test = load(movedNodeUuid, propertyPaths);
    TermNode movedNode = CdmBase.deproxy(load(movedNodeUuid, propertyPaths), TermNode.class);
    TermNode<?> targetNode = CdmBase.deproxy(load(targetNodeUuid, propertyPaths));
    TermNode<?> parent = CdmBase.deproxy(movedNode.getParent());
    parent.removeChild(movedNode);
    if (position < 0) {
        targetNode.addChild(movedNode);
    } else {
        targetNode.addChild(movedNode, position);
    }
    result.addUpdatedObject(targetNode);
    result.addUpdatedObject(parent);
    result.setCdmEntity(movedNode);
    return result;
}
Also used : ArrayList(java.util.ArrayList) TermNode(eu.etaxonomy.cdm.model.term.TermNode)

Example 23 with TermNode

use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.

the class TermNodeServiceImpl method addChildNode.

@Override
public UpdateResult addChildNode(UUID nodeUUID, UUID termChildUuid, int position) {
    UpdateResult result = new UpdateResult();
    TermNode node = load(nodeUUID);
    if (node == null) {
        result.setError();
        result.addException(new Exception("The parent node does not exist."));
        return result;
    }
    DefinedTermBase child = HibernateProxyHelper.deproxy(termService.load(termChildUuid), DefinedTermBase.class);
    if (node != null && node.getGraph() != null && !node.getGraph().isAllowDuplicates() && node.getGraph().getDistinctTerms().contains(child)) {
        result.setError();
        result.addException(new Exception("This term tree does not allow duplicate terms."));
        return result;
    }
    TermNode childNode;
    if (position < 0) {
        childNode = node.addChild(child);
    } else {
        childNode = node.addChild(child, position);
    }
    save(childNode);
    result.addUpdatedObject(node);
    result.setCdmEntity(childNode);
    return result;
}
Also used : DefinedTermBase(eu.etaxonomy.cdm.model.term.DefinedTermBase) TermNode(eu.etaxonomy.cdm.model.term.TermNode) DataChangeNoRollbackException(eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException) ReferencedObjectUndeletableException(eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException)

Example 24 with TermNode

use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.

the class DistributionAggregation method makeStatusOrder.

/**
 * Sets the priorities for presence and absence terms, the priorities are stored in extensions.
 * This method will start a new transaction and commits it after the work is done.
 */
private void makeStatusOrder() {
    TransactionStatus txStatus = startTransaction(false);
    @SuppressWarnings("rawtypes") TermCollection<PresenceAbsenceTerm, TermNode> stOrder = getConfig().getStatusOrder();
    if (stOrder == null) {
        stOrder = defaultStatusOrder();
    }
    if (stOrder.isInstanceOf(TermTree.class)) {
        statusOrder = CdmBase.deproxy(stOrder, TermTree.class).asTermList();
    } else if (stOrder.isInstanceOf(OrderedTermVocabulary.class)) {
        statusOrder = new ArrayList<>(CdmBase.deproxy(stOrder, OrderedTermVocabulary.class).getOrderedTerms());
    } else {
        throw new RuntimeException("TermCollection type for status order not supported: " + statusOrder.getClass().getSimpleName());
    }
    commitTransaction(txStatus);
}
Also used : ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) PresenceAbsenceTerm(eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm) TermNode(eu.etaxonomy.cdm.model.term.TermNode) OrderedTermVocabulary(eu.etaxonomy.cdm.model.term.OrderedTermVocabulary)

Example 25 with TermNode

use of eu.etaxonomy.cdm.model.term.TermNode in project cdmlib by cybertaxonomy.

the class TermNodeServiceImplTest method testSaveCharacterNode_representation.

@Test
public void testSaveCharacterNode_representation() {
    UUID characterTreeUuid = createAndSaveCharacterTree();
    TermTreeDto dto = termTreeService.getTermTreeDtoByUuid(characterTreeUuid);
    List<TermNodeDto> children = dto.getRoot().getChildren();
    CharacterNodeDto nodeDto = (CharacterNodeDto) children.get(0);
    TermDto termDto = nodeDto.getTerm();
    if (termDto instanceof CharacterDto) {
        CharacterDto characterDto = (CharacterDto) termDto;
        Representation rep = characterDto.getPreferredRepresentation(Language.DEFAULT());
        if (rep != null) {
            rep.setText("Test");
        } else {
            rep = Representation.NewInstance("Test", "", "", Language.DEFAULT());
            characterDto.addRepresentation(rep);
        }
        List<CharacterNodeDto> dtos = new ArrayList<>();
        dtos.add(nodeDto);
        termNodeService.saveCharacterNodeDtoList(dtos);
        commitAndStartNewTransaction();
        @SuppressWarnings("unchecked") TermTree<Feature> characterTree = termTreeService.load(characterTreeUuid);
        List<TermNode<Feature>> childNodes = characterTree.getRoot().getChildNodes();
        TermNode<Feature> child = childNodes.get(0);
        Assert.assertTrue(child.getTerm().getPreferredRepresentation(Language.DEFAULT()).getText().equals("Test"));
    } else {
        Assert.fail();
    }
}
Also used : ArrayList(java.util.ArrayList) Representation(eu.etaxonomy.cdm.model.term.Representation) TermNode(eu.etaxonomy.cdm.model.term.TermNode) TermNodeDto(eu.etaxonomy.cdm.persistence.dto.TermNodeDto) Feature(eu.etaxonomy.cdm.model.description.Feature) TermDto(eu.etaxonomy.cdm.persistence.dto.TermDto) CharacterDto(eu.etaxonomy.cdm.persistence.dto.CharacterDto) CharacterNodeDto(eu.etaxonomy.cdm.persistence.dto.CharacterNodeDto) TermTreeDto(eu.etaxonomy.cdm.persistence.dto.TermTreeDto) UUID(java.util.UUID) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test)

Aggregations

TermNode (eu.etaxonomy.cdm.model.term.TermNode)25 ArrayList (java.util.ArrayList)12 Feature (eu.etaxonomy.cdm.model.description.Feature)7 TermTree (eu.etaxonomy.cdm.model.term.TermTree)7 UUID (java.util.UUID)7 DefinedTerm (eu.etaxonomy.cdm.model.term.DefinedTerm)5 Representation (eu.etaxonomy.cdm.model.term.Representation)5 TermVocabulary (eu.etaxonomy.cdm.model.term.TermVocabulary)5 TermDto (eu.etaxonomy.cdm.persistence.dto.TermDto)4 HashSet (java.util.HashSet)4 Resource (com.hp.hpl.jena.rdf.model.Resource)3 ReferencedObjectUndeletableException (eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException)3 Media (eu.etaxonomy.cdm.model.media.Media)3 DefinedTermBase (eu.etaxonomy.cdm.model.term.DefinedTermBase)3 CdmTransactionalIntegrationTest (eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)3 Test (org.junit.Test)3 DataChangeNoRollbackException (eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException)2 MeasurementUnit (eu.etaxonomy.cdm.model.description.MeasurementUnit)2 StatisticalMeasure (eu.etaxonomy.cdm.model.description.StatisticalMeasure)2 TaxonNode (eu.etaxonomy.cdm.model.taxon.TaxonNode)2