View Javadoc
1   package ca.uhn.fhir.jpa.dao;
2   
3   import ca.uhn.fhir.context.FhirContext;
4   import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
5   import ca.uhn.fhir.model.dstu2.resource.Questionnaire;
6   import ca.uhn.fhir.rest.api.server.IBundleProvider;
7   import ca.uhn.fhir.rest.param.TokenParam;
8   import ca.uhn.fhir.rest.param.UriParam;
9   import org.hl7.fhir.instance.model.IdType;
10  import org.hl7.fhir.instance.model.StructureDefinition;
11  import org.hl7.fhir.instance.model.ValueSet;
12  import org.hl7.fhir.instance.model.ValueSet.ConceptSetComponent;
13  import org.hl7.fhir.instance.model.ValueSet.ValueSetExpansionComponent;
14  import org.hl7.fhir.instance.model.api.IBaseResource;
15  import org.springframework.beans.factory.annotation.Autowired;
16  import org.springframework.beans.factory.annotation.Qualifier;
17  
18  import javax.transaction.Transactional;
19  import javax.transaction.Transactional.TxType;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  /*
24   * #%L
25   * HAPI FHIR JPA Server
26   * %%
27   * Copyright (C) 2014 - 2019 University Health Network
28   * %%
29   * Licensed under the Apache License, Version 2.0 (the "License");
30   * you may not use this file except in compliance with the License.
31   * You may obtain a copy of the License at
32   * 
33   *      http://www.apache.org/licenses/LICENSE-2.0
34   * 
35   * Unless required by applicable law or agreed to in writing, software
36   * distributed under the License is distributed on an "AS IS" BASIS,
37   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38   * See the License for the specific language governing permissions and
39   * limitations under the License.
40   * #L%
41   */
42  
43  @Transactional(value = TxType.REQUIRED)
44  public class JpaValidationSupportDstu2 implements IJpaValidationSupportDstu2 {
45  
46  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JpaValidationSupportDstu2.class);
47  
48  	@Autowired
49  	@Qualifier("myFhirContextDstu2Hl7Org")
50  	private FhirContext myRiCtx;
51  
52  	@Autowired
53  	@Qualifier("myStructureDefinitionDaoDstu2")
54  	private IFhirResourceDao<ca.uhn.fhir.model.dstu2.resource.StructureDefinition> myStructureDefinitionDao;
55  
56  	@Autowired
57  	@Qualifier("myQuestionnaireDaoDstu2")
58  	private IFhirResourceDao<ca.uhn.fhir.model.dstu2.resource.Questionnaire> myQuestionnaireDao;
59  
60  	@Autowired
61  	@Qualifier("myValueSetDaoDstu2")
62  	private IFhirResourceDao<ca.uhn.fhir.model.dstu2.resource.ValueSet> myValueSetDao;
63  
64  	@Autowired
65  	@Qualifier("myFhirContextDstu2")
66  	private FhirContext myDstu2Ctx;
67  
68  	@Override
69  	public List<StructureDefinition> allStructures() {
70  		return new ArrayList<>();
71  	}
72  
73  	@Override
74  	@Transactional(value = TxType.SUPPORTS)
75  	public ValueSetExpansionComponent expandValueSet(FhirContext theCtx, ConceptSetComponent theInclude) {
76  		return null;
77  	}
78  
79  	@Override
80  	@Transactional(value = TxType.SUPPORTS)
81  	public ValueSet fetchCodeSystem(FhirContext theCtx, String theSystem) {
82  		return null;
83  	}
84  
85  	@Override
86  	public <T extends IBaseResource> T fetchResource(FhirContext theContext, Class<T> theClass, String theUri) {
87  		String resourceName = myRiCtx.getResourceDefinition(theClass).getName();
88  		IBundleProvider search;
89  		IdType uriAsId = new IdType(theUri);
90  		if ("ValueSet".equals(resourceName)) {
91  			SearchParameterMap params = new SearchParameterMap();
92  			params.add(ca.uhn.fhir.model.dstu2.resource.ValueSet.SP_URL, new UriParam(theUri));
93  			params.setLoadSynchronousUpTo(10);
94  			search = myValueSetDao.search(params);
95  		} else if ("StructureDefinition".equals(resourceName)) {
96  			search = myStructureDefinitionDao.search(new SearchParameterMap().setLoadSynchronous(true).add(ca.uhn.fhir.model.dstu2.resource.StructureDefinition.SP_URL, new UriParam(theUri)));
97  		} else if ("Questionnaire".equals(resourceName)) {
98  			search = myQuestionnaireDao.search(new SearchParameterMap().setLoadSynchronous(true).add(Questionnaire.SP_RES_ID, new TokenParam(null, theUri)));
99  		} else {
100 			throw new IllegalArgumentException("Can't fetch resource type: " + resourceName);
101 		}
102 
103 		if (search.size() == 0) {
104 			if ("ValueSet".equals(resourceName)) {
105 				SearchParameterMap params = new SearchParameterMap();
106 				params.add(ca.uhn.fhir.model.dstu2.resource.ValueSet.SP_RES_ID, new TokenParam(null, uriAsId.toUnqualifiedVersionless().getValue()));
107 				params.setLoadSynchronousUpTo(10);
108 				search = myValueSetDao.search(params);
109 				if (search.size() == 0) {
110 					return null;
111 				}
112 			} else {
113 				return null;
114 			}
115 		}
116 
117 		if (search.size() > 1) {
118 			ourLog.warn("Found multiple {} instances with URL search value of: {}", resourceName, theUri);
119 		}
120 
121 		IBaseResource res = search.getResources(0, 1).get(0);
122 
123 		/*
124 		 * Validator wants RI structures and not HAPI ones, so convert
125 		 *
126 		 * TODO: we really need a more efficient way of converting.. Or maybe this will just go away when we move to RI structures
127 		 */
128 		String encoded = myDstu2Ctx.newJsonParser().encodeResourceToString(res);
129 		return myRiCtx.newJsonParser().parseResource(theClass, encoded);
130 	}
131 
132 	@Override
133 	@Transactional(value = TxType.SUPPORTS)
134 	public boolean isCodeSystemSupported(FhirContext theCtx, String theSystem) {
135 		return false;
136 	}
137 
138 	@Override
139 	@Transactional(value = TxType.SUPPORTS)
140 	public CodeValidationResult validateCode(FhirContext theCtx, String theCodeSystem, String theCode, String theDisplay) {
141 		return null;
142 	}
143 
144 }