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