Skip to content

Commit

Permalink
changed import , removed default initialization and added it to get a…
Browse files Browse the repository at this point in the history
…s virtual revision.
  • Loading branch information
alexsilaghi committed Dec 17, 2024
1 parent 14fdde6 commit 159ecb0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
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
Expand Up @@ -35,7 +35,7 @@ public Class<GetEntityPostCoordinationRequest> getRequestClass() {
@Override
public Mono<GetEntityPostCoordinationResponse> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +32,7 @@ public class EntityPostCoordinationHistory {
@Indexed(name = "entityIriProjectId_idx")
private final String projectId;


@Field(SPEC_REVISIONS)
private final List<PostCoordinationSpecificationRevision> postCoordinationRevisions;

Expand All @@ -46,7 +49,7 @@ public EntityPostCoordinationHistory(@JsonProperty(WHOFIC_ENTITY_IRI) String who
public static EntityPostCoordinationHistory create(String whoficEntityIri,
String projectId,
List<PostCoordinationSpecificationRevision> postCoordinationRevisions) {
return new EntityPostCoordinationHistory(whoficEntityIri, projectId, postCoordinationRevisions);
return new EntityPostCoordinationHistory(whoficEntityIri, projectId , postCoordinationRevisions);
}

@JsonProperty(WHOFIC_ENTITY_IRI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,8 +31,37 @@ public static PostCoordinationSpecificationRevision create(UserId userId, Set<Po
return new PostCoordinationSpecificationRevision(userId, Instant.now().toEpochMilli(), postCoordinationEventList, status, changeRequestId != null ? changeRequestId.id() : null);
}

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

public static PostCoordinationSpecificationRevision createDefaultInitialRevision(String entityType,
List<LinearizationDefinition> definitionList,
List<TableConfiguration> configurations) {
Set<PostCoordinationViewEvent> 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<PostCoordinationSpecificationEvent> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ public PostCoordinationService(PostCoordinationRepository repository,

public void createFirstSpecificationImport(String documentLocation, ProjectId projectId, UserId userId) {
var stream = documentRepository.fetchPostCoordinationSpecifications(documentLocation);
List<LinearizationDefinition> definitionList = linearizationService.getLinearizationDefinitions();
List<TableConfiguration> configurations = configRepository.getALlTableConfiguration();
readWriteLock.executeWriteLock(() -> {
stream.collect(StreamUtils.batchCollector(500, createBatchProcessorForSavingPaginatedHistories(projectId, userId, definitionList, configurations)));
stream.collect(StreamUtils.batchCollector(500, createBatchProcessorForSavingPaginatedHistories(projectId, userId)));
});
}

Expand Down Expand Up @@ -94,39 +92,28 @@ private Consumer<List<WhoficCustomScalesValues>> createBatchProcessorForSavingPa
}


private Consumer<List<WhoficEntityPostCoordinationSpecification>> createBatchProcessorForSavingPaginatedHistories(ProjectId projectId, UserId userId, List<LinearizationDefinition> definitionList, List<TableConfiguration> configurations) {
private Consumer<List<WhoficEntityPostCoordinationSpecification>> createBatchProcessorForSavingPaginatedHistories(ProjectId projectId, UserId userId) {
return page -> {
if (isNotEmpty(page)) {
Set<EntityPostCoordinationHistory> 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<PostCoordinationViewEvent> 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);
}
};
}
Expand All @@ -140,33 +127,6 @@ private void saveMultipleEntityPostCoordinationHistories(Set<EntityPostCoordinat
}


PostCoordinationSpecification enrichWithMissingAxis(String entityType, PostCoordinationSpecification specification, List<LinearizationDefinition> definitionList, List<TableConfiguration> 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);
}
Expand All @@ -178,10 +138,8 @@ public void addSpecificationRevision(WhoficEntityPostCoordinationSpecification n
existingHistoryOptional.ifPresentOrElse(history -> {

WhoficEntityPostCoordinationSpecification oldSpec = eventProcessor.processHistory(history);

Set<PostCoordinationViewEvent> specEvents = SpecificationToEventsMapper.createEventsFromDiff(oldSpec, newSpecification);


if (!specEvents.isEmpty()) {
var newRevision = PostCoordinationSpecificationRevision.create(userId, specEvents, changeRequestId);
repository.addSpecificationRevision(newSpecification.whoficEntityIri(), projectId, newRevision);
Expand Down Expand Up @@ -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<LinearizationDefinition> definitionList = linearizationService.getLinearizationDefinitions();
List<TableConfiguration> 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));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<LinearizationDefinition> linearizationDefinitions = objectMapper.readValue(labels, new TypeReference<>() {
});
File tableConfig = new File("src/test/resources/postcoordinationTableConfig.json");
List<TableConfiguration> 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<LinearizationDefinition> linearizationDefinitions = objectMapper.readValue(labels, new TypeReference<>() {
});
File tableConfig = new File("src/test/resources/postcoordinationTableConfig.json");
List<TableConfiguration> 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() {
Expand Down

0 comments on commit 159ecb0

Please sign in to comment.