001package ca.uhn.fhir.cql.dstu3.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 ca.uhn.fhir.i18n.Msg; 024import ca.uhn.fhir.cql.common.provider.LibraryResolutionProvider; 025import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; 026import ca.uhn.fhir.jpa.partition.SystemRequestDetails; 027import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; 028import ca.uhn.fhir.rest.api.server.RequestDetails; 029import ca.uhn.fhir.rest.param.StringParam; 030import ca.uhn.fhir.rest.param.TokenParam; 031import ca.uhn.fhir.rest.param.UriParam; 032import org.hl7.fhir.dstu3.model.IdType; 033import org.hl7.fhir.dstu3.model.Library; 034import org.hl7.fhir.instance.model.api.IBaseResource; 035import org.springframework.beans.factory.annotation.Autowired; 036import org.springframework.stereotype.Component; 037 038import java.util.ArrayList; 039import java.util.List; 040import java.util.Objects; 041 042@Component 043public class LibraryResolutionProviderImpl implements LibraryResolutionProvider<Library> { 044 @Autowired 045 private IFhirResourceDao<Library> myLibraryDao; 046 047 // TODO: Figure out if we should throw an exception or something here. 048 @Override 049 public void update(Library library) { 050 myLibraryDao.update(library); 051 } 052 053 @Override 054 public Library resolveLibraryById(String libraryId, RequestDetails theRequestDetails) { 055 try { 056 return myLibraryDao.read(new IdType(libraryId), theRequestDetails); 057 } catch (Exception e) { 058 throw new IllegalArgumentException(Msg.code(1641) + String.format("Could not resolve library id %s", libraryId)); 059 } 060 } 061 062 @Override 063 public Library resolveLibraryByCanonicalUrl(String url, RequestDetails theRequestDetails) { 064 Objects.requireNonNull(url, "url must not be null"); 065 066 String[] parts = url.split("\\|"); 067 String resourceUrl = parts[0]; 068 String version = null; 069 if (parts.length > 1) { 070 version = parts[1]; 071 } 072 073 SearchParameterMap map = SearchParameterMap.newSynchronous(); 074 map.add("url", new UriParam(resourceUrl)); 075 if (version != null) { 076 map.add("version", new TokenParam(version)); 077 } 078 079 ca.uhn.fhir.rest.api.server.IBundleProvider bundleProvider = myLibraryDao.search(map, theRequestDetails); 080 081 if (bundleProvider.size() == null || bundleProvider.size() == 0) { 082 return null; 083 } 084 List<IBaseResource> resourceList = bundleProvider.getAllResources(); 085 return LibraryResolutionProvider.selectFromList(resolveLibraries(resourceList), version, x -> x.getVersion()); 086 } 087 088 @Override 089 public Library resolveLibraryByName(String libraryName, String libraryVersion) { 090 Iterable<org.hl7.fhir.dstu3.model.Library> libraries = getLibrariesByName(libraryName); 091 org.hl7.fhir.dstu3.model.Library library = LibraryResolutionProvider.selectFromList(libraries, libraryVersion, 092 x -> x.getVersion()); 093 094 if (library == null) { 095 throw new IllegalArgumentException(Msg.code(1642) + String.format("Could not resolve library name %s", libraryName)); 096 } 097 098 return library; 099 } 100 101 private Iterable<org.hl7.fhir.dstu3.model.Library> getLibrariesByName(String name) { 102 // Search for libraries by name 103 SearchParameterMap map = SearchParameterMap.newSynchronous(); 104 map.add("name", new StringParam(name, true)); 105 ca.uhn.fhir.rest.api.server.IBundleProvider bundleProvider = myLibraryDao.search(map, new SystemRequestDetails()); 106 107 if (bundleProvider.size() == null || bundleProvider.size() == 0) { 108 return new ArrayList<>(); 109 } 110 List<IBaseResource> resourceList = bundleProvider.getAllResources(); 111 return resolveLibraries(resourceList); 112 } 113 114 private Iterable<org.hl7.fhir.dstu3.model.Library> resolveLibraries(List<IBaseResource> resourceList) { 115 List<org.hl7.fhir.dstu3.model.Library> ret = new ArrayList<>(); 116 for (IBaseResource res : resourceList) { 117 Class<?> clazz = res.getClass(); 118 ret.add((org.hl7.fhir.dstu3.model.Library) clazz.cast(res)); 119 } 120 return ret; 121 } 122}