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.IFhirResourceDaoValueSet;
24  import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult;
25  import ca.uhn.fhir.jpa.util.JpaConstants;
26  import ca.uhn.fhir.rest.annotation.IdParam;
27  import ca.uhn.fhir.rest.annotation.Operation;
28  import ca.uhn.fhir.rest.annotation.OperationParam;
29  import ca.uhn.fhir.rest.api.server.RequestDetails;
30  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
31  import org.hl7.fhir.r4.model.*;
32  
33  import javax.servlet.http.HttpServletRequest;
34  
35  import static org.apache.commons.lang3.StringUtils.isNotBlank;
36  
37  public class BaseJpaResourceProviderValueSetR4 extends JpaResourceProviderR4<ValueSet> {
38  
39  	@Operation(name = JpaConstants.OPERATION_EXPAND, idempotent = true)
40  	public ValueSet expand(
41  		HttpServletRequest theServletRequest,
42  		@IdParam(optional = true) IdType theId,
43  		@OperationParam(name = "valueSet", min = 0, max = 1) ValueSet theValueSet,
44  		@OperationParam(name = "url", min = 0, max = 1) UriType theUrl,
45  		@OperationParam(name = "filter", min = 0, max = 1) StringType theFilter,
46  		RequestDetails theRequestDetails) {
47  
48  		boolean haveId = theId != null && theId.hasIdPart();
49  		boolean haveIdentifier = theUrl != null && isNotBlank(theUrl.getValue());
50  		boolean haveValueSet = theValueSet != null && theValueSet.isEmpty() == false;
51  
52  		if (!haveId && !haveIdentifier && !haveValueSet) {
53  			throw new InvalidRequestException("$expand operation at the type level (no ID specified) requires a url or a valueSet as a part of the request");
54  		}
55  
56  		if (moreThanOneTrue(haveId, haveIdentifier, haveValueSet)) {
57  			throw new InvalidRequestException("$expand must EITHER be invoked at the instance level, or have a url specified, or have a ValueSet specified. Can not combine these options.");
58  		}
59  
60  		startRequest(theServletRequest);
61  		try {
62  			IFhirResourceDaoValueSet<ValueSet, Coding, CodeableConcept> dao = (IFhirResourceDaoValueSet<ValueSet, Coding, CodeableConcept>) getDao();
63  			if (haveId) {
64  				return dao.expand(theId, toFilterString(theFilter), theRequestDetails);
65  			} else if (haveIdentifier) {
66  				return dao.expandByIdentifier(theUrl.getValue(), toFilterString(theFilter));
67  			} else {
68  				return dao.expand(theValueSet, toFilterString(theFilter));
69  			}
70  
71  		} finally {
72  			endRequest(theServletRequest);
73  		}
74  	}
75  
76  
77  	private String toFilterString(StringType theFilter) {
78  		return theFilter != null ? theFilter.getValue() : null;
79  	}
80  
81  
82  	@SuppressWarnings("unchecked")
83  	@Operation(name = JpaConstants.OPERATION_VALIDATE_CODE, idempotent = true, returnParameters = {
84  		@OperationParam(name = "result", type = BooleanType.class, min = 1),
85  		@OperationParam(name = "message", type = StringType.class),
86  		@OperationParam(name = "display", type = StringType.class)
87  	})
88  	public Parameters validateCode(
89  		HttpServletRequest theServletRequest,
90  		@IdParam(optional = true) IdType theId,
91  		@OperationParam(name = "url", min = 0, max = 1) UriType theValueSetUrl,
92  		@OperationParam(name = "code", min = 0, max = 1) CodeType theCode,
93  		@OperationParam(name = "system", min = 0, max = 1) UriType theSystem,
94  		@OperationParam(name = "display", min = 0, max = 1) StringType theDisplay,
95  		@OperationParam(name = "coding", min = 0, max = 1) Coding theCoding,
96  		@OperationParam(name = "codeableConcept", min = 0, max = 1) CodeableConcept theCodeableConcept,
97  		RequestDetails theRequestDetails
98  	) {
99  
100 		startRequest(theServletRequest);
101 		try {
102 			IFhirResourceDaoValueSet<ValueSet, Coding, CodeableConcept> dao = (IFhirResourceDaoValueSet<ValueSet, Coding, CodeableConcept>) getDao();
103 			ValidateCodeResult result = dao.validateCode(theValueSetUrl, theId, theCode, theSystem, theDisplay, theCoding, theCodeableConcept, theRequestDetails);
104 			Parameters retVal = new Parameters();
105 			retVal.addParameter().setName("result").setValue(new BooleanType(result.isResult()));
106 			if (isNotBlank(result.getMessage())) {
107 				retVal.addParameter().setName("message").setValue(new StringType(result.getMessage()));
108 			}
109 			if (isNotBlank(result.getDisplay())) {
110 				retVal.addParameter().setName("display").setValue(new StringType(result.getDisplay()));
111 			}
112 			return retVal;
113 		} finally {
114 			endRequest(theServletRequest);
115 		}
116 	}
117 
118 
119 	private static boolean moreThanOneTrue(boolean... theBooleans) {
120 		boolean haveOne = false;
121 		for (boolean next : theBooleans) {
122 			if (next) {
123 				if (haveOne) {
124 					return true;
125 				} else {
126 					haveOne = true;
127 				}
128 			}
129 		}
130 		return false;
131 	}
132 
133 
134 }