From 159ecb0fda052cdd2d2c28f65b37d226625326dd Mon Sep 17 00:00:00 2001 From: alexsilaghi Date: Tue, 17 Dec 2024 19:50:08 +0200 Subject: [PATCH] changed import , removed default initialization and added it to get as virtual revision. --- .../GetEntityPostCoordinationResponse.java | 3 - ...tEntityPostCoordinationCommandHandler.java | 2 +- .../model/EntityPostCoordinationHistory.java | 5 +- ...PostCoordinationSpecificationRevision.java | 41 ++++++++- .../services/PostCoordinationService.java | 91 ++++++------------- .../services/PostCoordinationServiceIT.java | 45 --------- 6 files changed, 69 insertions(+), 118 deletions(-) diff --git a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/dto/GetEntityPostCoordinationResponse.java b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/dto/GetEntityPostCoordinationResponse.java index dfddec1..ccf7bbe 100644 --- a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/dto/GetEntityPostCoordinationResponse.java +++ b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/dto/GetEntityPostCoordinationResponse.java @@ -13,9 +13,6 @@ public record GetEntityPostCoordinationResponse(@JsonProperty("entityIri") String entityIri, - @JsonProperty("lastRevisionDate") - Date lastRevisionDate, - @JsonProperty("postCoordinationSpecification") WhoficEntityPostCoordinationSpecification postCoordinationSpecification) implements Response { } diff --git a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/GetEntityPostCoordinationCommandHandler.java b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/GetEntityPostCoordinationCommandHandler.java index 3afa0da..991b8fc 100644 --- a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/GetEntityPostCoordinationCommandHandler.java +++ b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/handlers/GetEntityPostCoordinationCommandHandler.java @@ -35,7 +35,7 @@ public Class getRequestClass() { @Override public Mono handleRequest(GetEntityPostCoordinationRequest request, ExecutionContext executionContext) { - GetEntityPostCoordinationResponse processedSpec = postCoordService.fetchHistory(request.entityIRI(), request.projectId()); + GetEntityPostCoordinationResponse processedSpec = postCoordService.fetchHistory(request.entityIRI(), request.projectId(), "ICD"); return Mono.just(processedSpec); } diff --git a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/model/EntityPostCoordinationHistory.java b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/model/EntityPostCoordinationHistory.java index 74687a5..7092a23 100644 --- a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/model/EntityPostCoordinationHistory.java +++ b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/model/EntityPostCoordinationHistory.java @@ -22,6 +22,8 @@ public class EntityPostCoordinationHistory { public static final String PROJECT_ID = "projectId"; public static final String SPEC_REVISIONS = "postCoordinationRevisions"; + public static final String ENTITY_TYPE = "entityType"; + @Field(WHOFIC_ENTITY_IRI) @Indexed(name = "entityIriSpec_idx") private final String whoficEntityIri; @@ -30,6 +32,7 @@ public class EntityPostCoordinationHistory { @Indexed(name = "entityIriProjectId_idx") private final String projectId; + @Field(SPEC_REVISIONS) private final List postCoordinationRevisions; @@ -46,7 +49,7 @@ public EntityPostCoordinationHistory(@JsonProperty(WHOFIC_ENTITY_IRI) String who public static EntityPostCoordinationHistory create(String whoficEntityIri, String projectId, List postCoordinationRevisions) { - return new EntityPostCoordinationHistory(whoficEntityIri, projectId, postCoordinationRevisions); + return new EntityPostCoordinationHistory(whoficEntityIri, projectId , postCoordinationRevisions); } @JsonProperty(WHOFIC_ENTITY_IRI) diff --git a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/model/PostCoordinationSpecificationRevision.java b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/model/PostCoordinationSpecificationRevision.java index 0752593..3062da5 100644 --- a/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/model/PostCoordinationSpecificationRevision.java +++ b/src/main/java/edu/stanford/protege/webprotege/postcoordinationservice/model/PostCoordinationSpecificationRevision.java @@ -4,10 +4,16 @@ 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.dto.LinearizationDefinition; +import edu.stanford.protege.webprotege.postcoordinationservice.events.AddToDefaultAxisEvent; +import edu.stanford.protege.webprotege.postcoordinationservice.events.AddToNotAllowedAxisEvent; +import edu.stanford.protege.webprotege.postcoordinationservice.events.PostCoordinationSpecificationEvent; import org.jetbrains.annotations.NotNull; import org.springframework.data.mongodb.core.index.*; -import java.time.Instant; +import java.time.*; +import java.util.HashSet; +import java.util.List; import java.util.Set; public record PostCoordinationSpecificationRevision(UserId userId, @@ -25,8 +31,37 @@ public static PostCoordinationSpecificationRevision create(UserId userId, Set definitionList, + List configurations) { + Set postCoordinationEvents = new HashSet<>(); + + + for (LinearizationDefinition definition : definitionList) { + TableConfiguration tableConfiguration = configurations.stream() + .filter(config -> config.getEntityType().equalsIgnoreCase(entityType)) + .findFirst() + .orElseThrow(() -> new RuntimeException("Couldn't find the equivalent entity type " + entityType)); + + List specificationEvents = tableConfiguration.getPostCoordinationAxes().stream() + .map(availableAxis -> { + if (definition.getCoreLinId() != null && !definition.getCoreLinId().isEmpty()) { + return new AddToDefaultAxisEvent(availableAxis, definition.getWhoficEntityIri()); + } else { + return new AddToNotAllowedAxisEvent(availableAxis, definition.getWhoficEntityIri()); + } + }).toList(); + postCoordinationEvents.add(new PostCoordinationViewEvent(definition.getWhoficEntityIri(), specificationEvents)); + + } + + return new PostCoordinationSpecificationRevision(UserId.valueOf("initialRevision"), + Instant.EPOCH.getEpochSecond(), + postCoordinationEvents, + CommitStatus.COMMITTED, + null + ); } @Override 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 9ae75b4..244b57c 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 @@ -56,10 +56,8 @@ public PostCoordinationService(PostCoordinationRepository repository, public void createFirstSpecificationImport(String documentLocation, ProjectId projectId, UserId userId) { var stream = documentRepository.fetchPostCoordinationSpecifications(documentLocation); - List definitionList = linearizationService.getLinearizationDefinitions(); - List configurations = configRepository.getALlTableConfiguration(); readWriteLock.executeWriteLock(() -> { - stream.collect(StreamUtils.batchCollector(500, createBatchProcessorForSavingPaginatedHistories(projectId, userId, definitionList, configurations))); + stream.collect(StreamUtils.batchCollector(500, createBatchProcessorForSavingPaginatedHistories(projectId, userId))); }); } @@ -94,39 +92,28 @@ private Consumer> createBatchProcessorForSavingPa } - private Consumer> createBatchProcessorForSavingPaginatedHistories(ProjectId projectId, UserId userId, List definitionList, List configurations) { + private Consumer> createBatchProcessorForSavingPaginatedHistories(ProjectId projectId, UserId userId) { return page -> { if (isNotEmpty(page)) { Set histories = new HashSet<>(); for (WhoficEntityPostCoordinationSpecification specification : page) { - - for (LinearizationDefinition linearizationDefinition : definitionList) { - boolean linearizationExists = specification.postcoordinationSpecifications().stream().anyMatch(spec -> - spec.getLinearizationView().equalsIgnoreCase(linearizationDefinition.getWhoficEntityIri())); - if (!linearizationExists) { - specification.postcoordinationSpecifications().add(new PostCoordinationSpecification(linearizationDefinition.getWhoficEntityIri(), - new ArrayList<>(), - new ArrayList<>(), - new ArrayList<>(), - new ArrayList<>())); - } - - } - Set events = specification.postcoordinationSpecifications().stream() - .map(spec -> enrichWithMissingAxis(specification.entityType(), spec, definitionList, configurations)) .map(spec -> new PostCoordinationViewEvent(spec.getLinearizationView(), SpecificationToEventsMapper.convertFromSpecification(spec)) ) + .filter(spec -> !spec.axisEvents().isEmpty()) .collect(Collectors.toSet()); PostCoordinationSpecificationRevision revision = PostCoordinationSpecificationRevision.create(userId, events); - EntityPostCoordinationHistory history = new EntityPostCoordinationHistory(specification.whoficEntityIri(), projectId.id(), List.of(revision)); - histories.add(history); + EntityPostCoordinationHistory history = new EntityPostCoordinationHistory(specification.whoficEntityIri(), projectId.id(), List.of(revision)); + if(!events.isEmpty()) { + histories.add(history); + } + } + if(!histories.isEmpty()) { + saveMultipleEntityPostCoordinationHistories(histories); } - saveMultipleEntityPostCoordinationHistories(histories); - - newRevisionsEventEmitter.emitNewRevisionsEventForSpecHistory(projectId, histories.stream().toList(), null); + //newRevisionsEventEmitter.emitNewRevisionsEventForSpecHistory(projectId, histories.stream().toList(), null); } }; } @@ -140,33 +127,6 @@ private void saveMultipleEntityPostCoordinationHistories(Set definitionList, List configurations) { - LinearizationDefinition definition = definitionList.stream() - .filter(linearizationDefinition -> linearizationDefinition.getWhoficEntityIri().equalsIgnoreCase(specification.getLinearizationView())) - .findFirst() - .orElseThrow(() -> new RuntimeException("Couldn't find the linearization definition " + specification.getLinearizationView())); - - TableConfiguration tableConfiguration = configurations.stream() - .filter(config -> config.getEntityType().equalsIgnoreCase(entityType)) - .findFirst() - .orElseThrow(() -> new RuntimeException("Couldn't find the equivalent entity type " + entityType)); - - for (String availableAxis : tableConfiguration.getPostCoordinationAxes()) { - boolean isAlreadySet = specification.getRequiredAxes().contains(availableAxis) || - specification.getAllowedAxes().contains(availableAxis) || - specification.getNotAllowedAxes().contains(availableAxis) || - specification.getDefaultAxes().contains(availableAxis); - if (!isAlreadySet) { - if (definition.getCoreLinId() != null && !definition.getCoreLinId().isEmpty()) { - specification.getDefaultAxes().add(availableAxis); - } else { - specification.getNotAllowedAxes().add(availableAxis); - } - } - } - return specification; - } - public void addSpecificationRevision(WhoficEntityPostCoordinationSpecification newSpecification, UserId userId, ProjectId projectId) { addSpecificationRevision(newSpecification, userId, projectId, null); } @@ -178,10 +138,8 @@ public void addSpecificationRevision(WhoficEntityPostCoordinationSpecification n existingHistoryOptional.ifPresentOrElse(history -> { WhoficEntityPostCoordinationSpecification oldSpec = eventProcessor.processHistory(history); - Set specEvents = SpecificationToEventsMapper.createEventsFromDiff(oldSpec, newSpecification); - if (!specEvents.isEmpty()) { var newRevision = PostCoordinationSpecificationRevision.create(userId, specEvents, changeRequestId); repository.addSpecificationRevision(newSpecification.whoficEntityIri(), projectId, newRevision); @@ -272,20 +230,23 @@ public GetEntityCustomScaleValueResponse fetchCustomScalesHistory(String entityI } - public GetEntityPostCoordinationResponse fetchHistory(String entityIri, ProjectId projectId) { + public GetEntityPostCoordinationResponse fetchHistory(String entityIri, ProjectId projectId, String entityType) { + List definitionList = linearizationService.getLinearizationDefinitions(); + List configurations = configRepository.getALlTableConfiguration(); return this.repository.getExistingHistoryOrderedByRevision(entityIri, projectId) .map(history -> { - Date lastChangeDate = null; - if (!history.getPostCoordinationRevisions().isEmpty()) { - long lastTimestamp = history.getPostCoordinationRevisions().get(history.getPostCoordinationRevisions().size() - 1).timestamp(); - lastChangeDate = Date.from(Instant.ofEpochMilli(lastTimestamp)); - } - - return new GetEntityPostCoordinationResponse(entityIri, lastChangeDate, eventProcessor.processHistory(history)); - }) - .orElseGet(() -> new GetEntityPostCoordinationResponse(entityIri, - null, - new WhoficEntityPostCoordinationSpecification(entityIri, null, Collections.emptyList()))); + history.getPostCoordinationRevisions().add(0, PostCoordinationSpecificationRevision.createDefaultInitialRevision( + entityType, + definitionList, + configurations)); + return new GetEntityPostCoordinationResponse(entityIri, eventProcessor.processHistory(history)); + } + ) + .orElseGet(() -> { + var specs = Arrays.asList(PostCoordinationSpecificationRevision.createDefaultInitialRevision(entityType, definitionList, configurations)); + var history = new EntityPostCoordinationHistory(entityIri, projectId.id(), specs); + return new GetEntityPostCoordinationResponse(entityIri, eventProcessor.processHistory(history)); + }); } } diff --git a/src/test/java/edu/stanford/protege/webprotege/postcoordinationservice/services/PostCoordinationServiceIT.java b/src/test/java/edu/stanford/protege/webprotege/postcoordinationservice/services/PostCoordinationServiceIT.java index a36a41a..7712e59 100644 --- a/src/test/java/edu/stanford/protege/webprotege/postcoordinationservice/services/PostCoordinationServiceIT.java +++ b/src/test/java/edu/stanford/protege/webprotege/postcoordinationservice/services/PostCoordinationServiceIT.java @@ -76,51 +76,6 @@ public void setUp() throws IOException { projectId = ProjectId.generate(); } - @Test - public void GIVEN_mainLinearization_WHEN_enriching_THEN_missingAxisAreSetToNotAllowed() throws IOException { - - File labels = new File("src/test/resources/LinearizationDefinitions.json"); - List linearizationDefinitions = objectMapper.readValue(labels, new TypeReference<>() { - }); - File tableConfig = new File("src/test/resources/postcoordinationTableConfig.json"); - List tableConfigs = objectMapper.readValue(tableConfig, new TypeReference<>() { - }); - - PostCoordinationSpecification specification = new PostCoordinationSpecification( - "http://id.who.int/icd/release/11/mms", - Arrays.asList("http://id.who.int/icd/schema/hasSeverity", "http://id.who.int/icd/schema/medication"), - new ArrayList<>(), - new ArrayList<>(), - new ArrayList<>() - ); - - postCoordinationService.enrichWithMissingAxis("ICD", specification, linearizationDefinitions, tableConfigs); - - assertEquals(27, specification.getNotAllowedAxes().size()); - } - - - @Test - public void GIVEN_telescopicLinearization_WHEN_enriching_THEN_missingAxisAreSetToDefault() throws IOException { - File labels = new File("src/test/resources/LinearizationDefinitions.json"); - List linearizationDefinitions = objectMapper.readValue(labels, new TypeReference<>() { - }); - File tableConfig = new File("src/test/resources/postcoordinationTableConfig.json"); - List tableConfigs = objectMapper.readValue(tableConfig, new TypeReference<>() { - }); - - PostCoordinationSpecification specification = new PostCoordinationSpecification( - "http://id.who.int/icd/release/11/ocu", - Arrays.asList("http://id.who.int/icd/schema/hasSeverity", "http://id.who.int/icd/schema/medication"), - new ArrayList<>(), - new ArrayList<>(), - new ArrayList<>() - ); - - postCoordinationService.enrichWithMissingAxis("ICD", specification, linearizationDefinitions, tableConfigs); - - assertEquals(27, specification.getDefaultAxes().size()); - } @Test public void GIVEN_existingFile_WHEN_firstImport_THEN_allEventsAreGenerated() {