001package ca.uhn.fhir.cql.common.provider;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server - Clinical Quality Language
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.cqframework.cql.cql2elm.FhirLibrarySourceProvider;
024import org.hl7.elm.r1.VersionedIdentifier;
025import org.opencds.cqf.cql.evaluator.cql2elm.content.LibraryContentType;
026
027import java.io.ByteArrayInputStream;
028import java.io.InputStream;
029import java.util.function.Function;
030
031public class LibraryContentProvider<LibraryType, AttachmentType>
032        implements org.opencds.cqf.cql.evaluator.cql2elm.content.LibraryContentProvider {
033        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(LibraryContentProvider.class);
034
035        private FhirLibrarySourceProvider innerProvider;
036        private LibraryResolutionProvider<LibraryType> provider;
037        private Function<LibraryType, Iterable<AttachmentType>> getAttachments;
038        private Function<AttachmentType, String> getContentType;
039        private Function<AttachmentType, byte[]> getContent;
040
041        public LibraryContentProvider(LibraryResolutionProvider<LibraryType> provider,
042                                                                                  Function<LibraryType, Iterable<AttachmentType>> getAttachments,
043                                                                                  Function<AttachmentType, String> getContentType, Function<AttachmentType, byte[]> getContent) {
044
045                this.innerProvider = new FhirLibrarySourceProvider();
046
047                this.provider = provider;
048                this.getAttachments = getAttachments;
049                this.getContentType = getContentType;
050                this.getContent = getContent;
051        }
052
053        @Override
054        public InputStream getLibraryContent(VersionedIdentifier versionedIdentifier, LibraryContentType libraryContentType){
055
056                // TODO: Support loading ELM
057                if (libraryContentType != LibraryContentType.CQL) {
058                        return null;
059                }
060
061                try {
062                        LibraryType lib = this.provider.resolveLibraryByName(versionedIdentifier.getId(),
063                                versionedIdentifier.getVersion());
064                        for (AttachmentType attachment : this.getAttachments.apply(lib)) {
065                                if ("text/cql".equals(this.getContentType.apply(attachment))) {
066                                        return new ByteArrayInputStream(this.getContent.apply(attachment));
067                                }
068                        }
069                } catch (Exception e) {
070                        ourLog.warn("Failed to parse Library source for VersionedIdentifier '" + versionedIdentifier + "'!"
071                                + System.lineSeparator() + e.getMessage(), e);
072                }
073
074                return this.innerProvider.getLibrarySource(versionedIdentifier);
075        }
076}