-
Notifications
You must be signed in to change notification settings - Fork 24
/
BaseFhirLibrarySourceProvider.java
63 lines (49 loc) · 2.49 KB
/
BaseFhirLibrarySourceProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package org.opencds.cqf.fhir.cql.cql2elm.content;
import static java.util.Objects.requireNonNull;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.cqframework.cql.cql2elm.LibraryContentType;
import org.cqframework.cql.cql2elm.LibrarySourceProvider;
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.ICompositeType;
import org.opencds.cqf.fhir.utility.adapter.AdapterFactory;
import org.opencds.cqf.fhir.utility.adapter.AttachmentAdapter;
import org.opencds.cqf.fhir.utility.adapter.LibraryAdapter;
/**
* This class implements logic for extracting content from a FHIR Library resource and provides an
* extension point for implementing the fetch of a FHIR library matching a specific identifier.
*/
public abstract class BaseFhirLibrarySourceProvider implements LibrarySourceProvider {
protected AdapterFactory adapterFactory;
protected BaseFhirLibrarySourceProvider(AdapterFactory adapterFactory) {
this.adapterFactory = requireNonNull(adapterFactory, "adapterFactory can not be null");
}
@Override
public InputStream getLibraryContent(VersionedIdentifier libraryIdentifier, LibraryContentType libraryContentType) {
requireNonNull(libraryIdentifier, "versionedIdentifier can not be null.");
requireNonNull(libraryContentType, "libraryContentType can not be null.");
IBaseResource library = this.getLibrary(libraryIdentifier);
if (library == null) {
return null;
}
return this.getContentStream(library, libraryContentType.mimeType());
}
protected InputStream getContentStream(IBaseResource library, String contentType) {
LibraryAdapter libraryAdapter = this.adapterFactory.createLibrary(library);
if (libraryAdapter.hasContent()) {
for (ICompositeType attachment : libraryAdapter.getContent()) {
AttachmentAdapter attachmentAdapter = this.adapterFactory.createAttachment(attachment);
if (attachmentAdapter.getContentType().equals(contentType)) {
return new ByteArrayInputStream(attachmentAdapter.getData());
}
}
}
return null;
}
@Override
public InputStream getLibrarySource(VersionedIdentifier libraryIdentifier) {
return getLibraryContent(libraryIdentifier, LibraryContentType.CQL);
}
protected abstract IBaseResource getLibrary(VersionedIdentifier libraryIdentifier);
}