Search in sources :

Example 1 with TermNode

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

the class TermNodeServiceImplTest method testSaveCharacterNode_supportedData.

@Test
@DataSets({ @DataSet(loadStrategy = CleanSweepInsertLoadStrategy.class, value = "/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"), @DataSet(value = "/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml") })
public void testSaveCharacterNode_supportedData() {
    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;
        characterDto.setSupportsCategoricalData(true);
        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().isSupportsCategoricalData());
    } else {
        Assert.fail();
    }
}
Also used : ArrayList(java.util.ArrayList) 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) DataSets(org.unitils.dbunit.annotation.DataSets)

Example 2 with TermNode

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

the class OwlExportUtil method addChildNode.

private static void addChildNode(TermNode parentNode, Resource parentResourceNode, ICdmRepository repo, StructureTreeOwlExportState state) {
    List<TermNode> childNodes = parentNode.getChildNodes();
    for (TermNode child : childNodes) {
        // create node resource with term
        Resource nodeResource = OwlExportUtil.createNodeResource(child, false, repo, state);
        // add node to parent node
        parentResourceNode.addProperty(OwlUtil.propHasSubStructure, nodeResource);
        addChildNode(child, nodeResource, repo, state);
    }
}
Also used : Resource(com.hp.hpl.jena.rdf.model.Resource) TermNode(eu.etaxonomy.cdm.model.term.TermNode)

Example 3 with TermNode

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

the class Cdm2CdmVocabularyImportTest method testInvokeVocabulary.

