Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
06e5842
[Enhancement kbss-cvut/termit-ui#820] Add DataController endpoint for…
lukaskabc Aug 27, 2026
f32fe22
[Test] Add DataDao tests for custom attribute usage retrieval
lukaskabc Aug 27, 2026
02a126e
[Enhancement kbss-cvut/termit-ui#820] Add DataController endpoint for…
lukaskabc Aug 27, 2026
224f0bf
[Docs] Add missing javadoc for DataRepositoryService#removeCustomAttr…
lukaskabc Aug 27, 2026
0eea469
[Test] Add DataRepositoryService tests for custom attribute removal
lukaskabc Aug 27, 2026
32afc11
[Ref] Fix code indention in DataController
lukaskabc Aug 27, 2026
5dc8db4
[Ref] Remove domain and range validation when removing custom attribu…
lukaskabc Aug 27, 2026
fb7d69b
[Ref] Replace findCustomAttributeUsage with simple ask query in isCus…
lukaskabc Aug 27, 2026
3dfcde3
[Bug] Add missing authorization for custom attribute manipulation end…
lukaskabc Aug 27, 2026
0adcf39
[Ref] Add logging for custom attribute removal
lukaskabc Aug 27, 2026
324d9df
[Bug] Evict jopa cache for contexts affected by custom attribute removal
lukaskabc Aug 28, 2026
f740477
[Ref] Replace force parameter for custom attribute removal with remov…
lukaskabc Sep 2, 2026
88b44ed
[Bug] Ensure custom artifact removal does not affect vocabulary snaps…
lukaskabc Sep 2, 2026
89b8179
[Ref] Merge custom attribute usage query into a single final where qu…
lukaskabc Sep 2, 2026
ef81f16
[Test] Fix failing tests due to missing context is vocabulary axiom
lukaskabc Sep 2, 2026
82f03bf
[Test] Add custom attribute creation helper method
lukaskabc Sep 2, 2026
8c85bf1
[Bug] Update custom attribute endpoint authorization to require ROLE_…
lukaskabc Sep 4, 2026
60fa2b7
[Docs] Remove references to old force parameter that was replaced wit…
lukaskabc Sep 4, 2026
2b5a14b
[Docs] Remove references to old force parameter that was replaced wit…
lukaskabc Sep 4, 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
172 changes: 172 additions & 0 deletions src/main/java/cz/cvut/kbss/termit/persistence/dao/DataDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import cz.cvut.kbss.jopa.exceptions.NoResultException;
import cz.cvut.kbss.jopa.exceptions.NoUniqueResultException;
import cz.cvut.kbss.jopa.model.Cache;
import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
Expand All @@ -34,22 +35,34 @@
import cz.cvut.kbss.termit.model.CustomAttribute;
import cz.cvut.kbss.termit.model.CustomAttribute_;
import cz.cvut.kbss.termit.model.RdfsResource;
import cz.cvut.kbss.termit.model.Vocabulary_;
import cz.cvut.kbss.termit.persistence.dao.spec.Specification;
import cz.cvut.kbss.termit.persistence.dao.util.Quad;
import cz.cvut.kbss.termit.service.export.ExportFormat;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Configuration.Persistence;
import cz.cvut.kbss.termit.util.TypeAwareByteArrayResource;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import cz.cvut.kbss.termit.util.Vocabulary;
import jakarta.annotation.Nullable;
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.query.BindingSet;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandler;
import org.eclipse.rdf4j.rio.Rio;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.io.ByteArrayOutputStream;
Expand All @@ -73,6 +86,21 @@ public class DataDao {

private static final URI RDFS_LABEL = URI.create(RDFS.LABEL);

/**
* SPARQL where clause matching custom attribute usage in vocabulary graphs, while excluding snapshots
*/
private static final String CUSTOM_ATTRIBUTE_USAGE_WHERE_CLAUSE = """
WHERE {
GRAPH ?context {
?subject ?attribute ?object .
?context a ?vocabulary .
FILTER NOT EXISTS {
?context a ?versionOfVocabulary .
}
}
}
""";

private final EntityManager em;

private final Persistence config;
Expand Down Expand Up @@ -317,4 +345,148 @@ public void insertRawData(Collection<Quad> data) {
con.commit();
}
}

private int countCustomAttributeUsage(RepositoryConnection con, IRI predicate) {
final TupleQuery countQuery = con.prepareTupleQuery("SELECT (COUNT(*) AS ?count) " + CUSTOM_ATTRIBUTE_USAGE_WHERE_CLAUSE);

bindCustomAttributeUsageQueryParameters(countQuery, predicate);
countQuery.setIncludeInferred(false);

try (TupleQueryResult result = countQuery.evaluate()) {
final BindingSet bindings = result.next();
return ((Literal) bindings.getValue("count")).intValue();
}
}

private List<Statement> findCustomAttributeUsage(RepositoryConnection con, IRI predicate, long offset, int resultLimit) {
final TupleQuery pageQuery = con.prepareTupleQuery("SELECT ?subject ?attribute ?object ?context "+
CUSTOM_ATTRIBUTE_USAGE_WHERE_CLAUSE +
"LIMIT " + resultLimit +
" OFFSET " + offset);

bindCustomAttributeUsageQueryParameters(pageQuery, predicate);
pageQuery.setIncludeInferred(false);

final List<Statement> statements = new ArrayList<>(resultLimit);

try (TupleQueryResult result = pageQuery.evaluate()) {
while (result.hasNext()) {
final BindingSet bindings = result.next();

final Resource subject = (Resource) bindings.getValue("subject");
final IRI statementPredicate = (IRI) bindings.getValue("attribute");
final Value object = bindings.getValue("object");
final Resource context = (Resource) bindings.getValue("context");

statements.add(Values.getValueFactory().createStatement(
subject,
statementPredicate,
object,
context
));
}
}

return statements;
}

/**
* Finds statements where the specified custom attribute is used as a predicate.
*
* @param identifier Custom attribute identifier
* @param pageable {@link Pageable}
* @return Page of RDF statements
*/
public Page<Statement> findCustomAttributeUsage(URI identifier, Pageable pageable) {
Objects.requireNonNull(identifier);
Objects.requireNonNull(pageable);

final org.eclipse.rdf4j.repository.Repository repo = em.unwrap(org.eclipse.rdf4j.repository.Repository.class);
final IRI predicate = Values.iri(identifier.toString());

try (RepositoryConnection con = repo.getConnection()) {
final int totalCount = countCustomAttributeUsage(con, predicate);

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

int maxResults = Math.min(totalCount, pageable.getPageSize());
final List<Statement> statements = findCustomAttributeUsage(con, predicate, pageable.getOffset(), maxResults);

return new PageImpl<>(statements, pageable, totalCount);
} catch (RuntimeException e) {
throw new PersistenceException("Failed to find custom attribute usage", e);
}
}

/**
* Finds contexts in which the custom attribute is used
*
* @param customAttribute the custom attribute to look up
* @return contexts where the attribute URI is used as a predicate
*/
public List<URI> findCustomAttributeUsageContexts(CustomAttribute customAttribute) {
Objects.requireNonNull(customAttribute);
Objects.requireNonNull(customAttribute.getUri());
try {
return em.createNativeQuery("SELECT DISTINCT ?context " + CUSTOM_ATTRIBUTE_USAGE_WHERE_CLAUSE, URI.class)
.setParameter("attribute", customAttribute.getUri())
.setParameter("vocabulary", Vocabulary_.entityClassIRI)
.setParameter("versionOfVocabulary", URI.create(Vocabulary.s_c_version_of_vocabulary))
.getResultList();
} catch (RuntimeException e) {
throw new PersistenceException("Failed to find custom attribute usage contexts", e);
}
}

/**
* Removes all triples where the {@link CustomAttribute} is used as predicate
*
* @param attribute {@link CustomAttribute} whose usages should be removed
*/
public void removeAllCustomAttributeUsages(CustomAttribute attribute) {
Objects.requireNonNull(attribute);
Objects.requireNonNull(attribute.getUri());
try {
em.createNativeQuery("""
DELETE {
GRAPH ?context {
?subject ?attribute ?object .
}
}
""" + CUSTOM_ATTRIBUTE_USAGE_WHERE_CLAUSE)
.setParameter("attribute", attribute.getUri())
.setParameter("vocabulary", Vocabulary_.entityClassIRI)
.setParameter("versionOfVocabulary", URI.create(Vocabulary.s_c_version_of_vocabulary))
.executeUpdate();
} catch (RuntimeException e) {
throw new PersistenceException("Failed to remove all custom attribute usages", e);
}
}

private void bindCustomAttributeUsageQueryParameters(TupleQuery query, IRI predicate) {
query.setBinding("attribute", predicate);
query.setBinding("vocabulary", Values.iri(Vocabulary_.entityClassIRI.toString()));
query.setBinding("versionOfVocabulary", Values.iri(Vocabulary.s_c_version_of_vocabulary));
}

public void removeCustomAttribute(CustomAttribute attribute) {
Objects.requireNonNull(attribute);
try {
em.remove(attribute);
} catch (RuntimeException e) {
throw new PersistenceException("Failed to remove custom attribute", e);
}
}

/**
* Evicts {@link cz.cvut.kbss.jopa.model.EntityManagerFactory} cache for each specified context.
*
* @param contexts context to evict from cache
*/
public void evictCacheForContexts(List<URI> contexts) {
final Cache emfCache = em.getEntityManagerFactory().getCache();
contexts.forEach(emfCache::evict);
}
}
75 changes: 74 additions & 1 deletion src/main/java/cz/cvut/kbss/termit/rest/DataController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,30 @@
import cz.cvut.kbss.termit.exception.NotFoundException;
import cz.cvut.kbss.termit.model.CustomAttribute;
import cz.cvut.kbss.termit.model.RdfsResource;
import cz.cvut.kbss.termit.rest.doc.ApiDocConstants;
import cz.cvut.kbss.termit.rest.util.RestUtils;
import cz.cvut.kbss.termit.security.SecurityConstants;
import cz.cvut.kbss.termit.service.IdentifierResolver;
import cz.cvut.kbss.termit.service.repository.DataRepositoryService;
import cz.cvut.kbss.termit.util.Constants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
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.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -58,10 +66,12 @@ public class DataController {
private static final Logger LOG = LoggerFactory.getLogger(DataController.class);

private final DataRepositoryService dataService;
private final IdentifierResolver identifierResolver;

@Autowired
public DataController(DataRepositoryService dataService) {
public DataController(DataRepositoryService dataService, IdentifierResolver identifierResolver) {
this.dataService = dataService;
this.identifierResolver = identifierResolver;
}

@Operation(description = "Gets all unique RDF properties used by the data in the system.")
Expand Down Expand Up @@ -109,6 +119,7 @@ public ResponseEntity<Void> createCustomAttribute(@Parameter(description = "Attr
@ApiResponse(responseCode = "404", description = "Attribute not found.")
})
@ResponseStatus(HttpStatus.NO_CONTENT)
@PreAuthorize("hasRole('" + SecurityConstants.ROLE_FULL_USER + "')")
Comment thread
lukaskabc marked this conversation as resolved.
Resolved
@PutMapping(value = "/custom-attributes/{localName}",
consumes = {MediaType.APPLICATION_JSON_VALUE, JsonLd.MEDIA_TYPE})
public void updateCustomAttribute(@Parameter(
Expand All @@ -121,6 +132,68 @@ public void updateCustomAttribute(@Parameter(
LOG.debug("Updated custom attribute {}.", update);
}

@Operation(security = {@SecurityRequirement(name = "bearer-key")},
description = "Retrieves the triples where the specified custom attribute is used as predicate.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "The list of triples with the custom attribute as predicate"),
@ApiResponse(responseCode = "404", description = "Attribute not found")
})
@PreAuthorize("hasRole('" + SecurityConstants.ROLE_FULL_USER + "')")
@GetMapping(value = "/custom-attributes/{localName}/usage", produces = {MediaType.APPLICATION_JSON_VALUE, JsonLd.MEDIA_TYPE})
public ResponseEntity<List<Statement>> getCustomAttributeUsage(@Parameter(
description = "Locally (in the context of the namespace) unique part of the attribute identifier.",
example = "custom-attribute")
@PathVariable String localName,
@Parameter(
description = "Custom attribute identifier namespace",
example = "http://onto.fel.cvut.cz/ontologies/application/termit/custom-attribute/"
)
@RequestParam String namespace,
@Parameter(description = ApiDocConstants.PAGE_SIZE_DESCRIPTION)
@RequestParam(name = Constants.QueryParams.PAGE_SIZE, required = false) Integer pageSize,
@Parameter(description = ApiDocConstants.PAGE_NO_DESCRIPTION)
@RequestParam(name = Constants.QueryParams.PAGE, required = false) Integer pageNo) {
Pageable pageable = Constants.DEFAULT_PAGE_SPEC;
if (pageSize != null && pageNo != null) {
pageable = PageRequest.of(pageNo, pageSize);
}
final URI identifier = identifierResolver.resolveIdentifier(namespace, localName);
final Page<Statement> result = dataService.findCustomAttributeUsage(identifier, pageable);
return ResponseEntity.ok()
.header(Constants.X_TOTAL_COUNT_HEADER, Long.toString(result.getTotalElements()))
.body(result.getContent());
}

@Operation(security = {@SecurityRequirement(name = "bearer-key")},
description = "Removes the custom attribute, " +
"if it has usages, force parameter is required and the removal will remove all the usages as well.")
Comment thread
lukaskabc marked this conversation as resolved.
Resolved
@ApiResponses({
@ApiResponse(responseCode = "204", description = "Custom attribute was removed"),
@ApiResponse(responseCode = "400", description = "Force parameter is required for removal of attribute with usages"),
Comment thread
lukaskabc marked this conversation as resolved.
Resolved
@ApiResponse(responseCode = "404", description = "Attribute not found")
})
@PreAuthorize("hasRole('" + SecurityConstants.ROLE_FULL_USER + "')")
Comment thread
lukaskabc marked this conversation as resolved.
Resolved
@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping(value = "/custom-attributes/{localName}")
public void removeCustomAttribute(@Parameter(
description = "Locally (in the context of the namespace) unique part of the attribute identifier.",
example = "custom-attribute")
@PathVariable String localName,
@Parameter(
description = "Custom attribute identifier namespace",
example = "http://onto.fel.cvut.cz/ontologies/application/termit/custom-attribute/"
)
@RequestParam String namespace,
@Parameter(
description = "Indicates whether to remove the usages of the custom attribute as well" +
"When false, only the attribute itself will be removed, its usages will be kept."
)
@RequestParam boolean removeUsages) {
final URI identifier = identifierResolver.resolveIdentifier(namespace, localName);
dataService.removeCustomAttribute(identifier, removeUsages);
LOG.debug("Removed custom attribute: {}", identifier);
}

@Operation(description = "Gets basic metadata for a RDFS resource with the specified IRI.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "RDSF resource metadata."),
Expand Down
Loading