Skip to content

Commit

Permalink
MAT-7060 update includedLibraries on cql change, move common logic to…
Browse files Browse the repository at this point in the history
… utils
  • Loading branch information
adongare committed Aug 2, 2024
1 parent 9865bea commit a11cea6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package gov.cms.madie.cqllibraryservice.config;

import gov.cms.madie.cqllibraryservice.repositories.CqlLibraryRepository;
import gov.cms.madie.cqllibraryservice.utils.LibraryUtils;
import gov.cms.madie.models.common.IncludedLibrary;
import gov.cms.madie.models.library.CqlLibrary;
import gov.cms.mat.cql.CqlTextParser;
import gov.cms.mat.cql.elements.IncludeProperties;
import io.mongock.api.annotations.ChangeUnit;
import io.mongock.api.annotations.Execution;
import io.mongock.api.annotations.RollbackExecution;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
Expand All @@ -32,7 +30,7 @@ public void addIncludedLibrariesProp(CqlLibraryRepository repository) {
.map(
library -> {
List<IncludedLibrary> includedLibraries =
getIncludedLibraries(library.getCql());
LibraryUtils.getIncludedLibraries(library.getCql());
return library.toBuilder().includedLibraries(includedLibraries).build();
})
.toList();
Expand All @@ -52,21 +50,4 @@ public void rollbackExecution(MongoTemplate mongoTemplate) {
bulkOperations.execute();
log.info("Rollback included libraries update changelog complete");
}

private List<IncludedLibrary> getIncludedLibraries(String cql) {
if (StringUtils.isBlank(cql)) {
return List.of();
}
CqlTextParser cqlTextParser = new CqlTextParser(cql);
List<IncludeProperties> includeProperties = cqlTextParser.getIncludes();

return includeProperties.stream()
.map(
include ->
IncludedLibrary.builder()
.name(include.getName())
.version(include.getVersion())
.build())
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gov.cms.madie.cqllibraryservice.services.ActionLogService;
import gov.cms.madie.cqllibraryservice.services.LibrarySetService;
import gov.cms.madie.cqllibraryservice.utils.AuthUtils;
import gov.cms.madie.cqllibraryservice.utils.LibraryUtils;
import gov.cms.madie.models.common.ActionType;
import gov.cms.madie.models.library.CqlLibrary;
import gov.cms.madie.models.library.CqlLibraryDraft;
Expand All @@ -19,6 +20,7 @@
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import gov.cms.madie.models.library.LibrarySet;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -131,8 +133,9 @@ public ResponseEntity<CqlLibrary> updateCqlLibrary(
if (cqlLibraryService.isCqlLibraryNameChanged(cqlLibrary, persistedLibrary)) {
cqlLibraryService.checkDuplicateCqlLibraryName(cqlLibrary.getCqlLibraryName());
}
// update includedLibraries if cql changed
if (!StringUtils.equals(cqlLibrary.getCql(), persistedLibrary.getCql())) {
// get the elm
cqlLibrary.setIncludedLibraries(LibraryUtils.getIncludedLibraries(cqlLibrary.getCql()));
}
cqlLibrary.setLibrarySet(persistedLibrary.getLibrarySet());
cqlLibrary.setDraft(persistedLibrary.isDraft());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gov.cms.madie.cqllibraryservice.utils;

import gov.cms.madie.models.common.IncludedLibrary;
import gov.cms.mat.cql.CqlTextParser;
import gov.cms.mat.cql.elements.IncludeProperties;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class LibraryUtils {
public static List<IncludedLibrary> getIncludedLibraries(String cql) {
if (StringUtils.isBlank(cql)) {
return List.of();
}
CqlTextParser cqlTextParser = new CqlTextParser(cql);
List<IncludeProperties> includeProperties = cqlTextParser.getIncludes();

return includeProperties.stream()
.map(
include ->
IncludedLibrary.builder()
.name(include.getName())
.version(include.getVersion())
.build())
.toList();
}
}

0 comments on commit a11cea6

Please sign in to comment.