View Javadoc
1   package ca.uhn.fhir.jpa.provider;
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 static org.apache.commons.lang3.StringUtils.isNotBlank;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import ca.uhn.fhir.jpa.dao.IFhirResourceDaoCodeSystem;
28  import ca.uhn.fhir.jpa.dao.IFhirResourceDaoCodeSystem.LookupCodeResult;
29  import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet;
30  import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult;
31  import ca.uhn.fhir.jpa.util.JpaConstants;
32  import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt;
33  import ca.uhn.fhir.model.dstu2.composite.CodingDt;
34  import ca.uhn.fhir.model.dstu2.resource.Parameters;
35  import ca.uhn.fhir.model.dstu2.resource.ValueSet;
36  import ca.uhn.fhir.model.primitive.*;
37  import ca.uhn.fhir.rest.annotation.*;
38  import ca.uhn.fhir.rest.api.server.RequestDetails;
39  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
40  import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
41  
42  public class BaseJpaResourceProviderValueSetDstu2 extends JpaResourceProviderDstu2<ValueSet> {
43  
44  
45  	@Operation(name = JpaConstants.OPERATION_EXPAND, idempotent = true)
46  	public ValueSet expand(
47  			HttpServletRequest theServletRequest,
48  			@IdParam(optional=true) IdDt theId,
49  			@OperationParam(name="valueSet", min=0, max=1) ValueSet theValueSet,
50  			@OperationParam(name="identifier", min=0, max=1) UriDt theIdentifier,
51  			@OperationParam(name = "filter", min=0, max=1) StringDt theFilter,
52  			RequestDetails theRequestDetails) {
53  
54  		boolean haveId = theId != null && theId.hasIdPart();
55  		boolean haveIdentifier = theIdentifier != null && isNotBlank(theIdentifier.getValue());
56  		boolean haveValueSet = theValueSet != null && theValueSet.isEmpty() == false;
57  		
58  		if (!haveId && !haveIdentifier && !haveValueSet) {
59  			throw new InvalidRequestException("$expand operation at the type level (no ID specified) requires an identifier or a valueSet as a part of the request");
60  		}
61  
62  		if (moreThanOneTrue(haveId, haveIdentifier, haveValueSet)) {
63  			throw new InvalidRequestException("$expand must EITHER be invoked at the type level, or have an identifier specified, or have a ValueSet specified. Can not combine these options.");
64  		}
65  		
66  		startRequest(theServletRequest);
67  		try {
68  			IFhirResourceDaoValueSet<ValueSet, CodingDt, CodeableConceptDt> dao = (IFhirResourceDaoValueSet<ValueSet, CodingDt, CodeableConceptDt>) getDao();
69  			if (haveId) {
70  				return dao.expand(theId, toFilterString(theFilter), theRequestDetails);
71  			} else if (haveIdentifier) {
72  				return dao.expandByIdentifier(theIdentifier.getValue(), toFilterString(theFilter));
73  			} else {
74  				return dao.expand(theValueSet, toFilterString(theFilter));
75  			}
76  			
77  		} finally {
78  			endRequest(theServletRequest);
79  		}
80  	}
81  
82  
83  	private static boolean moreThanOneTrue(boolean... theBooleans) {
84  		boolean haveOne = false;
85  		for (boolean next : theBooleans) {
86  			if (next) {
87  				if (haveOne) {
88  					return true;
89  				} else {
90  					haveOne = true;
91  				}
92  			}
93  		}
94  		return false;
95  	}
96  
97  
98  	private String toFilterString(StringDt theFilter) {
99  		return theFilter != null ? theFilter.getValue() : null;
100 	}
101 
102 	@Operation(name = JpaConstants.OPERATION_LOOKUP, idempotent = true, returnParameters= {
103 		@OperationParam(name="name", type=StringDt.class, min=1),
104 		@OperationParam(name="version", type=StringDt.class, min=0),
105 		@OperationParam(name="display", type=StringDt.class, min=1),
106 		@OperationParam(name="abstract", type=BooleanDt.class, min=1),
107 	})
108 	public Parameters lookup(
109 			HttpServletRequest theServletRequest,
110 			@OperationParam(name="code", min=0, max=1) CodeDt theCode, 
111 			@OperationParam(name="system", min=0, max=1) UriDt theSystem,
112 			@OperationParam(name="coding", min=0, max=1) CodingDt theCoding,
113 			RequestDetails theRequestDetails 
114 			) {
115 
116 		startRequest(theServletRequest);
117 		try {
118 			IFhirResourceDaoCodeSystem<ValueSet, CodingDt, CodeableConceptDt> dao = (IFhirResourceDaoCodeSystem<ValueSet, CodingDt, CodeableConceptDt>) getDao();
119 			LookupCodeResult result = dao.lookupCode(theCode, theSystem, theCoding, theRequestDetails);
120 			if (result.isFound()==false) {
121 				throw new ResourceNotFoundException("Unable to find code[" + result.getSearchedForCode() + "] in system[" + result.getSearchedForSystem() + "]");
122 			}
123 			Parameters retVal = new Parameters();
124 			retVal.addParameter().setName("name").setValue(new StringDt(result.getCodeSystemDisplayName()));
125 			if (isNotBlank(result.getCodeSystemVersion())) {
126 				retVal.addParameter().setName("version").setValue(new StringDt(result.getCodeSystemVersion()));
127 			}
128 			retVal.addParameter().setName("display").setValue(new StringDt(result.getCodeDisplay()));
129 			retVal.addParameter().setName("abstract").setValue(new BooleanDt(result.isCodeIsAbstract()));			
130 			return retVal;
131 		} finally {
132 			endRequest(theServletRequest);
133 		}
134 	}
135 	
136 	
137 	@Operation(name = JpaConstants.OPERATION_VALIDATE_CODE, idempotent = true, returnParameters= {
138 		@OperationParam(name="result", type=BooleanDt.class, min=1),
139 		@OperationParam(name="message", type=StringDt.class),
140 		@OperationParam(name="display", type=StringDt.class)
141 	})
142 	public Parameters validateCode(
143 			HttpServletRequest theServletRequest,
144 			@IdParam(optional=true) IdDt theId, 
145 			@OperationParam(name="identifier", min=0, max=1) UriDt theValueSetIdentifier, 
146 			@OperationParam(name="code", min=0, max=1) CodeDt theCode, 
147 			@OperationParam(name="system", min=0, max=1) UriDt theSystem,
148 			@OperationParam(name="display", min=0, max=1) StringDt theDisplay,
149 			@OperationParam(name="coding", min=0, max=1) CodingDt theCoding,
150 			@OperationParam(name="codeableConcept", min=0, max=1) CodeableConceptDt theCodeableConcept, 
151 			RequestDetails theRequestDetails
152 			) {
153 
154 		startRequest(theServletRequest);
155 		try {
156 			IFhirResourceDaoValueSet<ValueSet, CodingDt, CodeableConceptDt> dao = (IFhirResourceDaoValueSet<ValueSet, CodingDt, CodeableConceptDt>) getDao();
157 			ValidateCodeResult result = dao.validateCode(theValueSetIdentifier, theId, theCode, theSystem, theDisplay, theCoding, theCodeableConcept, theRequestDetails);
158 			Parameters retVal = new Parameters();
159 			retVal.addParameter().setName("result").setValue(new BooleanDt(result.isResult()));
160 			if (isNotBlank(result.getMessage())) {
161 				retVal.addParameter().setName("message").setValue(new StringDt(result.getMessage()));
162 			}
163 			if (isNotBlank(result.getDisplay())) {
164 				retVal.addParameter().setName("display").setValue(new StringDt(result.getDisplay()));
165 			}
166 			return retVal;
167 		} finally {
168 			endRequest(theServletRequest);
169 		}
170 	}
171 
172 	
173 }