Skip to content

Commit

Permalink
saving project revisions . add revert mechanism on fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsilaghi committed Nov 27, 2024
1 parent d94acb3 commit b873b8e
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-ipc</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ ProjectDetailsRepository getProjectDetailsRepository(MongoTemplate mongoTemplate
return new ProjectDetailsRepository(mongoTemplate, objectMapper);
}

@Singleton
@Bean
ProjectRevisionRepository projectRevisionRepository(MongoTemplate mongoTemplate, ObjectMapper objectMapper) {
return new ProjectRevisionRepository(mongoTemplate, objectMapper);
}

@Bean
@Singleton
SlackWebhookRepository getSlackWebhookRepository(MongoTemplate mongoTemplate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,6 @@ RevertRevisionActionHandler revertRevisionActionHandler(AccessManager p1,
return new RevertRevisionActionHandler(p1, p3, p4, p5);
}


@Bean
GetPerspectiveLayoutActionHandler getPerspectiveLayoutActionHandler(PerspectivesManager p1) {
return new GetPerspectiveLayoutActionHandler(p1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,8 @@ ChangeManager changeManager(ProjectId p1,
IriReplacerFactory p25,
GeneratedAnnotationsGenerator p26,
EventDispatcher p27,
NewRevisionsEventEmitterService p28) {
NewRevisionsEventEmitterService p28,
ProjectRevisionRepository p29) {
return new ChangeManager(p1,
p2,
p3,
Expand All @@ -671,7 +672,8 @@ ChangeManager changeManager(ProjectId p1,
p25,
p26,
p27,
p28);
p28,
p29);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package edu.stanford.protege.webprotege.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,62 @@
package edu.stanford.protege.webprotege.project;

import edu.stanford.protege.webprotege.change.RevertRevisionAction;
import edu.stanford.protege.webprotege.change.RevertRevisionActionHandler;
import edu.stanford.protege.webprotege.events.EntityUpdateFailedEvent;
import edu.stanford.protege.webprotege.ipc.EventHandler;
import edu.stanford.protege.webprotege.ipc.ExecutionContext;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;



@Component
public class ApiEventUpdateFailedHandler implements EventHandler<EntityUpdateFailedEvent> {


private final ProjectRevisionRepository projectRevisionRepository;

private final ProjectCache projectCache;


public ApiEventUpdateFailedHandler(ProjectRevisionRepository projectRevisionRepository, ProjectCache projectCache) {
this.projectRevisionRepository = projectRevisionRepository;
this.projectCache = projectCache;
}


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

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

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

@Override
public void handleEvent(EntityUpdateFailedEvent event) {
throw new RuntimeException("Method not supported");
}

@Override
public void handleEvent(EntityUpdateFailedEvent event, ExecutionContext executionContext) {
List<ProjectRevision> revisions = projectRevisionRepository.findByChangeRequest(event.projectId(), event.changeRequestId());
RevertRevisionActionHandler revertRevisionActionHandler = (RevertRevisionActionHandler)
projectCache.getActionHandlerRegistry(event.projectId()).getActionHandler(new RevertRevisionAction(null ,null,null));
for(ProjectRevision projectRevision : revisions) {
revertRevisionActionHandler.execute(new RevertRevisionAction(projectRevision.changeRequestId(), projectRevision.projectId(), projectRevision.revisionNumber()), executionContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package edu.stanford.protege.webprotege.project;

import edu.stanford.protege.webprotege.common.ChangeRequestId;
import edu.stanford.protege.webprotege.common.ProjectId;
import edu.stanford.protege.webprotege.common.UserId;
import edu.stanford.protege.webprotege.revision.RevisionNumber;

public record ProjectRevision(ProjectId projectId, ChangeRequestId changeRequestId, UserId userId, RevisionNumber revisionNumber) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package edu.stanford.protege.webprotege.project;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoCollection;
import edu.stanford.protege.webprotege.common.ChangeRequestId;
import edu.stanford.protege.webprotege.common.ProjectId;
import edu.stanford.protege.webprotege.inject.ApplicationSingleton;
import edu.stanford.protege.webprotege.persistence.Repository;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;



@ApplicationSingleton
public class ProjectRevisionRepository implements Repository {

public static final String CHANGE_REQUEST_ID = "changeRequestId";
public static final String PROJECTS_REVISIONS = "ProjectsRevisions";
public static final String PROJECT_ID = "projectId";

private final MongoCollection<Document> collection;
private final ObjectMapper objectMapper;

@Inject
public ProjectRevisionRepository(MongoTemplate mongoTemplate, ObjectMapper objectMapper) {

this.collection = mongoTemplate.getCollection(PROJECTS_REVISIONS);

this.objectMapper = objectMapper;
}

@Override
public void ensureIndexes() {

}

public void save(@Nonnull ProjectRevision projectRevision) {
var document = objectMapper.convertValue(projectRevision, Document.class);
collection.insertOne(document);
}

private static Document withProjectId(@Nonnull ProjectId projectId) {
return new Document(PROJECT_ID, projectId.id());
}

public List<ProjectRevision> findByChangeRequest(ProjectId projectId, ChangeRequestId changeRequestId) {
ArrayList<ProjectRevision> result = new ArrayList<>();
collection.find(withProjectIdAndWithChangeRequestId(projectId, changeRequestId))
.map(d -> objectMapper.convertValue(d, ProjectRevision.class)).into(result);
return result;
}

private static Document withProjectIdAndWithChangeRequestId(@Nonnull ProjectId projectId,
@Nonnull ChangeRequestId changeRequestId) {
return new Document(PROJECT_ID, projectId.id()).append(CHANGE_REQUEST_ID, changeRequestId.id());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public class ChangeManager implements HasApplyChanges {
@Nonnull
private final RootIndex rootIndex;

private final ProjectRevisionRepository projectRevisionRepository;

@Nonnull
private final DictionaryManager dictionaryManager;

Expand Down Expand Up @@ -161,7 +163,8 @@ public ChangeManager(@Nonnull ProjectId projectId,
@Nonnull IriReplacerFactory iriReplacerFactory,
@Nonnull GeneratedAnnotationsGenerator generatedAnnotationsGenerator,
@Nonnull EventDispatcher eventDispatcher,
@Nonnull NewRevisionsEventEmitterService newRevisionsEmitter) {
@Nonnull NewRevisionsEventEmitterService newRevisionsEmitter,
@Nonnull ProjectRevisionRepository projectRevisionRepository) {
this.projectId = projectId;
this.dataFactory = dataFactory;
this.dictionaryUpdatesProcessor = dictionaryUpdatesProcessor;
Expand All @@ -172,6 +175,7 @@ public ChangeManager(@Nonnull ProjectId projectId,
this.projectChangedWebhookInvoker = projectChangedWebhookInvoker;
this.eventTranslatorManagerProvider = eventTranslatorManagerProvider;
this.entityCrudKitHandlerCache = entityCrudKitHandlerCache;
this.projectRevisionRepository = projectRevisionRepository;
this.eventDispatcher = eventDispatcher;
this.changeManager = changeManager;
this.rootIndex = rootIndex;
Expand Down Expand Up @@ -335,6 +339,8 @@ public <R> ChangeApplicationResult<R> applyChanges(@Nonnull final UserId userId,
eventTranslatorManager,
revision);

revision.ifPresent(value -> projectRevisionRepository.save(new ProjectRevision(projectId, changeRequestId, userId, value.getRevisionNumber())));

newRevisionsEmitter.emitNewRevisionsEvent(revision);

} finally {
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ webprotege.entityGraph.edgeLimit=3000

webprotege.rabbitmq.requestqueue=webprotege-backend-queue
webprotege.rabbitmq.responsequeue=webprotege-backend-response-queue
webprotege.rabbitmq.event-subscribe=true
webprotege.rabbitmq.eventsqueue=webprotege-backend-events-queue

webprotege.rabbitmq.timeout=60000
webprotege.rabbitmq.event-subscribe=false

webprotege.minio.end-point=http://minio:9000
webprotege.minio.access-key=webprotege
Expand Down

0 comments on commit b873b8e

Please sign in to comment.