diff --git a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/repositories/PostCoordinationRepository.java b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/repositories/PostCoordinationRepository.java index dc59db5..7b2105a 100644 --- a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/repositories/PostCoordinationRepository.java +++ b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/repositories/PostCoordinationRepository.java @@ -62,6 +62,7 @@ public EntityPostCoordinationHistory saveNewSpecificationHistory(EntityPostCoord return readWriteLock.executeWriteLock(() -> mongoTemplate.save(specificationHistory, POSTCOORDINATION_HISTORY_COLLECTION)); } + public void addCustomScalesRevision(String whoficEntityIri, ProjectId projectId, PostCoordinationCustomScalesRevision customScalesRevision) { Query query = new Query(); query.addCriteria(Criteria.where(WHOFIC_ENTITY_IRI).is(whoficEntityIri) @@ -138,26 +139,68 @@ public void deletePostCoordinationCustomScalesRevision(ChangeRequestId changeReq Query query = new Query(); query.addCriteria( - Criteria.where(CHANGE_REQUEST_ID).is(changeRequestId) - .and(WHOFIC_ENTITY_IRI).is(entityIri) + Criteria.where(WHOFIC_ENTITY_IRI).is(entityIri) .and(PROJECT_ID).is(projectId.value()) ); - Update update = new Update().pull("postCoordinationCustomScalesRevisions", Query.query(Criteria.where("changeRequestId").is(changeRequestId))); + Update update = new Update().pull("postCoordinationCustomScalesRevisions", + new Document("changeRequestId._id", changeRequestId.id())); + readWriteLock.executeReadLock(() -> Optional.of(mongoTemplate.updateFirst(query,update, EntityCustomScalesValuesHistory.class, CUSTOM_SCALE_REVISIONS )) ); } public void deletePostCoordinationSpecificationRevision(ChangeRequestId changeRequestId, ProjectId projectId, String entityIri) { + Query query = new Query(); query.addCriteria( - Criteria.where(CHANGE_REQUEST_ID).is(changeRequestId) - .and(WHOFIC_ENTITY_IRI).is(entityIri) + Criteria.where(WHOFIC_ENTITY_IRI).is(entityIri) .and(PROJECT_ID).is(projectId.value()) ); - Update update = new Update().pull("postCoordinationRevisions", Query.query(Criteria.where("changeRequestId").is(changeRequestId))); + Update update = new Update().pull("postCoordinationRevisions", + new Document("changeRequestId._id", changeRequestId.id())); + + readWriteLock.executeReadLock(() -> { + + UpdateResult updateResult = mongoTemplate.updateFirst(query,update, EntityPostCoordinationHistory.class, POSTCOORDINATION_HISTORY_COLLECTION); + System.out.println(updateResult); + return null; + } + + ); + } + + public void commitPostCoordinationSpecificationRevision(ChangeRequestId changeRequestId, ProjectId projectId, String entityIri){ + Query query = new Query(Criteria.where(WHOFIC_ENTITY_IRI) + .is(entityIri) + .and(PROJECT_ID).is(projectId.id()) + .and("postCoordinationRevisions") + .elemMatch( + Criteria.where("changeRequestId._id").is(changeRequestId) + .and("commitStatus").is(CommitStatus.UNCOMMITTED.name()) + ) + ); + Update update = new Update().set("postCoordinationRevisions.$.commitStatus", CommitStatus.COMMITTED.name()); + readWriteLock.executeReadLock(() -> Optional.of(mongoTemplate.updateFirst(query,update, EntityPostCoordinationHistory.class, POSTCOORDINATION_HISTORY_COLLECTION)) ); } + + public void commitPostCoordinationCustomScalesRevision(ChangeRequestId changeRequestId, ProjectId projectId, String entityIri){ + Query query = new Query(Criteria.where(WHOFIC_ENTITY_IRI).is(entityIri) + .and(PROJECT_ID).is(projectId.id()) + .and("postCoordinationCustomScalesRevisions") + .elemMatch( + Criteria.where("changeRequestId._id").is(changeRequestId) + .and("commitStatus").is(CommitStatus.UNCOMMITTED.name()) + ) + ); + + Update update = new Update().set("postCoordinationCustomScalesRevisions.$.commitStatus", CommitStatus.COMMITTED.name()); + + readWriteLock.executeReadLock(() -> + Optional.of(mongoTemplate.updateFirst(query,update, EntityCustomScalesValuesHistory.class, CUSTOM_SCALE_REVISIONS )) + ); + } } diff --git a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/services/PostCoordinationService.java b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/services/PostCoordinationService.java index d52baab..dd3070b 100644 --- a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/services/PostCoordinationService.java +++ b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/services/PostCoordinationService.java @@ -224,7 +224,7 @@ public void addCustomScaleRevision(WhoficCustomScalesValues newScales, newRevisionsEventEmitter.emitNewRevisionsEvent(projectId, newScales.whoficEntityIri(), newRevision); } }, () -> { - var newHistory = createNewEntityCustomScalesHistory(newScales, projectId, userId); + var newHistory = createNewEntityCustomScalesHistory(newScales, projectId, userId, changeRequestId); var savedHistory = repository.saveNewCustomScalesHistory(newHistory); savedHistory.getPostCoordinationCustomScalesRevisions() .stream() @@ -238,10 +238,11 @@ public void addCustomScaleRevision(WhoficCustomScalesValues newScales, private EntityCustomScalesValuesHistory createNewEntityCustomScalesHistory(WhoficCustomScalesValues newScales, ProjectId projectId, - UserId userId) { + UserId userId, + ChangeRequestId changeRequestId) { WhoficCustomScalesValues oldSpec = WhoficCustomScalesValues.create(newScales.whoficEntityIri(), Collections.emptyList()); Set events = SpecificationToEventsMapper.createScaleEventsFromDiff(oldSpec, newScales); - var revision = PostCoordinationCustomScalesRevision.create(userId, events); + var revision = PostCoordinationCustomScalesRevision.create(userId, events, changeRequestId); return EntityCustomScalesValuesHistory.create(newScales.whoficEntityIri(), projectId.value(), List.of(revision)); } diff --git a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/services/RevisionCommitService.java b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/services/RevisionCommitService.java index 4ba0aef..f30f689 100644 --- a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/services/RevisionCommitService.java +++ b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/services/RevisionCommitService.java @@ -26,52 +26,13 @@ public RevisionCommitService(PostCoordinationRepository postCoordinationReposito @Transactional public void rollbackRevision(ChangeRequestId changeRequestId, ProjectId projectId, String entityIri) { - postCoordinationRepository.deletePostCoordinationSpecificationRevision(changeRequestId, projectId, entityIri); postCoordinationRepository.deletePostCoordinationCustomScalesRevision(changeRequestId, projectId, entityIri); + postCoordinationRepository.deletePostCoordinationSpecificationRevision(changeRequestId, projectId, entityIri); } public void commitRevision(ChangeRequestId changeRequestId, ProjectId projectId, String entityIri) { - Optional postCoordinationHistory = postCoordinationRepository.getExistingHistoryOrderedByRevision(entityIri, projectId); - try { - if (postCoordinationHistory.isPresent()) { - List uncommittedSpecifications = postCoordinationHistory.get().getPostCoordinationRevisions() - .stream() - .filter(revision -> revision.changeRequestId() != null && - revision.changeRequestId().equals(changeRequestId) && - revision.commitStatus().equals(CommitStatus.UNCOMMITTED)) - .toList(); - postCoordinationHistory.get().getPostCoordinationRevisions().removeAll(uncommittedSpecifications); - postCoordinationHistory.get().getPostCoordinationRevisions().addAll(uncommittedSpecifications.stream() - .map(PostCoordinationSpecificationRevision::createCommittedClone).toList()); - postCoordinationRepository.saveNewSpecificationHistory(postCoordinationHistory.get()); - } else { - LOGGER.error("Error finding postcoordinationHistory for " + entityIri); - } - }catch (Exception e){ - LOGGER.error("Error committing post coordinationHistory for " + entityIri, e); - } - - try{ - Optional customScalesHistory = postCoordinationRepository.getExistingCustomScaleHistoryOrderedByRevision(entityIri, projectId); - if(customScalesHistory.isPresent()) { - List uncommittedSpecifications = customScalesHistory.get().getPostCoordinationCustomScalesRevisions() - .stream() - .filter(revision -> revision.changeRequestId() != null && - revision.changeRequestId().equals(changeRequestId) && - revision.commitStatus().equals(CommitStatus.UNCOMMITTED)) - .toList(); - customScalesHistory.get().getPostCoordinationCustomScalesRevisions().removeAll(uncommittedSpecifications); - customScalesHistory.get().getPostCoordinationCustomScalesRevisions().addAll(uncommittedSpecifications.stream() - .map(PostCoordinationCustomScalesRevision::createCommittedClone).toList()); - postCoordinationRepository.saveNewCustomScalesHistory(customScalesHistory.get()); - } else { - LOGGER.error("Error finding postCoordinationCustomScales for " + entityIri); - } - - }catch (Exception e){ - LOGGER.error("Error committing postCoordinationCustomScales for " + entityIri, e); - - } + postCoordinationRepository.commitPostCoordinationSpecificationRevision(changeRequestId, projectId, entityIri); + postCoordinationRepository.commitPostCoordinationCustomScalesRevision(changeRequestId, projectId, entityIri); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 73b6a8b..8e359a5 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -25,6 +25,8 @@ webprotege: requestqueue: webprotege-postcoordination-service-queue responsequeue: webprotege-postcoordination-service-response-queue timeout: 60000 + eventsqueue: webprotege-postcoordination-events-queue + event-subscribe: true readWriteLock: timeoutInMillies: 1000 maxRetries: 5