View Javadoc
1   package ca.uhn.fhir.jpa.provider.dstu3;
2   
3   import ca.uhn.fhir.jpa.dao.FulltextSearchSvcImpl.Suggestion;
4   import ca.uhn.fhir.jpa.dao.IFhirSystemDao;
5   import ca.uhn.fhir.jpa.dao.IFulltextSearchSvc;
6   import ca.uhn.fhir.jpa.provider.BaseJpaSystemProviderDstu2Plus;
7   import ca.uhn.fhir.jpa.util.JpaConstants;
8   import ca.uhn.fhir.model.api.annotation.Description;
9   import ca.uhn.fhir.rest.annotation.*;
10  import ca.uhn.fhir.rest.api.server.RequestDetails;
11  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
12  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
13  import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
14  import org.hl7.fhir.convertors.VersionConvertor_30_40;
15  import org.hl7.fhir.dstu3.model.*;
16  import org.hl7.fhir.dstu3.model.Parameters.ParametersParameterComponent;
17  import org.hl7.fhir.exceptions.FHIRException;
18  import org.hl7.fhir.instance.model.api.IIdType;
19  import org.springframework.beans.factory.annotation.Autowired;
20  import org.springframework.beans.factory.annotation.Qualifier;
21  
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  import java.util.TreeMap;
27  
28  import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
29  import static org.apache.commons.lang3.StringUtils.isBlank;
30  
31  /*
32   * #%L
33   * HAPI FHIR JPA Server
34   * %%
35   * Copyright (C) 2014 - 2018 University Health Network
36   * %%
37   * Licensed under the Apache License, Version 2.0 (the "License");
38   * you may not use this file except in compliance with the License.
39   * You may obtain a copy of the License at
40   * 
41   *      http://www.apache.org/licenses/LICENSE-2.0
42   * 
43   * Unless required by applicable law or agreed to in writing, software
44   * distributed under the License is distributed on an "AS IS" BASIS,
45   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46   * See the License for the specific language governing permissions and
47   * limitations under the License.
48   * #L%
49   */
50  
51  public class JpaSystemProviderDstu3 extends BaseJpaSystemProviderDstu2Plus<Bundle, Meta> {
52  
53  	@Autowired()
54  	@Qualifier("mySystemDaoDstu3")
55  	private IFhirSystemDao<Bundle, Meta> mySystemDao;
56  
57  	@Autowired(required = false)
58  	private IFulltextSearchSvc mySearchDao;
59  
60  	@Operation(name = JpaConstants.OPERATION_EXPUNGE, idempotent = false, returnParameters = {
61  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_OUT_PARAM_EXPUNGE_COUNT, type = IntegerType.class)
62  	})
63  	public Parameters expunge(
64  		@IdParam IIdType theIdParam,
65  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_LIMIT) IntegerType theLimit,
66  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_EXPUNGE_DELETED_RESOURCES) BooleanType theExpungeDeletedResources,
67  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_EXPUNGE_PREVIOUS_VERSIONS) BooleanType theExpungeOldVersions,
68  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_EXPUNGE_EVERYTHING) BooleanType theExpungeEverything
69  	) {
70  		org.hl7.fhir.r4.model.Parameters retVal = super.doExpunge(theLimit, theExpungeDeletedResources, theExpungeOldVersions, theExpungeEverything);
71  		try {
72  			return VersionConvertor_30_40.convertParameters(retVal);
73  		} catch (FHIRException e) {
74  			throw new InternalErrorException(e);
75  		}
76  	}
77  
78  	@Operation(name = JpaConstants.OPERATION_EXPUNGE, idempotent = false, returnParameters = {
79  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_OUT_PARAM_EXPUNGE_COUNT, type = IntegerType.class)
80  	})
81  	public Parameters expunge(
82  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_LIMIT) IntegerType theLimit,
83  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_EXPUNGE_DELETED_RESOURCES) BooleanType theExpungeDeletedResources,
84  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_EXPUNGE_PREVIOUS_VERSIONS) BooleanType theExpungeOldVersions,
85  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_EXPUNGE_EVERYTHING) BooleanType theExpungeEverything
86  	) {
87  		org.hl7.fhir.r4.model.Parameters retVal = super.doExpunge(theLimit, theExpungeDeletedResources, theExpungeOldVersions, theExpungeEverything);
88  		try {
89  			return VersionConvertor_30_40.convertParameters(retVal);
90  		} catch (FHIRException e) {
91  			throw new InternalErrorException(e);
92  		}
93  	}
94  
95  	// This is generated by hand:
96  	// ls hapi-fhir-structures-dstu2/target/generated-sources/tinder/ca/uhn/fhir/model/dstu2/resource/ | sort | sed "s/.java//" | sed "s/^/@OperationParam(name=\"/" | sed "s/$/\", type=IntegerType.class, min=0, max=1),/"
97  	@Operation(name = JpaConstants.OPERATION_GET_RESOURCE_COUNTS, idempotent = true, returnParameters = {
98  		@OperationParam(name = "AllergyIntolerance", type = IntegerType.class, min = 0, max = 1),
99  		@OperationParam(name = "Appointment", type = IntegerType.class, min = 0, max = 1),
100 		@OperationParam(name = "AppointmentResponse", type = IntegerType.class, min = 0, max = 1),
101 		@OperationParam(name = "AuditEvent", type = IntegerType.class, min = 0, max = 1),
102 		@OperationParam(name = "Basic", type = IntegerType.class, min = 0, max = 1),
103 		@OperationParam(name = "Binary", type = IntegerType.class, min = 0, max = 1),
104 		@OperationParam(name = "BodySite", type = IntegerType.class, min = 0, max = 1),
105 		@OperationParam(name = "Bundle", type = IntegerType.class, min = 0, max = 1),
106 		@OperationParam(name = "CarePlan", type = IntegerType.class, min = 0, max = 1),
107 		@OperationParam(name = "CarePlan2", type = IntegerType.class, min = 0, max = 1),
108 		@OperationParam(name = "Claim", type = IntegerType.class, min = 0, max = 1),
109 		@OperationParam(name = "ClaimResponse", type = IntegerType.class, min = 0, max = 1),
110 		@OperationParam(name = "ClinicalImpression", type = IntegerType.class, min = 0, max = 1),
111 		@OperationParam(name = "Communication", type = IntegerType.class, min = 0, max = 1),
112 		@OperationParam(name = "CommunicationRequest", type = IntegerType.class, min = 0, max = 1),
113 		@OperationParam(name = "Composition", type = IntegerType.class, min = 0, max = 1),
114 		@OperationParam(name = "ConceptMap", type = IntegerType.class, min = 0, max = 1),
115 		@OperationParam(name = "Condition", type = IntegerType.class, min = 0, max = 1),
116 		@OperationParam(name = "Conformance", type = IntegerType.class, min = 0, max = 1),
117 		@OperationParam(name = "Contract", type = IntegerType.class, min = 0, max = 1),
118 		@OperationParam(name = "Contraindication", type = IntegerType.class, min = 0, max = 1),
119 		@OperationParam(name = "Coverage", type = IntegerType.class, min = 0, max = 1),
120 		@OperationParam(name = "DataElement", type = IntegerType.class, min = 0, max = 1),
121 		@OperationParam(name = "Device", type = IntegerType.class, min = 0, max = 1),
122 		@OperationParam(name = "DeviceComponent", type = IntegerType.class, min = 0, max = 1),
123 		@OperationParam(name = "DeviceMetric", type = IntegerType.class, min = 0, max = 1),
124 		@OperationParam(name = "DeviceUseRequest", type = IntegerType.class, min = 0, max = 1),
125 		@OperationParam(name = "DeviceUseStatement", type = IntegerType.class, min = 0, max = 1),
126 		@OperationParam(name = "DiagnosticOrder", type = IntegerType.class, min = 0, max = 1),
127 		@OperationParam(name = "DiagnosticReport", type = IntegerType.class, min = 0, max = 1),
128 		@OperationParam(name = "DocumentManifest", type = IntegerType.class, min = 0, max = 1),
129 		@OperationParam(name = "DocumentReference", type = IntegerType.class, min = 0, max = 1),
130 		@OperationParam(name = "EligibilityRequest", type = IntegerType.class, min = 0, max = 1),
131 		@OperationParam(name = "EligibilityResponse", type = IntegerType.class, min = 0, max = 1),
132 		@OperationParam(name = "Encounter", type = IntegerType.class, min = 0, max = 1),
133 		@OperationParam(name = "EnrollmentRequest", type = IntegerType.class, min = 0, max = 1),
134 		@OperationParam(name = "EnrollmentResponse", type = IntegerType.class, min = 0, max = 1),
135 		@OperationParam(name = "EpisodeOfCare", type = IntegerType.class, min = 0, max = 1),
136 		@OperationParam(name = "ExplanationOfBenefit", type = IntegerType.class, min = 0, max = 1),
137 		@OperationParam(name = "FamilyMemberHistory", type = IntegerType.class, min = 0, max = 1),
138 		@OperationParam(name = "Flag", type = IntegerType.class, min = 0, max = 1),
139 		@OperationParam(name = "Goal", type = IntegerType.class, min = 0, max = 1),
140 		@OperationParam(name = "Group", type = IntegerType.class, min = 0, max = 1),
141 		@OperationParam(name = "HealthcareService", type = IntegerType.class, min = 0, max = 1),
142 		@OperationParam(name = "ImagingObjectSelection", type = IntegerType.class, min = 0, max = 1),
143 		@OperationParam(name = "ImagingStudy", type = IntegerType.class, min = 0, max = 1),
144 		@OperationParam(name = "Immunization", type = IntegerType.class, min = 0, max = 1),
145 		@OperationParam(name = "ImmunizationRecommendation", type = IntegerType.class, min = 0, max = 1),
146 		@OperationParam(name = "ListResource", type = IntegerType.class, min = 0, max = 1),
147 		@OperationParam(name = "Location", type = IntegerType.class, min = 0, max = 1),
148 		@OperationParam(name = "Media", type = IntegerType.class, min = 0, max = 1),
149 		@OperationParam(name = "Medication", type = IntegerType.class, min = 0, max = 1),
150 		@OperationParam(name = "MedicationAdministration", type = IntegerType.class, min = 0, max = 1),
151 		@OperationParam(name = "MedicationDispense", type = IntegerType.class, min = 0, max = 1),
152 		@OperationParam(name = "MedicationPrescription", type = IntegerType.class, min = 0, max = 1),
153 		@OperationParam(name = "MedicationStatement", type = IntegerType.class, min = 0, max = 1),
154 		@OperationParam(name = "MessageHeader", type = IntegerType.class, min = 0, max = 1),
155 		@OperationParam(name = "NamingSystem", type = IntegerType.class, min = 0, max = 1),
156 		@OperationParam(name = "NutritionOrder", type = IntegerType.class, min = 0, max = 1),
157 		@OperationParam(name = "Observation", type = IntegerType.class, min = 0, max = 1),
158 		@OperationParam(name = "OperationDefinition", type = IntegerType.class, min = 0, max = 1),
159 		@OperationParam(name = "OperationOutcome", type = IntegerType.class, min = 0, max = 1),
160 		@OperationParam(name = "Order", type = IntegerType.class, min = 0, max = 1),
161 		@OperationParam(name = "OrderResponse", type = IntegerType.class, min = 0, max = 1),
162 		@OperationParam(name = "Organization", type = IntegerType.class, min = 0, max = 1),
163 		@OperationParam(name = "Parameters", type = IntegerType.class, min = 0, max = 1),
164 		@OperationParam(name = "Patient", type = IntegerType.class, min = 0, max = 1),
165 		@OperationParam(name = "PaymentNotice", type = IntegerType.class, min = 0, max = 1),
166 		@OperationParam(name = "PaymentReconciliation", type = IntegerType.class, min = 0, max = 1),
167 		@OperationParam(name = "Person", type = IntegerType.class, min = 0, max = 1),
168 		@OperationParam(name = "Practitioner", type = IntegerType.class, min = 0, max = 1),
169 		@OperationParam(name = "Procedure", type = IntegerType.class, min = 0, max = 1),
170 		@OperationParam(name = "ProcedureRequest", type = IntegerType.class, min = 0, max = 1),
171 		@OperationParam(name = "ProcessRequest", type = IntegerType.class, min = 0, max = 1),
172 		@OperationParam(name = "ProcessResponse", type = IntegerType.class, min = 0, max = 1),
173 		@OperationParam(name = "Provenance", type = IntegerType.class, min = 0, max = 1),
174 		@OperationParam(name = "Questionnaire", type = IntegerType.class, min = 0, max = 1),
175 		@OperationParam(name = "QuestionnaireAnswers", type = IntegerType.class, min = 0, max = 1),
176 		@OperationParam(name = "ReferralRequest", type = IntegerType.class, min = 0, max = 1),
177 		@OperationParam(name = "RelatedPerson", type = IntegerType.class, min = 0, max = 1),
178 		@OperationParam(name = "RiskAssessment", type = IntegerType.class, min = 0, max = 1),
179 		@OperationParam(name = "Schedule", type = IntegerType.class, min = 0, max = 1),
180 		@OperationParam(name = "SearchParameter", type = IntegerType.class, min = 0, max = 1),
181 		@OperationParam(name = "Slot", type = IntegerType.class, min = 0, max = 1),
182 		@OperationParam(name = "Specimen", type = IntegerType.class, min = 0, max = 1),
183 		@OperationParam(name = "StructureDefinition", type = IntegerType.class, min = 0, max = 1),
184 		@OperationParam(name = "Subscription", type = IntegerType.class, min = 0, max = 1),
185 		@OperationParam(name = "Substance", type = IntegerType.class, min = 0, max = 1),
186 		@OperationParam(name = "Supply", type = IntegerType.class, min = 0, max = 1),
187 		@OperationParam(name = "ValueSet", type = IntegerType.class, min = 0, max = 1),
188 		@OperationParam(name = "VisionPrescription", type = IntegerType.class, min = 0, max = 1)
189 	})
190 	@Description(shortDefinition = "Provides the number of resources currently stored on the server, broken down by resource type")
191 	public Parameters getResourceCounts() {
192 		Parameters retVal = new Parameters();
193 
194 		Map<String, Long> counts = mySystemDao.getResourceCountsFromCache();
195 		counts = defaultIfNull(counts, Collections.emptyMap());
196 		counts = new TreeMap<>(counts);
197 		for (Entry<String, Long> nextEntry : counts.entrySet()) {
198 			retVal.addParameter().setName((nextEntry.getKey())).setValue(new IntegerType(nextEntry.getValue().intValue()));
199 		}
200 
201 		return retVal;
202 	}
203 
204 	@Operation(name = JpaConstants.OPERATION_META, idempotent = true, returnParameters = {
205 		@OperationParam(name = "return", type = Meta.class)
206 	})
207 	public Parameters meta(RequestDetails theRequestDetails) {
208 		Parameters parameters = new Parameters();
209 		parameters.addParameter().setName("return").setValue(getDao().metaGetOperation(theRequestDetails));
210 		return parameters;
211 	}
212 
213 	@Operation(name = JpaConstants.OPERATION_SUGGEST_KEYWORDS, idempotent = true)
214 	public Parameters suggestKeywords(
215 		@OperationParam(name = "context", min = 1, max = 1) String theContext,
216 		@OperationParam(name = "searchParam", min = 1, max = 1) String theSearchParam,
217 		@OperationParam(name = "text", min = 1, max = 1) String theText
218 	) {
219 
220 
221 		if (isBlank(theContext)) {
222 			throw new InvalidRequestException("Parameter 'context' must be provided");
223 		}
224 		if (isBlank(theSearchParam)) {
225 			throw new InvalidRequestException("Parameter 'searchParam' must be provided");
226 		}
227 		if (isBlank(theText)) {
228 			throw new InvalidRequestException("Parameter 'text' must be provided");
229 		}
230 
231 		List<Suggestion> keywords = mySearchDao.suggestKeywords(theContext, theSearchParam, theText);
232 
233 		Parameters retVal = new Parameters();
234 		for (Suggestion next : keywords) {
235 			retVal.addParameter()
236 				.addPart(new ParametersParameterComponent().setName("keyword").setValue(new StringType(next.getTerm())))
237 				.addPart(new ParametersParameterComponent().setName("score").setValue(new DecimalType(next.getScore())));
238 		}
239 
240 		return retVal;
241 	}
242 
243 	@Transaction
244 	public Bundle transaction(RequestDetails theRequestDetails, @TransactionParam Bundle theResources) {
245 		startRequest(((ServletRequestDetails) theRequestDetails).getServletRequest());
246 		try {
247 			return getDao().transaction(theRequestDetails, theResources);
248 		} finally {
249 			endRequest(((ServletRequestDetails) theRequestDetails).getServletRequest());
250 		}
251 	}
252 
253 	public static void validateFulltextSearchEnabled(IFulltextSearchSvc theSearchDao) {
254 		if (theSearchDao == null || theSearchDao.isDisabled()) {
255 			throw new InvalidRequestException("Fulltext searching is disabled on this server");
256 		}
257 	}
258 
259 }