Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d012878
[Ref] Add HasProperties interface for entities with JOPA's properties…
lukaskabc Sep 18, 2026
ae68a90
[Enhancement kbss-cvut/termit-ui#790] Implement update change record …
lukaskabc Sep 18, 2026
6de470e
[Test kbss-cvut/termit-ui#790] Implement tests for change rollback dao
lukaskabc Sep 21, 2026
0048e35
[Enhancement kbss-cvut/termit-ui#790] Add MultilingualString and Void…
lukaskabc Sep 22, 2026
35ed4fd
[Test kbss-cvut/termit-ui#790] Implement tests for metamodel rollback…
lukaskabc Sep 22, 2026
134a5bc
[Enhancement kbss-cvut/termit-ui#790] Implement support for rollbacki…
lukaskabc Sep 22, 2026
40a19dc
[Test kbss-cvut/termit-ui#790] Implement tests for ChangeRollbackServ…
lukaskabc Sep 22, 2026
9980448
[Enhancement kbss-cvut/termit-ui#790] Introduce termit:reversible-cha…
lukaskabc Sep 22, 2026
d68abb6
[Test] Fix failing tests because of missing bean in context and datab…
lukaskabc Sep 22, 2026
062bebe
[Ref] Rename primitives to literals in RollbackValidator
lukaskabc Sep 22, 2026
42f5ace
[Test kbss-cvut/termit-ui#790] Add integration tests for rollbacking …
lukaskabc Sep 22, 2026
05c4f6a
[Test kbss-cvut/termit-ui#790] Add tests for TermController ensuring …
lukaskabc Sep 23, 2026
d936d99
[Enhancement kbss-cvut/termit-ui#790] Include reversible types for ch…
lukaskabc Sep 23, 2026
b89839f
[Docs] Add missing api response documentation for rollback change end…
lukaskabc Sep 23, 2026
1d69e17
[Test kbss-cvut/termit-ui#790] Add tests for VocabularyController ens…
lukaskabc Sep 23, 2026
2b32015
[Test kbss-cvut/termit-ui#790] Add tests for ChangeRollbackService#wi…
lukaskabc Sep 23, 2026
33ea5c8
[Test kbss-cvut/termit-ui#790] Add test for ChangeRollbackService#fin…
lukaskabc Sep 23, 2026
4e05d4a
[Test kbss-cvut/termit-ui#790] Add tests for DataRepositoryService#fi…
lukaskabc Sep 23, 2026
408e909
[Test kbss-cvut/termit-ui#790] Add security tests to change rollback …
lukaskabc Sep 23, 2026
8a04135
[Test kbss-cvut/termit-ui#790] Add test ensuring that rollback of mul…
lukaskabc Sep 23, 2026
def14fb
[Test] Verify new jopa version is merging multilingual strings as exp…
lukaskabc Sep 29, 2026
f8286e6
[Ref] Inline optional null resolution in ChangeRollbackService
lukaskabc Oct 1, 2026
92bae3a
[Ref] Remove find and findRequired methods from ChangeRecordService a…
lukaskabc Oct 1, 2026
7a95974
[Ref] Extract update change record rollback endpoints to RollbackCont…
lukaskabc Oct 1, 2026
2d9914a
[Ref] Change Types value in AbstractChangeRecord from URI to String
lukaskabc Oct 1, 2026
5bed19b
[Ref] Remove ChangeRollbackService from Term and Vocabulary controlle…
lukaskabc Oct 1, 2026
3145488
[Ref] Remove UpdateChangeRecord#REVERSIBLE_CHANGE_CLASS constant and …
lukaskabc Oct 2, 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
8 changes: 8 additions & 0 deletions ontology/termit.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix dd: <http://onto.fel.cvut.cz/ontologies/data-description/> .

<http://onto.fel.cvut.cz/ontologies/application/termit>
a skos:ConceptScheme , owl:Ontology ;
Expand Down Expand Up @@ -719,3 +720,10 @@ termit:has-related-vocabulary
<http://onto.fel.cvut.cz/ontologies/application/termit> ;
<http://www.w3.org/2004/02/skos/core#prefLabel>
"Has related vocabulary"@en , "Má související slovník"@cs .

termit:reversible-change
a skos:Concept , owl:Class ;
rdfs:subClassOf dd:update-of-entity ;
skos:broader dd:update-of-entity ;
skos:inScheme <http://onto.fel.cvut.cz/ontologies/application/termit> ;
skos:prefLabel "Reversible Change"@en , "Vratná změna"@cs .
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cz.cvut.kbss.termit.exception;

/**
* Indicates a failure during {@link cz.cvut.kbss.termit.model.changetracking.UpdateChangeRecord UpdateChangeRecord}
* rollback
*/
public class UpdateChangeRecordRollbackException extends TermItException {
public UpdateChangeRecordRollbackException() {
}

public UpdateChangeRecordRollbackException(String message) {
super(message);
}
}
5 changes: 4 additions & 1 deletion src/main/java/cz/cvut/kbss/termit/model/Term.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import cz.cvut.kbss.termit.dto.TermInfo;
import cz.cvut.kbss.termit.model.assignment.TermDefinitionSource;
import cz.cvut.kbss.termit.model.changetracking.Audited;
import cz.cvut.kbss.termit.model.util.HasProperties;
import cz.cvut.kbss.termit.model.util.HasTypes;
import cz.cvut.kbss.termit.model.util.SupportsSnapshots;
import cz.cvut.kbss.termit.validation.Disjoint;
Expand All @@ -49,7 +50,7 @@
@Disjoint({"parentTerms", "related"})
@Disjoint({"parentTerms", "relatedMatch"})
@Disjoint({"parentTerms", "exactMatchTerms"})
public class Term extends AbstractTerm implements SupportsSnapshots, HasTypes {
public class Term extends AbstractTerm implements SupportsSnapshots, HasTypes, HasProperties {

@OWLAnnotationProperty(iri = SKOS.ALT_LABEL)
private Set<MultilingualString> altLabels;
Expand Down Expand Up @@ -252,10 +253,12 @@ public void setDefinitionSource(TermDefinitionSource definitionSource) {
this.definitionSource = definitionSource;
}

@Override
public Map<String, Set<Object>> getProperties() {
return properties;
}

@Override
public void setProperties(Map<String, Set<Object>> properties) {
this.properties = properties;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/cz/cvut/kbss/termit/model/Vocabulary.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import cz.cvut.kbss.termit.model.changetracking.Audited;
import cz.cvut.kbss.termit.model.resource.Document;
import cz.cvut.kbss.termit.model.util.AssetVisitor;
import cz.cvut.kbss.termit.model.util.HasProperties;
import cz.cvut.kbss.termit.model.util.HasTypes;
import cz.cvut.kbss.termit.model.util.SupportsSnapshots;
import cz.cvut.kbss.termit.model.util.validation.HasPrimaryLanguage;
Expand All @@ -53,7 +54,7 @@
@OWLClass(iri = SKOS.CONCEPT_SCHEME)
@JsonLdAttributeOrder({"uri", "label", "description"})
public class Vocabulary extends Asset<MultilingualString>
implements HasTypes, SupportsSnapshots, HasPrimaryLanguage, Serializable {
implements HasTypes, HasProperties, SupportsSnapshots, HasPrimaryLanguage, Serializable {

@ParticipationConstraints(nonEmpty = true)
@OWLAnnotationProperty(iri = DC.Terms.TITLE)
Expand Down Expand Up @@ -233,10 +234,12 @@ public void removeRootTerm(Term toRemove) {
}
}

@Override
public Map<String, Set<Object>> getProperties() {
return properties;
}

@Override
public void setProperties(Map<String, Set<Object>> properties) {
this.properties = properties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraints;
import cz.cvut.kbss.jopa.model.annotations.Types;
import cz.cvut.kbss.jopa.vocabulary.DC;
import cz.cvut.kbss.termit.model.AbstractEntity;
import cz.cvut.kbss.termit.model.Asset;
import cz.cvut.kbss.termit.model.User;
import cz.cvut.kbss.termit.model.util.HasTypes;
import cz.cvut.kbss.termit.util.Vocabulary;
import jakarta.annotation.Nonnull;

import java.net.URI;
import java.time.Instant;
import java.util.Objects;
import java.util.Set;

/**
* Represents a change to an asset.
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "className")
@OWLClass(iri = Vocabulary.s_c_change)
public class AbstractChangeRecord extends AbstractEntity implements Comparable<AbstractChangeRecord> {
public class AbstractChangeRecord extends AbstractEntity implements Comparable<AbstractChangeRecord>, HasTypes {

@ParticipationConstraints(nonEmpty = true)
@OWLDataProperty(iri = DC.Terms.MODIFIED)
Expand All @@ -53,6 +56,9 @@ public class AbstractChangeRecord extends AbstractEntity implements Comparable<A
@OWLObjectProperty(iri = Vocabulary.s_p_has_changed_entity)
private URI changedEntity;

@Types(readOnly = true)
private Set<String> types;

Comment thread
lukaskabc marked this conversation as resolved.
Resolved
public AbstractChangeRecord() {
}

Expand Down Expand Up @@ -84,6 +90,16 @@ public void setChangedEntity(URI changedEntity) {
this.changedEntity = changedEntity;
}

@Override
public Set<String> getTypes() {
return types;
}

@Override
public void setTypes(Set<String> types) {
this.types = types;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/cz/cvut/kbss/termit/model/util/HasProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cz.cvut.kbss.termit.model.util;

import java.util.Map;
import java.util.Set;

/**
* Implemented by entities with {@link cz.cvut.kbss.jopa.model.annotations.Properties Properties} attribute.
*/
public interface HasProperties {

Map<String, Set<Object>> getProperties();

void setProperties(Map<String, Set<Object>> properties);
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,20 @@ public Set<User> getAuthors(HasIdentifier asset) {
throw new PersistenceException(e);
}
}

/**
* Finds a change record of the specified type and identifier.
*
* @param recordClass Change record type
* @param recordUri Change record identifier
* @return Matching record, or an empty optional if none is found
*/
public <T extends AbstractChangeRecord> Optional<T> find(Class<T> recordClass, URI recordUri) {
Objects.requireNonNull(recordUri);
try {
return Optional.ofNullable(em.find(recordClass, recordUri));
} catch (RuntimeException e) {
throw new PersistenceException("Failed to find UpdateChangeRecord: " + Utils.uriToString(recordUri), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package cz.cvut.kbss.termit.persistence.dao.changetracking;

import cz.cvut.kbss.jopa.model.EntityManager;
import cz.cvut.kbss.jopa.model.IRI;
import cz.cvut.kbss.jopa.model.metamodel.Attribute;
import cz.cvut.kbss.jopa.query.QueryHints;
import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
import cz.cvut.kbss.termit.exception.PersistenceException;
import cz.cvut.kbss.termit.exception.UpdateChangeRecordRollbackException;
import cz.cvut.kbss.termit.model.Asset;
import cz.cvut.kbss.termit.model.changetracking.UpdateChangeRecord;
import cz.cvut.kbss.termit.model.util.HasProperties;
import cz.cvut.kbss.termit.util.Utils;
import org.springframework.stereotype.Repository;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Repository
public class ChangeRollbackDao {
private final EntityManager em;

public ChangeRollbackDao(EntityManager em) {
this.em = em;
}

/**
* Checks whether an entity with the given identifier exists
*
* @param entityIdentifier the identifier of the entity
* @return {@code true} when the entity exists, {@code false} otherwise.
*/
public boolean entityExists(URI entityIdentifier) {
Objects.requireNonNull(entityIdentifier);
try {
return em.createNativeQuery("ASK { ?x a ?type }", Boolean.class)
.setParameter("x", entityIdentifier)
.setHint(QueryHints.DISABLE_INFERENCE, "true")
.getSingleResult();
} catch (RuntimeException e) {
throw new PersistenceException("Failed check existence of entity " +
Utils.uriToString(entityIdentifier), e);
}
}

/**
* Resolves the entity class from {@link cz.cvut.kbss.jopa.model.metamodel.Metamodel Metamodel}
* and tries to find attribute matching the changed attribute IRI.
*
* @param entityType the type of the changed entity
* @param record the change record
* @return The attribute if any attribute matching the IRI was found
*/
public <T> Optional<Attribute<? super T, ?>> resolveClassAttribute(Class<T> entityType, UpdateChangeRecord record) {
final IRI changedAttributeIRI = IRI.create(record.getChangedAttribute().toString());
return em.getMetamodel().entity(entityType).getAttributes().stream()
.filter(attribute -> changedAttributeIRI.equals(attribute.getIRI()))
.findAny();
}

/**
* Sets the given {@code originalValue} as the new value of the changed Java attribute.
* The required class of the value is inspected from the {@code classAttribute} and identifiers
* are mapped to the respective entity objects when required.
*
* @param originalValue the value to set
* @param changedAsset the changed entity
* @param classAttribute the changed Java attribute
*/
public void rollbackClassAttribute(Set<Object> originalValue, Asset<?> changedAsset, Attribute<?, ?> classAttribute) {
final Set<Object> mappedOriginalValue = resolveEntityReferences(originalValue, classAttribute);
final Object newValue = getNewValue(mappedOriginalValue, classAttribute);
EntityPropertiesUtils.setFieldValue(classAttribute.getJavaField(), changedAsset, newValue);
}

/**
* Maps the given values to their respective entity reference objects when the {@code classAttribute}
* holds entity objects.
*
* @param values Values to map
* @param classAttribute the attribute description
* @return Mapped {@code values} when the value type of the {@code classAttribute} is a Jopa entity type.
* Unchanged {@code values} otherwise.
*/
private Set<Object> resolveEntityReferences(Set<Object> values, Attribute<?, ?> classAttribute) {
final Class<?> valueType = classAttribute.getValueJavaType();
if (values == null || !isEntityClass(valueType)) {
return values;
}

return values.stream().map(identifier -> em.getReference(valueType, identifier))
.collect(Collectors.toSet());
}

/**
* Checks whether metamodel contains the given entity class.
*
* @param clazz Class to check
* @return {@code true} when the metamodel contains the given entity {@code class}, {@code false} otherwise.
*/
private boolean isEntityClass(Class<?> clazz) {
try {
return em.getMetamodel().entity(clazz) != null;
} catch (IllegalArgumentException e) {
return false;
}
}

/**
* Maps the given {@code originalValue} to a collection or a single value based on the {@code classAttribute}.
*
* @param originalValue the original value
* @param classAttribute the Java attribute
* @return a mapped collection when attribute is a collection,
* the first value from the {@code originalValue} when the attribute is singular,
* {@code null} otherwise
*/
private Object getNewValue(Set<Object> originalValue, Attribute<?, ?> classAttribute) {
if (classAttribute.isCollection()) {
Class<?> collectionClass = classAttribute.getJavaType();
if (List.class.equals(collectionClass)) {
return new ArrayList<>(originalValue);
}
if (Collection.class.equals(collectionClass) || Set.class.equals(collectionClass)) {
return originalValue;
}
throw new UpdateChangeRecordRollbackException("Unsupported attribute type: " + collectionClass.getName());
}

if (originalValue == null || originalValue.isEmpty()) {
return null;
} else {
assert originalValue.size() == 1;
return originalValue.iterator().next();
}
}

public void rollbackNativeProperty(Set<Object> originalValue, Asset<?> changedAsset, URI changedAttributeUri) {
if (originalValue != null && !originalValue.isEmpty()) {
getProperties(changedAsset).put(changedAttributeUri.toString(), originalValue);
} else {
getProperties(changedAsset).remove(changedAttributeUri.toString());
}
}

private Map<String, Set<Object>> getProperties(Asset<?> asset) {
if (asset instanceof HasProperties withProperties) {
if (withProperties.getProperties() == null) {
withProperties.setProperties(new HashMap<>());
}
return withProperties.getProperties();
}
throw new IllegalArgumentException("Asset does not implement HasProperties interface! " + Utils.uriToString(asset.getUri()));
}
}
Loading
Loading