diff --git a/src/main/java/cz/cvut/kbss/termit/dto/TermInfo.java b/src/main/java/cz/cvut/kbss/termit/dto/TermInfo.java index 07d43e20b..140dcf1b1 100644 --- a/src/main/java/cz/cvut/kbss/termit/dto/TermInfo.java +++ b/src/main/java/cz/cvut/kbss/termit/dto/TermInfo.java @@ -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) { @@ -92,6 +93,7 @@ public Term toTerm() { result.setLabel(getLabel()); result.setVocabulary(getVocabulary()); result.setState(getState()); + result.setTypes(getTypes()); return result; } 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 696848bf2..b8c1dcf53 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 @@ -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; @@ -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; @@ -77,6 +88,28 @@ public class TermDao extends BaseAssetDao implements SnapshotProvider . + FILTER (?relation NOT IN ( + + )) + } + } + """; + private final Cache> subTermsCache; private final VocabularyContextMapper contextMapper; @@ -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. * @@ -1276,4 +1317,142 @@ public Set findWithAllAncestors(Set 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 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 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 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 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 terms) { + if (terms == null || terms.isEmpty()) { + return; + } + terms.forEach(term -> { + em.getEntityManagerFactory().getCache().evict(TermDescription.class, term.getUri(), term.getVocabulary()); + }); + } } diff --git a/src/main/java/cz/cvut/kbss/termit/rest/TermController.java b/src/main/java/cz/cvut/kbss/termit/rest/TermController.java index 2666ec524..9ee1e2c42 100644 --- a/src/main/java/cz/cvut/kbss/termit/rest/TermController.java +++ b/src/main/java/cz/cvut/kbss/termit/rest/TermController.java @@ -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; @@ -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; @@ -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 namespace) { + @RequestParam(name = QueryParams.NAMESPACE, required = false) Optional 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> 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 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 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({ diff --git a/src/main/java/cz/cvut/kbss/termit/service/business/TermService.java b/src/main/java/cz/cvut/kbss/termit/service/business/TermService.java index 353861a96..e5f8ae716 100644 --- a/src/main/java/cz/cvut/kbss/termit/service/business/TermService.java +++ b/src/main/java/cz/cvut/kbss/termit/service/business/TermService.java @@ -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; @@ -486,6 +489,18 @@ public List 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 findReferences(Term term, Pageable pageable) { + return repositoryService.findReferences(term, pageable); + } + /** * Gets aggregated info about occurrences of the specified Term. * @@ -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. *

diff --git a/src/main/java/cz/cvut/kbss/termit/service/repository/TermRepositoryService.java b/src/main/java/cz/cvut/kbss/termit/service/repository/TermRepositoryService.java index b26ce1d03..b5ac97340 100644 --- a/src/main/java/cz/cvut/kbss/termit/service/repository/TermRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/termit/service/repository/TermRepositoryService.java @@ -18,7 +18,6 @@ package cz.cvut.kbss.termit.service.repository; import cz.cvut.kbss.jopa.model.MultilingualString; -import cz.cvut.kbss.jopa.vocabulary.SKOS; import cz.cvut.kbss.termit.dto.Snapshot; import cz.cvut.kbss.termit.dto.TermInfo; import cz.cvut.kbss.termit.dto.assignment.TermOccurrences; @@ -38,6 +37,7 @@ import cz.cvut.kbss.termit.persistence.namespace.VocabularyNamespaceResolver; import cz.cvut.kbss.termit.service.IdentifierResolver; import cz.cvut.kbss.termit.service.business.TermOccurrenceService; +import cz.cvut.kbss.termit.service.repository.removal.TermRemovalParams; import cz.cvut.kbss.termit.service.snapshot.SnapshotProvider; import cz.cvut.kbss.termit.service.term.AssertedInferredValueDifferentiator; import cz.cvut.kbss.termit.service.term.OrphanedInverseTermRelationshipRemover; @@ -45,6 +45,10 @@ import cz.cvut.kbss.termit.util.Utils; import jakarta.annotation.Nonnull; import jakarta.validation.Validator; +import org.eclipse.rdf4j.model.Statement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -65,6 +69,7 @@ @Service public class TermRepositoryService extends BaseAssetRepositoryService implements SnapshotProvider { + private static final Logger LOG = LoggerFactory.getLogger(TermRepositoryService.class); private final IdentifierResolver idResolver; private final TermDao termDao; @@ -146,6 +151,18 @@ public Term findRequiredWithPopulatedCustomAttributes(@Nonnull URI id) { return result; } + /** + * 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 + */ + @Transactional(readOnly = true) + public Page findReferences(@Nonnull Term term, @Nonnull Pageable pageable) { + return termDao.findReferences(term, pageable); + } + @Override protected void preUpdate(@Nonnull Term instance) { if (instance.getPrimaryLanguage() == null) { @@ -247,6 +264,12 @@ private void addTermAsRootToVocabulary(Term instance, URI vocabularyIri) { toUpdate.addRootTerm(instance); } + /** + * Creates new term from the {@code instance} and sets its {@code parentTerm} + * + * @param instance a new Term to persist + * @param parentTerm the parent term to set for the {@code instance} + */ @Transactional public void addChildTerm(Term instance, Term parentTerm) { Objects.requireNonNull(instance); @@ -618,6 +641,58 @@ public Optional findTermVocabulary(URI termId) { return termDao.findTermVocabulary(termId); } + /** + * Removes a term according to the specified removal parameters. + * + * @param removalParams Params describing how the term should be removed + */ + @Transactional + public void remove(TermRemovalParams removalParams) { + Objects.requireNonNull(removalParams); + Objects.requireNonNull(removalParams.termToRemove().getUri()); + + // Refresh the instance from storage, then detach it before traversing term relationships. + // JOPA cannot manage the same individual as both Term and TermInfo, + // which would otherwise prevent loading a child whose parent is the term currently being removed. + final URI termUri = removalParams.termToRemove().getUri(); + Term toRemove = findRequired(termUri); + removalParams = removalParams.withTerm(toRemove); + termDao.detach(toRemove); + + LOG.debug("Removing term <{}>", termUri); + + LOG.debug("Applying sub-terms removal strategy for term <{}>", termUri); + removalParams.subTermsStrategy().apply(removalParams, this); + termDao.flushAndClear(); + + if (removalParams.removeOccurrences()) { + LOG.debug("Removing occurrences of term <{}>", toRemove.getUri()); + termOccurrenceService.removeAllOf(toRemove); + } + + if (removalParams.removeRelationships()) { + LOG.debug("Removing references to term <{}>", toRemove.getUri()); + termDao.removeReferencesTo(toRemove); + } + + this.remove(toRemove); // calls pre and post remove + LOG.debug("Removed term <{}>", toRemove.getUri()); + } + + /** + * Removes the specified term from the repository. + * The term must not have any children, no occurrence must exist + * and there must be no references to the term. + * + * @param instance The instance to remove + * @see #remove(TermRemovalParams) + * @see #forceRemove(Term) + */ + @Override + public void remove(Term instance) { + super.remove(instance); + } + /** * Checks that a term can be removed. *

@@ -643,16 +718,9 @@ protected void preRemove(@Nonnull Term instance) { if ((subTerms != null) && !subTerms.isEmpty()) { throw hasSubTermsException(subTerms); } - if (instance.getProperties() != null) { - Set props = instance.getProperties().keySet(); - List properties = props.stream().filter(s -> (s.startsWith(SKOS.NAMESPACE)) && !( - s.equalsIgnoreCase(SKOS.CHANGE_NOTE) - || s.equalsIgnoreCase(SKOS.EDITORIAL_NOTE) - || s.equalsIgnoreCase(SKOS.HISTORY_NOTE) - || s.equalsIgnoreCase(SKOS.NOTE))).collect(toList()); - if (!properties.isEmpty()) { - throw hasSkosRelationships(properties); - } + if (termDao.referencesToTermExist(instance)) { + throw new AssetRemovalException( + "Cannot delete the term. References to this term exist!", "error.term.remove.relationshipsExist"); } } @@ -664,7 +732,7 @@ private static TermItException annotationsExistException(List a "error.term.remove.annotationsExist").addParameter("resources", resources); } - private static TermItException hasSubTermsException(Set subTerms) { + public static TermItException hasSubTermsException(Set subTerms) { final String children = subTerms.stream().map(t -> t.getUri().toString()).collect(joining(",")); return new AssetRemovalException( "Cannot delete the term. It is a parent of other terms: " + children, @@ -672,14 +740,6 @@ private static TermItException hasSubTermsException(Set subTerms) { .addParameter("subTerms", children); } - private static TermItException hasSkosRelationships(List properties) { - final String propertiesStr = String.join(", ", properties); - return new AssetRemovalException( - "Cannot delete the term. It is linked to another term through properties " - + String.join(",", properties), "error.term.remove.skosRelationshipsExist") - .addParameter("properties", propertiesStr); - } - @Override protected void postRemove(@Nonnull Term instance) { super.postRemove(instance); @@ -688,6 +748,8 @@ protected void postRemove(@Nonnull Term instance) { v.removeRootTerm(instance); } termOccurrenceService.removeAllOf(instance); + instance.consolidateParents(); + termDao.evictFromCache(instance.getParentTerms()); } /** @@ -697,6 +759,8 @@ protected void postRemove(@Nonnull Term instance) { * specified instance. * * @param instance Term to remove + * @see #remove(TermRemovalParams) + * @see #remove(Term) */ @Transactional public void forceRemove(@Nonnull Term instance) { diff --git a/src/main/java/cz/cvut/kbss/termit/service/repository/removal/SubTermRemovalStrategy.java b/src/main/java/cz/cvut/kbss/termit/service/repository/removal/SubTermRemovalStrategy.java new file mode 100644 index 000000000..27f81250b --- /dev/null +++ b/src/main/java/cz/cvut/kbss/termit/service/repository/removal/SubTermRemovalStrategy.java @@ -0,0 +1,156 @@ +package cz.cvut.kbss.termit.service.repository.removal; + +import cz.cvut.kbss.termit.dto.TermInfo; +import cz.cvut.kbss.termit.model.Term; +import cz.cvut.kbss.termit.model.util.HasIdentifier; +import cz.cvut.kbss.termit.service.repository.TermRepositoryService; + +import java.util.Collection; +import java.util.Collections; +import java.util.Objects; +import java.util.Optional; + +/** + * Describes an operation how should be sub-terms handled during term removal + */ +public enum SubTermRemovalStrategy implements TermRemovalParamsApplier { + /** + * All sub-terms will be removed using the same {@link TermRemovalParams} + */ + CASCADE(CascadeRemoveApplier.INSTANCE), + + /** + * Sub-terms will be moved one level up, becoming children of the parent(s) of the term being removed. + * When the term being removed has no parents in the current vocabulary, + * the sub-terms will become root terms of the vocabulary. + */ + RECONNECT(ReconnectApplier.INSTANCE), + + /** + * If the term has sub-terms, the removal will fail + */ + FAIL(FailApplier.INSTANCE); + + private final TermRemovalParamsApplier applier; + + SubTermRemovalStrategy(TermRemovalParamsApplier applier) { + this.applier = Objects.requireNonNull(applier); + } + + @Override + public void apply(TermRemovalParams removalParams, TermRepositoryService repositoryService) { + applier.apply(removalParams, repositoryService); + } + + /** + * Removes every child of the given term + */ + private static class CascadeRemoveApplier implements TermRemovalParamsApplier { + static final CascadeRemoveApplier INSTANCE = new CascadeRemoveApplier(); + + @Override + public void apply(TermRemovalParams removalParams, TermRepositoryService repositoryService) { + final Term term = removalParams.termToRemove(); + if (term.getSubTerms() != null) { + term.getSubTerms().forEach(subTermInfo -> { + final Term subTerm = repositoryService.findRequired(subTermInfo.getUri()); + repositoryService.remove(removalParams.withTerm(subTerm)); + }); + term.getSubTerms().clear(); + } + } + } + + /** + * Moves every subterm one level up. + * Every child becomes child of its grandparent. + * If the term being removed has no parents in the current vocabulary, children become root terms. + */ + private static class ReconnectApplier implements TermRemovalParamsApplier { + static final ReconnectApplier INSTANCE = new ReconnectApplier(); + + @Override + public void apply(TermRemovalParams removalParams, TermRepositoryService repositoryService) { + final Term term = removalParams.termToRemove(); + final TermInfo termInfo = new TermInfo(term); + + final Collection subTerms = Optional.ofNullable(term.getSubTerms()) + .map(children -> children.stream() + .map(HasIdentifier::getUri) + .map(repositoryService::findRequired) + .map(t -> { + t.consolidateParents(); + return t; + }) + .toList()) + .orElse(Collections.emptyList()); + + removeParent(subTerms, termInfo); + + final boolean hasParents = term.getParentTerms() != null && !term.getParentTerms().isEmpty(); + final boolean hasExternalParents = term.getExternalParentTerms() != null && !term.getExternalParentTerms().isEmpty(); + if (hasParents || hasExternalParents) { + // term has some parents to which children should be reconnected + reconnectToParents(subTerms, term); + } + + subTerms.forEach(subTerm -> { + subTerm.splitExternalAndInternalParents(); + repositoryService.update(subTerm); + }); + + if (term.getSubTerms() != null) { + term.getSubTerms().clear(); // they were reconnected + } + } + + /** + * Removes the given parent from every given children. + * + * @param children children from which the {@code parentToRemove} should be removed + * @param parentToRemove the parent that should be removed from {@code children} + */ + private void removeParent(Collection children, TermInfo parentToRemove) { + children.forEach(subTerm -> { + final Collection childParents = subTerm.getParentTerms(); + if (childParents != null && !childParents.isEmpty()) { + childParents.removeIf(parentToRemove::equals); + } + }); + } + + /** + * Adds every parent of {@code term} as parent of every sub-term from {@code subTerms}. + * Removes the {@code term} from parents of {@code subTerms}. + * + * @param subTerms sub-terms whose parents should be modified + * @param term the parent of {@code subTerms} + */ + private void reconnectToParents(Collection subTerms, Term term) { + final TermInfo oldParentInfo = new TermInfo(term); + // add every sub-term + subTerms.forEach(subTerm -> { + subTerm.getParentTerms().removeIf(oldParentInfo::equals); + // as a sub-term of each parent + term.getParentTerms().forEach(parent -> { + subTerm.getParentTerms().add(parent); + }); + }); + } + } + + /** + * Throws an exception when the term to be removed has sub-terms. + */ + private static class FailApplier implements TermRemovalParamsApplier { + static final FailApplier INSTANCE = new FailApplier(); + @Override + public void apply(TermRemovalParams removalParams, TermRepositoryService repositoryService) { + final Term term = removalParams.termToRemove(); + if (!term.getSubTerms().isEmpty()) { + throw TermRepositoryService.hasSubTermsException(term.getSubTerms()); + } + } + } + +} diff --git a/src/main/java/cz/cvut/kbss/termit/service/repository/removal/TermRemovalParams.java b/src/main/java/cz/cvut/kbss/termit/service/repository/removal/TermRemovalParams.java new file mode 100644 index 000000000..56b29aeca --- /dev/null +++ b/src/main/java/cz/cvut/kbss/termit/service/repository/removal/TermRemovalParams.java @@ -0,0 +1,43 @@ +package cz.cvut.kbss.termit.service.repository.removal; + +import cz.cvut.kbss.termit.model.Term; +import cz.cvut.kbss.termit.model.Vocabulary; + +import java.util.Objects; + +/** + * Describes how a term should be removed + * @param termToRemove The Term that will be removed + * @param subTermsStrategy Strategy to use for sub-terms handling + * @param removeOccurrences Whether {@link cz.cvut.kbss.termit.model.assignment.TermOccurrence term occurrences} should be removed. + * When {@code false} and a term occurrence exists, the removal will fail + * and the term will not be removed. + * @param removeRelationships Whether relationships referencing the term should be removed. + * When {@code false} and the term is referenced in some relationship, the removal will fail + * and the term will not be removed. + * @see cz.cvut.kbss.termit.service.repository.TermRepositoryService#remove(TermRemovalParams, Vocabulary) + */ +public record TermRemovalParams( + Term termToRemove, + SubTermRemovalStrategy subTermsStrategy, + boolean removeOccurrences, + boolean removeRelationships +) { + public TermRemovalParams(Term termToRemove, SubTermRemovalStrategy subTermsStrategy, boolean removeOccurrences, + boolean removeRelationships) { + this.termToRemove = Objects.requireNonNull(termToRemove); + this.subTermsStrategy = subTermsStrategy != null ? subTermsStrategy : SubTermRemovalStrategy.FAIL; + this.removeOccurrences = removeOccurrences; + this.removeRelationships = removeRelationships; + } + + /** + * Creates a copy of this params replacing the term to remove. + * + * @param term the new term to remove + * @return copy of the params with replaced term instance + */ + public TermRemovalParams withTerm(Term term) { + return new TermRemovalParams(term, subTermsStrategy, removeOccurrences, removeRelationships); + } +} diff --git a/src/main/java/cz/cvut/kbss/termit/service/repository/removal/TermRemovalParamsApplier.java b/src/main/java/cz/cvut/kbss/termit/service/repository/removal/TermRemovalParamsApplier.java new file mode 100644 index 000000000..1ddab8873 --- /dev/null +++ b/src/main/java/cz/cvut/kbss/termit/service/repository/removal/TermRemovalParamsApplier.java @@ -0,0 +1,16 @@ +package cz.cvut.kbss.termit.service.repository.removal; + +import cz.cvut.kbss.termit.service.repository.TermRepositoryService; + +/** + * Removes terms and executes any necessary related operations. + */ +public interface TermRemovalParamsApplier { + /** + * Removes a term, performing additional operations based on the specified parameters. + * + * @param removalParams Params describing how the term should be removed. + * @param repositoryService Service for interacting with the term repository + */ + void apply(TermRemovalParams removalParams, TermRepositoryService repositoryService); +} diff --git a/src/test/java/cz/cvut/kbss/termit/environment/Generator.java b/src/test/java/cz/cvut/kbss/termit/environment/Generator.java index 9282e22f6..06e81d1c9 100644 --- a/src/test/java/cz/cvut/kbss/termit/environment/Generator.java +++ b/src/test/java/cz/cvut/kbss/termit/environment/Generator.java @@ -258,6 +258,7 @@ public static cz.cvut.kbss.termit.model.Vocabulary generateVocabulary() { vocabulary.setLabel(MultilingualString.create("Vocabulary" + randomInt(), primaryLanguage)); vocabulary.setDescription(MultilingualString.create( "Description of vocabulary " + vocabulary.getPrimaryLabel(), primaryLanguage)); + vocabulary.setRootTerms(new HashSet<>()); return vocabulary; } 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 bcc5fbadf..710f72a1d 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 @@ -43,8 +43,12 @@ import cz.cvut.kbss.termit.util.Constants; import org.eclipse.rdf4j.model.IRI; import org.eclipse.rdf4j.model.Literal; +import org.eclipse.rdf4j.model.Resource; import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.Value; import org.eclipse.rdf4j.model.ValueFactory; +import org.eclipse.rdf4j.model.util.Values; +import org.eclipse.rdf4j.model.vocabulary.RDF; import org.eclipse.rdf4j.model.vocabulary.XSD; import org.eclipse.rdf4j.repository.Repository; import org.eclipse.rdf4j.repository.RepositoryConnection; @@ -54,6 +58,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.test.annotation.DirtiesContext; @@ -1519,6 +1524,111 @@ void findSubTermsRetrievesTermSubTerms() { result.stream().map(Asset::getUri).toList()); } + @Test + void findReferencesReturnsStatementsReferencingTermFromVocabularyGraphs() { + final Term term = Generator.generateTermWithId(vocabulary.getUri()); + addTermsAndSave(List.of(term), vocabulary); + + final Statement expectedOne = statement( + Values.iri(Environment.BASE_URI + "/term/source-1"), + Values.iri(SKOS.BROADER), + Values.iri(term.getUri().toString()), + Values.iri(vocabulary.getUri().toString()) + ); + final Statement unexpected = statement( + Values.iri(Environment.BASE_URI + "/term/source-2"), + Values.iri(SKOS.RELATED), + Values.iri(Generator.generateUriString()), + Values.iri(vocabulary.getUri().toString()) + ); + + withStatements(vocabulary.getUri(), expectedOne, unexpected); + + readOnlyTransactional(() -> { + final var result = sut.findReferences(term, PageRequest.of(0, 10)); + + assertEquals(1, result.getContent().size()); + assertEquals(1, result.getTotalElements()); + assertTrue(result.getContent().contains(expectedOne)); + assertFalse(result.getContent().contains(unexpected)); + }); + } + + @Test + void findReferencesRespectsPaging() { + final Term term = Generator.generateTermWithId(vocabulary.getUri()); + addTermsAndSave(List.of(term), vocabulary); + + final List expected = List.of( + statement( + Values.iri(Environment.BASE_URI + "/term/source-1"), + Values.iri(SKOS.BROADER), + Values.iri(term.getUri().toString()), + Values.iri(vocabulary.getUri().toString()) + ), + statement( + Values.iri(Environment.BASE_URI + "/term/source-2"), + Values.iri(SKOS.RELATED), + Values.iri(term.getUri().toString()), + Values.iri(vocabulary.getUri().toString()) + ), + statement( + Values.iri(Environment.BASE_URI + "/term/source-3"), + Values.iri(SKOS.EXACT_MATCH), + Values.iri(term.getUri().toString()), + Values.iri(vocabulary.getUri().toString()) + ) + ); + + withStatements(vocabulary.getUri(), expected.toArray(Statement[]::new)); + + readOnlyTransactional(() -> { + final Page allReferences = sut.findReferences(term, PageRequest.of(0, 10)); + final Page firstPage = sut.findReferences(term, PageRequest.of(0, 2)); + final Page secondPage = sut.findReferences(term, PageRequest.of(1, 2)); + + final long totalElements = expected.size(); + assertEquals(totalElements, allReferences.getNumberOfElements()); + assertEquals(totalElements, firstPage.getTotalElements()); + assertEquals(totalElements, secondPage.getTotalElements()); + assertEquals(2, firstPage.getSize()); + assertEquals(2, secondPage.getSize()); + assertEquals(List.of(allReferences.getContent().get(0), allReferences.getContent().get(1)), firstPage.getContent()); + assertEquals(List.of(allReferences.getContent().get(2)), secondPage.getContent()); + }); + } + + @Test + void findReferencesExcludesStatementsFromVocabularySnapshotGraphs() { + final Term term = Generator.generateTermWithId(vocabulary.getUri()); + addTermsAndSave(List.of(term), vocabulary); + + final Statement expected = statement( + Values.iri(Environment.BASE_URI + "/term/source-live"), + Values.iri(SKOS.BROADER), + Values.iri(term.getUri().toString()), + Values.iri(vocabulary.getUri().toString())); + final URI snapshotUri = URI.create(vocabulary.getUri().toString() + "/version/test"); + final Statement snapshotReference = statement( + Values.iri(Environment.BASE_URI + "/term/source-snapshot"), + Values.iri(SKOS.RELATED), + Values.iri(term.getUri().toString()), + Values.iri(snapshotUri.toString())); + + withStatements(vocabulary.getUri(), expected); + withStatements(snapshotUri, snapshotReference); + persistVocabularySnapshotType(snapshotUri, vocabulary.getUri()); + + readOnlyTransactional(() -> { + final var result = sut.findReferences(term, PageRequest.of(0, 10)); + + assertEquals(1, result.getContent().size()); + assertEquals(1, result.getTotalElements()); + assertTrue(result.getContent().contains(expected)); + assertFalse(result.getContent().contains(snapshotReference)); + }); + } + @Test void findAllFlatWithoutVocabularyBySearchStringReturnsTermsWithMatchingLabel() { final Term t1 = Generator.generateTermWithId(vocabulary.getUri()); @@ -1620,4 +1730,52 @@ static Set mapToIdentifiers(Collection withIdentif return withIdentifier.stream().map(HasIdentifier::getUri) .collect(Collectors.toSet()); } + + /** + * Adds {@code snaphost a versionOfVocabulary} and {@code snaphost isVersionOfVocabulary vocabulary} statements + */ + private void persistVocabularySnapshotType(URI snapshotUri, URI vocabulary) { + transactional(() -> { + final Repository repo = em.unwrap(Repository.class); + try (RepositoryConnection conn = repo.getConnection()) { + final IRI snapshot = Values.iri(snapshotUri.toString()); + final IRI context = Values.iri(snapshotUri.toString()); + conn.add(snapshot, RDF.TYPE, Values.iri(cz.cvut.kbss.termit.util.Vocabulary.s_c_version_of_vocabulary), + context); + conn.add(snapshot, + Values.iri(cz.cvut.kbss.termit.util.Vocabulary.s_p_is_version_of_vocabulary), + Values.iri(vocabulary.toString()), context); + } + }); + } + + /** + * Persists the given statements in the given context + * + * @param context context to which the statements should be persisted + * @param statements statements to persist + */ + protected void withStatements(URI context, Statement... statements) { + final IRI contextIri = Values.iri(context.toString()); + transactional(() -> { + final Repository repo = em.unwrap(Repository.class); + try (final RepositoryConnection connection = repo.getConnection()) { + for (Statement s : statements) { + connection.add(s.getSubject(), s.getPredicate(), s.getObject(), contextIri); + } + connection.commit(); + } + }); + } + + /** + * Creates RDF4J {@link Statement} + */ + protected static Statement statement(Resource subject, IRI predicate, Value object, Resource context) { + return Values.getValueFactory().createStatement( + subject, + predicate, + object, + context); + } } diff --git a/src/test/java/cz/cvut/kbss/termit/rest/TermControllerTest.java b/src/test/java/cz/cvut/kbss/termit/rest/TermControllerTest.java index a5daac06c..80ddd4d63 100644 --- a/src/test/java/cz/cvut/kbss/termit/rest/TermControllerTest.java +++ b/src/test/java/cz/cvut/kbss/termit/rest/TermControllerTest.java @@ -45,6 +45,7 @@ import cz.cvut.kbss.termit.service.export.ExportConfig; import cz.cvut.kbss.termit.service.export.ExportFormat; import cz.cvut.kbss.termit.service.export.ExportType; +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; @@ -53,6 +54,8 @@ import cz.cvut.kbss.termit.util.Vocabulary; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.util.Values; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -61,6 +64,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpHeaders; @@ -270,6 +274,43 @@ void getSubTermsLoadsSubTermsOfParentTerm() throws Exception { verify(termServiceMock).findSubTerms(term); } + @Test + void getReferencesToTermLoadsReferencingStatements() throws Exception { + final URI termUri = initTermUriResolution(); + final Term term = Generator.generateTerm(); + term.setUri(termUri); + + when(termServiceMock.findRequired(termUri)).thenReturn(term); + final Pageable pageRequest = PageRequest.of(0, 5); + + final List references = List.of( + Values.getValueFactory().createStatement(Values.iri(Environment.BASE_URI + "/term/source-1"), + Values.iri(SKOS.BROADER), + Values.iri(termUri.toString())), + Values.getValueFactory().createStatement(Values.iri(Environment.BASE_URI + "/term/source-2"), + Values.iri(SKOS.RELATED), + Values.iri(termUri.toString())) + ); + when(termServiceMock.findReferences(term, pageRequest)).thenReturn( + new PageImpl<>(references, pageRequest, references.size()) + ); + + final MvcResult mvcResult = mockMvc.perform( + get(PATH + VOCABULARY_NAME + "/terms/" + TERM_NAME + "/references") + .param(PAGE, "0") + .param(PAGE_SIZE, "5")) + .andExpect(status().isOk()) + .andReturn(); + + final String expected = objectMapper.writeValueAsString(references); + + assertEquals(expected, mvcResult.getResponse().getContentAsString()); + assertEquals(Integer.toString(references.size()), + mvcResult.getResponse().getHeader(Constants.X_TOTAL_COUNT_HEADER)); + verify(termServiceMock).findRequired(termUri); + verify(termServiceMock).findReferences(term, pageRequest); + } + @Test void getSubTermsReturnsEmptyListForTermWithoutSubTerms() throws Exception { final URI termUri = initTermUriResolution(); @@ -844,7 +885,7 @@ void removeRemovesTermWithSpecifiedIdentifier() throws Exception { when(termServiceMock.findRequired(termUri)).thenReturn(toRemove); mockMvc.perform(delete(PATH + VOCABULARY_NAME + "/terms/" + TERM_NAME)).andExpect(status().isNoContent()); - verify(termServiceMock).remove(toRemove); + verify(termServiceMock).remove(new TermRemovalParams(toRemove, null, false, false)); } @Test @@ -855,7 +896,7 @@ void removeThrowsNotFoundWhenTermToRemoveDoesNotExist() throws Exception { mockMvc.perform( delete(PATH + VOCABULARY_NAME + "/terms/" + TERM_NAME)) .andExpect(status().isNotFound()); - verify(termServiceMock, never()).remove(any()); + verify(termServiceMock, never()).remove(any(Term.class)); } @Test diff --git a/src/test/java/cz/cvut/kbss/termit/service/repository/TermRepositoryServiceTest.java b/src/test/java/cz/cvut/kbss/termit/service/repository/TermRepositoryServiceTest.java index 139da7d97..0219535ca 100644 --- a/src/test/java/cz/cvut/kbss/termit/service/repository/TermRepositoryServiceTest.java +++ b/src/test/java/cz/cvut/kbss/termit/service/repository/TermRepositoryServiceTest.java @@ -39,6 +39,8 @@ import cz.cvut.kbss.termit.model.selector.TextPositionSelector; import cz.cvut.kbss.termit.persistence.context.DescriptorFactory; import cz.cvut.kbss.termit.service.BaseServiceTestRunner; +import cz.cvut.kbss.termit.service.repository.removal.SubTermRemovalStrategy; +import cz.cvut.kbss.termit.service.repository.removal.TermRemovalParams; import cz.cvut.kbss.termit.util.Constants; import org.eclipse.rdf4j.model.ValueFactory; import org.eclipse.rdf4j.repository.Repository; @@ -63,14 +65,15 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.emptyCollectionOf; import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -464,6 +467,169 @@ void removeRemovesNonReferencedNonOccurringTerm() { assertNull(result); } + @Test + void removeWithParamsReconnectsChildrenToParents() { + final Term parent = Generator.generateTermWithId(vocabulary.getUri()); + final Term toRemove = Generator.generateTermWithId(vocabulary.getUri()); + final Term child = Generator.generateTermWithId(vocabulary.getUri()); + + transactional(() -> sut.addRootTermToVocabulary(parent, vocabulary)); + transactional(() -> sut.addChildTerm(toRemove, parent)); + transactional(() -> sut.addChildTerm(child, toRemove)); + transactional(() -> em.merge(vocabulary, descriptorFactory.vocabularyDescriptor(vocabulary))); + + sut.remove(new TermRemovalParams(toRemove, SubTermRemovalStrategy.RECONNECT, false, false)); + + final Term updatedParent = sut.findRequired(parent.getUri()); + assertFalse(updatedParent.getSubTerms().stream().anyMatch(new TermInfo(toRemove)::equals), + "Term to remove must be removed from its parent"); + assertTrue(updatedParent.getSubTerms().stream().anyMatch(new TermInfo(child)::equals), + "Child of removed term must be reconnected to the parent"); + } + + @Test + void removeWithParamsCascadesToNestedChildren() { + final Term parent = Generator.generateTermWithId(vocabulary.getUri()); + final Term child = Generator.generateTermWithId(vocabulary.getUri()); + final Term grandChild = Generator.generateTermWithId(vocabulary.getUri()); + + transactional(() -> sut.addRootTermToVocabulary(parent, vocabulary)); + transactional(() -> sut.addChildTerm(child, parent)); + transactional(() -> sut.addChildTerm(grandChild, child)); + + // verify setup + assertNotNull(em.find(Term.class, parent.getUri())); + assertNotNull(em.find(Term.class, child.getUri())); + assertNotNull(em.find(Term.class, grandChild.getUri())); + + // remove parent and all children + sut.remove(new TermRemovalParams(parent, SubTermRemovalStrategy.CASCADE, false, true)); + + assertNull(em.find(Term.class, parent.getUri())); + assertNull(em.find(Term.class, child.getUri())); + assertNull(em.find(Term.class, grandChild.getUri())); + final Vocabulary result = em.find(Vocabulary.class, vocabulary.getUri(), + descriptorFactory.vocabularyDescriptor(vocabulary)); + assertTrue(result.getRootTerms().isEmpty()); + } + + /** + * Prepares and persists term with definition containing an occurrence of other therm. + * + * @param occurrenceUri the URI to use for the created occurrence + * @return the occurring term + */ + private Term prepareTermWithSuggestedOccurrence(URI occurrenceUri) { + enableRdfsInference(em); + + final Term toRemove = Generator.generateTermWithId(vocabulary.getUri()); + final Term referencing = Generator.generateTermWithId(vocabulary.getUri()); + + toRemove.setVocabulary(vocabulary.getUri()); + vocabulary.addRootTerm(referencing); + vocabulary.addRootTerm(toRemove); + + final TermOccurrence occ = new TermDefinitionalOccurrence(toRemove.getUri(), + new DefinitionalOccurrenceTarget(referencing)); + occ.addType(cz.cvut.kbss.termit.util.Vocabulary.s_c_suggested_term_occurrence); + occ.getTarget().setSelectors(Set.of(new TextPositionSelector(0, 10))); + occ.setUri(occurrenceUri); + + transactional(() -> { + em.persist(toRemove, descriptorFactory.termDescriptor(toRemove)); + em.persist(referencing, descriptorFactory.termDescriptor(referencing)); + em.merge(vocabulary, descriptorFactory.vocabularyDescriptor(vocabulary)); + em.persist(occ); + em.persist(occ.getTarget()); + }); + + return sut.findRequired(toRemove.getUri()); + } + + @Test + void removeWithParamsRemovesSuggestedOccurrencesWhenRemoveOccurrencesIsTrue() { + final URI occurrenceUri = Generator.generateUri(); + final Term toRemove = prepareTermWithSuggestedOccurrence(occurrenceUri); + + sut.remove(new TermRemovalParams(toRemove, SubTermRemovalStrategy.FAIL, true, false)); + assertNull(em.find(Term.class, toRemove.getUri())); + assertNull(em.find(TermDefinitionalOccurrence.class, occurrenceUri)); + } + + /** + * Prepares and persists two terms referencing each other + * + * @return the created terms + */ + private List prepareTermsWithRelationship() { + final Term toRemove = Generator.generateTermWithId(vocabulary.getUri()); + final Term related = Generator.generateTermWithId(vocabulary.getUri()); + toRemove.setVocabulary(vocabulary.getUri()); + related.setVocabulary(vocabulary.getUri()); + vocabulary.addRootTerm(toRemove); + vocabulary.addRootTerm(related); + + transactional(() -> { + em.persist(toRemove, descriptorFactory.termDescriptor(vocabulary)); + em.persist(related, descriptorFactory.termDescriptor(vocabulary)); + em.merge(vocabulary, descriptorFactory.vocabularyDescriptor(vocabulary)); + generateRelatedInverse(toRemove, related, Environment.BASE_URI + "/example-custom-attribute"); + generateRelatedInverse(related, toRemove, Environment.BASE_URI + "/different-example-custom-attribute"); + }); + + return List.of(sut.findRequired(toRemove.getUri()), + sut.findRequired(related.getUri())); + } + + @Test + void removeWithParamsThrowsWhenTermRelationshipsExistAndRemoveRelationshipsIsFalse() { + final List terms = prepareTermsWithRelationship(); + final Term toRemove = terms.getFirst(); + final Term related = terms.getLast(); + assertNotEquals(toRemove, related); + + final AssetRemovalException exception = assertThrows(AssetRemovalException.class, + () -> sut.remove(new TermRemovalParams(toRemove, SubTermRemovalStrategy.FAIL, + false, false))); + assertEquals("error.term.remove.relationshipsExist", exception.getMessageId()); + // none of the terms must be removed because of the exception + assertNotNull(em.find(Term.class, toRemove.getUri())); + assertNotNull(em.find(Term.class, related.getUri())); + } + + @Test + void removeWithParamsRemovesTermRelationshipsWhenRemoveRelationshipsIsTrue() { + final List terms = prepareTermsWithRelationship(); + final Term toRemove = terms.getFirst(); + final Term related = terms.getLast(); + assertNotEquals(toRemove, related); + + sut.remove(new TermRemovalParams(toRemove, SubTermRemovalStrategy.FAIL, + false, true)); + + assertNull(em.find(Term.class, toRemove.getUri()), "The term must be removed"); + assertNotNull(em.find(Term.class, related.getUri()), "The other term must not be removed"); + } + + @Test + void removeWithParamsPromotesChildToRootTermWhenItsParentRootTermIsRemoved() { + final Term root = Generator.generateTermWithId(vocabulary.getUri()); + final Term child = Generator.generateTermWithId(vocabulary.getUri()); + final Term childB = Generator.generateTermWithId(vocabulary.getUri()); + transactional(() -> sut.addRootTermToVocabulary(root, vocabulary)); + transactional(() -> sut.addChildTerm(child, root)); + transactional(() -> sut.addChildTerm(childB, root)); + + sut.remove(new TermRemovalParams(root, SubTermRemovalStrategy.RECONNECT, false, false)); + + final Vocabulary result = em.find(Vocabulary.class, vocabulary.getUri(), + descriptorFactory.vocabularyDescriptor(vocabulary)); + assertTrue(result.getRootTerms().contains(child.getUri()), + "Child of removed root term must become a root term"); + assertTrue(result.getRootTerms().contains(childB.getUri()), + "Second child of removed root term must become a root term"); + } + private void generateRelatedInverse(Term term, Term related, String property) { final Repository repo = em.unwrap(Repository.class); try (final RepositoryConnection conn = repo.getConnection()) { @@ -736,6 +902,24 @@ void removeRemovesHasTopConceptReferenceToRemovedTerm() { .setParameter("term", term).getSingleResult()); } + @Test + void removeThrowsWhenTermIsReferencedByOtherTerm() { + final Term term = Generator.generateTermWithId(vocabulary.getUri()); + final Term other = Generator.generateTermWithId(vocabulary.getUri()); + other.addRelatedTerm(new TermInfo(term)); + + transactional(() -> { + em.merge(vocabulary, descriptorFactory.vocabularyDescriptor(vocabulary)); + em.persist(term, descriptorFactory.termDescriptor(term)); + em.flush(); + em.clear(); + em.persist(other, descriptorFactory.termDescriptor(other)); + }); + + final AssetRemovalException exception = assertThrows(AssetRemovalException.class, () -> sut.remove(term)); + assertEquals("error.term.remove.relationshipsExist", exception.getMessageId()); + } + @Test void removeThrowsAssetRemovalExceptionWhenTermIsReferencedByConfirmedOccurrences() { enableRdfsInference(em); diff --git a/src/test/java/cz/cvut/kbss/termit/service/repository/removal/SubTermRemovalStrategyTest.java b/src/test/java/cz/cvut/kbss/termit/service/repository/removal/SubTermRemovalStrategyTest.java new file mode 100644 index 000000000..1dec01074 --- /dev/null +++ b/src/test/java/cz/cvut/kbss/termit/service/repository/removal/SubTermRemovalStrategyTest.java @@ -0,0 +1,174 @@ +package cz.cvut.kbss.termit.service.repository.removal; + +import cz.cvut.kbss.termit.dto.TermInfo; +import cz.cvut.kbss.termit.environment.Generator; +import cz.cvut.kbss.termit.exception.TermItException; +import cz.cvut.kbss.termit.model.Term; +import cz.cvut.kbss.termit.model.Vocabulary; +import cz.cvut.kbss.termit.service.repository.TermRepositoryService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class SubTermRemovalStrategyTest { + + @Mock + private TermRepositoryService repositoryService; + + private Vocabulary vocabulary; + + private Term term; + + @BeforeEach + void setUp() { + vocabulary = Generator.generateVocabularyWithId(); + term = Generator.generateTermWithId(vocabulary.getUri()); + vocabulary.addRootTerm(term); + term.setSubTerms(new HashSet<>()); + term.setParentTerms(new HashSet<>()); + term.setExternalParentTerms(new HashSet<>()); + + lenient().when(repositoryService.findRequired(term.getUri())).thenReturn(term); + + lenient().doAnswer(answer -> { + Term t = answer.getArgument(0,Term.class); + Vocabulary voc = answer.getArgument(1, Vocabulary.class); + t.setVocabulary(voc.getUri()); + voc.addRootTerm(t); + return null; + }).when(repositoryService).addRootTermToVocabulary(any(), any()); + } + + private TermRemovalParams withStrategy(SubTermRemovalStrategy strategy) { + return new TermRemovalParams(term, strategy, false, false); + } + + @Test + void failThrowsWhenTermHasChildren() { + final TermRemovalParams params = withStrategy(SubTermRemovalStrategy.FAIL); + final TermInfo child = Generator.generateTermInfoWithId(); + term.setSubTerms(Set.of(child)); + + final TermItException exception = assertThrows(TermItException.class, () -> params.subTermsStrategy().apply(params, repositoryService)); + assertEquals("error.term.remove.hasSubTerms", exception.getMessageId()); + } + + @Test + void failDoesNotThrowWhenTermHasNoChildren() { + final TermRemovalParams params = withStrategy(SubTermRemovalStrategy.FAIL); + assertDoesNotThrow(() -> params.subTermsStrategy().apply(params, repositoryService)); + } + + @Test + void cascadeRemovesAllChildrenUsingSameRemovalParameters() { + final TermRemovalParams params = withStrategy(SubTermRemovalStrategy.CASCADE); + final TermInfo firstChildInfo = Generator.generateTermInfoWithId(); + final TermInfo secondChildInfo = Generator.generateTermInfoWithId(); + + final Term firstChild = Generator.generateTermWithId(vocabulary.getUri()); + firstChild.setUri(firstChildInfo.getUri()); + + final Term secondChild = Generator.generateTermWithId(vocabulary.getUri()); + secondChild.setUri(secondChildInfo.getUri()); + + term.setSubTerms(new HashSet<>()); + term.getSubTerms().add(firstChildInfo); + term.getSubTerms().add(secondChildInfo); + + when(repositoryService.findRequired(firstChildInfo.getUri())).thenReturn(firstChild); + when(repositoryService.findRequired(secondChildInfo.getUri())).thenReturn(secondChild); + + params.subTermsStrategy().apply(params, repositoryService); + + verify(repositoryService).findRequired(firstChildInfo.getUri()); + verify(repositoryService).findRequired(secondChildInfo.getUri()); + verify(repositoryService).remove(params.withTerm(firstChild)); + verify(repositoryService).remove(params.withTerm(secondChild)); + verifyNoMoreInteractions(repositoryService); + } + + @Test + void reconnectUpdatesChildrenToHandleVocabularyRootTermAddition() { + final TermRemovalParams params = withStrategy(SubTermRemovalStrategy.RECONNECT); + final TermInfo firstChild = Generator.generateTermInfoWithId(); + final TermInfo secondChild = Generator.generateTermInfoWithId(); + + final Term firstChildTerm = firstChild.toTerm(); + final Term secondChildTerm = secondChild.toTerm(); + + makeParent(firstChildTerm, term); + makeParent(secondChildTerm, term); + + firstChild.setVocabulary(vocabulary.getUri()); + secondChild.setVocabulary(Generator.generateUri()); + + when(repositoryService.findRequired(firstChild.getUri())).thenReturn(firstChildTerm); + when(repositoryService.findRequired(secondChild.getUri())).thenReturn(secondChildTerm); + + params.subTermsStrategy().apply(params, repositoryService); + + verify(repositoryService, atLeastOnce()).findRequired(any()); + // repository post-update handles root term assignment + verify(repositoryService).update(firstChildTerm); + verify(repositoryService).update(secondChildTerm); + verifyNoMoreInteractions(repositoryService); + } + + @Test + void reconnectLinksChildrenToAllParents() { + final TermRemovalParams params = withStrategy(SubTermRemovalStrategy.RECONNECT); + + final Term child = Generator.generateTermWithId(vocabulary.getUri()); + final Term parent = Generator.generateTermWithId(vocabulary.getUri()); + final Vocabulary externalVocabulary = Generator.generateVocabularyWithId(); + final Term externalParent = Generator.generateTermWithId(externalVocabulary.getUri()); + + makeParent(child, term); + makeParent(term, parent); + makeParent(term, externalParent); + + vocabulary.getRootTerms().clear(); + vocabulary.addRootTerm(parent); + + term.setExternalParentTerms(Set.of(externalParent.toTermInfo())); + + when(repositoryService.findRequired(child.getUri())).thenReturn(child); + params.subTermsStrategy().apply(params, repositoryService); + + assertEquals(Set.of(parent.toTermInfo()), child.getParentTerms(), "Child must be assigned to the parent of the removed term"); + assertEquals(Set.of(externalParent.toTermInfo()), child.getExternalParentTerms()); + + assertEquals(Set.of(parent.getUri()), vocabulary.getRootTerms(), "Vocabulary root terms must remain unchanged"); + + verify(repositoryService).update(child); + verifyNoMoreInteractions(repositoryService); + } + + private void makeParent(Term childTerm, Term parentTerm) { + if (childTerm.getParentTerms() == null) { + childTerm.setParentTerms(new HashSet<>()); + } + if (parentTerm.getSubTerms() == null) { + parentTerm.setSubTerms(new HashSet<>()); + } + + childTerm.getParentTerms().add(new TermInfo(parentTerm)); + parentTerm.getSubTerms().add(new TermInfo(childTerm)); + } +}