diff --git a/src/main/java/org/ihtsdo/otf/transformationandtemplate/service/componenttransform/HighLevelAuthoringService.java b/src/main/java/org/ihtsdo/otf/transformationandtemplate/service/componenttransform/HighLevelAuthoringService.java index 0cb6354..861a376 100644 --- a/src/main/java/org/ihtsdo/otf/transformationandtemplate/service/componenttransform/HighLevelAuthoringService.java +++ b/src/main/java/org/ihtsdo/otf/transformationandtemplate/service/componenttransform/HighLevelAuthoringService.java @@ -13,6 +13,7 @@ import java.util.concurrent.TimeoutException; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.google.common.collect.Sets.difference; import static java.lang.String.format; @@ -209,12 +210,38 @@ public List> updateDescriptions( List concepts = new ArrayList <>(); for (List descriptionProcessingBatch : Iterables.partition(descriptions, processingBatchMaxSize)) { Set descriptionIds = descriptionProcessingBatch.stream().map(DescriptionPojo::getDescriptionId).collect(Collectors.toSet()); - concepts.addAll(snowstormClient.getFullConcepts(SnowstormClient.ConceptBulkLoadRequest.byDescriptionId(descriptionIds), projectBranchPath)); + List fullConcepts = snowstormClient.getFullConcepts(SnowstormClient.ConceptBulkLoadRequest.byDescriptionId(descriptionIds), projectBranchPath); + concepts.addAll(fullConcepts.stream() + .filter(c1 -> concepts.stream().noneMatch(c2 -> c1.equals(c2))) + .collect(Collectors.toList()) + ); } - Set descriptionsFound = new HashSet<>(); Map conceptMap = concepts.stream().collect(Collectors.toMap(ConceptPojo::getConceptId, Function.identity())); Map descriptionIdMap = descriptions.stream().collect(Collectors.toMap(DescriptionPojo::getDescriptionId, Function.identity())); + + Map descriptionsFound = new HashMap <>(); + for (ConceptPojo loadedConcept : concepts) { + for (DescriptionPojo loadedDescription : loadedConcept.getDescriptions()) { + DescriptionPojo descriptionUpdate = descriptionIdMap.get(loadedDescription.getDescriptionId()); + if (descriptionUpdate != null) { + descriptionsFound.put(descriptionUpdate.getDescriptionId(), loadedDescription.getConceptId()); + } + } + } + + // Fail all descriptions which were not found to update + for (String notFoundDescriptionId : difference(descriptionIdMap.keySet(), descriptionsFound.keySet())) { + getChangeResult(changes, descriptionIdMap.get(notFoundDescriptionId), DESCRIPTION_WITH_ID_COMPARATOR).fail("Description not found on the specified branch."); + } + + // Update conceptID to ChangeResult + for (ChangeResult changeResult : changes) { + if (descriptionsFound.containsKey(changeResult.id())) { + changeResult.getComponent().setConceptId(descriptionsFound.get(changeResult.id())); + } + } + // Split into batches int batchNumber = 0; for (List conceptTaskBatch : Iterables.partition(conceptMap.values(), request.getBatchSize())) { @@ -223,15 +250,10 @@ public List> updateDescriptions( // Split into smaller batches if the number of per branch changes exceeds the number of concepts which should be processed at a time. for (List conceptProcessingBatch : Iterables.partition(conceptTaskBatch, processingBatchMaxSize)) { - descriptionsFound.addAll(updateDescriptionBatch(conceptProcessingBatch, descriptionIdMap, changes, branchPath)); + updateDescriptionBatch(conceptProcessingBatch, descriptionIdMap, changes, branchPath); } } - // Fail all descriptions which were not found to update - for (String notFoundDescriptionId : difference(descriptionIdMap.keySet(), descriptionsFound)) { - getChangeResult(changes, descriptionIdMap.get(notFoundDescriptionId), DESCRIPTION_WITH_ID_COMPARATOR).fail("Description not found on the specified branch."); - } - // Mark all changes which have not failed as successful changes.stream().filter(change -> change.getSuccess() == null).forEach(ChangeResult::success); } catch (WebClientException | TimeoutException e) {// This RuntimeException is thrown by WebClient @@ -522,15 +544,13 @@ public List> updateAxioms( return new ArrayList<>(changes); } - private Set updateDescriptionBatch(List concepts, Map descriptionIdMap, List> changes, String branchPath) throws BusinessServiceException, TimeoutException { + private void updateDescriptionBatch(List concepts, Map descriptionIdMap, List> changes, String branchPath) throws BusinessServiceException, TimeoutException { // Update existing descriptions - Set descriptionsFound = new HashSet<>(); for (ConceptPojo loadedConcept : concepts) { for (DescriptionPojo loadedDescription : loadedConcept.getDescriptions()) { DescriptionPojo descriptionUpdate = descriptionIdMap.get(loadedDescription.getDescriptionId()); if (descriptionUpdate != null) { descriptionUpdate.setConceptId(loadedConcept.getConceptId()); - descriptionsFound.add(descriptionUpdate.getDescriptionId()); if (descriptionUpdate.getCaseSignificance() != null) { loadedDescription.setCaseSignificance(descriptionUpdate.getCaseSignificance()); } @@ -549,12 +569,8 @@ private Set updateDescriptionBatch(List concepts, Map conceptMap = concepts.stream().collect(Collectors.toMap(ConceptPojo::getConceptId, Function.identity())); - bulkValidateThenUpdateConcepts(conceptMap, branchPath, changes); - } - - return descriptionsFound; + Map conceptMap = concepts.stream().collect(Collectors.toMap(ConceptPojo::getConceptId, Function.identity())); + bulkValidateThenUpdateConcepts(conceptMap, branchPath, changes); } private void updateAxiomBatch(List axiomBatch, List> changesBatch, String branchPath) throws BusinessServiceException, TimeoutException { @@ -657,7 +673,7 @@ public void bulkValidateThenUpdateConcepts(Map change.getSuccess() == null && change.getComponent().getConceptId().equals(conceptWithError)) + .filter(change -> change.getSuccess() == null && change.getComponent().getConceptId() != null && change.getComponent().getConceptId().equals(conceptWithError)) .forEach(changeResult -> changeResult.fail(format("Concept validation errors: %s", conceptValidationResultMap.get(conceptWithError).toString()))); conceptMap.remove(conceptWithError); // Whole concept removed from map so changes will not appear in the update request.