@Test
public void testInvokeVocabulary() {
    @SuppressWarnings("unchecked") TermVocabulary<DefinedTerm> voc = vocService.find(uuidStructVoc);
    Assert.assertNull("Vocabulary must not exist before invoke", voc);
    ImportResult result = defaultImport.invoke(this.configurator);
    Assert.assertTrue(result.isSuccess());
    commitAndStartNewTransaction();
    voc = vocService.find(uuidStructVoc);
    Assert.assertNotNull("Vocabulary must exist after invoke", voc);
    @SuppressWarnings("unchecked") TermVocabulary<DefinedTerm> otherVoc = otherRepository.getVocabularyService().find(uuidStructVoc);
    Assert.assertNotSame(otherVoc, voc);
    Assert.assertEquals(1, voc.getTerms().size());
    // add term in other
    UUID uuidSecond = UUID.fromString("56546e58-e4ea-47f9-ae49-de772a416003");
    DefinedTerm secondTerm = getStructure("2.", uuidSecond);
    TransactionStatus tx = otherRepository.startTransaction();
    otherVoc = otherRepository.getVocabularyService().find(uuidStructVoc);
    otherVoc.addTerm(secondTerm);
    otherRepository.getTermService().saveOrUpdate(secondTerm);
    otherRepository.commitTransaction(tx);
    // test if added term gets imported
    commitAndStartNewTransaction();
    voc = vocService.find(uuidStructVoc);
    Assert.assertEquals(1, voc.getTerms().size());
    commitAndStartNewTransaction();
    result = defaultImport.invoke(this.configurator);
    commitAndStartNewTransaction();
    voc = vocService.find(uuidStructVoc);
    Assert.assertEquals(2, voc.getTerms().size());
    Assert.assertTrue("As contains works on equal() the term should be contained", voc.getTerms().contains(secondTerm));
    voc.getTerms().stream().filter(a -> a.getUuid().equals(uuidSecond)).forEach(t -> {
        Assert.assertEquals(secondTerm, t);
        Assert.assertNotSame(secondTerm, t);
    });
    // test invoke for graph
    configurator.setGraphFilter(new HashSet<>(Arrays.asList(uuidStructGraph)));
    TermTree<DefinedTerm> graph = treeService.find(uuidStructGraph);
    Assert.assertNull("Graph must not exist before invoke", graph);
    result = defaultImport.invoke(this.configurator);
    Assert.assertTrue(result.isSuccess());
    commitAndStartNewTransaction();
    graph = treeService.find(uuidStructGraph);
    Assert.assertNotNull("Graph must exist after invoke", graph);
    TransactionStatus txOther = otherRepository.startTransaction();
    @SuppressWarnings("unchecked") TermTree<DefinedTerm> otherGraph = otherRepository.getTermTreeService().find(uuidStructGraph);
    Assert.assertNotSame(otherGraph, graph);
    Assert.assertEquals(otherGraph.getRoot(), graph.getRoot());
    Assert.assertNotSame(otherGraph.getRoot(), graph.getRoot());
    Assert.assertEquals(1, graph.getRootChildren().size());
    TermNode<DefinedTerm> otherSingleChild = otherGraph.getRootChildren().iterator().next();
    TermNode<DefinedTerm> thisSingleChild = graph.getRootChildren().iterator().next();
    Assert.assertEquals(otherSingleChild, thisSingleChild);
    Assert.assertNotSame(otherSingleChild, thisSingleChild);
    otherRepository.commitTransaction(txOther);
}
Also used : Arrays(java.util.Arrays) SpringBeanByType(org.unitils.spring.annotation.SpringBeanByType) TermVocabulary(eu.etaxonomy.cdm.model.term.TermVocabulary) BeforeClass(org.junit.BeforeClass) ICdmDataSource(eu.etaxonomy.cdm.database.ICdmDataSource) HashSet(java.util.HashSet) Logger(org.apache.log4j.Logger) LoggingIoObserver(eu.etaxonomy.cdm.io.common.events.LoggingIoObserver) CdmDataSource(eu.etaxonomy.cdm.database.CdmDataSource) TermTree(eu.etaxonomy.cdm.model.term.TermTree) CdmApplicationController(eu.etaxonomy.cdm.api.application.CdmApplicationController) Before(org.junit.Before) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) TermNode(eu.etaxonomy.cdm.model.term.TermNode) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) UUID(java.util.UUID) DefinedTerm(eu.etaxonomy.cdm.model.term.DefinedTerm) FileNotFoundException(java.io.FileNotFoundException) ImportResult(eu.etaxonomy.cdm.io.common.ImportResult) ITermTreeService(eu.etaxonomy.cdm.api.service.ITermTreeService) IVocabularyService(eu.etaxonomy.cdm.api.service.IVocabularyService) TermType(eu.etaxonomy.cdm.model.term.TermType) Ignore(org.junit.Ignore) DbSchemaValidation(eu.etaxonomy.cdm.database.DbSchemaValidation) TransactionStatus(org.springframework.transaction.TransactionStatus) CdmApplicationAwareDefaultImport(eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport) Assert(org.junit.Assert) SpringBeanByName(org.unitils.spring.annotation.SpringBeanByName) ImportResult(eu.etaxonomy.cdm.io.common.ImportResult) TransactionStatus(org.springframework.transaction.TransactionStatus) UUID(java.util.UUID) DefinedTerm(eu.etaxonomy.cdm.model.term.DefinedTerm) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test)

Example 4 with TermNode

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

the class StructureTreeOwlImportTest method testImportStructureTree.

