|
| 1 | +package org.opencds.cqf.tooling.packaging; |
| 2 | + |
| 3 | +import ca.uhn.fhir.context.FhirContext; |
| 4 | +import ca.uhn.fhir.rest.client.api.IGenericClient; |
| 5 | +import org.apache.commons.io.FilenameUtils; |
| 6 | +import org.hl7.fhir.instance.model.api.IBaseResource; |
| 7 | +import org.opencds.cqf.tooling.utilities.IOUtils; |
| 8 | +import org.opencds.cqf.tooling.utilities.ResourceUtils; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.LinkedHashSet; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +public abstract class Package<T extends IBaseResource> { |
| 17 | + |
| 18 | + private static final Logger logger = LoggerFactory.getLogger(Package.class); |
| 19 | + |
| 20 | + /* |
| 21 | +
|
| 22 | + CRMI Packaging Guidance |
| 23 | +
|
| 24 | + In general, artifacts such as libraries, measures, and test cases can be packaged as a Bundle of type transaction. |
| 25 | + However, since large artifact packages may span multiple bundles, the type collection MAY be used as well. In that |
| 26 | + case, the bundles SHOULD be processed as a unit (i.e. as a single transaction). The intent of splitting the bundles |
| 27 | + is to allow large packages to be processed, but in the case that they are split, transactional considerations are |
| 28 | + the responsibility of the consumer. |
| 29 | +
|
| 30 | + An artifact bundle contains the artifact as the first entry in the bundle, and optionally the dependencies and |
| 31 | + associated artifacts as subsequent entries as follows: |
| 32 | + 1. Artifact: The main artifact resource for the package |
| 33 | + 2. Dependencies: Any dependent artifact referenced by the main artifact |
| 34 | + 3. Test Cases: Any test cases defined for the artifact |
| 35 | +
|
| 36 | + */ |
| 37 | + |
| 38 | + private T mainArtifact; |
| 39 | + private IBaseResource primaryLibrary; |
| 40 | + private Set<IBaseResource> dependencies; |
| 41 | + private TestPackage<?,?> testPackage; |
| 42 | + private String igRoot; |
| 43 | + private FhirContext fhirContext; |
| 44 | + private boolean includeDependencies; |
| 45 | + private boolean includeTerminology; |
| 46 | + private boolean includeTests; |
| 47 | + private String fhirServerUrl; |
| 48 | + private IGenericClient fhirClient; |
| 49 | + private String bundleOutputPath; |
| 50 | + |
| 51 | + public Package(String igRoot, FhirContext fhirContext, boolean includeDependencies, boolean includeTerminology, boolean includeTests, String fhirServerUrl) { |
| 52 | + this.igRoot = igRoot; |
| 53 | + this.fhirContext = fhirContext; |
| 54 | + this.includeDependencies = includeDependencies; |
| 55 | + this.includeTerminology = includeTerminology; |
| 56 | + this.includeTests = includeTests; |
| 57 | + this.fhirServerUrl = fhirServerUrl; |
| 58 | + if (this.fhirServerUrl != null) { |
| 59 | + this.fhirClient = this.fhirContext.newRestfulGenericClient(fhirServerUrl); |
| 60 | + } |
| 61 | + this.bundleOutputPath = FilenameUtils.concat(igRoot, "bundles"); |
| 62 | + } |
| 63 | + |
| 64 | + public abstract T resolveMainArtifact(); |
| 65 | + public abstract Set<IBaseResource> resolveDependencies(T mainArtifact); |
| 66 | + public abstract TestPackage<?, ?> resolveTests(T mainArtifact); |
| 67 | + public abstract void output(); |
| 68 | + |
| 69 | + public void packageArtifact() { |
| 70 | + this.mainArtifact = resolveMainArtifact(); |
| 71 | + this.dependencies = resolveDependencies(this.mainArtifact); |
| 72 | + if (includeTests) { |
| 73 | + this.testPackage = resolveTests(this.mainArtifact); |
| 74 | + } |
| 75 | + output(); |
| 76 | + } |
| 77 | + |
| 78 | + public void resolvePrimaryLibraryDependencies(IBaseResource mainArtifact, FhirContext fhirContext, LinkedHashSet<IBaseResource> dependencies) { |
| 79 | + var primaryLibrary = IOUtils.getLibraryUrlMap(fhirContext).get( |
| 80 | + ResourceUtils.getPrimaryLibraryUrl(mainArtifact, fhirContext)); |
| 81 | + if (getPrimaryLibrary() == null) { // we want to save the primary library for the artifact being bundled not dependency libraries |
| 82 | + setPrimaryLibrary(primaryLibrary); |
| 83 | + } |
| 84 | + |
| 85 | + var missingDependencies = new HashSet<String>(); |
| 86 | + if (includeDependencies) { |
| 87 | + dependencies.add(primaryLibrary); |
| 88 | + var dependencyLibraries = ResourceUtils.getDepLibraryResources( |
| 89 | + primaryLibrary, fhirContext, true, false, missingDependencies); |
| 90 | + dependencies.addAll(dependencyLibraries.values()); |
| 91 | + } |
| 92 | + |
| 93 | + if (includeTerminology) { |
| 94 | + var dependencyValueSets = ResourceUtils.getDepValueSetResources( |
| 95 | + primaryLibrary, fhirContext, true, missingDependencies); |
| 96 | + dependencies.addAll(dependencyValueSets.values()); |
| 97 | + } |
| 98 | + |
| 99 | + missingDependencies.forEach(missing -> logger.warn("Unable to package dependency: {}", missing)); |
| 100 | + } |
| 101 | + |
| 102 | + public T getMainArtifact() { |
| 103 | + return mainArtifact; |
| 104 | + } |
| 105 | + |
| 106 | + public void setMainArtifact(T mainArtifact) { |
| 107 | + this.mainArtifact = mainArtifact; |
| 108 | + } |
| 109 | + |
| 110 | + public IBaseResource getPrimaryLibrary() { |
| 111 | + return primaryLibrary; |
| 112 | + } |
| 113 | + |
| 114 | + public void setPrimaryLibrary(IBaseResource primaryLibrary) { |
| 115 | + this.primaryLibrary = primaryLibrary; |
| 116 | + } |
| 117 | + |
| 118 | + public Set<IBaseResource> getDependencies() { |
| 119 | + return dependencies; |
| 120 | + } |
| 121 | + |
| 122 | + public void setDependencies(Set<IBaseResource> dependencies) { |
| 123 | + this.dependencies = dependencies; |
| 124 | + } |
| 125 | + |
| 126 | + public TestPackage<?,?> getTestPackage() { |
| 127 | + return testPackage; |
| 128 | + } |
| 129 | + |
| 130 | + public void setTests(TestPackage<?,?> testPackage) { |
| 131 | + this.testPackage = testPackage; |
| 132 | + } |
| 133 | + |
| 134 | + public String getIgRoot() { |
| 135 | + return igRoot; |
| 136 | + } |
| 137 | + |
| 138 | + public void setIgRoot(String igRoot) { |
| 139 | + this.igRoot = igRoot; |
| 140 | + } |
| 141 | + |
| 142 | + public FhirContext getFhirContext() { |
| 143 | + return fhirContext; |
| 144 | + } |
| 145 | + |
| 146 | + public void setFhirContext(FhirContext fhirContext) { |
| 147 | + this.fhirContext = fhirContext; |
| 148 | + } |
| 149 | + |
| 150 | + public boolean isIncludeDependencies() { |
| 151 | + return includeDependencies; |
| 152 | + } |
| 153 | + |
| 154 | + public void setIncludeDependencies(boolean includeDependencies) { |
| 155 | + this.includeDependencies = includeDependencies; |
| 156 | + } |
| 157 | + |
| 158 | + public boolean isIncludeTerminology() { |
| 159 | + return includeTerminology; |
| 160 | + } |
| 161 | + |
| 162 | + public void setIncludeTerminology(boolean includeTerminology) { |
| 163 | + this.includeTerminology = includeTerminology; |
| 164 | + } |
| 165 | + |
| 166 | + public boolean isIncludeTests() { |
| 167 | + return includeTests; |
| 168 | + } |
| 169 | + |
| 170 | + public void setIncludeTests(boolean includeTests) { |
| 171 | + this.includeTests = includeTests; |
| 172 | + } |
| 173 | + |
| 174 | + public String getFhirServerUrl() { |
| 175 | + return fhirServerUrl; |
| 176 | + } |
| 177 | + |
| 178 | + public void setFhirServerUrl(String fhirServerUrl) { |
| 179 | + this.fhirServerUrl = fhirServerUrl; |
| 180 | + } |
| 181 | + |
| 182 | + public IGenericClient getFhirClient() { |
| 183 | + return fhirClient; |
| 184 | + } |
| 185 | + |
| 186 | + public void setFhirClient(IGenericClient fhirClient) { |
| 187 | + this.fhirClient = fhirClient; |
| 188 | + } |
| 189 | + |
| 190 | + public String getBundleOutputPath() { |
| 191 | + return bundleOutputPath; |
| 192 | + } |
| 193 | + |
| 194 | + public void setBundleOutputPath(String bundleOutputPath) { |
| 195 | + this.bundleOutputPath = bundleOutputPath; |
| 196 | + } |
| 197 | +} |
0 commit comments