-
Notifications
You must be signed in to change notification settings - Fork 1
MAT-10217 Import and Organize CQL Libraries from Downloaded IG Packages #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ef30c8e
MAT-10217 Import and Organize CQL Libraries from Downloaded IG Packages
adongare 4f978fb
MAT-10217 local ObjectMapper
adongare 48f8623
MAT-10217 codacy checks
adongare 81e90e3
MAT-10217 increase test coverage, renamed method
adongare 4949fe1
MAT-10217 remove unused vars and import
adongare 21a6952
MAT-10217 change docs
adongare ca30ebb
MAT-10217 update docs
adongare 6efd678
MAT-10217 update docs
adongare be6fd72
MAT-10217 remove unnecessary doc comments
adongare 7772549
MAT-10217 update method docs
adongare 37b4843
MAT-10217 update method docs
adongare 07ff3c5
MAT-10217 update method signature to make it more reasbale
adongare 35ffe1a
MAT-10217 remove duplicate line
adongare 17933ff
MAT-10217 externalize default owner
adongare da67bd3
MAT-10217 ignore terminology packages, update docs/comments
adongare 6b88636
MAT-10217 renamed local vars to be more relevant
adongare b1e1ca3
MAT-10217 update docstring
adongare eca563b
Merge branch 'develop' into MAT-10217
adongare f81c300
MAT-10217 add test case
adongare d1faf17
MAT-10217 changed insatnce var canonical to packageCanonical
adongare b3ba9bf
MAT-10217 use jackson 3 classes
adongare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/gov/cms/madie/cqllibraryservice/models/ExternalLibrary.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package gov.cms.madie.cqllibraryservice.models; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
| import org.springframework.data.annotation.Id; | ||
| import org.springframework.data.mongodb.core.index.CompoundIndex; | ||
| import org.springframework.data.mongodb.core.mapping.Document; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| /** | ||
| * Represents a External CQL Library imported from a FHIR Implementation Guide (IG) package. | ||
| * | ||
| * <p>External CQL Libraries are separate from user-created CQL Libraries. They are not editable by | ||
| * users and are not included in user library validations, but are available for use by translation, | ||
| * execution, and the CQL Builder. | ||
| */ | ||
| @Data | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Document | ||
| @CompoundIndex( | ||
| name = "ns_canonical_library_name_version_idx", | ||
| def = "{'packageCanonical': 1, 'libraryName': 1, 'version': 1}", | ||
| unique = true) | ||
| public class ExternalLibrary { | ||
|
|
||
| @Id private String id; | ||
| private String librarySetId; | ||
|
|
||
| /** Machine-readable library name (e.g. FHIRCommon) */ | ||
| private String libraryName; | ||
|
|
||
| /** Human-readable library title (e.g. "FHIR Common") */ | ||
| private String libraryTitle; | ||
|
|
||
| private String description; | ||
| private String version; | ||
|
|
||
| /** | ||
| * The canonical URL of the IG that contains this library (from {@code package.json#canonical}). | ||
| */ | ||
| private String packageCanonical; | ||
|
|
||
| /** The NPM package name of the IG that contains this library (from {@code package.json#name}). */ | ||
| private String namespacePrefix; | ||
|
|
||
| /** The raw CQL source text. */ | ||
| private String cqlContent; | ||
|
|
||
| /** Always {@code false} – imported common libraries are never in draft state. */ | ||
| private boolean draft; | ||
|
|
||
| /** The system owner for common libraries. */ | ||
| private String publisher; | ||
|
|
||
| /** The system owner for common libraries. */ | ||
| private String createdBy; | ||
|
|
||
| /** Timestamp when this library was first imported into the system. */ | ||
| private Instant dateImported; | ||
|
|
||
| /** The original FHIR resource JSON string */ | ||
| private String fhirResource; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/gov/cms/madie/cqllibraryservice/repositories/ExternalLibraryRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package gov.cms.madie.cqllibraryservice.repositories; | ||
|
|
||
| import gov.cms.madie.cqllibraryservice.models.ExternalLibrary; | ||
| import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface ExternalLibraryRepository extends MongoRepository<ExternalLibrary, String> { | ||
|
|
||
| Optional<ExternalLibrary> findByPackageCanonicalAndLibraryName( | ||
| String namespaceCanonical, String libraryName); | ||
|
|
||
| boolean existsByPackageCanonicalAndLibraryNameAndVersion( | ||
| String canonical, String libraryName, String version); | ||
| } |
255 changes: 255 additions & 0 deletions
255
src/main/java/gov/cms/madie/cqllibraryservice/services/ExternalLibraryDiscoveryService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| package gov.cms.madie.cqllibraryservice.services; | ||
|
|
||
| import tools.jackson.databind.JsonNode; | ||
| import tools.jackson.databind.ObjectMapper; | ||
| import tools.jackson.databind.node.ObjectNode; | ||
| import gov.cms.madie.cqllibraryservice.models.ExternalLibrary; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.hl7.fhir.utilities.json.model.JsonObject; | ||
| import org.hl7.fhir.utilities.npm.NpmPackage; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.Base64; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Discovers valid CQL Libraries from a FHIR NPM package. | ||
| * | ||
| * <p>A valid CQL Library must satisfy: | ||
| * | ||
| * <ul> | ||
| * <li>{@code resourceType == "Library"} | ||
| * <li>{@code type.coding[*].code == "logic-library"} | ||
| * <li>At least one {@code content} entry with {@code contentType == "text/cql"} | ||
| * </ul> | ||
| */ | ||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class ExternalLibraryDiscoveryService { | ||
|
|
||
| /** | ||
| * Discovers all valid CQL logic libraries from the given package path. | ||
| * | ||
| * @param packagePath the filesystem path of the extracted NPM package | ||
| * @param igPackageId the IG package ID (e.g. {@code hl7.fhir.us.qicore}) | ||
| * @param igPackageVersion the IG package version (e.g. {@code 6.0.0}) | ||
| * @return candidate {@link ExternalLibrary} objects ready for persistence | ||
| */ | ||
| public List<ExternalLibrary> discoverLibrariesForPackage( | ||
| String packagePath, String igPackageId, String igPackageVersion) { | ||
| List<ExternalLibrary> allLibraries = new ArrayList<>(); | ||
| try { | ||
| NpmPackage npmPackage = NpmPackage.fromFolder(packagePath); | ||
| allLibraries.addAll(discoverLibraries(npmPackage, igPackageId, igPackageVersion)); | ||
| } catch (IOException e) { | ||
| log.error( | ||
| "Failed to load NpmPackage from path [{}] for package [{}#{}]: {}", | ||
| packagePath, | ||
| igPackageId, | ||
| igPackageVersion, | ||
| e.getMessage()); | ||
| } | ||
| return allLibraries; | ||
| } | ||
|
|
||
| /** | ||
| * Discovers all valid CQL logic libraries from the given in-memory {@link NpmPackage}. | ||
| * | ||
| * @param npmPackage the loaded NPM package | ||
| * @param igPackageId the IG package ID | ||
| * @param igPackageVersion the IG package version | ||
| * @return candidate {@link ExternalLibrary} objects ready for persistence | ||
| */ | ||
| public List<ExternalLibrary> discoverLibraries( | ||
| NpmPackage npmPackage, String igPackageId, String igPackageVersion) { | ||
| List<ExternalLibrary> discoveredLibraries = new ArrayList<>(); | ||
|
|
||
| JsonObject npm = npmPackage.getNpm(); | ||
| if (npm == null) { | ||
| log.warn("Package [{}#{}] has no package.json – skipping", igPackageId, igPackageVersion); | ||
| return discoveredLibraries; | ||
| } | ||
|
|
||
| String packageCanonical = npm.asString("canonical"); | ||
| String namespacePrefix = npm.asString("name"); | ||
|
|
||
| if (StringUtils.isBlank(packageCanonical)) { | ||
| log.warn( | ||
| "Package [{}#{}] package.json is missing 'canonical' – skipping", | ||
| igPackageId, | ||
| igPackageVersion); | ||
| return discoveredLibraries; | ||
| } | ||
| if (StringUtils.isBlank(namespacePrefix)) { | ||
| log.warn( | ||
| "Package [{}#{}] package.json is missing 'name' – skipping discovery", | ||
| igPackageId, | ||
| igPackageVersion); | ||
| } | ||
|
|
||
| List<String> libraryFiles; | ||
| try { | ||
| libraryFiles = npmPackage.listResources("Library"); | ||
| } catch (IOException e) { | ||
| log.error( | ||
| "Failed to list Library resources in package [{}#{}]: {}", | ||
| igPackageId, | ||
| igPackageVersion, | ||
| e.getMessage()); | ||
| return discoveredLibraries; | ||
| } | ||
|
|
||
| log.info( | ||
| "Found {} Library resource(s) in package [{}#{}]", | ||
| libraryFiles.size(), | ||
| igPackageId, | ||
| igPackageVersion); | ||
|
|
||
| for (String filename : libraryFiles) { | ||
| try (InputStream resourceStream = npmPackage.load("package", filename)) { | ||
| ExternalLibrary discoveredLibrary = | ||
| parseLibraryResource(resourceStream, packageCanonical, namespacePrefix, filename); | ||
| if (discoveredLibrary != null) { | ||
| discoveredLibraries.add(discoveredLibrary); | ||
| log.debug( | ||
| "Discovered CQL Library [{}] v[{}] from package [{}#{}]", | ||
| discoveredLibrary.getLibraryName(), | ||
| discoveredLibrary.getVersion(), | ||
| igPackageId, | ||
| igPackageVersion); | ||
| } | ||
| } catch (Exception e) { | ||
| log.warn( | ||
| "Skipping Library resource [{}] in package [{}#{}] due to error: {}", | ||
| filename, | ||
| igPackageId, | ||
| igPackageVersion, | ||
| e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| return discoveredLibraries; | ||
| } | ||
|
|
||
| /** | ||
| * Parses a single FHIR Library JSON resource and returns a {@link ExternalLibrary} candidate if | ||
| * it meets all criteria, or {@code null} if it should be ignored. | ||
| */ | ||
| private ExternalLibrary parseLibraryResource( | ||
| InputStream stream, String packageCanonical, String namespacePrefix, String filename) { | ||
|
|
||
| JsonNode root = new ObjectMapper().readTree(stream); | ||
|
adongare marked this conversation as resolved.
|
||
|
|
||
| // 1. Must be resourceType == "Library" | ||
| if (!"Library".equals(root.path("resourceType").asString(null))) { | ||
| log.debug("Ignoring non-Library resource in file [{}]", filename); | ||
| return null; | ||
| } | ||
|
|
||
| // 2. Must have type.coding[*].code == "logic-library" | ||
| if (!hasLogicLibraryType(root)) { | ||
| log.debug("Ignoring Library resource [{}] – not a logic-library type", filename); | ||
| return null; | ||
| } | ||
|
|
||
| // 3. Must have cql(contentType == "text/cql") | ||
| String cqlContent = extractCqlContent(root); | ||
| if (cqlContent == null) { | ||
| log.debug("Ignoring Library resource [{}] – no text/cql content found", filename); | ||
| return null; | ||
| } | ||
|
|
||
| String libraryName = root.path("name").asString(null); | ||
| String version = root.path("version").asString(null); | ||
| String title = root.path("title").asString(null); | ||
| String description = root.path("description").asString(null); | ||
| String publisher = root.path("publisher").asString(null); | ||
|
|
||
| if (StringUtils.isBlank(libraryName)) { | ||
| log.warn("Library resource [{}] is missing 'name' – skipping", filename); | ||
| return null; | ||
| } | ||
| if (StringUtils.isBlank(version)) { | ||
| log.warn( | ||
| "Library resource [{}] (name={}) is missing 'version' – skipping", filename, libraryName); | ||
| return null; | ||
| } | ||
|
|
||
| return ExternalLibrary.builder() | ||
| .libraryName(libraryName) | ||
| .libraryTitle(title) | ||
| .version(version) | ||
| .description(description) | ||
| .packageCanonical(packageCanonical) | ||
| .namespacePrefix(namespacePrefix) | ||
| .publisher(publisher) | ||
| .cqlContent(cqlContent) | ||
| .fhirResource(stripContentData(root)) | ||
| .draft(false) | ||
| .dateImported(Instant.now()) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a JSON string of the FHIR resource with the entire {@code content} array removed. | ||
| * | ||
| * <p>FHIR Library resources embed base64-encoded payloads (CQL, ELM XML, ELM JSON, etc.) inside | ||
| * the {@code content} array. These can be several MB each and, when stored verbatim, produce | ||
| * documents large enough to crash MongoDB Compass. The decoded CQL is already persisted in the | ||
|
adongare marked this conversation as resolved.
|
||
| * dedicated {@code cqlContent} field, so dropping the whole array here is safe. | ||
| */ | ||
| private String stripContentData(JsonNode root) { | ||
| ObjectNode copy = root.deepCopy().asObject(); | ||
| copy.remove("content"); | ||
| return copy.toString(); | ||
| } | ||
|
|
||
| /** Returns {@code true} if the resource has {@code type.coding[*].code == "logic-library"}. */ | ||
| private boolean hasLogicLibraryType(JsonNode root) { | ||
| JsonNode codings = root.path("type").path("coding"); | ||
| if (codings.isMissingNode() || !codings.isArray()) { | ||
| return false; | ||
| } | ||
| for (JsonNode coding : codings) { | ||
| if ("logic-library".equals(coding.path("code").asString(null))) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Finds the first {@code content} entry whose {@code contentType} is {@code "text/cql"} and | ||
| * returns the base64-decoded CQL string. Returns {@code null} if no such entry exists. | ||
| */ | ||
| private String extractCqlContent(JsonNode root) { | ||
| JsonNode contentArray = root.path("content"); | ||
| if (contentArray.isMissingNode() || !contentArray.isArray()) { | ||
| return null; | ||
| } | ||
| for (JsonNode content : contentArray) { | ||
| if ("text/cql".equals(content.path("contentType").asString(null))) { | ||
| String base64Data = content.path("data").asString(null); | ||
| if (base64Data != null) { | ||
| try { | ||
| return new String(Base64.getDecoder().decode(base64Data), StandardCharsets.UTF_8); | ||
| } catch (IllegalArgumentException e) { | ||
| log.warn("Failed to base64-decode CQL content: {}", e.getMessage()); | ||
| return null; | ||
| } | ||
| } | ||
| // data field absent but contentType matches – treat as no CQL available | ||
| return null; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.