@Test
@DataSet(value = "/eu/etaxonomy/cdm/database/BlankDataSet.xml")
public void testImportStructureTree() throws URISyntaxException {
    URL url = this.getClass().getResource("/eu/etaxonomy/cdm/io/owl/in/test_structures.owl");
    URI uri = URI.fromUrl(url);
    assertNotNull(url);
    StructureTreeOwlImportConfigurator configurator = StructureTreeOwlImportConfigurator.NewInstance(uri);
    boolean result = defaultImport.invoke(configurator).isSuccess();
    assertTrue("Return value for import.invoke should be true", result);
    this.setComplete();
    this.endTransaction();
    String treeLabel = "test_structures";
    List<TermTree> trees = termTreeServcie.listByTitle(TermTree.class, treeLabel, MatchMode.EXACT, null, null, null, null, null);
    List<String> nodeProperties = new ArrayList<>();
    nodeProperties.add("term");
    nodeProperties.add("term.media");
    TermTree<Feature> tree = termTreeServcie.loadWithNodes(trees.iterator().next().getUuid(), null, nodeProperties);
    assertNotNull("featureTree should not be null", tree);
    assertEquals("Tree has wrong term type", TermType.Structure, tree.getTermType());
    assertEquals("Wrong number of distinct features", 4, tree.getDistinctTerms().size());
    List rootChildren = tree.getRootChildren();
    assertEquals("Wrong number of root children", 1, rootChildren.size());
    Object entirePlant = rootChildren.iterator().next();
    assertTrue("Root is no feature node", entirePlant instanceof TermNode);
    assertEquals("Root node has wrong term type", TermType.Structure, ((TermNode) entirePlant).getTermType());
    TermNode<DefinedTerm> entirePlantNode = (TermNode<DefinedTerm>) entirePlant;
    List<TermNode<DefinedTerm>> childNodes = entirePlantNode.getChildNodes();
    assertEquals("Wrong number of children", 2, childNodes.size());
    String inflorescenceLabel = "inflorescence";
    String inflorescenceDescription = " the part of the plant that bears the flowers, including all its bracts  branches and flowers  but excluding unmodified leaves               ";
    List<DefinedTerm> records = termService.findByRepresentationText(inflorescenceDescription, DefinedTerm.class, null, null).getRecords();
    assertEquals("wrong number of terms found for \"inflorescence\"", 1, records.size());
    DefinedTerm inflorescence = records.iterator().next();
    assertEquals(inflorescenceLabel, inflorescence.getLabel(Language.ENGLISH()));
    for (TermNode<DefinedTerm> termNode : childNodes) {
        assertTrue("Child node not found. Found node with term: " + termNode.getTerm().getLabel(), termNode.getTerm().getUuid().equals(inflorescence.getUuid()) || termNode.getTerm().getLabel(Language.ENGLISH()).equals("Flower"));
        if (termNode.getTerm().getUuid().equals(inflorescence.getUuid())) {
            assertEquals("Term mismatch", inflorescence, termNode.getTerm());
            inflorescence = termNode.getTerm();
            assertEquals("wrong id in vocabulary", "inflorescence", inflorescence.getIdInVocabulary());
            assertEquals("wrong symbol", "infloSymbol", inflorescence.getSymbol());
            assertEquals("wrong symbol2", "infloSymbol2", inflorescence.getSymbol2());
            Set<Media> mediaSet = inflorescence.getMedia();
            assertEquals("wrong number of media", 1, mediaSet.size());
            Media media = mediaSet.iterator().next();
            MediaRepresentationPart part = MediaUtils.getFirstMediaRepresentationPart(media);
            assertNotNull("media part not found", part);
            assertEquals("incorrect URI", URI.create("https://upload.wikimedia.org/wikipedia/commons/8/82/Aloe_hereroensis_Auob_C15.JPG"), part.getUri());
            assertEquals("incorrect title", "Aloe hereroensis", media.getTitle(Language.DEFAULT()).getText());
            Representation englishRepresentation = inflorescence.getRepresentation(Language.ENGLISH());
            assertTrue("Description not found", CdmUtils.isNotBlank(englishRepresentation.getDescription()));
            assertEquals("Description wrong", inflorescenceDescription, englishRepresentation.getDescription());
            assertEquals("wrong plural", "inflorescences", englishRepresentation.getPlural());
            assertEquals("wrong label abbrev", "inflo", englishRepresentation.getAbbreviatedLabel());
            // german representation
            assertEquals("wrong number of representations", 2, inflorescence.getRepresentations().size());
            Representation germanRepresentation = inflorescence.getRepresentation(Language.GERMAN());
            assertNotNull("Representation is null for " + Language.GERMAN(), germanRepresentation);
            assertEquals("wrong description", "Der Teil der Pflanze, der die Bluete traegt", germanRepresentation.getDescription());
            assertEquals("wrong label", "Infloreszenz", germanRepresentation.getLabel());
        }
    }
    assertNotNull("term is null", inflorescence);
    assertEquals("Wrong term type", TermType.Structure, inflorescence.getTermType());
    String vocLabel = "03 Generative Structures";
    List<TermVocabulary> vocs = vocabularyService.findByTitle(TermVocabulary.class, vocLabel, MatchMode.EXACT, null, null, null, null, Arrays.asList("terms")).getRecords();
    assertEquals("wrong number of vocabularies", 1, vocs.size());
    TermVocabulary termVoc = vocs.iterator().next();
    assertEquals("Wrong vocabulary label", vocLabel, termVoc.getTitleCache());
    Collection<TermDto> topLevelTerms = vocabularyService.getTopLevelTerms(termVoc.getUuid());
    assertEquals("wrong number of top level terms", 4, topLevelTerms.size());
}
Also used : StructureTreeOwlImportConfigurator(eu.etaxonomy.cdm.io.descriptive.owl.in.StructureTreeOwlImportConfigurator) ArrayList(java.util.ArrayList) TermNode(eu.etaxonomy.cdm.model.term.TermNode) URI(eu.etaxonomy.cdm.common.URI) Feature(eu.etaxonomy.cdm.model.description.Feature) URL(java.net.URL) ArrayList(java.util.ArrayList) List(java.util.List) MediaRepresentationPart(eu.etaxonomy.cdm.model.media.MediaRepresentationPart) TermTree(eu.etaxonomy.cdm.model.term.TermTree) Media(eu.etaxonomy.cdm.model.media.Media) TermVocabulary(eu.etaxonomy.cdm.model.term.TermVocabulary) Representation(eu.etaxonomy.cdm.model.term.Representation) TermDto(eu.etaxonomy.cdm.persistence.dto.TermDto) DefinedTerm(eu.etaxonomy.cdm.model.term.DefinedTerm) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test) DataSet(org.unitils.dbunit.annotation.DataSet)

