From a11cea66cfdef9c553b7777ee1f91b24333acfff Mon Sep 17 00:00:00 2001 From: adongare Date: Fri, 2 Aug 2024 12:38:27 -0400 Subject: [PATCH] MAT-7060 update includedLibraries on cql change, move common logic to utils --- .../AddIncludedLibrariesPropChangeUnit.java | 23 ++-------------- .../controllers/CqlLibraryController.java | 5 +++- .../cqllibraryservice/utils/LibraryUtils.java | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 src/main/java/gov/cms/madie/cqllibraryservice/utils/LibraryUtils.java diff --git a/src/main/java/gov/cms/madie/cqllibraryservice/config/AddIncludedLibrariesPropChangeUnit.java b/src/main/java/gov/cms/madie/cqllibraryservice/config/AddIncludedLibrariesPropChangeUnit.java index 2049416..55049ae 100644 --- a/src/main/java/gov/cms/madie/cqllibraryservice/config/AddIncludedLibrariesPropChangeUnit.java +++ b/src/main/java/gov/cms/madie/cqllibraryservice/config/AddIncludedLibrariesPropChangeUnit.java @@ -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; @@ -32,7 +30,7 @@ public void addIncludedLibrariesProp(CqlLibraryRepository repository) { .map( library -> { List includedLibraries = - getIncludedLibraries(library.getCql()); + LibraryUtils.getIncludedLibraries(library.getCql()); return library.toBuilder().includedLibraries(includedLibraries).build(); }) .toList(); @@ -52,21 +50,4 @@ public void rollbackExecution(MongoTemplate mongoTemplate) { bulkOperations.execute(); log.info("Rollback included libraries update changelog complete"); } - - private List getIncludedLibraries(String cql) { - if (StringUtils.isBlank(cql)) { - return List.of(); - } - CqlTextParser cqlTextParser = new CqlTextParser(cql); - List includeProperties = cqlTextParser.getIncludes(); - - return includeProperties.stream() - .map( - include -> - IncludedLibrary.builder() - .name(include.getName()) - .version(include.getVersion()) - .build()) - .toList(); - } } diff --git a/src/main/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryController.java b/src/main/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryController.java index 1742955..0cefdce 100644 --- a/src/main/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryController.java +++ b/src/main/java/gov/cms/madie/cqllibraryservice/controllers/CqlLibraryController.java @@ -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; @@ -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; @@ -131,8 +133,9 @@ public ResponseEntity 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()); diff --git a/src/main/java/gov/cms/madie/cqllibraryservice/utils/LibraryUtils.java b/src/main/java/gov/cms/madie/cqllibraryservice/utils/LibraryUtils.java new file mode 100644 index 0000000..408e27e --- /dev/null +++ b/src/main/java/gov/cms/madie/cqllibraryservice/utils/LibraryUtils.java @@ -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 getIncludedLibraries(String cql) { + if (StringUtils.isBlank(cql)) { + return List.of(); + } + CqlTextParser cqlTextParser = new CqlTextParser(cql); + List includeProperties = cqlTextParser.getIncludes(); + + return includeProperties.stream() + .map( + include -> + IncludedLibrary.builder() + .name(include.getName()) + .version(include.getVersion()) + .build()) + .toList(); + } +}