Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
308dcd4
[Enhancement kbss-cvut/termit-ui#744] Introduce TermRemovalParams to …
lukaskabc Sep 2, 2026
afcacb4
[Test] Implement tests for SubTermRemovalStrategy
lukaskabc Sep 2, 2026
0a00d0d
[Test] Implement tests for TermRepositoryService and fix missing null…
lukaskabc Sep 2, 2026
cb7aaac
[Bug] Fix persistence context cache issue with keeping TermInfo insta…
lukaskabc Sep 2, 2026
f806604
[Ref] Fix TermController#removeTerm parameters being required, add de…
lukaskabc Sep 2, 2026
6e6938f
[Enhancement kbss-cvut/termit-ui#744] Add endpoint for finding refere…
lukaskabc Sep 4, 2026
e57ef7d
[Test] Ensure remove term throws when term is referenced by other term
lukaskabc Sep 4, 2026
bae66a9
[Bug] Copy types into TermInfo when constructing it from AbstractTerm…
lukaskabc Sep 4, 2026
5f0376a
[Enhancement kbss-cvut/termit-ui#744] Fix persistent context issues w…
lukaskabc Sep 4, 2026
894e3f2
[Test] Fix invalid verification of parent terms in reconnectLinksChil…
lukaskabc Sep 4, 2026
bbf0465
[Test] Refactor tests for new remove term with params method to impro…
lukaskabc Sep 4, 2026
2c9a77a
[Enhancement kbss-cvut/termit-ui#744] Update SubTermRemovalStrategy t…
lukaskabc Sep 4, 2026
3f2137f
[Enhancement kbss-cvut/termit-ui#744] Refactor sub term reconnecting …
lukaskabc Sep 4, 2026
050c866
[Ref] Include contexts in term reference statements
lukaskabc Sep 4, 2026
08b65de
[Ref] Fix fetching term from database twice during term removal
lukaskabc Sep 7, 2026
a633193
[Ref] Remove unused Vocabulary parameter from remove term with params…
lukaskabc Sep 7, 2026
c1b8dc3
[Enhancement kbss-cvut/termit-ui#744] Order references to term by sub…
lukaskabc Sep 7, 2026
531cfad
[Test] Fix term removal with params test compilation after updating m…
lukaskabc Sep 7, 2026
38ab391
[Test] Fix fialing tests by adding empty hashset as root terms for ge…
lukaskabc Sep 7, 2026
c2c3dc4
[Bug] Replace unwrapping entity manager to Repository with unwrapping…
lukaskabc Sep 14, 2026
582474c
[Ref] Rename TermParamsApplier to TermRemovalParamsApplier
lukaskabc Sep 14, 2026
939aa40
[Ref] Do not call term update during term removal
lukaskabc Sep 15, 2026
cbd8302
[Ref] Simplify occurrence removal during term removal
lukaskabc Sep 15, 2026
72f8e55
[Enhancement] Evict parents of removed term from entity manager facto…
lukaskabc Sep 15, 2026
1940783
Merge branch 'development' into lukaskabc/ui/744-support-term-force-r…
lukaskabc Sep 15, 2026
44b7658
[Test] Add test ensuring that when a root term is removed, its childr…
lukaskabc Sep 16, 2026
188a522
Merge branch 'development' into lukaskabc/ui/744-support-term-force-r…
lukaskabc Sep 16, 2026
74c244c
[Docs] Fix method references in javadoc in TermRepositoryService
lukaskabc Sep 16, 2026
50adae1
[Ref] Update javadocs for term removal params and its applier
lukaskabc Sep 17, 2026
939a633
Merge branch 'development' into lukaskabc/ui/744-support-term-force-r…
lukaskabc Sep 18, 2026
0d0340e
[Ref] Replace RDF4J usage in TermDao#countReferences with JOPA native…
lukaskabc Sep 21, 2026
ca96be6
[Ref] Remove unused logger from BaseDao
lukaskabc Sep 21, 2026
2e25f12
[Ref] Rename term_removal package to removal
lukaskabc Sep 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/cz/cvut/kbss/termit/dto/TermInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public TermInfo(AbstractTerm term) {
this.label = new MultilingualString(term.getLabel().getValue());
this.vocabulary = term.getVocabulary();
this.state = term.getState();
this.types = term.getTypes();
}

public TermInfo(TermInfo other) {
Expand All @@ -92,6 +93,7 @@ public Term toTerm() {
result.setLabel(getLabel());
result.setVocabulary(getVocabulary());
result.setState(getState());
result.setTypes(getTypes());
return result;
}

Expand Down
179 changes: 179 additions & 0 deletions src/main/java/cz/cvut/kbss/termit/persistence/dao/TermDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import cz.cvut.kbss.jopa.vocabulary.SKOS;
import cz.cvut.kbss.termit.asset.provenance.ModifiesData;
import cz.cvut.kbss.termit.dto.Snapshot;
import cz.cvut.kbss.termit.dto.TermDescription;
import cz.cvut.kbss.termit.dto.TermInfo;
import cz.cvut.kbss.termit.dto.listing.FlatTermDto;
import cz.cvut.kbss.termit.dto.listing.TermDto;
Expand All @@ -48,8 +49,18 @@
import cz.cvut.kbss.termit.service.snapshot.SnapshotProvider;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Utils;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.util.Values;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -77,6 +88,28 @@ public class TermDao extends BaseAssetDao<Term> implements SnapshotProvider<Term
private static final URI TERM_FROM_VOCABULARY = URI.create(SKOS.IN_SCHEME);
private static final URI DC_TERMS_LANGUAGE = URI.create(DC.Terms.LANGUAGE);

/**
* Matches triples where the term is an object in a vocabulary graph excluding vocabulary snapshots.
* {@code skos:hasTopConcept} relations are excluded
* @implNote Bindings are required in {@link #countReferences(AbstractTerm)},
* {@link #findReferencesInternal(AbstractTerm, Pageable)}, {@link #removeReferencesTo(AbstractTerm)}
* and {@link #referencesToTermExist(AbstractTerm)}
*/
private static final String REFERENCES_TO_TERM_WHERE_CLAUSE = """
WHERE {
GRAPH ?context {
FILTER NOT EXISTS {
?context a ?versionOfVocabulary .
}
?other ?relation ?term .
?context a <http://www.w3.org/2004/02/skos/core#ConceptScheme> .
FILTER (?relation NOT IN (
<http://www.w3.org/2004/02/skos/core#hasTopConcept>
))
}
}
""";

private final Cache<URI, Set<TermInfo>> subTermsCache;

private final VocabularyContextMapper contextMapper;
Expand Down Expand Up @@ -153,6 +186,14 @@ private void postLoad(Term r) {
r.setInverseExactMatchTerms(loadInverseExactMatchTerms(r));
}

/**
* Flushes pending term changes and clears the persistence context.
*/
public void flushAndClear() {
em.flush();
em.clear();
}

/**
* Loads terms whose relatedness to the specified term is inferred due to the symmetry of SKOS related.
*
Expand Down Expand Up @@ -1276,4 +1317,142 @@ public Set<TermInfoWithParents> findWithAllAncestors(Set<URI> termUris) {
throw new PersistenceException(e);
}
}

/**
* Checks whether there is any triple in any vocabulary where the term is an object.
* Excluding vocabulary snapshots.
*
* @param term term to which references should be checked
* @return true if there is any triple where the term is an object
*/
public boolean referencesToTermExist(AbstractTerm term) {
Objects.requireNonNull(term.getUri(), "Term URI cannot be null");
try {
return em.createNativeQuery("ASK " + REFERENCES_TO_TERM_WHERE_CLAUSE, Boolean.class)
.setParameter("term", term.getUri())
.setParameter("versionOfVocabulary", URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_c_version_of_vocabulary))
.getSingleResult();
} catch (RuntimeException e) {
throw new PersistenceException("Failed to find references for term " + Utils.uriToString(term.getUri()), e);
}
}

/**
* Finds statements from vocabulary graphs referencing the specified term as an object.
*
* @param term term whose incoming references should be returned
* @param pageable paging specification applied to the constructed RDF4J query
* @return page of statements referencing the specified term
*/
public Page<Statement> findReferences(AbstractTerm term, Pageable pageable) {
Objects.requireNonNull(term, "Term cannot be null");
Objects.requireNonNull(term.getUri(), "Term URI cannot be null");
Objects.requireNonNull(pageable, "Pageable cannot be null");

try {
final long totalCount = countReferences(term);
if (totalCount == 0 || pageable.getOffset() >= totalCount) {
return new PageImpl<>(List.of(), pageable, totalCount);
}

final List<Statement> statements = findReferencesInternal(term, pageable);
return new PageImpl<>(statements, pageable, totalCount);
} catch (RuntimeException e) {
throw new PersistenceException("Failed to find references to term " + Utils.uriToString(term.getUri()), e);
}
}

/**
* Counts the total amount of references to the specified term
*
* @param term the term to which references should be counted
* @return the total number of statements referencing the term as object
*/
private long countReferences(AbstractTerm term) {
try {
return em.createNativeQuery("SELECT (COUNT(*) AS ?count) " + REFERENCES_TO_TERM_WHERE_CLAUSE, Long.class)
.setParameter("term", term.getUri())
.setParameter("versionOfVocabulary", cz.cvut.kbss.termit.util.Vocabulary.s_c_version_of_vocabulary)
.getSingleResult();
} catch (RuntimeException e) {
throw new PersistenceException("Failed to count references to term " + Utils.uriToString(term.getUri()), e);
}
}

/**
* Finds a page contents of references to the specified term.
*
* @param term the term to which references should be found
* @param pageable page spec
* @return the list of statements referencing the term as object
*/
private List<Statement> findReferencesInternal(AbstractTerm term, Pageable pageable) {
// On purpose not using auto-closable with try statement to prevent closing the connection here
// the connection is managed by Entity Manager
final RepositoryConnection con = em.unwrap(RepositoryConnection.class);
// The connection is guaranteed to be open by first counting the total references count
final TupleQuery query = con.prepareTupleQuery(
"SELECT ?other ?relation ?term ?context " + REFERENCES_TO_TERM_WHERE_CLAUSE +
" ORDER BY ?other ?relation ?context" +
" OFFSET " + pageable.getOffset() +
" LIMIT " + pageable.getPageSize());

query.setBinding("term", Values.iri(term.getUri().toString()));
query.setBinding("versionOfVocabulary",
Values.iri(cz.cvut.kbss.termit.util.Vocabulary.s_c_version_of_vocabulary));

query.setIncludeInferred(false);

final List<Statement> statements = new ArrayList<>(pageable.getPageSize());
try (TupleQueryResult result = query.evaluate()) {
while (result.hasNext()) {
final BindingSet bindings = result.next();
statements.add(Values.getValueFactory().createStatement(
(Resource) bindings.getValue("other"),
(IRI) bindings.getValue("relation"),
bindings.getValue("term"),
(Resource) bindings.getValue("context")
));
}
}
return statements;
}

/**
* Removes all triples from vocabularies where the given term is references as object.
*
* @param toRemove term to which references should be removed
*/
public void removeReferencesTo(AbstractTerm toRemove) {
Objects.requireNonNull(toRemove.getUri(), "Term URI cannot be null");

try {
em.createNativeQuery("""
DELETE {
GRAPH ?context {
?other ?relation ?term .
}
}
""" + REFERENCES_TO_TERM_WHERE_CLAUSE)
.setParameter("term", toRemove.getUri())
.setParameter("versionOfVocabulary", URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_c_version_of_vocabulary))
.executeUpdate();
} catch (RuntimeException e) {
throw new PersistenceException("Failed to remove references to term " + Utils.uriToString(toRemove.getUri()), e);
}
}

/**
* Evict specified instances from the Jopa's Entity Manager Factory cache.
*
* @param terms terms to evict from the cache
*/
public void evictFromCache(Collection<? extends TermDescription> terms) {
if (terms == null || terms.isEmpty()) {
return;
}
terms.forEach(term -> {
em.getEntityManagerFactory().getCache().evict(TermDescription.class, term.getUri(), term.getVocabulary());
});
}
}
43 changes: 41 additions & 2 deletions src/main/java/cz/cvut/kbss/termit/rest/TermController.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import cz.cvut.kbss.termit.service.business.util.TermSelectionParams;
import cz.cvut.kbss.termit.service.export.ExportConfig;
import cz.cvut.kbss.termit.service.export.ExportType;
import cz.cvut.kbss.termit.service.repository.removal.SubTermRemovalStrategy;
import cz.cvut.kbss.termit.service.repository.removal.TermRemovalParams;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Constants;
import cz.cvut.kbss.termit.util.Constants.QueryParams;
Expand All @@ -49,9 +51,11 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.eclipse.rdf4j.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -510,12 +514,47 @@ public void removeTerm(
@Parameter(description = ApiDoc.ID_TERM_LOCAL_NAME_DESCRIPTION, example = ApiDoc.ID_TERM_LOCAL_NAME_EXAMPLE)
@PathVariable String termLocalName,
@Parameter(description = ApiDoc.ID_NAMESPACE_DESCRIPTION, example = ApiDoc.ID_NAMESPACE_EXAMPLE)
@RequestParam(name = QueryParams.NAMESPACE, required = false) Optional<String> namespace) {
@RequestParam(name = QueryParams.NAMESPACE, required = false) Optional<String> namespace,
@Parameter(description = "Strategy to use for sub-terms handling.")
@RequestParam(name = "subTermsStrategy", required = false) SubTermRemovalStrategy subTermsStrategy,
@Parameter(description = "Whether occurrences should be removed. When false, the removal will fail if any occurrence exists.")
@RequestParam(name = "removeOccurrences", required = false) boolean removeOccurrences,
@Parameter(description = "Whether relationships referencing the term should be removed. When false, the removal will fail if any reference to the term exists.")
@RequestParam(name = "removeRelationships", required = false) boolean removeRelationships) {
final URI termUri = getTermUri(localName, termLocalName, namespace);
termService.remove(termService.findRequired(termUri));
final Term toRemove = termService.findRequired(termUri);
termService.remove(
new TermRemovalParams(toRemove, subTermsStrategy, removeOccurrences, removeRelationships)
);
LOG.debug("Term {} removed.", termUri);
}

@Operation(security = {@SecurityRequirement(name = "bearer-key")},
description = "Gets statements referencing the term with the specified local name from the vocabulary with the specified identifier.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Statements referencing the term."),
@ApiResponse(responseCode = "404", description = "Vocabulary or term not found.")
})
@GetMapping(value = "/vocabularies/{localName}/terms/{termLocalName}/references",
produces = {MediaType.APPLICATION_JSON_VALUE, JsonLd.MEDIA_TYPE})
public ResponseEntity<List<Statement>> getReferencesToTerm(
@Parameter(description = ApiDoc.ID_LOCAL_NAME_DESCRIPTION, example = ApiDoc.ID_LOCAL_NAME_EXAMPLE)
@PathVariable String localName,
@Parameter(description = ApiDoc.ID_TERM_LOCAL_NAME_DESCRIPTION, example = ApiDoc.ID_TERM_LOCAL_NAME_EXAMPLE)
@PathVariable String termLocalName,
@Parameter(description = ApiDoc.ID_NAMESPACE_DESCRIPTION, example = ApiDoc.ID_NAMESPACE_EXAMPLE)
@RequestParam(name = QueryParams.NAMESPACE, required = false) Optional<String> namespace,
@Parameter(description = ApiDocConstants.PAGE_SIZE_DESCRIPTION)
@RequestParam(name = QueryParams.PAGE_SIZE, required = false) Integer pageSize,
@Parameter(description = ApiDocConstants.PAGE_NO_DESCRIPTION)
@RequestParam(name = QueryParams.PAGE, required = false) Integer pageNo) {
final Term term = getById(localName, termLocalName, namespace, false, false);
final Page<Statement> result = termService.findReferences(term, createPageRequest(pageSize, pageNo));
return ResponseEntity.ok()
.header(Constants.X_TOTAL_COUNT_HEADER, Long.toString(result.getTotalElements()))
.body(result.getContent());
}

@Operation(security = {@SecurityRequirement(name = "bearer-key")},
description = "Gets sub-terms of the term with the specified local name in the vocabulary with the specified identifier")
@ApiResponses({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@
import cz.cvut.kbss.termit.service.language.LanguageService;
import cz.cvut.kbss.termit.service.repository.ChangeRecordService;
import cz.cvut.kbss.termit.service.repository.TermRepositoryService;
import cz.cvut.kbss.termit.service.repository.removal.TermRemovalParams;
import cz.cvut.kbss.termit.service.security.authorization.TermAuthorizationService;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import cz.cvut.kbss.termit.util.Utils;
import cz.cvut.kbss.termit.util.throttle.Throttle;
import jakarta.annotation.Nonnull;
import org.eclipse.rdf4j.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
Expand Down Expand Up @@ -486,6 +489,18 @@ public List<TermDto> findSubTerms(Term parent) {
return repositoryService.findSubTerms(parent);
}

/**
* Gets referenes to the given term.
*
* @param term the term to which references should be found
* @param pageable Page spec
* @return Page of statements where the given term is object
*/
@PreAuthorize("@termAuthorizationService.canRead(#term)")
public Page<Statement> findReferences(Term term, Pageable pageable) {
return repositoryService.findReferences(term, pageable);
}

/**
* Gets aggregated info about occurrences of the specified Term.
*
Expand Down Expand Up @@ -631,6 +646,21 @@ public void remove(@Nonnull Term term) {
repositoryService.remove(term);
}

/**
* Removes the specified term according to the removal parameters.
*
* @param termRemovalParams parameters describing how the term should be removed
*/
@Transactional
@PreAuthorize("@termAuthorizationService.canRemove(#termRemovalParams.termToRemove())")
public void remove(TermRemovalParams termRemovalParams) {
Objects.requireNonNull(termRemovalParams);
Objects.requireNonNull(termRemovalParams.termToRemove());
Objects.requireNonNull(termRemovalParams.termToRemove().getVocabulary());

repositoryService.remove(termRemovalParams);
}

/**
* Executes text analysis on the specified term's definition.
* <p>
Expand Down
Loading
Loading