Skip to content

Commit

Permalink
Merge pull request #10 from protegeproject/39-create-uihistoryconcern…
Browse files Browse the repository at this point in the history
…-package-for-postcoordination-similar-to-the-one-in-linearization-service

work for ui history
  • Loading branch information
alexsilaghi authored Oct 14, 2024
2 parents 8ed31b8 + a599688 commit 5aa388f
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 246 deletions.

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package edu.stanford.protege.webprotegeeventshistory.config.events;

import com.fasterxml.jackson.annotation.*;
import com.google.common.base.Objects;
import edu.stanford.protege.webprotege.common.*;

import java.util.Set;

@JsonTypeName(UpdateUiHistoryEvent.CHANNEL)
public record UpdateUiHistoryEvent(EventId eventId,
ProjectId projectId,
Set<String> affectedEntityIris) implements ProjectEvent {

@JsonCreator
public static UpdateUiHistoryEvent create(@JsonProperty("eventId") EventId eventId,
@JsonProperty("projectId") ProjectId projectId,
@JsonProperty("afectedEntityIris") Set<String> afectedEntityIris
) {
return new UpdateUiHistoryEvent(eventId, projectId, afectedEntityIris);
}

public static final String CHANNEL = "webprotege.events.projects.uiHistory.UpdateUiHistoryEvent";

@JsonProperty("projectId")
public ProjectId projectId() {
return this.projectId;
}

@Override
@JsonProperty("eventId")
public EventId eventId() {
return eventId;
}

@Override
@JsonProperty("afectedEntityIris")
public Set<String> affectedEntityIris() {
return affectedEntityIris;
}

@Override
public String getChannel() {
return CHANNEL;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UpdateUiHistoryEvent that = (UpdateUiHistoryEvent) o;
return Objects.equal(eventId, that.eventId) && Objects.equal(projectId, that.projectId) && Objects.equal(affectedEntityIris, that.affectedEntityIris);
}

@Override
public int hashCode() {
return Objects.hashCode(eventId, projectId, affectedEntityIris);
}

@Override
public String toString() {
return "UpdateUiHistoryEvent{" +
"eventId=" + eventId +
", projectId=" + projectId +
", subjects=" + affectedEntityIris +
'}';
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.events;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.*;
import edu.stanford.protege.webprotege.common.*;
import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.dto.ProjectChangeForEntity;
import jakarta.validation.constraints.NotNull;

import java.util.*;
import java.util.Set;

import static edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.events.NewRevisionsEvent.CHANNEL;

Expand All @@ -18,23 +17,27 @@ public record NewRevisionsEvent(
) implements ProjectEvent {
public final static String CHANNEL = "webprotege.events.projects.uiHistory.NewRevisionsEvent";

public static NewRevisionsEvent create(EventId eventId,
ProjectId projectId,
Set<ProjectChangeForEntity> changes) {
@JsonCreator
public static NewRevisionsEvent create(@JsonProperty("eventId") EventId eventId,
@JsonProperty("projectId") ProjectId projectId,
@JsonProperty("changes") Set<ProjectChangeForEntity> changes) {
return new NewRevisionsEvent(eventId, projectId, changes);
}

@Override
@JsonProperty("eventId")
public ProjectId projectId() {
return projectId;
}

@Override
@JsonProperty("projectId")
public EventId eventId() {
return eventId;
}

public Set<ProjectChangeForEntity> getProjectChanges() {
@JsonProperty("changes")
public Set<ProjectChangeForEntity> changes() {
return changes;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.handlers;

import edu.stanford.protege.webprotege.common.EventId;
import edu.stanford.protege.webprotege.ipc.EventHandler;
import edu.stanford.protege.webprotegeeventshistory.config.events.UpdateUiHistoryEvent;
import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.events.NewRevisionsEvent;
import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.services.NewRevisionsEventService;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.*;

@Component
public class NewRevisionsEventHandler implements EventHandler<NewRevisionsEvent> {

private final NewRevisionsEventService linRevisionsEventService;
private final NewRevisionsEventService newRevisionsEventService;
private final ApplicationEventPublisher eventPublisher;

public NewRevisionsEventHandler(NewRevisionsEventService linRevisionsEventService) {
this.linRevisionsEventService = linRevisionsEventService;
public NewRevisionsEventHandler(NewRevisionsEventService newRevisionsEventService,
ApplicationEventPublisher eventPublisher) {
this.newRevisionsEventService = newRevisionsEventService;
this.eventPublisher = eventPublisher;
}

@Nonnull
Expand All @@ -35,6 +42,12 @@ public Class<NewRevisionsEvent> getEventClass() {

@Override
public void handleEvent(NewRevisionsEvent event) {
linRevisionsEventService.registerEvent(event);
newRevisionsEventService.registerEvent(event);
Set<String> entitySubjects = new HashSet<>();
event.changes().forEach(change -> {
entitySubjects.add(change.whoficEntityIri());
});

eventPublisher.publishEvent(UpdateUiHistoryEvent.create(EventId.generate(), event.projectId(), entitySubjects));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.stanford.protege.webprotege.change.ProjectChange;
import edu.stanford.protege.webprotege.common.Page;
import edu.stanford.protege.webprotege.common.*;
import edu.stanford.protege.webprotegeeventshistory.config.events.UpdateUiHistoryEvent;
import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.events.*;
import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.mappers.*;
import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.repositories.RevisionsEventRepository;
Expand Down Expand Up @@ -32,8 +33,8 @@ public NewRevisionsEventServiceImpl(RevisionsEventRepository repository,
}

@Override
public void registerEvent(NewRevisionsEvent newLinRevEvent) {
List<RevisionsEvent> revisionsEvents = revisionEventMapper.mapNewRevisionsEventToRevisionsEvents(newLinRevEvent);
public void registerEvent(NewRevisionsEvent newRevEvent) {
List<RevisionsEvent> revisionsEvents = revisionEventMapper.mapNewRevisionsEventToRevisionsEvents(newRevEvent);

repository.saveAll(revisionsEvents);
}
Expand All @@ -60,15 +61,16 @@ public Page<ProjectChange> fetchPaginatedProjectChanges(ProjectId projectId, Opt
Pageable pageable = PageRequest.of(pageNumber - 1, pageSize, Sort.by(Sort.Direction.DESC, TIMESTAMP));

org.springframework.data.domain.Page<RevisionsEvent> revisionsEventPage = repository.findAll(example, pageable);
if (revisionsEventPage.getTotalElements() == 0) {
return Page.emptyPage();
}

List<ProjectChange> changes = revisionsEventPage.get()
.map(revisionsEvent -> projectChangeMapper.mapProjectChangeDocumentToProjectChange(revisionsEvent.projectChange()))
.toList();

int pageCount = revisionsEventPage.getTotalPages() == 0 ? 1 : revisionsEventPage.getTotalPages();

//Page number from ui is starting from 1
//PageRequest from spring-data is starting from 0
return Page.create(pageNumber, pageCount, changes, revisionsEventPage.getTotalElements());
return Page.create(pageNumber, revisionsEventPage.getTotalPages(), changes, revisionsEventPage.getTotalElements());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.services;

import edu.stanford.protege.webprotege.common.*;

public interface ProjectChangeDispatcher {
void dispatchEvent(ProjectId projectId, ProjectEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.services;

import edu.stanford.protege.webprotege.common.*;
import edu.stanford.protege.webprotege.ipc.EventDispatcher;
import edu.stanford.protege.webprotegeeventshistory.dto.PackagedProjectChangeEvent;
import org.springframework.stereotype.*;

import java.util.List;

@Component
public class ProjectChangeDispatcherImpl implements ProjectChangeDispatcher {

private final EventDispatcher eventDispatcher;

public ProjectChangeDispatcherImpl(EventDispatcher eventDispatcher) {
this.eventDispatcher = eventDispatcher;
}

@Override
public void dispatchEvent(ProjectId projectId, ProjectEvent event) {
var packagedProjectChange = new PackagedProjectChangeEvent(projectId, EventId.generate(), List.of(event));
eventDispatcher.dispatchEvent(packagedProjectChange);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.services;

import edu.stanford.protege.webprotege.common.*;

public interface ProjectChangeService {
void emitProjectChange(ProjectId projectId, ProjectEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.services;

import edu.stanford.protege.webprotege.common.*;
import edu.stanford.protege.webprotegeeventshistory.config.events.UpdateUiHistoryEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class ProjectChangeServiceImpl implements ProjectChangeService {

private final ProjectChangeDispatcher changeDispatcher;

public ProjectChangeServiceImpl(ProjectChangeDispatcher changeDispatcher) {
this.changeDispatcher = changeDispatcher;
}

@Async
@EventListener
public void handleUiHistoryEvent(UpdateUiHistoryEvent event) {
// Emit the project change when the event is handled
emitProjectChange(event.projectId(), UpdateUiHistoryEvent.create(EventId.generate(), event.projectId(), event.affectedEntityIris()));
}

@Override
public void emitProjectChange(ProjectId projectId, ProjectEvent event) {
this.changeDispatcher.dispatchEvent(projectId, event);
}
}

0 comments on commit 5aa388f

Please sign in to comment.