Skip to content

Commit

Permalink
wip refactor addRevision logic
Browse files Browse the repository at this point in the history
  • Loading branch information
soimugeo committed Oct 24, 2024
1 parent 1480141 commit 10146c3
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package edu.stanford.protege.webprotege.postcoordinationservice.handlers;


import edu.stanford.protege.webprotege.ipc.CommandHandler;
import edu.stanford.protege.webprotege.ipc.ExecutionContext;
import edu.stanford.protege.webprotege.ipc.WebProtegeHandler;
import edu.stanford.protege.webprotege.postcoordinationservice.services.PostCoordinationEventProcessor;
import edu.stanford.protege.webprotege.ipc.*;
import edu.stanford.protege.webprotege.postcoordinationservice.services.PostCoordinationService;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;

@WebProtegeHandler
public class AddEntityCustomScalesRevisionCommandHandler implements CommandHandler<AddEntityCustomScalesRevisionRequest, AddEntityCustomScalesRevisionResponse> {

private final PostCoordinationEventProcessor eventProcessor;
private final PostCoordinationService postCoordService;

public AddEntityCustomScalesRevisionCommandHandler(PostCoordinationEventProcessor eventProcessor) {
this.eventProcessor = eventProcessor;
public AddEntityCustomScalesRevisionCommandHandler(PostCoordinationService postCoordService) {
this.postCoordService = postCoordService;
}

@NotNull
Expand All @@ -31,7 +29,7 @@ public Class<AddEntityCustomScalesRevisionRequest> getRequestClass() {
@Override
public Mono<AddEntityCustomScalesRevisionResponse> handleRequest(AddEntityCustomScalesRevisionRequest request, ExecutionContext executionContext) {

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

return Mono.just(new AddEntityCustomScalesRevisionResponse());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package edu.stanford.protege.webprotege.postcoordinationservice.handlers;


import edu.stanford.protege.webprotege.ipc.CommandHandler;
import edu.stanford.protege.webprotege.ipc.ExecutionContext;
import edu.stanford.protege.webprotege.ipc.WebProtegeHandler;
import edu.stanford.protege.webprotege.postcoordinationservice.services.PostCoordinationEventProcessor;
import edu.stanford.protege.webprotege.ipc.*;
import edu.stanford.protege.webprotege.postcoordinationservice.services.PostCoordinationService;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;

@WebProtegeHandler
public class AddEntitySpecificationRevisionCommandHandler implements CommandHandler<AddEntitySpecificationRevisionRequest, AddEntitySpecificationRevisionResponse> {

private final PostCoordinationEventProcessor eventProcessor;
private final PostCoordinationService postCoordService;

public AddEntitySpecificationRevisionCommandHandler(PostCoordinationEventProcessor eventProcessor) {
this.eventProcessor = eventProcessor;
public AddEntitySpecificationRevisionCommandHandler(PostCoordinationService postCoordService) {
this.postCoordService = postCoordService;
}

@NotNull
Expand All @@ -30,7 +28,7 @@ public Class<AddEntitySpecificationRevisionRequest> getRequestClass() {

@Override
public Mono<AddEntitySpecificationRevisionResponse> handleRequest(AddEntitySpecificationRevisionRequest request, ExecutionContext executionContext) {
eventProcessor.saveNewSpecificationRevision(request.entitySpecification(), executionContext.userId(), request.projectId());
postCoordService.addSpecificationRevision(request.entitySpecification(), executionContext.userId(), request.projectId());
return Mono.just(new AddEntitySpecificationRevisionResponse());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.stanford.protege.webprotege.ipc.*;
import edu.stanford.protege.webprotege.postcoordinationservice.dto.*;
import edu.stanford.protege.webprotege.postcoordinationservice.model.WhoficEntityPostCoordinationSpecification;
import edu.stanford.protege.webprotege.postcoordinationservice.repositories.PostCoordinationRepository;
import edu.stanford.protege.webprotege.postcoordinationservice.services.*;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;
Expand All @@ -12,13 +13,19 @@
@WebProtegeHandler
public class CreatePostcoordinationFromParentCommandHandler implements CommandHandler<CreatePostcoordinationFromParentRequest, CreatePostcoordinationFromParentResponse> {

private final PostCoordinationEventProcessor postCoordProcessor;
private final PostCoordinationService postCoordService;
private final PostCoordinationRepository repo;
private final PostCoordinationEventProcessor eventProcessor;
private final LinearizationService linService;

public CreatePostcoordinationFromParentCommandHandler(PostCoordinationEventProcessor postCoordProcessor,
public CreatePostcoordinationFromParentCommandHandler(PostCoordinationService postCoordService,
PostCoordinationRepository repo,
PostCoordinationEventProcessor eventProcessor,
LinearizationService linService) {

this.postCoordProcessor = postCoordProcessor;
this.postCoordService = postCoordService;
this.repo = repo;
this.eventProcessor = eventProcessor;
this.linService = linService;
}

Expand All @@ -37,33 +44,38 @@ public Class<CreatePostcoordinationFromParentRequest> getRequestClass() {
public Mono<CreatePostcoordinationFromParentResponse> handleRequest(CreatePostcoordinationFromParentRequest request, ExecutionContext executionContext) {
List<LinearizationDefinition> definitionList = linService.getLinearizationDefinitions();

var parentWhoficSpec = postCoordProcessor.fetchHistory(request.parentEntityIri().toString(), request.projectId());
List<PostCoordinationSpecification> newSpecsList = new ArrayList<>();
var parentWhoficHistoryOptional = repo.getExistingHistoryOrderedByRevision(request.parentEntityIri().toString(), request.projectId());
parentWhoficHistoryOptional.ifPresent(parentWhoficHistory ->{
List<PostCoordinationSpecification> newSpecsList = new ArrayList<>();

parentWhoficSpec.postcoordinationSpecifications().forEach(spec -> {
var currDef = definitionList.stream().filter(lin -> lin.getWhoficEntityIri().equalsIgnoreCase(spec.getLinearizationView())).findFirst();
if (currDef.isEmpty()) {
return;
}
var allAxes = new ArrayList<>(spec.getAllowedAxes());
allAxes.addAll(spec.getDefaultAxes());
allAxes.addAll(spec.getRequiredAxes());
allAxes.addAll(spec.getNotAllowedAxes());
PostCoordinationSpecification newSpec = new PostCoordinationSpecification(spec.getLinearizationView(), null, null, null, null);
var parentWhoficSpec = eventProcessor.processHistory(parentWhoficHistory);

if (currDef.get().getCoreLinId() != null) {
newSpec.getDefaultAxes().addAll(allAxes);
} else {
newSpec.getNotAllowedAxes().addAll(allAxes);
}
newSpecsList.add(newSpec);
parentWhoficSpec.postcoordinationSpecifications().forEach(spec -> {
var currDef = definitionList.stream().filter(lin -> lin.getWhoficEntityIri().equalsIgnoreCase(spec.getLinearizationView())).findFirst();
if (currDef.isEmpty()) {
return;
}
var allAxes = new ArrayList<>(spec.getAllowedAxes());
allAxes.addAll(spec.getDefaultAxes());
allAxes.addAll(spec.getRequiredAxes());
allAxes.addAll(spec.getNotAllowedAxes());
PostCoordinationSpecification newSpec = new PostCoordinationSpecification(spec.getLinearizationView(), null, null, null, null);

if (currDef.get().getCoreLinId() != null) {
newSpec.getDefaultAxes().addAll(allAxes);
} else {
newSpec.getNotAllowedAxes().addAll(allAxes);
}
newSpecsList.add(newSpec);
});

postCoordService.addSpecificationRevision(
WhoficEntityPostCoordinationSpecification.create(request.newEntityIri().toString(), parentWhoficSpec.entityType(), newSpecsList),
executionContext.userId(),
request.projectId()
);
});

postCoordProcessor.saveNewSpecificationRevision(
WhoficEntityPostCoordinationSpecification.create(request.newEntityIri().toString(), parentWhoficSpec.entityType(), newSpecsList),
executionContext.userId(),
request.projectId()
);
return Mono.just(CreatePostcoordinationFromParentResponse.create());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@
import edu.stanford.protege.webprotege.postcoordinationservice.dto.GetEntityPostCoordinationRequest;
import edu.stanford.protege.webprotege.postcoordinationservice.dto.GetEntityPostCoordinationResponse;
import edu.stanford.protege.webprotege.postcoordinationservice.model.WhoficEntityPostCoordinationSpecification;
import edu.stanford.protege.webprotege.postcoordinationservice.repositories.PostCoordinationSpecificationsRepository;
import edu.stanford.protege.webprotege.postcoordinationservice.services.PostCoordinationEventProcessor;
import edu.stanford.protege.webprotege.postcoordinationservice.services.PostCoordinationService;
import edu.stanford.protege.webprotege.postcoordinationservice.services.*;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;

import java.util.Collections;

@WebProtegeHandler
public class GetEntityPostCoordinationCommandHandler implements CommandHandler<GetEntityPostCoordinationRequest, GetEntityPostCoordinationResponse> {

private final PostCoordinationEventProcessor postCoordinationEventProcessor;
private final PostCoordinationService postCoordService;


public GetEntityPostCoordinationCommandHandler(PostCoordinationEventProcessor postCoordinationEventProcessor) {
this.postCoordinationEventProcessor = postCoordinationEventProcessor;
public GetEntityPostCoordinationCommandHandler(PostCoordinationService postCoordService) {
this.postCoordService = postCoordService;
}

@NotNull
Expand All @@ -39,7 +35,7 @@ public Class<GetEntityPostCoordinationRequest> getRequestClass() {
@Override
public Mono<GetEntityPostCoordinationResponse> handleRequest(GetEntityPostCoordinationRequest request, ExecutionContext executionContext) {

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

return Mono.just(new GetEntityPostCoordinationResponse(request.entityIRI(), processedSpec));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import static edu.stanford.protege.webprotege.postcoordinationservice.model.EntityPostCoordinationHistory.*;

@Repository
public class PostCoordinationSpecificationsRepository {
public class PostCoordinationRepository {


private final MongoTemplate mongoTemplate;
private final ReadWriteLockService readWriteLock;


public PostCoordinationSpecificationsRepository(MongoTemplate mongoTemplate, ReadWriteLockService readWriteLock) {
public PostCoordinationRepository(MongoTemplate mongoTemplate, ReadWriteLockService readWriteLock) {
this.mongoTemplate = mongoTemplate;
this.readWriteLock = readWriteLock;
}
Expand Down Expand Up @@ -100,7 +100,6 @@ public Optional<EntityPostCoordinationHistory> getExistingHistoryOrderedByRevisi
.stream()
.sorted(Comparator.comparingLong(PostCoordinationSpecificationRevision::timestamp))
.collect(Collectors.toList());
// Return a new EntityLinearizationHistory object with the sorted revisions
return new EntityPostCoordinationHistory(history.getWhoficEntityIri(), history.getProjectId(), sortedRevisions);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package edu.stanford.protege.webprotege.postcoordinationservice.services;

import edu.stanford.protege.webprotege.common.*;
import edu.stanford.protege.webprotege.common.ProjectId;
import edu.stanford.protege.webprotege.postcoordinationservice.dto.*;
import edu.stanford.protege.webprotege.postcoordinationservice.events.*;
import edu.stanford.protege.webprotege.postcoordinationservice.mappers.SpecificationToEventsMapper;
import edu.stanford.protege.webprotege.postcoordinationservice.model.*;
import edu.stanford.protege.webprotege.postcoordinationservice.repositories.PostCoordinationSpecificationsRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Nonnull;
Expand All @@ -14,63 +12,20 @@
@Service
public class PostCoordinationEventProcessor {

private final PostCoordinationSpecificationsRepository repository;

private final NewRevisionsEventEmitterService newRevisionsEventEmitter;

public PostCoordinationEventProcessor(PostCoordinationSpecificationsRepository repository,
NewRevisionsEventEmitterService newRevisionsEventEmitter) {
this.repository = repository;
public PostCoordinationEventProcessor(NewRevisionsEventEmitterService newRevisionsEventEmitter) {
this.newRevisionsEventEmitter = newRevisionsEventEmitter;
}

public void saveNewSpecificationRevision(WhoficEntityPostCoordinationSpecification newSpecification, UserId userId, ProjectId projectId) {
WhoficEntityPostCoordinationSpecification existingSpecification = fetchHistory(newSpecification.whoficEntityIri(), projectId);
Set<PostCoordinationViewEvent> events = SpecificationToEventsMapper.createEventsFromDiff(existingSpecification, newSpecification);

if (!events.isEmpty()) {
PostCoordinationSpecificationRevision revision = new PostCoordinationSpecificationRevision(userId, new Date().getTime(), events);


repository.addSpecificationRevision(newSpecification.whoficEntityIri(), projectId, revision);
newRevisionsEventEmitter.emitNewRevisionsEvent(projectId, newSpecification.whoficEntityIri(), revision);
}
}

public void saveNewCustomScalesRevision(WhoficCustomScalesValues newScales, UserId userId, ProjectId projectId) {
WhoficCustomScalesValues oldScales = fetchCustomScalesHistory(newScales.whoficEntityIri(), projectId);

Set<PostCoordinationCustomScalesValueEvent> events = SpecificationToEventsMapper.createScaleEventsFromDiff(oldScales, newScales);

if (!events.isEmpty()) {
PostCoordinationCustomScalesRevision revision = new PostCoordinationCustomScalesRevision(userId, new Date().getTime(), events);

repository.addCustomScalesRevision(newScales.whoficEntityIri(), projectId, revision);
newRevisionsEventEmitter.emitNewRevisionsEvent(projectId, newScales.whoficEntityIri(), revision);
}
}

public WhoficEntityPostCoordinationSpecification fetchHistory(String entityIri, ProjectId projectId) {
return this.repository.getExistingHistoryOrderedByRevision(entityIri, projectId)
.map(this::processHistory)
.orElseGet(() -> new WhoficEntityPostCoordinationSpecification(entityIri, null, Collections.emptyList()));
}

private PostCoordinationSpecification findSpecificationWithLinearizationView(String linearizationView, HashSet<PostCoordinationSpecification> postCoordinationSpecification) {
return postCoordinationSpecification.stream().filter(spec -> spec.getLinearizationView().equalsIgnoreCase(linearizationView))
.findFirst()
.orElse(new PostCoordinationSpecification(linearizationView, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>()));

}

public WhoficCustomScalesValues fetchCustomScalesHistory(String entityIri, ProjectId projectId) {
return this.repository.getExistingCustomScaleHistoryOrderedByRevision(entityIri, projectId)
.map(this::processCustomScaleHistory)
.orElseGet(() -> new WhoficCustomScalesValues(entityIri, Collections.emptyList()));

}

private WhoficCustomScalesValues processCustomScaleHistory(EntityCustomScalesValuesHistory entityCustomScalesValuesHistory) {
public WhoficCustomScalesValues processCustomScaleHistory(EntityCustomScalesValuesHistory entityCustomScalesValuesHistory) {
WhoficCustomScalesValues response = new WhoficCustomScalesValues(entityCustomScalesValuesHistory.getWhoficEntityIri(), new ArrayList<>());
for (PostCoordinationCustomScalesRevision revision : entityCustomScalesValuesHistory.getPostCoordinationCustomScalesRevisions()) {
for (PostCoordinationCustomScalesValueEvent event : revision.postCoordinationEvents()) {
Expand All @@ -83,7 +38,7 @@ private WhoficCustomScalesValues processCustomScaleHistory(EntityCustomScalesVal
return new WhoficCustomScalesValues(entityCustomScalesValuesHistory.getWhoficEntityIri(), nonEmptyCustomizations);
}

private WhoficEntityPostCoordinationSpecification processHistory(@Nonnull EntityPostCoordinationHistory postCoordinationHistory) {
public WhoficEntityPostCoordinationSpecification processHistory(@Nonnull EntityPostCoordinationHistory postCoordinationHistory) {

var postCoordinationSpecification = new HashSet<PostCoordinationSpecification>();
for (PostCoordinationSpecificationRevision revision : postCoordinationHistory.getPostCoordinationRevisions()) {
Expand Down
Loading

0 comments on commit 10146c3

Please sign in to comment.