From 137b4f28ff8bef911e3cbd1c50dafebd00cb2144 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Mon, 3 May 2021 14:08:11 +0200 Subject: [PATCH 1/7] [Enhancement #85] Make rdfs:subClassOf subproperty of skos:broader. --- ontology/termit-model.ttl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ontology/termit-model.ttl b/ontology/termit-model.ttl index ca6578d16..2e40fa99d 100644 --- a/ontology/termit-model.ttl +++ b/ontology/termit-model.ttl @@ -264,3 +264,7 @@ termit-pojem:je-publikován a owl:DatatypeProperty , ; rdfs:domain ; rdfs:subPropertyOf . + +rdfs:subClassOf + a rdf:Property ; + rdfs:subPropertyOf . \ No newline at end of file From b67c81d673b2c76542e0fdc120d8862bc3d3d282 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Mon, 3 May 2021 16:29:08 +0200 Subject: [PATCH 2/7] [Enhancement #85] Extend Term with superTypes attribute. Ensure inferred narrower Terms (e.g. based on superType being subPropertyOf broader) are taken into account when loading subTerms. --- .../java/cz/cvut/kbss/termit/model/Term.java | 16 ++++++++++++-- .../termit/persistence/DescriptorFactory.java | 2 ++ .../kbss/termit/persistence/dao/TermDao.java | 14 ++++++------- .../persistence/DescriptorFactoryTest.java | 14 +++++++++++++ .../dao/TermDaoWorkspacesTest.java | 21 +++++++++++++++++++ 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/termit/model/Term.java b/src/main/java/cz/cvut/kbss/termit/model/Term.java index 0776134e5..9fbcc5327 100644 --- a/src/main/java/cz/cvut/kbss/termit/model/Term.java +++ b/src/main/java/cz/cvut/kbss/termit/model/Term.java @@ -6,6 +6,7 @@ import cz.cvut.kbss.jopa.model.annotations.*; import cz.cvut.kbss.jopa.model.descriptors.Descriptor; import cz.cvut.kbss.jopa.vocabulary.DC; +import cz.cvut.kbss.jopa.vocabulary.RDFS; import cz.cvut.kbss.jopa.vocabulary.SKOS; import cz.cvut.kbss.jsonld.annotation.JsonLdAttributeOrder; import cz.cvut.kbss.termit.dto.TermInfo; @@ -42,8 +43,7 @@ public class Term extends AbstractTerm implements HasTypes { public static final List EXPORT_COLUMNS = Collections .unmodifiableList( Arrays.asList("IRI", "Label", "Alternative Labels", "Hidden Labels", "Definition", "Description", - "Types", "Sources", "Parent term", - "SubTerms", "Draft")); + "Types", "Sources", "Parent term", "SubTerms", "Draft")); @Autowired @Transient @@ -64,6 +64,10 @@ public class Term extends AbstractTerm implements HasTypes { @OWLObjectProperty(iri = SKOS.BROADER, fetch = FetchType.EAGER) private Set parentTerms; + @OWLObjectProperty(iri = RDFS.SUB_CLASS_OF, fetch = FetchType.EAGER) + // TODO Replace with TermInfo when new the new model with TermInfo being entity is merged from KBSS + private Set superTypes; + @Inferred @OWLObjectProperty(iri = Vocabulary.s_p_ma_zdroj_definice_termu, fetch = FetchType.EAGER) private TermDefinitionSource definitionSource; @@ -144,6 +148,14 @@ public void addParentTerm(Term term) { parentTerms.add(term); } + public Set getSuperTypes() { + return superTypes; + } + + public void setSuperTypes(Set superTypes) { + this.superTypes = superTypes; + } + public Set getSources() { return sources; } diff --git a/src/main/java/cz/cvut/kbss/termit/persistence/DescriptorFactory.java b/src/main/java/cz/cvut/kbss/termit/persistence/DescriptorFactory.java index 8cd946b8c..dc419311f 100644 --- a/src/main/java/cz/cvut/kbss/termit/persistence/DescriptorFactory.java +++ b/src/main/java/cz/cvut/kbss/termit/persistence/DescriptorFactory.java @@ -248,7 +248,9 @@ public Descriptor termDescriptor(URI vocabularyUri) { persistenceUtils.getCanonicalContainerContexts().forEach(parentDescriptor::addContext); // Allow indefinite length of the ancestor chain parentDescriptor.addAttributeDescriptor(fieldSpec(Term.class, "parentTerms"), parentDescriptor); + parentDescriptor.addAttributeDescriptor(fieldSpec(Term.class, "superTypes"), parentDescriptor); descriptor.addAttributeDescriptor(fieldSpec(Term.class, "parentTerms"), parentDescriptor); + descriptor.addAttributeDescriptor(fieldSpec(Term.class, "superTypes"), parentDescriptor); // Definition source is inferred. That means it is in a special context in GraphDB. Therefore, we need to use // the default context to prevent JOPA from thinking the value has changed on merge descriptor.addAttributeContext(fieldSpec(Term.class, "definitionSource"), null); diff --git a/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java b/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java index 2edd00147..c87a9579a 100644 --- a/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java +++ b/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java @@ -641,19 +641,17 @@ public List findAllSubTerms(Term parent) { graphs.forEach(descriptor::addContext); final TypedQuery query = em.createNativeQuery("SELECT DISTINCT ?term WHERE {" + "GRAPH ?g { " + - "?term ?broader ?parent ;" + - "a ?type ;" + - "?hasLabel ?label . " + - "FILTER (lang(?label) = ?labelLang) ." + + "?term a ?type ." + "}" + + "?term ?broader ?parent ." + "FILTER (?g in (?graphs))" + - "} ORDER BY LCASE(?label)", Term.class).setParameter("type", typeUri) + "}", Term.class).setParameter("type", typeUri) .setParameter("broader", URI.create(SKOS.BROADER)) .setParameter("parent", parent) - .setParameter("hasLabel", LABEL_PROP) .setParameter("graphs", graphs) - .setParameter("labelLang", config.get(ConfigParam.LANGUAGE)) .setDescriptor(descriptor); - return executeQueryAndLoadSubTerms(query, graphs); + final List terms = executeQueryAndLoadSubTerms(query, graphs); + terms.sort(Comparator.comparing(t -> t.getLabel().get(config.get(ConfigParam.LANGUAGE)))); + return terms; } } diff --git a/src/test/java/cz/cvut/kbss/termit/persistence/DescriptorFactoryTest.java b/src/test/java/cz/cvut/kbss/termit/persistence/DescriptorFactoryTest.java index 6a4352b96..7d733adc6 100644 --- a/src/test/java/cz/cvut/kbss/termit/persistence/DescriptorFactoryTest.java +++ b/src/test/java/cz/cvut/kbss/termit/persistence/DescriptorFactoryTest.java @@ -161,4 +161,18 @@ private Set generateCanonicalContainer() { }); return statements.stream().map(s -> URI.create(s.getObject().stringValue())).collect(Collectors.toSet()); } + + @Test + void termDescriptorCreatesDescriptorWithSuperTypesContextsContainingWorkspaceVocabulariesAndCanonicalCacheContainerContexts() throws Exception { + final FieldSpecification superTypesFieldSpec = mock(FieldSpecification.class); + when(superTypesFieldSpec.getJavaField()).thenReturn(Term.class.getDeclaredField("superTypes")); + final Set workspaceVocabularies = IntStream.range(0, 3).mapToObj(i -> Generator.generateUri()).collect(Collectors.toSet()); + final WorkspaceMetadata wsMetadata = workspaceMetadataProvider.getCurrentWorkspaceMetadata(); + doReturn(workspaceVocabularies).when(wsMetadata).getVocabularyContexts(); + final Set canonicalVocabularies = generateCanonicalContainer(); + final Descriptor result = sut.termDescriptor(term); + final Set parentContexts = result.getAttributeDescriptor(superTypesFieldSpec).getContexts(); + assertThat(parentContexts, hasItems(workspaceVocabularies.toArray(new URI[]{}))); + assertThat(parentContexts, hasItems(canonicalVocabularies.toArray(new URI[]{}))); + } } diff --git a/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoWorkspacesTest.java b/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoWorkspacesTest.java index 05419b0fe..3dd2e507e 100644 --- a/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoWorkspacesTest.java +++ b/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoWorkspacesTest.java @@ -847,4 +847,25 @@ void findAllRootsReturnsCorrectPageContent() { final List result = sut.findAllRoots(PageRequest.of(1, pageSize)); assertEquals(expected, result); } + + @Test + void updateSupportsTermWithSupertypeInCanonicalContainer() { + final Term term = Generator.generateTermWithId(); + final Term superType = Generator.generateTermWithId(); + persistTermIntoCanonicalContainer(superType); + final EntityDescriptor termDescriptor = new EntityDescriptor(vocabulary.getUri()); + transactional(() -> { + em.persist(term, termDescriptor); + Generator.addTermInVocabularyRelationship(term, vocabulary.getUri(), em); + }); + + term.setSuperTypes(Collections.singleton(superType)); + // This is normally inferred + term.setVocabulary(vocabulary.getUri()); + transactional(() -> sut.update(term)); + final Term result = em.find(Term.class, term.getUri()); + assertEquals(term, result); + assertEquals(term, result); + assertThat(result.getSuperTypes(), hasItem(superType)); + } } From ef76a446618b07781eb763f8c10e6e0d41b791c7 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Tue, 4 May 2021 10:04:31 +0200 Subject: [PATCH 3/7] [Enhancement #85] Load inferred broader terms when necessary. --- .../kbss/termit/persistence/dao/TermDao.java | 54 ++++++++++++++++--- .../termit/persistence/dao/TermDaoTest.java | 29 ++++++++++ .../dao/TermDaoWorkspacesTest.java | 49 +++++++++++++++++ 3 files changed, 125 insertions(+), 7 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java b/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java index c87a9579a..866700217 100644 --- a/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java +++ b/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java @@ -324,7 +324,15 @@ private List findAllFrom(Set contexts, Pageable final Descriptor descriptor = descriptorFactory.termDescriptor((URI) null); resolveWorkspaceAndCanonicalContexts().forEach(descriptor::addContext); query.setDescriptor(descriptor); - return executeQueryAndLoadSubTerms(query, contexts); + final List result = executeQueryAndLoadSubTerms(query, contexts); + if (TermDto.class.isAssignableFrom(resultType)) { + result.forEach(t -> { + final TermDto dto = (TermDto) t; + initParentTerms(dto); + dto.getParentTerms().addAll(loadInferredParentTerms(dto, contexts, dto.getParentTerms())); + }); + } + return result; } catch (RuntimeException e) { throw new PersistenceException(e); } @@ -409,12 +417,39 @@ private List findAllFrom(Set contexts, String searchString, Pageab final Descriptor descriptor = descriptorFactory.termDescriptor((URI) null); contexts.forEach(descriptor::addContext); query.setDescriptor(descriptor); - return executeQueryAndLoadSubTerms(query, contexts); + final List result = executeQueryAndLoadSubTerms(query, contexts); + result.forEach(t -> { + initParentTerms(t); + t.getParentTerms().addAll(loadInferredParentTerms(t, contexts, t.getParentTerms())); + }); + return result; } catch (RuntimeException e) { throw new PersistenceException(e); } } + private void initParentTerms(TermDto t) { + if (t.getParentTerms() == null) { + t.setParentTerms(new LinkedHashSet<>()); + } + } + + private List loadInferredParentTerms(TermDto term, Set graphs, Set exclude) { + return em.createNativeQuery("SELECT DISTINCT ?parent WHERE {" + + "GRAPH ?g { " + + "?parent a ?type ." + + "}" + + "?term ?broader ?parent ." + // Let broader be outside of the graph to include inference + "FILTER (?g IN (?graphs))" + + "FILTER (?parent NOT IN (?exclude))" + + "}", TermDto.class).setParameter("type", typeUri) + .setParameter("term", term) + .setParameter("broader", URI.create(SKOS.BROADER)) + .setParameter("graphs", graphs) + .setParameter("exclude", exclude) + .getResultList(); + } + /** * Returns true if the vocabulary does not contain any terms. * @@ -463,12 +498,13 @@ private void loadAdditionTermMetadata(AbstractTerm term, Set graphs) { */ private Set loadSubTermInfo(AbstractTerm parent, Set graphs) { final Stream subTermsStream = em.createNativeQuery("SELECT ?entity ?label ?vocabulary WHERE {" + - "GRAPH ?g { ?entity ?broader ?parent ;" + - "a ?type ;" + + "GRAPH ?g { " + + "?entity a ?type ;" + "?hasLabel ?label ." + "FILTER (lang(?label) = ?labelLang) ." + "}" + - "?entity ?inVocabulary ?vocabulary ." + + "?entity ?broader ?parent ; " + // Let broader be outside of the graph to allow including inferences + "?inVocabulary ?vocabulary ." + "FILTER (?g in (?graphs))" + "} ORDER BY LCASE(?label)", "TermInfo") .setParameter("type", typeUri) @@ -582,7 +618,11 @@ private List findAllImpl(String searchString, URI vocabularyIri) { .setParameter("searchString", searchString, config.get(ConfigParam.LANGUAGE)); query.setDescriptor(descriptorFactory.termDescriptor(vocabularyIri)); final List terms = executeQueryAndLoadSubTerms(query, Collections.singleton(vocabularyCtx)); - terms.forEach(t -> loadParentSubTerms(t, vocabularyCtx)); + terms.forEach(t -> { + loadParentSubTerms(t, vocabularyCtx); + initParentTerms(t); + t.getParentTerms().addAll(loadInferredParentTerms(t, Collections.singleton(vocabularyCtx), t.getParentTerms())); + }); return terms; } catch (RuntimeException e) { throw new PersistenceException(e); @@ -643,7 +683,7 @@ public List findAllSubTerms(Term parent) { "GRAPH ?g { " + "?term a ?type ." + "}" + - "?term ?broader ?parent ." + + "?term ?broader ?parent ." + // Let broader be outside of the graph to include inference "FILTER (?g in (?graphs))" + "}", Term.class).setParameter("type", typeUri) .setParameter("broader", URI.create(SKOS.BROADER)) diff --git a/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoTest.java b/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoTest.java index dcb2f4901..4b114f685 100644 --- a/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoTest.java +++ b/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoTest.java @@ -727,4 +727,33 @@ void subTermLoadingSortsThemByLabel() { assertEquals(child.getUri(), next.getUri()); } } + + @Test + void findAllBySearchStringAndVocabularyLoadsInferredParentTerms() { + final Term term = Generator.generateTermWithId(vocabulary.getUri()); + final String searchString = "test"; + term.getLabel().set(Constants.DEFAULT_LANGUAGE, searchString + " value"); + final Term parent = Generator.generateTermWithId(vocabulary.getUri()); + vocabulary.getGlossary().addRootTerm(parent); + transactional(() -> { + em.persist(term, descriptorFactory.termDescriptor(vocabulary)); + em.persist(parent, descriptorFactory.termDescriptor(vocabulary)); + em.merge(vocabulary.getGlossary(), descriptorFactory.glossaryDescriptor(vocabulary)); + addTermInVocabularyRelationship(term, vocabulary.getUri()); + addTermInVocabularyRelationship(parent, vocabulary.getUri()); + insertInferredBroaderRelationship(term, parent, em); + }); + + final List result = sut.findAll(searchString, vocabulary); + assertEquals(1, result.size()); + assertThat(result.get(0).getParentTerms(), hasItem(new TermDto(parent))); + } + + static void insertInferredBroaderRelationship(Term child, Term parent, EntityManager em) { + final Repository repo = em.unwrap(Repository.class); + try (final RepositoryConnection conn = repo.getConnection()) { + final ValueFactory vf = conn.getValueFactory(); + conn.add(vf.createIRI(child.getUri().toString()), vf.createIRI(SKOS.BROADER), vf.createIRI(parent.getUri().toString())); + } + } } diff --git a/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoWorkspacesTest.java b/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoWorkspacesTest.java index 3dd2e507e..e9fac162d 100644 --- a/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoWorkspacesTest.java +++ b/src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoWorkspacesTest.java @@ -868,4 +868,53 @@ void updateSupportsTermWithSupertypeInCanonicalContainer() { assertEquals(term, result); assertThat(result.getSuperTypes(), hasItem(superType)); } + + @Test + void findAllInCurrentWorkspaceBySearchStringLoadsInferredParentTerms() { + final String searchString = "search"; + final Term term = Generator.generateTermWithId(); + final Term parent = Generator.generateTermWithId(); + term.getLabel().set(Constants.DEFAULT_LANGUAGE, searchString + " string label"); + term.setGlossary(vocabulary.getGlossary().getUri()); + final Vocabulary anotherVocabularyInWs = Generator.generateVocabularyWithId(); + saveVocabulary(anotherVocabularyInWs); + parent.setGlossary(anotherVocabularyInWs.getGlossary().getUri()); + transactional(() -> { + em.persist(term, new EntityDescriptor(vocabulary.getUri())); + vocabulary.getGlossary().addRootTerm(term); + em.merge(vocabulary.getGlossary(), descriptorFactory.glossaryDescriptor(vocabulary)); + Generator.addTermInVocabularyRelationship(term, vocabulary.getUri(), em); + em.persist(parent, new EntityDescriptor(anotherVocabularyInWs.getUri())); + Generator.addTermInVocabularyRelationship(parent, anotherVocabularyInWs.getUri(), em); + TermDaoTest.insertInferredBroaderRelationship(term, parent, em); + }); + + final List result = sut.findAll(searchString); + assertEquals(1, result.size()); + assertThat(result.get(0).getParentTerms(), hasItem(new TermDto(parent))); + } + + @Test + void findAllByPageableLoadsInferredParentTerms() { + final Term term = Generator.generateTermWithId(); + final Term parent = Generator.generateTermWithId(); + term.setGlossary(vocabulary.getGlossary().getUri()); + final Vocabulary anotherVocabularyInWs = Generator.generateVocabularyWithId(); + saveVocabulary(anotherVocabularyInWs); + parent.setGlossary(anotherVocabularyInWs.getGlossary().getUri()); + transactional(() -> { + em.persist(term, new EntityDescriptor(vocabulary.getUri())); + vocabulary.getGlossary().addRootTerm(term); + em.merge(vocabulary.getGlossary(), descriptorFactory.glossaryDescriptor(vocabulary)); + Generator.addTermInVocabularyRelationship(term, vocabulary.getUri(), em); + em.persist(parent, new EntityDescriptor(anotherVocabularyInWs.getUri())); + Generator.addTermInVocabularyRelationship(parent, anotherVocabularyInWs.getUri(), em); + TermDaoTest.insertInferredBroaderRelationship(term, parent, em); + }); + + final List results = sut.findAll(Constants.DEFAULT_PAGE_SPEC); + final Optional res = results.stream().filter(t -> t.getUri().equals(term.getUri())).findFirst(); + assertTrue(res.isPresent()); + assertThat(res.get().getParentTerms(), hasItem(new TermDto(parent))); + } } From 64665abb1d195580f7a8615522e331791142942e Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Tue, 4 May 2021 10:08:21 +0200 Subject: [PATCH 4/7] [Enhancement #85] Update term root status based on it having supertypes as well. --- src/main/java/cz/cvut/kbss/termit/model/Term.java | 3 ++- .../java/cz/cvut/kbss/termit/model/TermTest.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/cz/cvut/kbss/termit/model/Term.java b/src/main/java/cz/cvut/kbss/termit/model/Term.java index 9fbcc5327..c91ef9594 100644 --- a/src/main/java/cz/cvut/kbss/termit/model/Term.java +++ b/src/main/java/cz/cvut/kbss/termit/model/Term.java @@ -282,7 +282,8 @@ public void toExcel(Row row) { * term at all */ public boolean hasParentInSameVocabulary() { - return parentTerms != null && parentTerms.stream().anyMatch(p -> p.getGlossary().equals(getGlossary())); + return parentTerms != null && parentTerms.stream().anyMatch(p -> p.getGlossary().equals(getGlossary())) + || superTypes != null && superTypes.stream().anyMatch(p -> p.getGlossary().equals(getGlossary())); } @Override diff --git a/src/test/java/cz/cvut/kbss/termit/model/TermTest.java b/src/test/java/cz/cvut/kbss/termit/model/TermTest.java index dec8a7070..44aec8702 100644 --- a/src/test/java/cz/cvut/kbss/termit/model/TermTest.java +++ b/src/test/java/cz/cvut/kbss/termit/model/TermTest.java @@ -297,4 +297,16 @@ void toExcelHandlesNullAltLabelsAttribute() { .forEach(v -> assertThat(row.getCell(3).getStringCellValue(), containsString(v)))); } + + @Test + void hasParentInSameVocabularyIncludesSuperTypesAsParents() { + final Term sut = Generator.generateTermWithId(); + final URI vocabularyUri = Generator.generateUri(); + sut.setGlossary(vocabularyUri); + final Term parent = Generator.generateTermWithId(); + parent.setGlossary(vocabularyUri); + sut.setSuperTypes(Collections.singleton(parent)); + + assertTrue(sut.hasParentInSameVocabulary()); + } } From b822ad52cd34cb1f61aea308b9b5302fa31614d0 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Tue, 4 May 2021 16:55:01 +0200 Subject: [PATCH 5/7] [Enhancement #85] Configure JSON attribute serialization order to prevent issues with reference resolution on the client. --- src/main/java/cz/cvut/kbss/termit/model/Term.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/cz/cvut/kbss/termit/model/Term.java b/src/main/java/cz/cvut/kbss/termit/model/Term.java index c91ef9594..104736577 100644 --- a/src/main/java/cz/cvut/kbss/termit/model/Term.java +++ b/src/main/java/cz/cvut/kbss/termit/model/Term.java @@ -34,7 +34,7 @@ @Configurable @Audited @OWLClass(iri = SKOS.CONCEPT) -@JsonLdAttributeOrder({"uri", "label", "description", "subTerms"}) +@JsonLdAttributeOrder({"uri", "label", "description", "parentTerms", "superTypes", "subTerms"}) public class Term extends AbstractTerm implements HasTypes { /** From 9f7815e32fb3a533c7034d25daf15858ca811b22 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Tue, 11 May 2021 15:58:28 +0200 Subject: [PATCH 6/7] [Fix] Fix failing build caused by KBSS maven repo no longer being available on HTTP. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 177fec00f..f03024a6b 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ kbss - http://kbss.felk.cvut.cz/m2repo + https://kbss.felk.cvut.cz/m2repo From 6351faa1bb2bcef5a45474dad90c293f12fd339a Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Wed, 12 May 2021 10:05:18 +0200 Subject: [PATCH 7/7] PR feedback. --- .../java/cz/cvut/kbss/termit/dto/TermDto.java | 9 +++++++++ .../kbss/termit/persistence/dao/TermDao.java | 17 ++++------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/termit/dto/TermDto.java b/src/main/java/cz/cvut/kbss/termit/dto/TermDto.java index 221b2e60e..8e4ee6e0f 100644 --- a/src/main/java/cz/cvut/kbss/termit/dto/TermDto.java +++ b/src/main/java/cz/cvut/kbss/termit/dto/TermDto.java @@ -9,6 +9,7 @@ import cz.cvut.kbss.termit.model.AbstractTerm; import cz.cvut.kbss.termit.model.Term; +import java.util.Collection; import java.util.LinkedHashSet; import java.util.Objects; import java.util.Set; @@ -56,4 +57,12 @@ public Set getParentTerms() { public void setParentTerms(Set parentTerms) { this.parentTerms = parentTerms; } + + public void addParentTerms(Collection parents) { + Objects.requireNonNull(parents); + if (parentTerms == null) { + this.parentTerms = new LinkedHashSet<>(); + } + parentTerms.addAll(parents); + } } diff --git a/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java b/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java index 866700217..0dfc938a9 100644 --- a/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java +++ b/src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java @@ -328,8 +328,7 @@ private List findAllFrom(Set contexts, Pageable if (TermDto.class.isAssignableFrom(resultType)) { result.forEach(t -> { final TermDto dto = (TermDto) t; - initParentTerms(dto); - dto.getParentTerms().addAll(loadInferredParentTerms(dto, contexts, dto.getParentTerms())); + dto.addParentTerms(loadInferredParentTerms(dto, contexts, dto.getParentTerms())); }); } return result; @@ -419,8 +418,7 @@ private List findAllFrom(Set contexts, String searchString, Pageab query.setDescriptor(descriptor); final List result = executeQueryAndLoadSubTerms(query, contexts); result.forEach(t -> { - initParentTerms(t); - t.getParentTerms().addAll(loadInferredParentTerms(t, contexts, t.getParentTerms())); + t.addParentTerms(loadInferredParentTerms(t, contexts, t.getParentTerms())); }); return result; } catch (RuntimeException e) { @@ -428,12 +426,6 @@ private List findAllFrom(Set contexts, String searchString, Pageab } } - private void initParentTerms(TermDto t) { - if (t.getParentTerms() == null) { - t.setParentTerms(new LinkedHashSet<>()); - } - } - private List loadInferredParentTerms(TermDto term, Set graphs, Set exclude) { return em.createNativeQuery("SELECT DISTINCT ?parent WHERE {" + "GRAPH ?g { " + @@ -446,7 +438,7 @@ private List loadInferredParentTerms(TermDto term, Set graphs, Set .setParameter("term", term) .setParameter("broader", URI.create(SKOS.BROADER)) .setParameter("graphs", graphs) - .setParameter("exclude", exclude) + .setParameter("exclude", exclude != null ? exclude : Collections.emptyList()) .getResultList(); } @@ -620,8 +612,7 @@ private List findAllImpl(String searchString, URI vocabularyIri) { final List terms = executeQueryAndLoadSubTerms(query, Collections.singleton(vocabularyCtx)); terms.forEach(t -> { loadParentSubTerms(t, vocabularyCtx); - initParentTerms(t); - t.getParentTerms().addAll(loadInferredParentTerms(t, Collections.singleton(vocabularyCtx), t.getParentTerms())); + t.addParentTerms(loadInferredParentTerms(t, Collections.singleton(vocabularyCtx), t.getParentTerms())); }); return terms; } catch (RuntimeException e) {