Skip to content

Commit

Permalink
[Enhancement kbss-cvut/termit-ui#530] Remove generics for DeleteChang…
Browse files Browse the repository at this point in the history
…eRecord

Using type capture instead.
  • Loading branch information
lukaskabc committed Oct 27, 2024
1 parent 67ae869 commit c3e096d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
/**
* Event published before an asset is deleted.
*/
public class BeforeAssetDeleteEvent<T extends Asset<?>> extends ApplicationEvent {
final T asset;
public BeforeAssetDeleteEvent(Object source, T asset) {
public class BeforeAssetDeleteEvent extends ApplicationEvent {
final Asset<?> asset;
public BeforeAssetDeleteEvent(Object source, Asset<?> asset) {
super(source);
this.asset = asset;
}

public T getAsset() {
public Asset<?> getAsset() {
return asset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,58 @@

import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
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.jopa.vocabulary.RDFS;
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 {
@OWLClass(iri = Vocabulary.s_c_smazani_entity)
public class DeleteChangeRecord extends AbstractChangeRecord {
@ParticipationConstraints(nonEmpty = true)
@OWLAnnotationProperty(iri = DC.Terms.TITLE)
private T label;
@OWLAnnotationProperty(iri = RDFS.LABEL)
private MultilingualString label;

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

public DeleteChangeRecord(Asset<T> changedEntity, URI vocabulary) {
/**
* Creates a new instance.
* @param changedEntity the changed asset
* @param vocabulary optional vocabulary URI
* @throws IllegalArgumentException If the label type is not String or MultilingualString
*/
public DeleteChangeRecord(Asset<?> changedEntity, URI vocabulary) {
super(changedEntity);
this.label = changedEntity.getLabel();

if (changedEntity.getLabel() instanceof String stringLabel) {
this.label = MultilingualString.create(stringLabel, null);
} else if (changedEntity.getLabel() instanceof MultilingualString multilingualLabel) {
this.label = multilingualLabel;
} else {
throw new IllegalArgumentException("Unsupported label type: " + changedEntity.getLabel().getClass());
}

this.vocabulary = vocabulary;
}

public T getLabel() {
public DeleteChangeRecord() {
super();
}

public MultilingualString getLabel() {
return label;
}

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

Expand All @@ -53,7 +70,7 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof DeleteChangeRecord<?> that)) {
if (!(o instanceof DeleteChangeRecord that)) {
return false;
}
if (!super.equals(o)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
* 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 Down Expand Up @@ -69,7 +68,7 @@ public T update(T entity) {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import static cz.cvut.kbss.termit.util.Constants.DEFAULT_PAGE_SIZE;
import static cz.cvut.kbss.termit.util.Constants.SKOS_CONCEPT_MATCH_RELATIONSHIPS;
Expand Down Expand Up @@ -229,7 +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));
eventPublisher.publishEvent(new BeforeAssetDeleteEvent(this, entity));
this.removeVocabulary(entity, true);
}

Expand Down Expand Up @@ -386,11 +386,13 @@ public List<AggregatedChangeInfo> getChangesOfContent(Vocabulary vocabulary) {
.setParameter("type", URI.create(
cz.cvut.kbss.termit.util.Vocabulary.s_c_uprava_entity)).getResultList();
updates.forEach(u -> u.addType(cz.cvut.kbss.termit.util.Vocabulary.s_c_uprava_entity));
final List<AggregatedChangeInfo> result = new ArrayList<>(persists.size() + updates.size());
result.addAll(persists);
result.addAll(updates);
Collections.sort(result);
return result;
final List<AggregatedChangeInfo> deletitions = createContentChangesQuery(vocabulary)
.setParameter("type", URI.create(cz.cvut.kbss.termit.util.Vocabulary.s_c_smazani_entity)).getResultList();
deletitions.forEach(d -> d.addType(cz.cvut.kbss.termit.util.Vocabulary.s_c_smazani_entity));
return Stream.of(persists, updates, deletitions)
.flatMap(List::stream)
.sorted()
.toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
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;
Expand Down Expand Up @@ -127,16 +126,16 @@ public void onAssetPersistEvent(@Nonnull AssetPersistEvent event) {
*/
@Transactional
@EventListener
public <L extends Serializable, T extends Asset<L>> void onBeforeAssetDeleteEvent(@Nonnull BeforeAssetDeleteEvent<T> event) {
final T asset = event.getAsset();
public void onBeforeAssetDeleteEvent(@Nonnull BeforeAssetDeleteEvent event) {
final Asset<?> 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);
final AbstractChangeRecord changeRecord = new DeleteChangeRecord(asset, vocabulary);
changeRecord.setAuthor(securityUtils.getCurrentUser().toUser());
changeRecord.setTimestamp(Utils.timestamp());

Expand Down

0 comments on commit c3e096d

Please sign in to comment.