Skip to content

Commit

Permalink
Set entity from api adding transactionality (#52)
Browse files Browse the repository at this point in the history
add commit/rollback mechanism. Add ChangeRequestId to Revision Events for rollback there as well.
  • Loading branch information
alexsilaghi authored Dec 11, 2024
1 parent fe81e02 commit 121edf6
Show file tree
Hide file tree
Showing 29 changed files with 423 additions and 79 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-ipc</artifactId>
<version>1.0.5</version>
<version>1.0.9</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import edu.stanford.protege.webprotege.common.Response;
import edu.stanford.protege.webprotege.postcoordinationservice.model.WhoficCustomScalesValues;

import java.util.Date;

import static edu.stanford.protege.webprotege.postcoordinationservice.dto.GetEntityCustomScaleValuesRequest.CHANNEL;


@JsonTypeName(CHANNEL)
public record GetEntityCustomScaleValueResponse(
@JsonProperty("lastRevisionDate") Date lastRevisionDate,
@JsonProperty("whoficCustomScaleValues") WhoficCustomScalesValues whoficCustomScalesValues
) implements Response {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
import edu.stanford.protege.webprotege.common.Response;
import edu.stanford.protege.webprotege.postcoordinationservice.model.WhoficEntityPostCoordinationSpecification;

import java.util.Date;

import static edu.stanford.protege.webprotege.postcoordinationservice.dto.GetEntityPostCoordinationRequest.CHANNEL;

@JsonTypeName(CHANNEL)
public record GetEntityPostCoordinationResponse(@JsonProperty("entityIri")
String entityIri,

@JsonProperty("lastRevisionDate")
Date lastRevisionDate,

@JsonProperty("postCoordinationSpecification")
WhoficEntityPostCoordinationSpecification postCoordinationSpecification) implements Response {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package edu.stanford.protege.webprotege.postcoordinationservice.events;

import com.fasterxml.jackson.annotation.JsonProperty;
import edu.stanford.protege.webprotege.common.ChangeRequestId;
import edu.stanford.protege.webprotege.common.EventId;
import edu.stanford.protege.webprotege.common.ProjectEvent;
import edu.stanford.protege.webprotege.common.ProjectId;

import javax.annotation.Nonnull;

public record EntityUpdateFailedEvent(@JsonProperty("projectId") ProjectId projectId,
@JsonProperty("eventId") EventId eventId,

@JsonProperty("entityIri") String entityIri,
@JsonProperty("changeRequestId")ChangeRequestId changeRequestId) implements ProjectEvent {

public static String CHANNEL = "webprotege.api.EntityUpdateFailed";


@Nonnull
@Override
public ProjectId projectId() {
return projectId;
}

@Nonnull
@Override
public EventId eventId() {
return eventId;
}

@Override
public String getChannel() {
return CHANNEL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package edu.stanford.protege.webprotege.postcoordinationservice.events;

import com.fasterxml.jackson.annotation.JsonProperty;
import edu.stanford.protege.webprotege.common.ChangeRequestId;
import edu.stanford.protege.webprotege.common.EventId;
import edu.stanford.protege.webprotege.common.ProjectEvent;
import edu.stanford.protege.webprotege.common.ProjectId;

import javax.annotation.Nonnull;

public record EntityUpdatedSuccessfullyEvent(@JsonProperty("projectId") ProjectId projectId,
@JsonProperty("eventId") EventId eventId,

@JsonProperty("entityIri") String entityIri,
@JsonProperty("changeRequestId") ChangeRequestId changeRequestId) implements ProjectEvent {

public static String CHANNEL = "webprotege.api.EntityUpdatedSuccessfully";


@Nonnull
@Override
public ProjectId projectId() {
return projectId;
}

@Nonnull
@Override
public EventId eventId() {
return eventId;
}

@Override
public String getChannel() {
return CHANNEL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Class<AddEntityCustomScalesRevisionRequest> getRequestClass() {
@Override
public Mono<AddEntityCustomScalesRevisionResponse> handleRequest(AddEntityCustomScalesRevisionRequest request, ExecutionContext executionContext) {

postCoordService.addCustomScaleRevision(request.entityCustomScaleValues(), request.projectId(), executionContext.userId());
postCoordService.addCustomScaleRevision(request.entityCustomScaleValues(), request.projectId(), executionContext.userId(), request.changeRequestId());

return Mono.just(new AddEntityCustomScalesRevisionResponse());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@
import edu.stanford.protege.webprotege.common.*;
import edu.stanford.protege.webprotege.postcoordinationservice.model.WhoficCustomScalesValues;

import javax.annotation.Nullable;

import static edu.stanford.protege.webprotege.postcoordinationservice.handlers.AddEntityCustomScalesRevisionRequest.CHANNEL;

@JsonTypeName(CHANNEL)
public record AddEntityCustomScalesRevisionRequest(@JsonProperty("projectId")
ProjectId projectId,
@JsonProperty("entityCustomScaleValues")
WhoficCustomScalesValues entityCustomScaleValues) implements Request<AddEntityCustomScalesRevisionResponse> {
WhoficCustomScalesValues entityCustomScaleValues,
@JsonProperty("changeRequestId") @Nullable ChangeRequestId changeRequestId) implements Request<AddEntityCustomScalesRevisionResponse> {

public final static String CHANNEL = "webprotege.postcoordination.AddEntityCustomScalesRevision";

@JsonCreator
public static AddEntityCustomScalesRevisionRequest create(@JsonProperty("projectId")
ProjectId projectId,
@JsonProperty("entityCustomScaleValues")
WhoficCustomScalesValues entityCustomScaleValues) {
return new AddEntityCustomScalesRevisionRequest(projectId, entityCustomScaleValues);
WhoficCustomScalesValues entityCustomScaleValues,
@JsonProperty("changeRequestId") @Nullable ChangeRequestId changeRequestId) {
return new AddEntityCustomScalesRevisionRequest(projectId, entityCustomScaleValues, changeRequestId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Class<AddEntitySpecificationRevisionRequest> getRequestClass() {

@Override
public Mono<AddEntitySpecificationRevisionResponse> handleRequest(AddEntitySpecificationRevisionRequest request, ExecutionContext executionContext) {
postCoordService.addSpecificationRevision(request.entitySpecification(), executionContext.userId(), request.projectId());
postCoordService.addSpecificationRevision(request.entitySpecification(), executionContext.userId(), request.projectId(), request.changeRequestId());
return Mono.just(new AddEntitySpecificationRevisionResponse());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import edu.stanford.protege.webprotege.common.ChangeRequestId;
import edu.stanford.protege.webprotege.common.ProjectId;
import edu.stanford.protege.webprotege.common.Request;
import edu.stanford.protege.webprotege.postcoordinationservice.model.WhoficEntityPostCoordinationSpecification;

import javax.annotation.Nullable;

import static edu.stanford.protege.webprotege.postcoordinationservice.handlers.AddEntitySpecificationRevisionRequest.CHANNEL;

@JsonTypeName(CHANNEL)
public record AddEntitySpecificationRevisionRequest(@JsonProperty("projectId")
ProjectId projectId,
@JsonProperty("entitySpecification")
WhoficEntityPostCoordinationSpecification entitySpecification) implements Request<AddEntitySpecificationRevisionResponse> {
WhoficEntityPostCoordinationSpecification entitySpecification,
@JsonProperty("changeRequestId") @Nullable ChangeRequestId changeRequestId) implements Request<AddEntitySpecificationRevisionResponse> {

public final static String CHANNEL = "webprotege.postcoordination.AddEntitySpecificationRevision";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edu.stanford.protege.webprotege.postcoordinationservice.handlers;


import edu.stanford.protege.webprotege.ipc.EventHandler;
import edu.stanford.protege.webprotege.postcoordinationservice.events.EntityUpdatedSuccessfullyEvent;
import edu.stanford.protege.webprotege.postcoordinationservice.services.RevisionCommitService;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

@Component
public class EntityUpdatedSuccessfullyHandler implements EventHandler<EntityUpdatedSuccessfullyEvent> {


private final RevisionCommitService revisionCommitService;

public EntityUpdatedSuccessfullyHandler(RevisionCommitService revisionCommitService) {
this.revisionCommitService = revisionCommitService;
}

@NotNull
@Override
public String getChannelName() {
return EntityUpdatedSuccessfullyEvent.CHANNEL;
}

@NotNull
@Override
public String getHandlerName() {
return this.getClass().getName();
}

@Override
public Class<EntityUpdatedSuccessfullyEvent> getEventClass() {
return EntityUpdatedSuccessfullyEvent.class;
}

@Override
public void handleEvent(EntityUpdatedSuccessfullyEvent event) {
revisionCommitService.commitRevision(event.changeRequestId(), event.projectId(), event.entityIri());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.stanford.protege.webprotege.postcoordinationservice.handlers;


import edu.stanford.protege.webprotege.ipc.EventHandler;
import edu.stanford.protege.webprotege.postcoordinationservice.events.EntityUpdateFailedEvent;
import edu.stanford.protege.webprotege.postcoordinationservice.services.RevisionCommitService;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

@Component
public class EventUpdateFailedHandler implements EventHandler<EntityUpdateFailedEvent> {

private final RevisionCommitService revisionCommitService;

public EventUpdateFailedHandler(RevisionCommitService revisionCommitService) {
this.revisionCommitService = revisionCommitService;
}

@NotNull
@Override
public String getChannelName() {
return EntityUpdateFailedEvent.CHANNEL;
}

@NotNull
@Override
public String getHandlerName() {
return EventUpdateFailedHandler.class.getName();
}

@Override
public Class<EntityUpdateFailedEvent> getEventClass() {
return EntityUpdateFailedEvent.class;
}

@Override
public void handleEvent(EntityUpdateFailedEvent event) {
revisionCommitService.rollbackRevision(event.changeRequestId(), event.projectId(), event.entityIri());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public Class<GetEntityCustomScaleValuesRequest> getRequestClass() {

@Override
public Mono<GetEntityCustomScaleValueResponse> handleRequest(GetEntityCustomScaleValuesRequest request, ExecutionContext executionContext) {
WhoficCustomScalesValues processedScales = postCoordService.fetchCustomScalesHistory(request.entityIRI(), request.projectId());
GetEntityCustomScaleValueResponse response = postCoordService.fetchCustomScalesHistory(request.entityIRI(), request.projectId());

return Mono.just(new GetEntityCustomScaleValueResponse(processedScales));
return Mono.just(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public Class<GetEntityPostCoordinationRequest> getRequestClass() {
@Override
public Mono<GetEntityPostCoordinationResponse> handleRequest(GetEntityPostCoordinationRequest request, ExecutionContext executionContext) {

WhoficEntityPostCoordinationSpecification processedSpec = postCoordService.fetchHistory(request.entityIRI(), request.projectId());
GetEntityPostCoordinationResponse processedSpec = postCoordService.fetchHistory(request.entityIRI(), request.projectId());

return Mono.just(new GetEntityPostCoordinationResponse(request.entityIRI(), processedSpec));
return Mono.just(processedSpec);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package edu.stanford.protege.webprotege.postcoordinationservice.model;

public enum CommitStatus {
COMMITTED,
UNCOMMITTED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


import com.fasterxml.jackson.annotation.*;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.*;

Expand All @@ -11,6 +13,9 @@

@JsonIgnoreProperties(ignoreUnknown = true)
@Document(collection = POSTCOORDINATION_CUSTOM_SCALES_COLLECTION)
@CompoundIndexes({
@CompoundIndex(name = "entity_iri_project_idx", def = "{'" + EntityCustomScalesValuesHistory.WHOFIC_ENTITY_IRI + "': 1, '" + EntityCustomScalesValuesHistory.PROJECT_ID + "': 1}")
})
public class EntityCustomScalesValuesHistory {

public static final String POSTCOORDINATION_CUSTOM_SCALES_COLLECTION = "EntityPostCoordinationCustomScales";
Expand All @@ -19,10 +24,11 @@ public class EntityCustomScalesValuesHistory {
public static final String PROJECT_ID = "projectId";
public static final String CUSTOM_SCALE_REVISIONS = "postCoordinationCustomScalesRevisions";
@Field(WHOFIC_ENTITY_IRI)
@Indexed(name = "entityIriScales")
@Indexed(name = "entityIriScales_idx")
private final String whoficEntityIri;

@Field(PROJECT_ID)
@Indexed(name = "entityProjectId_idx")
private final String projectId;

@Field(CUSTOM_SCALE_REVISIONS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package edu.stanford.protege.webprotege.postcoordinationservice.model;

import com.fasterxml.jackson.annotation.*;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.*;

import java.util.*;

@JsonIgnoreProperties(ignoreUnknown = true)
@Document(collection = EntityPostCoordinationHistory.POSTCOORDINATION_HISTORY_COLLECTION)
@CompoundIndexes({
@CompoundIndex(name = "entity_iri_project_idx", def = "{'" + EntityPostCoordinationHistory.WHOFIC_ENTITY_IRI + "': 1, '" + EntityPostCoordinationHistory.PROJECT_ID + "': 1}")
})
public class EntityPostCoordinationHistory {


Expand All @@ -18,10 +23,11 @@ public class EntityPostCoordinationHistory {
public static final String SPEC_REVISIONS = "postCoordinationRevisions";

@Field(WHOFIC_ENTITY_IRI)
@Indexed(name = "entityIriSpec")
@Indexed(name = "entityIriSpec_idx")
private final String whoficEntityIri;

@Field(PROJECT_ID)
@Indexed(name = "entityIriProjectId_idx")
private final String projectId;

@Field(SPEC_REVISIONS)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.stanford.protege.webprotege.postcoordinationservice.model;

import com.google.common.base.Objects;
import edu.stanford.protege.webprotege.common.ChangeRequestId;
import edu.stanford.protege.webprotege.common.UserId;
import edu.stanford.protege.webprotege.postcoordinationservice.events.PostCoordinationCustomScalesValueEvent;
import org.jetbrains.annotations.NotNull;
Expand All @@ -11,11 +12,22 @@

public record PostCoordinationCustomScalesRevision(UserId userId,
@Indexed(name = "rev_timestamp", direction = IndexDirection.DESCENDING) Long timestamp,
Set<PostCoordinationCustomScalesValueEvent> postCoordinationEvents) implements Comparable<PostCoordinationCustomScalesRevision> {
Set<PostCoordinationCustomScalesValueEvent> postCoordinationEvents,
CommitStatus commitStatus,
String changeRequestId) implements Comparable<PostCoordinationCustomScalesRevision> {


public static PostCoordinationCustomScalesRevision create(UserId userId, Set<PostCoordinationCustomScalesValueEvent> postCoordinationEventList) {
return new PostCoordinationCustomScalesRevision(userId, Instant.now().toEpochMilli(), postCoordinationEventList);
return create(userId, postCoordinationEventList, null);
}

public static PostCoordinationCustomScalesRevision create(UserId userId, Set<PostCoordinationCustomScalesValueEvent> postCoordinationEventList, ChangeRequestId changeRequestId) {
CommitStatus status = changeRequestId != null && changeRequestId.id() != null ? CommitStatus.UNCOMMITTED : CommitStatus.COMMITTED;
return new PostCoordinationCustomScalesRevision(userId, Instant.now().toEpochMilli(), postCoordinationEventList, status, changeRequestId != null ? changeRequestId.id() : null);
}

public static PostCoordinationCustomScalesRevision createCommittedClone(PostCoordinationCustomScalesRevision revision) {
return new PostCoordinationCustomScalesRevision(revision.userId(), revision.timestamp(), revision.postCoordinationEvents(), CommitStatus.COMMITTED, revision.changeRequestId());
}

@Override
Expand Down
Loading

0 comments on commit 121edf6

Please sign in to comment.