diff --git a/dstu3/src/main/java/org/opencds/cqf/dstu3/helpers/LibraryHelper.java b/dstu3/src/main/java/org/opencds/cqf/dstu3/helpers/LibraryHelper.java index 5e187525a..f59466d00 100644 --- a/dstu3/src/main/java/org/opencds/cqf/dstu3/helpers/LibraryHelper.java +++ b/dstu3/src/main/java/org/opencds/cqf/dstu3/helpers/LibraryHelper.java @@ -7,12 +7,8 @@ import org.cqframework.cql.cql2elm.ModelManager; import org.cqframework.cql.elm.execution.Library; import org.cqframework.cql.elm.execution.VersionedIdentifier; -import org.hl7.fhir.dstu3.model.Measure; -import org.hl7.fhir.dstu3.model.PlanDefinition; -import org.hl7.fhir.dstu3.model.Reference; -import org.hl7.fhir.dstu3.model.RelatedArtifact; +import org.hl7.fhir.dstu3.model.*; import org.hl7.fhir.dstu3.model.RelatedArtifact.RelatedArtifactType; -import org.hl7.fhir.dstu3.model.Resource; import org.opencds.cqf.common.evaluation.LibraryLoader; import org.opencds.cqf.common.providers.LibraryResolutionProvider; import org.opencds.cqf.common.providers.LibrarySourceProvider; @@ -59,8 +55,10 @@ public static List loadLibraries(Meas } org.hl7.fhir.dstu3.model.Library library = libraryResourceProvider.resolveLibraryById(id); - libraries.add(libraryLoader - .load(new VersionedIdentifier().withId(library.getName()).withVersion(library.getVersion()))); + if (library != null && isLogicLibrary(library)) { + libraries.add(libraryLoader + .load(new VersionedIdentifier().withId(library.getName()).withVersion(library.getVersion()))); + } } VersionedIdentifier primaryLibraryId = libraries.get(0).getIdentifier(); @@ -69,14 +67,12 @@ public static List loadLibraries(Meas if (artifact.hasType() && artifact.getType().equals(RelatedArtifactType.DEPENDSON) && artifact.hasResource() && artifact.getResource().hasReference()) { if (artifact.getResource().getReferenceElement().getResourceType().equals("Library")) { org.hl7.fhir.dstu3.model.Library library = libraryResourceProvider.resolveLibraryById(artifact.getResource().getReferenceElement().getIdPart()); - - if (library == null) { - throw new IllegalArgumentException(String.format("Unable to resolve library reference: %s", artifact.getResource().getReference())); - } - libraries.add( - libraryLoader.load(new VersionedIdentifier().withId(library.getName()).withVersion(library.getVersion())) - ); + if (library != null && isLogicLibrary(library)) { + libraries.add( + libraryLoader.load(new VersionedIdentifier().withId(library.getName()).withVersion(library.getVersion())) + ); + } } } } @@ -89,6 +85,29 @@ public static List loadLibraries(Meas return libraries; } + private static boolean isLogicLibrary(org.hl7.fhir.dstu3.model.Library library) { + if (library == null) { + return false; + } + + if (!library.hasType()) { + return false; + } + + if (!library.getType().hasCoding()) { + return false; + } + + for (Coding c : library.getType().getCoding()) { + if (c.hasSystem() && c.getSystem().equals("http://hl7.org/fhir/library-type") + && c.hasCode() && c.getCode().equals("logic-library")) { + return true; + } + } + + return false; + } + public static Library resolveLibraryById(String libraryId, org.opencds.cqf.cql.engine.execution.LibraryLoader libraryLoader, LibraryResolutionProvider libraryResourceProvider) { diff --git a/r4/src/main/java/org/opencds/cqf/r4/helpers/LibraryHelper.java b/r4/src/main/java/org/opencds/cqf/r4/helpers/LibraryHelper.java index 6a6fe6520..3779ab74c 100644 --- a/r4/src/main/java/org/opencds/cqf/r4/helpers/LibraryHelper.java +++ b/r4/src/main/java/org/opencds/cqf/r4/helpers/LibraryHelper.java @@ -8,11 +8,7 @@ import org.cqframework.cql.cql2elm.ModelManager; import org.cqframework.cql.elm.execution.Library; import org.cqframework.cql.elm.execution.VersionedIdentifier; -import org.hl7.fhir.r4.model.CanonicalType; -import org.hl7.fhir.r4.model.Measure; -import org.hl7.fhir.r4.model.PlanDefinition; -import org.hl7.fhir.r4.model.RelatedArtifact; -import org.hl7.fhir.r4.model.Resource; +import org.hl7.fhir.r4.model.*; import org.opencds.cqf.common.evaluation.LibraryLoader; import org.opencds.cqf.common.providers.LibraryResolutionProvider; import org.opencds.cqf.common.providers.LibrarySourceProvider; @@ -55,16 +51,17 @@ public static List loadLibraries(Meas // We just loaded it into the server so we can access it by Id org.hl7.fhir.r4.model.Library library = libraryResourceProvider.resolveLibraryById(id); - - libraries.add( - libraryLoader.load(new VersionedIdentifier().withId(library.getName()).withVersion(library.getVersion())) - ); + if (library != null && isLogicLibrary(library)) { + libraries.add( + libraryLoader.load(new VersionedIdentifier().withId(library.getName()).withVersion(library.getVersion())) + ); + } } VersionedIdentifier primaryLibraryId = libraries.get(0).getIdentifier(); org.hl7.fhir.r4.model.Library primaryLibrary = libraryResourceProvider.resolveLibraryByName(primaryLibraryId.getId(), primaryLibraryId.getVersion()); for (RelatedArtifact artifact : primaryLibrary.getRelatedArtifact()) { - if (artifact.hasType() && artifact.getType().equals(RelatedArtifact.RelatedArtifactType.DEPENDSON) && artifact.hasResource() && artifact.hasResource()) { + if (artifact.hasType() && artifact.getType().equals(RelatedArtifact.RelatedArtifactType.DEPENDSON) && artifact.hasResource()) { org.hl7.fhir.r4.model.Library library = null; // Raw references to Library/libraryId or libraryId if (artifact.getResource().startsWith("Library/") || ! artifact.getResource().contains("/")) { @@ -75,13 +72,11 @@ else if (artifact.getResource().contains(("/Library/"))) { library = libraryResourceProvider.resolveLibraryByCanonicalUrl(artifact.getResource()); } - if (library == null) { - throw new IllegalArgumentException(String.format("Unable to resolve library reference: %s", artifact.getResource())); + if (library != null && isLogicLibrary(library)) { + libraries.add( + libraryLoader.load(new VersionedIdentifier().withId(library.getName()).withVersion(library.getVersion())) + ); } - - libraries.add( - libraryLoader.load(new VersionedIdentifier().withId(library.getName()).withVersion(library.getVersion())) - ); } } @@ -93,6 +88,29 @@ else if (artifact.getResource().contains(("/Library/"))) { return libraries; } + private static boolean isLogicLibrary(org.hl7.fhir.r4.model.Library library) { + if (library == null) { + return false; + } + + if (!library.hasType()) { + return false; + } + + if (!library.getType().hasCoding()) { + return false; + } + + for (Coding c : library.getType().getCoding()) { + if (c.hasSystem() && c.getSystem().equals("http://terminology.hl7.org/CodeSystem/library-type") + && c.hasCode() && c.getCode().equals("logic-library")) { + return true; + } + } + + return false; + } + public static Library resolveLibraryById(String libraryId, org.opencds.cqf.cql.engine.execution.LibraryLoader libraryLoader, LibraryResolutionProvider libraryResourceProvider) {