View Javadoc
1   package ca.uhn.fhir.jpa.dao;
2   
3   /*
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import javax.annotation.PostConstruct;
24  
25  import org.hl7.fhir.instance.hapi.validation.FhirQuestionnaireResponseValidator;
26  import org.hl7.fhir.instance.hapi.validation.HapiWorkerContext;
27  import org.hl7.fhir.instance.hapi.validation.IValidationSupport;
28  import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
29  import org.hl7.fhir.instance.model.api.IBaseResource;
30  import org.hl7.fhir.instance.model.api.IIdType;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.beans.factory.annotation.Qualifier;
33  
34  import ca.uhn.fhir.context.FhirContext;
35  import ca.uhn.fhir.jpa.entity.ResourceTable;
36  import ca.uhn.fhir.model.dstu2.resource.OperationOutcome;
37  import ca.uhn.fhir.model.dstu2.resource.Questionnaire;
38  import ca.uhn.fhir.model.dstu2.resource.QuestionnaireResponse;
39  import ca.uhn.fhir.model.dstu2.resource.ValueSet;
40  import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
41  import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
42  import ca.uhn.fhir.validation.FhirValidator;
43  import ca.uhn.fhir.validation.IResourceLoader;
44  import ca.uhn.fhir.validation.ValidationResult;
45  
46  public class FhirResourceDaoQuestionnaireResponseDstu2 extends FhirResourceDaoDstu2<QuestionnaireResponse> {
47  
48  	@Autowired
49  	@Qualifier("myFhirContextDstu2Hl7Org")
50  	private FhirContext myRefImplCtx;
51  	
52  	private Boolean myValidateResponses;
53  
54  	@Autowired()
55  	@Qualifier("myJpaValidationSupportDstu2")
56  	private IValidationSupport myValidationSupport;
57  
58  	/**
59  	 * Initialize the bean
60  	 */
61  	@PostConstruct
62  	public void initialize() {
63  		try {
64  			Class.forName("org.hl7.fhir.instance.model.QuestionnaireResponse");
65  			myValidateResponses = true;
66  		} catch (ClassNotFoundException e) {
67  			myValidateResponses = Boolean.FALSE;
68  		}
69  	}
70  	
71  	@Override
72  	protected void validateResourceForStorage(QuestionnaireResponse theResource, ResourceTable theEntityToSave) {
73  		super.validateResourceForStorage(theResource, theEntityToSave);
74  		if (!myValidateResponses) {
75  			return;
76  		}
77  		
78  		QuestionnaireResponse qa = theResource;
79  		if (qa == null || qa.getQuestionnaire() == null || qa.getQuestionnaire().getReference() == null || qa.getQuestionnaire().getReference().isEmpty()) {
80  			return;
81  		}
82  
83  		FhirValidator val = myRefImplCtx.newValidator();
84  		val.setValidateAgainstStandardSchema(false);
85  		val.setValidateAgainstStandardSchematron(false);
86  
87  		FhirQuestionnaireResponseValidator module = new FhirQuestionnaireResponseValidator();
88  		module.setResourceLoader(new JpaResourceLoader());
89  		module.setWorkerContext(new HapiWorkerContext(myRefImplCtx, myValidationSupport));
90  		val.registerValidatorModule(module);
91  
92  		ValidationResult result = val.validateWithResult(myRefImplCtx.newJsonParser().parseResource(getContext().newJsonParser().encodeResourceToString(qa)));
93  		if (!result.isSuccessful()) {
94  			IBaseOperationOutcome oo = getContext().newJsonParser().parseResource(OperationOutcome.class, myRefImplCtx.newJsonParser().encodeResourceToString(result.toOperationOutcome()));
95  			throw new UnprocessableEntityException(getContext(), oo);
96  		}
97  	}
98  
99  	public class JpaResourceLoader implements IResourceLoader {
100 
101 		public JpaResourceLoader() {
102 			super();
103 		}
104 
105 		@Override
106 		public <T extends IBaseResource> T load(Class<T> theType, IIdType theId) throws ResourceNotFoundException {
107 
108 			/*
109 			 * The QuestionnaireResponse validator uses RI structures, so for now we need to convert between that and HAPI
110 			 * structures. This is a bit hackish, but hopefully it will go away at some point.
111 			 */
112 			if ("ValueSet".equals(theType.getSimpleName())) {
113 				IFhirResourceDao<ValueSet> dao = getDao(ValueSet.class);
114 				ValueSet in = dao.read(theId, null);
115 				String encoded = getContext().newJsonParser().encodeResourceToString(in);
116 
117 				// TODO: this is temporary until structures-dstu2 catches up to structures-hl7org.dstu2
118 				encoded = encoded.replace("\"define\"", "\"codeSystem\"");
119 
120 				return myRefImplCtx.newJsonParser().parseResource(theType, encoded);
121 			} else if ("Questionnaire".equals(theType.getSimpleName())) {
122 				IFhirResourceDao<Questionnaire> dao = getDao(Questionnaire.class);
123 				Questionnaire vs = dao.read(theId, null);
124 				return myRefImplCtx.newJsonParser().parseResource(theType, getContext().newJsonParser().encodeResourceToString(vs));
125 			} else {
126 				// Should not happen, validator will only ask for these two
127 				throw new IllegalStateException("Unexpected request to load resource of type " + theType);
128 			}
129 
130 		}
131 
132 	}
133 
134 }