Skip to content

Commit

Permalink
[Enhancement kbss-cvut/termit-ui#530] Create DeleteChangeRecord class…
Browse files Browse the repository at this point in the history
… and BeforeAssetDeleteEvent

Created classes required for recording an asset removal, implemented dispatching asset delete event and logic for ChangeTracker handling the event.
  • Loading branch information
lukaskabc committed Oct 19, 2024
1 parent fb08f3a commit e612cde
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cz.cvut.kbss.termit.event;

import cz.cvut.kbss.termit.model.Asset;
import org.springframework.context.ApplicationEvent;

/**
* Event published before an asset is deleted.
*/
public class BeforeAssetDeleteEvent<T extends Asset<?>> extends ApplicationEvent {
final T asset;
public BeforeAssetDeleteEvent(Object source, T asset) {
super(source);
this.asset = asset;
}

public T getAsset() {
return asset;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cz.cvut.kbss.termit.model.changetracking;

import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraints;
import cz.cvut.kbss.jopa.vocabulary.DC;
import cz.cvut.kbss.termit.model.Asset;
import cz.cvut.kbss.termit.util.Vocabulary;
import jakarta.annotation.Nonnull;

import java.io.Serializable;
import java.net.URI;
import java.util.Objects;

/**
* Represents a record of asset deletion.
* @param <T> The label type, {@link String} or {@link MultilingualString}
*/
//@OWLClass(iri = Vocabulary.s_c_smazani_entity) TODO: ontology for DeleteChangeRecord
public class DeleteChangeRecord<T extends Serializable> extends AbstractChangeRecord {
@ParticipationConstraints(nonEmpty = true)
@OWLAnnotationProperty(iri = DC.Terms.TITLE)
private T label;

@OWLObjectProperty(iri = Vocabulary.s_p_je_pojmem_ze_slovniku)
private URI vocabulary;

public DeleteChangeRecord(Asset<T> changedEntity, URI vocabulary) {
super(changedEntity);
this.label = changedEntity.getLabel();
this.vocabulary = vocabulary;
}

public T getLabel() {
return label;
}

public void setLabel(T label) {
this.label = label;
}

public URI getVocabulary() {
return vocabulary;
}

public void setVocabulary(URI vocabulary) {
this.vocabulary = vocabulary;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof DeleteChangeRecord<?> that)) {
return false;
}
if (!super.equals(o)) {
return false;
}
return Objects.equals(label, that.label) && Objects.equals(vocabulary, that.vocabulary);
}

@Override
public String toString() {
return "DeleteChangeRecord{" +
super.toString() +
", label=" + label +
(vocabulary != null ? ", vocabulary=" + vocabulary : "") +
'}';
}

@Override
public int compareTo(@Nonnull AbstractChangeRecord o) {
if (o instanceof UpdateChangeRecord) {
return 1;
}
if (o instanceof PersistChangeRecord) {
return 1;
}
return super.compareTo(o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public int compareTo(@Nonnull AbstractChangeRecord o) {
if (o instanceof UpdateChangeRecord) {
return -1;
}
if (o instanceof DeleteChangeRecord) {
return -1;
}
return super.compareTo(o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public int compareTo(@Nonnull AbstractChangeRecord o) {
if (o instanceof PersistChangeRecord) {
return 1;
}
if (o instanceof DeleteChangeRecord) {
return -1;
}
return super.compareTo(o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import cz.cvut.kbss.termit.dto.RecentlyCommentedAsset;
import cz.cvut.kbss.termit.event.AssetPersistEvent;
import cz.cvut.kbss.termit.event.AssetUpdateEvent;
import cz.cvut.kbss.termit.event.BeforeAssetDeleteEvent;
import cz.cvut.kbss.termit.exception.PersistenceException;
import cz.cvut.kbss.termit.model.Asset;
import cz.cvut.kbss.termit.model.User;
Expand All @@ -40,6 +41,7 @@
* Base DAO implementation for assets managed by the application.
*
* @param <T> Type of the asset
* @param <L> Type of the asset's label
*/
public abstract class BaseAssetDao<T extends Asset<?>> extends BaseDao<T> {

Expand All @@ -65,6 +67,12 @@ public T update(T entity) {
return super.update(entity);
}

@Override
public void remove(T entity) {
eventPublisher.publishEvent(new BeforeAssetDeleteEvent<T>(this, entity));
super.remove(entity);
}

/**
* Finds unique last commented assets.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import cz.cvut.kbss.termit.dto.Snapshot;
import cz.cvut.kbss.termit.event.AssetPersistEvent;
import cz.cvut.kbss.termit.event.AssetUpdateEvent;
import cz.cvut.kbss.termit.event.BeforeAssetDeleteEvent;
import cz.cvut.kbss.termit.event.RefreshLastModifiedEvent;
import cz.cvut.kbss.termit.event.VocabularyWillBeRemovedEvent;
import cz.cvut.kbss.termit.exception.PersistenceException;
Expand Down Expand Up @@ -228,6 +229,7 @@ public Vocabulary update(Vocabulary entity) {
@Override
public void remove(Vocabulary entity) {
eventPublisher.publishEvent(new VocabularyWillBeRemovedEvent(this, entity.getUri()));
eventPublisher.publishEvent(new BeforeAssetDeleteEvent<Vocabulary>(this, entity));
this.removeVocabulary(entity, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

import cz.cvut.kbss.termit.event.AssetPersistEvent;
import cz.cvut.kbss.termit.event.AssetUpdateEvent;
import cz.cvut.kbss.termit.event.BeforeAssetDeleteEvent;
import cz.cvut.kbss.termit.model.AbstractTerm;
import cz.cvut.kbss.termit.model.Asset;
import cz.cvut.kbss.termit.model.User;
import cz.cvut.kbss.termit.model.changetracking.AbstractChangeRecord;
import cz.cvut.kbss.termit.model.changetracking.DeleteChangeRecord;
import cz.cvut.kbss.termit.model.changetracking.PersistChangeRecord;
import cz.cvut.kbss.termit.model.changetracking.UpdateChangeRecord;
import cz.cvut.kbss.termit.model.resource.File;
Expand All @@ -37,6 +40,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.net.URI;
import java.time.Instant;
import java.util.Collection;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -114,4 +119,27 @@ public void onAssetPersistEvent(@Nonnull AssetPersistEvent event) {
changeRecord.setTimestamp(Utils.timestamp());
changeRecordDao.persist(changeRecord, added);
}

/**
* Records an asset deletion from the repository.
*
* @param event Event representing the asset deletion
*/
@Transactional
@EventListener
public <L extends Serializable, T extends Asset<L>> void onBeforeAssetDeleteEvent(@Nonnull BeforeAssetDeleteEvent<T> event) {
final T asset = event.getAsset();
LOG.trace("Recording deletion of asset {}.", asset);

URI vocabulary = null;
if (asset instanceof AbstractTerm term) {
vocabulary = term.getVocabulary();
}

final AbstractChangeRecord changeRecord = new DeleteChangeRecord<L>(asset, vocabulary);
changeRecord.setAuthor(securityUtils.getCurrentUser().toUser());
changeRecord.setTimestamp(Utils.timestamp());

changeRecordDao.persist(changeRecord, asset);
}
}
4 changes: 4 additions & 0 deletions src/test/resources/ontologies/popis-dat-model.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,7 @@ a-popis-dat-pojem:má-původní-hodnotu
a <https://slovník.gov.cz/základní/pojem/typ-vztahu> ;
rdfs:domain a-popis-dat-pojem:úprava-entity ;
rdfs:subPropertyOf <https://slovník.gov.cz/základní/pojem/vztah> .

a-popis-dat-pojem:smazání-entity
a <https://slovník.gov.cz/základní/pojem/typ-události>, owl:Class ;
rdfs:subClassOf a-popis-dat-pojem:změna .

0 comments on commit e612cde

Please sign in to comment.