View Javadoc
1   package ca.uhn.fhir.jpa.provider.dstu3;
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 ca.uhn.fhir.jpa.dao.IFhirResourceDaoConceptMap;
24  import ca.uhn.fhir.jpa.term.TranslationRequest;
25  import ca.uhn.fhir.jpa.term.TranslationResult;
26  import ca.uhn.fhir.jpa.util.JpaConstants;
27  import ca.uhn.fhir.rest.annotation.IdParam;
28  import ca.uhn.fhir.rest.annotation.Operation;
29  import ca.uhn.fhir.rest.annotation.OperationParam;
30  import ca.uhn.fhir.rest.api.server.RequestDetails;
31  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
32  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
33  import org.hl7.fhir.convertors.VersionConvertor_30_40;
34  import org.hl7.fhir.dstu3.model.*;
35  import org.hl7.fhir.exceptions.FHIRException;
36  
37  import javax.servlet.http.HttpServletRequest;
38  
39  public class BaseJpaResourceProviderConceptMapDstu3 extends JpaResourceProviderDstu3<ConceptMap> {
40  	@Operation(name = JpaConstants.OPERATION_TRANSLATE, idempotent = true, returnParameters = {
41  		@OperationParam(name = "result", type = BooleanType.class, min = 1, max = 1),
42  		@OperationParam(name = "message", type = StringType.class, min = 0, max = 1),
43  	})
44  	public Parameters translate(
45  		HttpServletRequest theServletRequest,
46  		@IdParam(optional = true) IdType theId,
47  		@OperationParam(name = "code", min = 0, max = 1) CodeType theSourceCode,
48  		@OperationParam(name = "system", min = 0, max = 1) UriType theSourceCodeSystem,
49  		@OperationParam(name = "version", min = 0, max = 1) StringType theSourceCodeSystemVersion,
50  		@OperationParam(name = "source", min = 0, max = 1) UriType theSourceValueSet,
51  		@OperationParam(name = "coding", min = 0, max = 1) Coding theSourceCoding,
52  		@OperationParam(name = "codeableConcept", min = 0, max = 1) CodeableConcept theSourceCodeableConcept,
53  		@OperationParam(name = "target", min = 0, max = 1) UriType theTargetValueSet,
54  		@OperationParam(name = "targetsystem", min = 0, max = 1) UriType theTargetCodeSystem,
55  		@OperationParam(name = "reverse", min = 0, max = 1) BooleanType theReverse,
56  		RequestDetails theRequestDetails
57  	) {
58  		boolean haveSourceCode = theSourceCode != null
59  			&& theSourceCode.hasValue();
60  		boolean haveSourceCodeSystem = theSourceCodeSystem != null
61  			&& theSourceCodeSystem.hasValue();
62  		boolean haveSourceCodeSystemVersion = theSourceCodeSystemVersion != null
63  			&& theSourceCodeSystemVersion.hasValue();
64  		boolean haveSourceValueSet = theSourceValueSet != null
65  			&& theSourceValueSet.hasValue();
66  		boolean haveSourceCoding = theSourceCoding != null
67  			&& theSourceCoding.hasCode();
68  		boolean haveSourceCodeableConcept= theSourceCodeableConcept != null
69  			&& theSourceCodeableConcept.hasCoding()
70  			&& theSourceCodeableConcept.getCodingFirstRep().hasCode();
71  		boolean haveTargetValueSet = theTargetValueSet != null
72  			&& theTargetValueSet.hasValue();
73  		boolean haveTargetCodeSystem = theTargetCodeSystem != null
74  			&& theTargetCodeSystem.hasValue();
75  		boolean haveReverse = theReverse != null;
76  		boolean haveId = theId != null && theId.hasIdPart();
77  
78  		// <editor-fold desc="Filters">
79  		if ((!haveSourceCode && !haveSourceCoding && !haveSourceCodeableConcept)
80  			|| moreThanOneTrue(haveSourceCode, haveSourceCoding, haveSourceCodeableConcept)) {
81  			throw new InvalidRequestException("One (and only one) of the in parameters (code, coding, codeableConcept) must be provided, to identify the code that is to be translated.");
82  		}
83  
84  		TranslationRequest translationRequest = new TranslationRequest();
85  		try {
86  			// Convert from DSTU3 to R4
87  			if (haveSourceCode) {
88  				translationRequest.getCodeableConcept().addCoding().setCodeElement(VersionConvertor_30_40.convertCode(theSourceCode));
89  
90  				if (haveSourceCodeSystem) {
91  					translationRequest.getCodeableConcept().getCodingFirstRep().setSystemElement(VersionConvertor_30_40.convertUri(theSourceCodeSystem));
92  				}
93  
94  				if (haveSourceCodeSystemVersion) {
95  					translationRequest.getCodeableConcept().getCodingFirstRep().setVersionElement(VersionConvertor_30_40.convertString(theSourceCodeSystemVersion));
96  				}
97  			} else if (haveSourceCoding) {
98  				translationRequest.getCodeableConcept().addCoding(VersionConvertor_30_40.convertCoding(theSourceCoding));
99  			} else {
100 				translationRequest.setCodeableConcept(VersionConvertor_30_40.convertCodeableConcept(theSourceCodeableConcept));
101 			}
102 
103 			if (haveSourceValueSet) {
104 				translationRequest.setSource(VersionConvertor_30_40.convertUri(theSourceValueSet));
105 			}
106 
107 			if (haveTargetValueSet) {
108 				translationRequest.setTarget(VersionConvertor_30_40.convertUri(theTargetValueSet));
109 			}
110 
111 			if (haveTargetCodeSystem) {
112 				translationRequest.setTargetSystem(VersionConvertor_30_40.convertUri(theTargetCodeSystem));
113 			}
114 
115 			if (haveReverse) {
116 				translationRequest.setReverse(VersionConvertor_30_40.convertBoolean(theReverse));
117 			}
118 
119 			if (haveId) {
120 				translationRequest.setResourceId(theId.getIdPartAsLong());
121 			}
122 		} catch (FHIRException fe) {
123 			throw new InternalErrorException(fe);
124 		}
125 
126 		startRequest(theServletRequest);
127 		try {
128 			IFhirResourceDaoConceptMap<ConceptMap> dao = (IFhirResourceDaoConceptMap<ConceptMap>) getDao();
129 			TranslationResult result = dao.translate(translationRequest, theRequestDetails);
130 
131 			// Convert from R4 to DSTU3
132 			return VersionConvertor_30_40.convertParameters(result.toParameters());
133 		} catch (FHIRException fe) {
134 			throw new InternalErrorException(fe);
135 		} finally {
136 			endRequest(theServletRequest);
137 		}
138 	}
139 
140 	private static boolean moreThanOneTrue(boolean... theBooleans) {
141 		boolean haveOne = false;
142 		for (boolean next : theBooleans) {
143 			if (next) {
144 				if (haveOne) {
145 					return true;
146 				} else {
147 					haveOne = true;
148 				}
149 			}
150 		}
151 		return false;
152 	}
153 }