View Javadoc
1   package ca.uhn.fhir.jpa.provider.r4;
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.InvalidRequestException;
32  import org.hl7.fhir.r4.model.*;
33  
34  import javax.servlet.http.HttpServletRequest;
35  
36  public class BaseJpaResourceProviderConceptMapR4 extends JpaResourceProviderR4<ConceptMap> {
37  	@Operation(name = JpaConstants.OPERATION_TRANSLATE, idempotent = true, returnParameters = {
38  		@OperationParam(name = "result", type = BooleanType.class, min = 1, max = 1),
39  		@OperationParam(name = "message", type = StringType.class, min = 0, max = 1),
40  	})
41  	public Parameters translate(
42  		HttpServletRequest theServletRequest,
43  		@IdParam(optional = true) IdType theId,
44  		@OperationParam(name = "code", min = 0, max = 1) CodeType theSourceCode,
45  		@OperationParam(name = "system", min = 0, max = 1) UriType theSourceCodeSystem,
46  		@OperationParam(name = "version", min = 0, max = 1) StringType theSourceCodeSystemVersion,
47  		@OperationParam(name = "source", min = 0, max = 1) UriType theSourceValueSet,
48  		@OperationParam(name = "coding", min = 0, max = 1) Coding theSourceCoding,
49  		@OperationParam(name = "codeableConcept", min = 0, max = 1) CodeableConcept theSourceCodeableConcept,
50  		@OperationParam(name = "target", min = 0, max = 1) UriType theTargetValueSet,
51  		@OperationParam(name = "targetsystem", min = 0, max = 1) UriType theTargetCodeSystem,
52  		@OperationParam(name = "reverse", min = 0, max = 1) BooleanType theReverse,
53  		RequestDetails theRequestDetails
54  	) {
55  		boolean haveSourceCode = theSourceCode != null
56  			&& theSourceCode.hasCode();
57  		boolean haveSourceCodeSystem = theSourceCodeSystem != null
58  			&& theSourceCodeSystem.hasValue();
59  		boolean haveSourceCodeSystemVersion = theSourceCodeSystemVersion != null
60  			&& theSourceCodeSystemVersion.hasValue();
61  		boolean haveSourceValueSet = theSourceValueSet != null
62  			&& theSourceValueSet.hasValue();
63  		boolean haveSourceCoding = theSourceCoding != null
64  			&& theSourceCoding.hasCode();
65  		boolean haveSourceCodeableConcept= theSourceCodeableConcept != null
66  			&& theSourceCodeableConcept.hasCoding()
67  			&& theSourceCodeableConcept.getCodingFirstRep().hasCode();
68  		boolean haveTargetValueSet = theTargetValueSet != null
69  			&& theTargetValueSet.hasValue();
70  		boolean haveTargetCodeSystem = theTargetCodeSystem != null
71  			&& theTargetCodeSystem.hasValue();
72  		boolean haveReverse = theReverse != null;
73  		boolean haveId = theId != null && theId.hasIdPart();
74  
75  		// <editor-fold desc="Filters">
76  		if ((!haveSourceCode && !haveSourceCoding && !haveSourceCodeableConcept)
77  			|| moreThanOneTrue(haveSourceCode, haveSourceCoding, haveSourceCodeableConcept)) {
78  			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.");
79  		}
80  
81  		TranslationRequest translationRequest = new TranslationRequest();
82  
83  		if (haveSourceCode) {
84  			translationRequest.getCodeableConcept().addCoding().setCodeElement(theSourceCode);
85  
86  			if (haveSourceCodeSystem) {
87  				translationRequest.getCodeableConcept().getCodingFirstRep().setSystemElement(theSourceCodeSystem);
88  			}
89  
90  			if (haveSourceCodeSystemVersion) {
91  				translationRequest.getCodeableConcept().getCodingFirstRep().setVersionElement(theSourceCodeSystemVersion);
92  			}
93  		} else if (haveSourceCoding) {
94  			translationRequest.getCodeableConcept().addCoding(theSourceCoding);
95  		} else {
96  			translationRequest.setCodeableConcept(theSourceCodeableConcept);
97  		}
98  
99  		if (haveSourceValueSet) {
100 			translationRequest.setSource(theSourceValueSet);
101 		}
102 
103 		if (haveTargetValueSet) {
104 			translationRequest.setTarget(theTargetValueSet);
105 		}
106 
107 		if (haveTargetCodeSystem) {
108 			translationRequest.setTargetSystem(theTargetCodeSystem);
109 		}
110 
111 		if (haveReverse) {
112 			translationRequest.setReverse(theReverse);
113 		}
114 
115 		if (haveId) {
116 			translationRequest.setResourceId(theId.getIdPartAsLong());
117 		}
118 
119 		startRequest(theServletRequest);
120 		try {
121 			IFhirResourceDaoConceptMap<ConceptMap> dao = (IFhirResourceDaoConceptMap<ConceptMap>) getDao();
122 			TranslationResult result = dao.translate(translationRequest, theRequestDetails);
123 			return result.toParameters();
124 		} finally {
125 			endRequest(theServletRequest);
126 		}
127 	}
128 
129 	private static boolean moreThanOneTrue(boolean... theBooleans) {
130 		boolean haveOne = false;
131 		for (boolean next : theBooleans) {
132 			if (next) {
133 				if (haveOne) {
134 					return true;
135 				} else {
136 					haveOne = true;
137 				}
138 			}
139 		}
140 		return false;
141 	}
142 }