From 6351faa1bb2bcef5a45474dad90c293f12fd339a Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Wed, 12 May 2021 10:05:18 +0200 Subject: [PATCH] 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) {