Skip to content

Commit

Permalink
PR feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed May 12, 2021
1 parent 9f7815e commit 6351faa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/main/java/cz/cvut/kbss/termit/dto/TermDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,4 +57,12 @@ public Set<TermDto> getParentTerms() {
public void setParentTerms(Set<TermDto> parentTerms) {
this.parentTerms = parentTerms;
}

public void addParentTerms(Collection<TermDto> parents) {
Objects.requireNonNull(parents);
if (parentTerms == null) {
this.parentTerms = new LinkedHashSet<>();
}
parentTerms.addAll(parents);
}
}
17 changes: 4 additions & 13 deletions src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,7 @@ private <T extends AbstractTerm> List<T> findAllFrom(Set<URI> 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;
Expand Down Expand Up @@ -419,21 +418,14 @@ private List<TermDto> findAllFrom(Set<URI> contexts, String searchString, Pageab
query.setDescriptor(descriptor);
final List<TermDto> 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) {
throw new PersistenceException(e);
}
}

private void initParentTerms(TermDto t) {
if (t.getParentTerms() == null) {
t.setParentTerms(new LinkedHashSet<>());
}
}

private List<TermDto> loadInferredParentTerms(TermDto term, Set<URI> graphs, Set<TermDto> exclude) {
return em.createNativeQuery("SELECT DISTINCT ?parent WHERE {" +
"GRAPH ?g { " +
Expand All @@ -446,7 +438,7 @@ private List<TermDto> loadInferredParentTerms(TermDto term, Set<URI> 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();
}

Expand Down Expand Up @@ -620,8 +612,7 @@ private List<TermDto> findAllImpl(String searchString, URI vocabularyIri) {
final List<TermDto> 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) {
Expand Down

0 comments on commit 6351faa

Please sign in to comment.