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