Skip to content
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

#240: Fixed library loader attempting to load non-logic-libraries #241

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,8 +55,10 @@ public static List<org.cqframework.cql.elm.execution.Library> 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();
Expand All @@ -69,14 +67,12 @@ public static List<org.cqframework.cql.elm.execution.Library> 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()))
);
}
}
}
}
Expand All @@ -89,6 +85,29 @@ public static List<org.cqframework.cql.elm.execution.Library> 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<org.hl7.fhir.dstu3.model.Library> libraryResourceProvider) {
Expand Down
50 changes: 34 additions & 16 deletions r4/src/main/java/org/opencds/cqf/r4/helpers/LibraryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,16 +51,17 @@ public static List<org.cqframework.cql.elm.execution.Library> 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("/")) {
Expand All @@ -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()))
);
}
}

Expand All @@ -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<org.hl7.fhir.r4.model.Library> libraryResourceProvider) {
Expand Down