Repository navigation
[Enhancement] Allow reverting asset change #437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ledsoft
merged 27 commits into
kbss-cvut:development
from
lukaskabc:lukaskabc/ui/790-allow-reverting-asset-change
Oct 2, 2026
Merged
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 ae68a90
[Enhancement kbss-cvut/termit-ui#790] Implement update change record …
lukaskabc 6de470e
[Test kbss-cvut/termit-ui#790] Implement tests for change rollback dao
lukaskabc 0048e35
[Enhancement kbss-cvut/termit-ui#790] Add MultilingualString and Void…
lukaskabc 35ed4fd
[Test kbss-cvut/termit-ui#790] Implement tests for metamodel rollback…
lukaskabc 134a5bc
[Enhancement kbss-cvut/termit-ui#790] Implement support for rollbacki…
lukaskabc 40a19dc
[Test kbss-cvut/termit-ui#790] Implement tests for ChangeRollbackServ…
lukaskabc 9980448
[Enhancement kbss-cvut/termit-ui#790] Introduce termit:reversible-cha…
lukaskabc d68abb6
[Test] Fix failing tests because of missing bean in context and datab…
lukaskabc 062bebe
[Ref] Rename primitives to literals in RollbackValidator
lukaskabc 42f5ace
[Test kbss-cvut/termit-ui#790] Add integration tests for rollbacking …
lukaskabc 05c4f6a
[Test kbss-cvut/termit-ui#790] Add tests for TermController ensuring …
lukaskabc d936d99
[Enhancement kbss-cvut/termit-ui#790] Include reversible types for ch…
lukaskabc b89839f
[Docs] Add missing api response documentation for rollback change end…
lukaskabc 1d69e17
[Test kbss-cvut/termit-ui#790] Add tests for VocabularyController ens…
lukaskabc 2b32015
[Test kbss-cvut/termit-ui#790] Add tests for ChangeRollbackService#wi…
lukaskabc 33ea5c8
[Test kbss-cvut/termit-ui#790] Add test for ChangeRollbackService#fin…
lukaskabc 4e05d4a
[Test kbss-cvut/termit-ui#790] Add tests for DataRepositoryService#fi…
lukaskabc 408e909
[Test kbss-cvut/termit-ui#790] Add security tests to change rollback …
lukaskabc 8a04135
[Test kbss-cvut/termit-ui#790] Add test ensuring that rollback of mul…
lukaskabc def14fb
[Test] Verify new jopa version is merging multilingual strings as exp…
lukaskabc f8286e6
[Ref] Inline optional null resolution in ChangeRollbackService
lukaskabc 92bae3a
[Ref] Remove find and findRequired methods from ChangeRecordService a…
lukaskabc 7a95974
[Ref] Extract update change record rollback endpoints to RollbackCont…
lukaskabc 2d9914a
[Ref] Change Types value in AbstractChangeRecord from URI to String
lukaskabc 5bed19b
[Ref] Remove ChangeRollbackService from Term and Vocabulary controlle…
lukaskabc 3145488
[Ref] Remove UpdateChangeRecord#REVERSIBLE_CHANGE_CLASS constant and …
lukaskabc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/cz/cvut/kbss/termit/exception/UpdateChangeRecordRollbackException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/cz/cvut/kbss/termit/model/util/HasProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
src/main/java/cz/cvut/kbss/termit/persistence/dao/changetracking/ChangeRollbackDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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())); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.