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();
}
}
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);
}
}
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);
}
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());
}
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();
}
}
}
}
Aggregations