Skip to content

Commit 81d0811

Browse files
committed
Added reusable feature set from hapifhir/hapi-fhir-jpaserver-starter#784
1 parent ecd0507 commit 81d0811

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package ca.uhn.fhir.jpa.packages;
2+
3+
import ca.uhn.fhir.context.FhirContext;
4+
import ca.uhn.fhir.util.BundleBuilder;
5+
import org.hl7.fhir.instance.model.api.IBaseBundle;
6+
import org.hl7.fhir.instance.model.api.IBaseResource;
7+
import org.hl7.fhir.utilities.npm.NpmPackage;
8+
import org.jetbrains.annotations.NotNull;
9+
import org.springframework.stereotype.Service;
10+
11+
import java.io.ByteArrayInputStream;
12+
import java.io.IOException;
13+
import java.util.stream.Collectors;
14+
import java.util.*;
15+
16+
@Service
17+
public class AdditionalResourcesParser {
18+
19+
public IBaseBundle collectAdditionalResources(
20+
Set<String> additionalResources, PackageInstallationSpec packageInstallationSpec, FhirContext fhirContext) {
21+
NpmPackage npmPackage;
22+
try {
23+
npmPackage = NpmPackage.fromPackage(new ByteArrayInputStream(packageInstallationSpec.getPackageContents()));
24+
} catch (IOException e) {
25+
throw new RuntimeException(e);
26+
}
27+
var resources = getAdditionalResources(additionalResources, npmPackage, fhirContext);
28+
29+
var bundleBuilder = new BundleBuilder(fhirContext);
30+
resources.forEach(bundleBuilder::addTransactionUpdateEntry);
31+
return bundleBuilder.getBundle();
32+
}
33+
34+
@NotNull
35+
public List<IBaseResource> getAdditionalResources(
36+
Set<String> folderNames, NpmPackage npmPackage, FhirContext fhirContext) {
37+
38+
var npmFolders = folderNames.stream()
39+
.map(name -> npmPackage.getFolders().get(name))
40+
.filter(Objects::nonNull)
41+
.collect(Collectors.toList());
42+
43+
var resources = new LinkedList<IBaseResource>();
44+
for (var folder : npmFolders) {
45+
List<String> fileNames;
46+
try {
47+
fileNames = folder.getTypes().values().stream()
48+
.flatMap(Collection::stream)
49+
.collect(Collectors.toList());
50+
} catch (IOException e) {
51+
throw new RuntimeException(e.getMessage(), e);
52+
}
53+
54+
resources.addAll(fileNames.stream()
55+
.map(fileName -> {
56+
try {
57+
return new String(folder.fetchFile(fileName));
58+
} catch (IOException e) {
59+
throw new RuntimeException(e.getMessage(), e);
60+
}
61+
})
62+
.map(fhirContext.newJsonParser().setSuppressNarratives(true)::parseResource)
63+
.collect(Collectors.toList()));
64+
}
65+
return resources;
66+
}
67+
}

hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/packages/PackageInstallationSpec.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.util.ArrayList;
3131
import java.util.List;
32+
import java.util.Set;
3233
import java.util.function.Supplier;
3334

3435
@Schema(name = "PackageInstallationSpec", description = "Defines a set of instructions for package installation")
@@ -39,7 +40,8 @@
3940
"installMode",
4041
"installResourceTypes",
4142
"validationMode",
42-
"reloadExisting"
43+
"reloadExisting",
44+
"additionalResourceFolders"
4345
})
4446
@ExampleSupplier({PackageInstallationSpec.ExampleSupplier.class, PackageInstallationSpec.ExampleSupplier2.class})
4547
@JsonInclude(JsonInclude.Include.NON_NULL)
@@ -90,6 +92,12 @@ public class PackageInstallationSpec {
9092
"Any values provided here will be interpreted as a regex. Dependencies with an ID matching any regex will be skipped.")
9193
private List<String> myDependencyExcludes;
9294

95+
@Schema(
96+
description =
97+
"List of folders in the package to extract additional resources from. Each folder listed will be scanned for resources and installed.")
98+
@JsonProperty("additionalResourceFolders")
99+
private Set<String> additionalResourceFolders;
100+
93101
@JsonIgnore
94102
private byte[] myPackageContents;
95103

@@ -177,6 +185,14 @@ public void setReloadExisting(boolean reloadExisting) {
177185
this.myReloadExisting = reloadExisting;
178186
}
179187

188+
public Set<String> getAdditionalResourceFolders() {
189+
return additionalResourceFolders;
190+
}
191+
192+
public void setAdditionalResourceFolders(Set<String> additionalResourceFolders) {
193+
this.additionalResourceFolders = additionalResourceFolders;
194+
}
195+
180196
public PackageInstallationSpec addDependencyExclude(String theExclude) {
181197
getDependencyExcludes().add(theExclude);
182198
return this;

0 commit comments

Comments
 (0)