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