From f38f7c951352a1e13c345603ed5f02f5832d277d Mon Sep 17 00:00:00 2001 From: soimugeo Date: Mon, 11 Nov 2024 11:51:20 +0200 Subject: [PATCH] Added ChangeType to ProjectChangeForEntity and RevisionsEvent --- .../uiHistoryConcern/dto/ChangeType.java | 7 +++++++ .../dto/ProjectChangeForEntity.java | 21 +++++++++++++++++-- .../events/RevisionsEvent.java | 9 ++++++-- .../mappers/RevisionEventMapper.java | 4 +++- .../NewRevisionsEventServiceImpl.java | 3 ++- ...toryViewCommandHandlerIntegrationTest.java | 3 ++- .../NewRevisionsEventServiceTest.java | 8 +++---- 7 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/dto/ChangeType.java diff --git a/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/dto/ChangeType.java b/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/dto/ChangeType.java new file mode 100644 index 0000000..802f3d7 --- /dev/null +++ b/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/dto/ChangeType.java @@ -0,0 +1,7 @@ +package edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.dto; + +public enum ChangeType { + CREATE_ENTITY, + DELETE_ENTITY, + UPDATE_ENTITY +} diff --git a/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/dto/ProjectChangeForEntity.java b/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/dto/ProjectChangeForEntity.java index 2d2f587..55fc729 100644 --- a/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/dto/ProjectChangeForEntity.java +++ b/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/dto/ProjectChangeForEntity.java @@ -2,11 +2,28 @@ import edu.stanford.protege.webprotege.change.ProjectChange; +import javax.annotation.Nonnull; + public record ProjectChangeForEntity(String whoficEntityIri, - ProjectChange projectChange) { + ChangeType changeType, + ProjectChange projectChange) implements Comparable { + + public static ProjectChangeForEntity create(String whoficEntityIri, + ChangeType changeType, + ProjectChange projectChange) { + return new ProjectChangeForEntity(whoficEntityIri, changeType, projectChange); + } + //All linearization/postcoordination changes are updates made on the whoficEntityIri. + // From this microservice we don't create or delete entities. that is the responsibility of the backend-service. public static ProjectChangeForEntity create(String whoficEntityIri, ProjectChange projectChange) { - return new ProjectChangeForEntity(whoficEntityIri, projectChange); + return new ProjectChangeForEntity(whoficEntityIri, ChangeType.UPDATE_ENTITY, projectChange); + } + + @Override + public int compareTo(@Nonnull ProjectChangeForEntity other) { + return Long.compare(this.projectChange.getTimestamp(), other.projectChange.getTimestamp()); + } } diff --git a/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/events/RevisionsEvent.java b/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/events/RevisionsEvent.java index 0bf16c9..c94aa64 100644 --- a/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/events/RevisionsEvent.java +++ b/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/events/RevisionsEvent.java @@ -2,6 +2,7 @@ import com.google.common.base.Objects; import edu.stanford.protege.webprotege.common.ProjectId; +import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.dto.ChangeType; import org.springframework.data.mongodb.core.index.*; import org.springframework.data.mongodb.core.mapping.Document; @@ -9,6 +10,7 @@ public record RevisionsEvent( ProjectId projectId, String whoficEntityIri, + ChangeType changeType, @Indexed(name = "timestamp", direction = IndexDirection.DESCENDING) long timestamp, org.bson.Document projectChange ) { @@ -16,13 +18,15 @@ public record RevisionsEvent( public static final String WHOFIC_ENTITY_IRI = "whoficEntityIri"; public static final String PROJECT_ID = "projectId"; public static final String TIMESTAMP = "timestamp"; + public static final String CHANGE_TYPE = "changeType"; public static final String PROJECT_CHANGE = "projectChange"; public static RevisionsEvent create(ProjectId projectId, String whoficEntityIri, + ChangeType changeType, long timestamp, org.bson.Document projectChange) { - return new RevisionsEvent(projectId, whoficEntityIri, timestamp, projectChange); + return new RevisionsEvent(projectId, whoficEntityIri, changeType, timestamp, projectChange); } @Override @@ -33,11 +37,12 @@ public boolean equals(Object o) { return timestamp == that.timestamp && Objects.equal(projectId, that.projectId) && Objects.equal(whoficEntityIri, that.whoficEntityIri) && + Objects.equal(changeType, that.changeType) && Objects.equal(projectChange, that.projectChange); } @Override public int hashCode() { - return Objects.hashCode(projectId, whoficEntityIri, timestamp, projectChange); + return Objects.hashCode(projectId, whoficEntityIri, changeType, timestamp, projectChange); } } diff --git a/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/mappers/RevisionEventMapper.java b/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/mappers/RevisionEventMapper.java index de09040..26c192e 100644 --- a/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/mappers/RevisionEventMapper.java +++ b/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/mappers/RevisionEventMapper.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import edu.stanford.protege.webprotege.change.ProjectChange; +import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.dto.ChangeType; import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.events.*; import org.bson.Document; import org.springframework.stereotype.Component; @@ -23,11 +24,12 @@ public List mapNewRevisionsEventToRevisionsEvents(NewRevisionsEv List revisionsEvents = newRevisionsEvent.changes().stream() .flatMap(projectChangeForEntity -> { String whoficIri = projectChangeForEntity.whoficEntityIri(); + ChangeType changeType = projectChangeForEntity.changeType(); ProjectChange projectChange = projectChangeForEntity.projectChange(); long timestamp = projectChange.getTimestamp(); var projectChangeDocument = objectMapper.convertValue(projectChange, Document.class); - return Stream.of(RevisionsEvent.create(newRevisionsEvent.projectId(), whoficIri, timestamp, projectChangeDocument)); + return Stream.of(RevisionsEvent.create(newRevisionsEvent.projectId(), whoficIri, changeType, timestamp, projectChangeDocument)); }) .toList(); diff --git a/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/services/NewRevisionsEventServiceImpl.java b/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/services/NewRevisionsEventServiceImpl.java index 89c7a95..fadcdd1 100644 --- a/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/services/NewRevisionsEventServiceImpl.java +++ b/src/main/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/services/NewRevisionsEventServiceImpl.java @@ -3,7 +3,6 @@ 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; @@ -45,6 +44,7 @@ public Page fetchPaginatedProjectChanges(ProjectId projectId, Opt RevisionsEvent probe = new RevisionsEvent( projectId, entityIriSubject, + null, 0, null ); @@ -52,6 +52,7 @@ public Page fetchPaginatedProjectChanges(ProjectId projectId, Opt .withMatcher(PROJECT_ID, ExampleMatcher.GenericPropertyMatchers.exact()) .withIgnorePaths(TIMESTAMP) .withMatcher(WHOFIC_ENTITY_IRI, ExampleMatcher.GenericPropertyMatchers.exact()) + .withIgnorePaths(CHANGE_TYPE) .withIgnoreNullValues(); Example example = Example.of(probe, matcher); diff --git a/src/test/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/handlers/GetProjectChangesForHistoryViewCommandHandlerIntegrationTest.java b/src/test/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/handlers/GetProjectChangesForHistoryViewCommandHandlerIntegrationTest.java index 80a93fa..826d70a 100644 --- a/src/test/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/handlers/GetProjectChangesForHistoryViewCommandHandlerIntegrationTest.java +++ b/src/test/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/handlers/GetProjectChangesForHistoryViewCommandHandlerIntegrationTest.java @@ -5,6 +5,7 @@ import edu.stanford.protege.webprotege.common.*; import edu.stanford.protege.webprotege.revision.RevisionNumber; import edu.stanford.protege.webprotegeeventshistory.*; +import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.dto.ChangeType; import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.events.RevisionsEvent; import org.bson.Document; import org.junit.jupiter.api.*; @@ -132,7 +133,7 @@ private OWLEntity mockOWLEntity(String iri) { private ProjectChange insertMockRevisionsEvent(ProjectId projectId, String whoficEntityIri, long timestamp) { ProjectChange projectChange = ProjectChange.get(RevisionNumber.getRevisionNumber(1), UserId.valueOf("user1"), timestamp, "Description1", 0, Page.emptyPage()); org.bson.Document projectChangeDocument = objectMapper.convertValue(projectChange, Document.class); - RevisionsEvent revisionsEvent = RevisionsEvent.create(projectId, whoficEntityIri, timestamp, projectChangeDocument); + RevisionsEvent revisionsEvent = RevisionsEvent.create(projectId, whoficEntityIri, ChangeType.UPDATE_ENTITY, timestamp, projectChangeDocument); mongoTemplate.save(revisionsEvent); return projectChange; diff --git a/src/test/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/services/NewRevisionsEventServiceTest.java b/src/test/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/services/NewRevisionsEventServiceTest.java index 072c20d..40b885d 100644 --- a/src/test/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/services/NewRevisionsEventServiceTest.java +++ b/src/test/java/edu/stanford/protege/webprotegeeventshistory/uiHistoryConcern/services/NewRevisionsEventServiceTest.java @@ -3,7 +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.uiHistoryConcern.dto.ProjectChangeForEntity; +import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.dto.*; import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.events.*; import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.mappers.*; import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.repositories.RevisionsEventRepository; @@ -43,7 +43,7 @@ public void GIVEN_validNewLinearizationRevisionsEvent_WHEN_registerEventCalled_T Set changes = Set.of(mock(ProjectChangeForEntity.class)); NewRevisionsEvent event = NewRevisionsEvent.create(EventId.generate(), projectId, changes); - RevisionsEvent mockRevisionsEvent = RevisionsEvent.create(projectId, "whoficEntityIri", 12345L, new Document()); + RevisionsEvent mockRevisionsEvent = RevisionsEvent.create(projectId, "whoficEntityIri", ChangeType.UPDATE_ENTITY, 12345L, new Document()); when(revisionEventMapper.mapNewRevisionsEventToRevisionsEvents(event)) .thenReturn(List.of(mockRevisionsEvent)); @@ -60,7 +60,7 @@ public void GIVEN_validProjectIdAndSubject_WHEN_fetchPaginatedProjectChangesCall IRI mockIri = IRI.create("http://example.com/entity"); when(mockEntity.getIRI()).thenReturn(mockIri); - RevisionsEvent mockRevisionsEvent = RevisionsEvent.create(projectId, mockIri.toString(), 12345L, new Document()); + RevisionsEvent mockRevisionsEvent = RevisionsEvent.create(projectId, mockIri.toString(), ChangeType.UPDATE_ENTITY,12345L, new Document()); PageRequest pageRequest = PageRequest.of(0, 1, Sort.by(Sort.Direction.DESC, "timestamp")); org.springframework.data.domain.Page mockPage = new PageImpl<>(List.of(mockRevisionsEvent), pageRequest, 1); @@ -83,7 +83,7 @@ public void GIVEN_validProjectIdAndSubject_WHEN_fetchPaginatedProjectChangesCall public void GIVEN_nullSubject_WHEN_fetchPaginatedProjectChangesCalled_THEN_returnPaginatedProjectChanges() { ProjectId projectId = new ProjectId("testProjectId"); - RevisionsEvent mockRevisionsEvent = RevisionsEvent.create(projectId, null, 12345L, new Document()); + RevisionsEvent mockRevisionsEvent = RevisionsEvent.create(projectId, null, ChangeType.CREATE_ENTITY,12345L, new Document()); PageRequest pageRequest = PageRequest.of(0, 1, Sort.by(Sort.Direction.DESC, "timestamp")); org.springframework.data.domain.Page mockPage = new PageImpl<>(List.of(mockRevisionsEvent), pageRequest, 1);