Example 5 with TermNode

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

the class PostMergeEntityListener method removeNullFromCollections.

private static void removeNullFromCollections(Object entity) {
    if (entity != null) {
        Class<?> entityClazz = entity.getClass();
        if (TaxonNode.class.isAssignableFrom(entityClazz)) {
            TaxonNode node = (TaxonNode) entity;
            node.removeNullValueFromChildren();
        } else if (PolytomousKeyNode.class.isAssignableFrom(entityClazz)) {
            PolytomousKeyNode node = (PolytomousKeyNode) entity;
            if (node.getChildren() != null && Hibernate.isInitialized(node.getChildren())) {
                node.removeNullValueFromChildren();
                for (PolytomousKeyNode childNode : node.getChildren()) {
                    removeNullFromCollections(childNode);
                }
            }
        } else if (PolytomousKey.class.isAssignableFrom(entityClazz)) {
            PolytomousKey key = (PolytomousKey) entity;
            PolytomousKeyNode node = key.getRoot();
            if (node != null && node.getChildren() != null && Hibernate.isInitialized(node.getChildren())) {
                node.removeNullValueFromChildren();
                for (PolytomousKeyNode childNode : node.getChildren()) {
                    removeNullFromCollections(childNode);
                }
            }
        } else if (TermTree.class.isAssignableFrom(entityClazz)) {
            TermTree<?> tree = (TermTree<?>) entity;
            tree.removeNullValueFromChildren();
            try {
                for (TermNode<?> node : tree.getRootChildren()) {
                    node.removeNullValueFromChildren();
                    if (node.getChildNodes() != null) {
                        for (TermNode<?> childNode : node.getChildNodes()) {
                            removeNullFromCollections(childNode);
                        }
                    }
                }
            } catch (LazyInitializationException e) {
                logger.warn("Cannot clean up uninitialized children without a session, skipping.");
            }
        } else if (TermNode.class.isAssignableFrom(entityClazz)) {
            TermNode<?> node = (TermNode<?>) entity;
            if (Hibernate.isInitialized(node.getChildNodes())) {
                node.removeNullValueFromChildren();
            }
        }
    }
}
Also used : TermTree(eu.etaxonomy.cdm.model.term.TermTree) TaxonNode(eu.etaxonomy.cdm.model.taxon.TaxonNode) LazyInitializationException(org.hibernate.LazyInitializationException) PolytomousKeyNode(eu.etaxonomy.cdm.model.description.PolytomousKeyNode) TermNode(eu.etaxonomy.cdm.model.term.TermNode) PolytomousKey(eu.etaxonomy.cdm.model.description.PolytomousKey)

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