View Javadoc
1   package ca.uhn.fhir.jpa.dao.r4;
2   
3   import ca.uhn.fhir.context.FhirContext;
4   import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
5   import ca.uhn.fhir.jpa.dao.SearchParameterMap;
6   import ca.uhn.fhir.rest.api.server.IBundleProvider;
7   import ca.uhn.fhir.rest.param.StringParam;
8   import ca.uhn.fhir.rest.param.UriParam;
9   import org.hl7.fhir.instance.model.api.IAnyResource;
10  import org.hl7.fhir.instance.model.api.IBaseResource;
11  import org.hl7.fhir.r4.model.*;
12  import org.hl7.fhir.r4.model.ValueSet.ConceptSetComponent;
13  import org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionComponent;
14  import org.springframework.beans.BeansException;
15  import org.springframework.beans.factory.annotation.Autowired;
16  import org.springframework.context.ApplicationContext;
17  import org.springframework.context.ApplicationContextAware;
18  
19  import javax.annotation.PostConstruct;
20  import javax.transaction.Transactional;
21  import javax.transaction.Transactional.TxType;
22  import java.util.Collections;
23  import java.util.List;
24  
25  /*
26   * #%L
27   * HAPI FHIR JPA Server
28   * %%
29   * Copyright (C) 2014 - 2018 University Health Network
30   * %%
31   * Licensed under the Apache License, Version 2.0 (the "License");
32   * you may not use this file except in compliance with the License.
33   * You may obtain a copy of the License at
34   * 
35   *      http://www.apache.org/licenses/LICENSE-2.0
36   * 
37   * Unless required by applicable law or agreed to in writing, software
38   * distributed under the License is distributed on an "AS IS" BASIS,
39   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40   * See the License for the specific language governing permissions and
41   * limitations under the License.
42   * #L%
43   */
44  
45  @Transactional(value = TxType.REQUIRED)
46  public class JpaValidationSupportR4 implements IJpaValidationSupportR4, ApplicationContextAware {
47  
48  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JpaValidationSupportR4.class);
49  
50  	private IFhirResourceDao<StructureDefinition> myStructureDefinitionDao;
51  	private IFhirResourceDao<ValueSet> myValueSetDao;
52  	private IFhirResourceDao<Questionnaire> myQuestionnaireDao;
53  	private IFhirResourceDao<CodeSystem> myCodeSystemDao;
54  
55  	@Autowired
56  	private FhirContext myR4Ctx;
57  	private ApplicationContext myApplicationContext;
58  
59  	/**
60  	 * Constructor
61  	 */
62  	public JpaValidationSupportR4() {
63  		super();
64  	}
65  
66  
67  	@Override
68  	@Transactional(value = TxType.SUPPORTS)
69  	public ValueSetExpansionComponent expandValueSet(FhirContext theCtx, ConceptSetComponent theInclude) {
70  		return null;
71  	}
72  
73  	@Override
74  	public List<IBaseResource> fetchAllConformanceResources(FhirContext theContext) {
75  		return null;
76  	}
77  
78  	@Override
79  	@Transactional(value = TxType.SUPPORTS)
80  	public List<StructureDefinition> fetchAllStructureDefinitions(FhirContext theContext) {
81  		return Collections.emptyList();
82  	}
83  
84  	@Override
85  	public CodeSystem fetchCodeSystem(FhirContext theCtx, String theSystem) {
86  		return fetchResource(theCtx, CodeSystem.class, theSystem);
87  	}
88  
89  	@SuppressWarnings("unchecked")
90  	@Override
91  	public <T extends IBaseResource> T fetchResource(FhirContext theContext, Class<T> theClass, String theUri) {
92  		IdType id = new IdType(theUri);
93  		boolean localReference = false;
94  		if (id.hasBaseUrl() == false && id.hasIdPart() == true) {
95  			localReference = true;
96  		}
97  
98  		String resourceName = myR4Ctx.getResourceDefinition(theClass).getName();
99  		IBundleProvider search;
100 		if ("ValueSet".equals(resourceName)) {
101 			if (localReference) {
102 				SearchParameterMap params = new SearchParameterMap();
103 				params.setLoadSynchronousUpTo(1);
104 				params.add(IAnyResource.SP_RES_ID, new StringParam(theUri));
105 				search = myValueSetDao.search(params);
106 				if (search.size() == 0) {
107 					params = new SearchParameterMap();
108 					params.setLoadSynchronousUpTo(1);
109 					params.add(ValueSet.SP_URL, new UriParam(theUri));
110 					search = myValueSetDao.search(params);
111 				}
112 			} else {
113 				SearchParameterMap params = new SearchParameterMap();
114 				params.setLoadSynchronousUpTo(1);
115 				params.add(ValueSet.SP_URL, new UriParam(theUri));
116 				search = myValueSetDao.search(params);
117 			}
118 		} else if ("StructureDefinition".equals(resourceName)) {
119 			if (theUri.startsWith("http://hl7.org/fhir/StructureDefinition/")) {
120 				return null;
121 			}
122 			SearchParameterMap params = new SearchParameterMap();
123 			params.setLoadSynchronousUpTo(1);
124 			params.add(StructureDefinition.SP_URL, new UriParam(theUri));
125 			search = myStructureDefinitionDao.search(params);
126 		} else if ("Questionnaire".equals(resourceName)) {
127 			SearchParameterMap params = new SearchParameterMap();
128 			params.setLoadSynchronousUpTo(1);
129 			params.add(IAnyResource.SP_RES_ID, new StringParam(id.getIdPart()));
130 			search = myQuestionnaireDao.search(params);
131 		} else if ("CodeSystem".equals(resourceName)) {
132 			SearchParameterMap params = new SearchParameterMap();
133 			params.setLoadSynchronousUpTo(1);
134 			params.add(CodeSystem.SP_URL, new UriParam(theUri));
135 			search = myCodeSystemDao.search(params);
136 		} else {
137 			throw new IllegalArgumentException("Can't fetch resource type: " + resourceName);
138 		}
139 
140 		if (search.size() == 0) {
141 			return null;
142 		}
143 
144 		if (search.size() > 1) {
145 			ourLog.warn("Found multiple {} instances with URL search value of: {}", resourceName, theUri);
146 		}
147 
148 		return (T) search.getResources(0, 1).get(0);
149 	}
150 
151 	@Override
152 	public StructureDefinition fetchStructureDefinition(FhirContext theCtx, String theUrl) {
153 		return fetchResource(theCtx, StructureDefinition.class, theUrl);
154 	}
155 
156 	@Override
157 	@Transactional(value = TxType.SUPPORTS)
158 	public boolean isCodeSystemSupported(FhirContext theCtx, String theSystem) {
159 		return false;
160 	}
161 
162 	@Override
163 	public void setApplicationContext(ApplicationContext theApplicationContext) throws BeansException {
164 		myApplicationContext = theApplicationContext;
165 	}
166 
167 	@PostConstruct
168 	public void start() {
169 		myStructureDefinitionDao = myApplicationContext.getBean("myStructureDefinitionDaoR4", IFhirResourceDao.class);
170 		myValueSetDao = myApplicationContext.getBean("myValueSetDaoR4", IFhirResourceDao.class);
171 		myQuestionnaireDao = myApplicationContext.getBean("myQuestionnaireDaoR4", IFhirResourceDao.class);
172 		myCodeSystemDao = myApplicationContext.getBean("myCodeSystemDaoR4", IFhirResourceDao.class);
173 	}
174 
175 	@Override
176 	@Transactional(value = TxType.SUPPORTS)
177 	public CodeValidationResult validateCode(FhirContext theCtx, String theCodeSystem, String theCode, String theDisplay) {
178 		return null;
179 	}
180 
181 }