View Javadoc
1   package ca.uhn.fhir.jpa.dao;
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.context.RuntimeResourceDefinition;
24  import ca.uhn.fhir.jpa.entity.ResourceTable;
25  import ca.uhn.fhir.jpa.entity.TagDefinition;
26  import ca.uhn.fhir.jpa.provider.ServletSubRequestDetails;
27  import ca.uhn.fhir.jpa.util.DeleteConflict;
28  import ca.uhn.fhir.model.api.IResource;
29  import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
30  import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt;
31  import ca.uhn.fhir.model.dstu2.composite.MetaDt;
32  import ca.uhn.fhir.model.dstu2.resource.Bundle;
33  import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
34  import ca.uhn.fhir.model.dstu2.resource.Bundle.EntryResponse;
35  import ca.uhn.fhir.model.dstu2.resource.OperationOutcome;
36  import ca.uhn.fhir.model.dstu2.valueset.BundleTypeEnum;
37  import ca.uhn.fhir.model.dstu2.valueset.HTTPVerbEnum;
38  import ca.uhn.fhir.model.dstu2.valueset.IssueSeverityEnum;
39  import ca.uhn.fhir.model.primitive.IdDt;
40  import ca.uhn.fhir.model.primitive.InstantDt;
41  import ca.uhn.fhir.model.primitive.UriDt;
42  import ca.uhn.fhir.parser.DataFormatException;
43  import ca.uhn.fhir.parser.IParser;
44  import ca.uhn.fhir.rest.api.Constants;
45  import ca.uhn.fhir.rest.api.RequestTypeEnum;
46  import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
47  import ca.uhn.fhir.rest.api.server.RequestDetails;
48  import ca.uhn.fhir.rest.server.RestfulServerUtils;
49  import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
50  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
51  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
52  import ca.uhn.fhir.rest.server.exceptions.NotModifiedException;
53  import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetails;
54  import ca.uhn.fhir.rest.server.method.BaseMethodBinding;
55  import ca.uhn.fhir.rest.server.method.BaseResourceReturningMethodBinding;
56  import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
57  import ca.uhn.fhir.util.FhirTerser;
58  import ca.uhn.fhir.util.UrlUtil;
59  import ca.uhn.fhir.util.UrlUtil.UrlParts;
60  import com.google.common.collect.ArrayListMultimap;
61  import org.apache.http.NameValuePair;
62  import org.hl7.fhir.instance.model.api.IBaseResource;
63  import org.hl7.fhir.instance.model.api.IIdType;
64  import org.springframework.beans.factory.annotation.Autowired;
65  import org.springframework.transaction.PlatformTransactionManager;
66  import org.springframework.transaction.TransactionDefinition;
67  import org.springframework.transaction.TransactionStatus;
68  import org.springframework.transaction.annotation.Propagation;
69  import org.springframework.transaction.annotation.Transactional;
70  import org.springframework.transaction.support.TransactionCallback;
71  import org.springframework.transaction.support.TransactionCallbackWithoutResult;
72  import org.springframework.transaction.support.TransactionTemplate;
73  
74  import javax.persistence.TypedQuery;
75  import java.util.*;
76  
77  import static org.apache.commons.lang3.StringUtils.*;
78  
79  public class FhirSystemDaoDstu2 extends BaseHapiFhirSystemDao<Bundle, MetaDt> {
80  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirSystemDaoDstu2.class);
81  
82  	@Autowired
83  	private PlatformTransactionManager myTxManager;
84  	@Autowired
85  	private DaoRegistry myDaoRegistry;
86  
87  	private Bundle batch(final RequestDetails theRequestDetails, Bundle theRequest) {
88  		ourLog.info("Beginning batch with {} resources", theRequest.getEntry().size());
89  		long start = System.currentTimeMillis();
90  
91  		TransactionTemplate txTemplate = new TransactionTemplate(myTxManager);
92  		txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
93  
94  		Bundle resp = new Bundle();
95  		resp.setType(BundleTypeEnum.BATCH_RESPONSE);
96  
97  		/*
98  		 * For batch, we handle each entry as a mini-transaction in its own database transaction so that if one fails, it doesn't prevent others
99  		 */
100 
101 		for (final Entry nextRequestEntry : theRequest.getEntry()) {
102 
103 			TransactionCallback<Bundle> callback = new TransactionCallback<Bundle>() {
104 				@Override
105 				public Bundle doInTransaction(TransactionStatus theStatus) {
106 					Bundle subRequestBundle = new Bundle();
107 					subRequestBundle.setType(BundleTypeEnum.TRANSACTION);
108 					subRequestBundle.addEntry(nextRequestEntry);
109 					return transaction((ServletRequestDetails) theRequestDetails, subRequestBundle, "Batch sub-request");
110 				}
111 			};
112 
113 			BaseServerResponseException caughtEx;
114 			try {
115 				Bundle nextResponseBundle;
116 				if (nextRequestEntry.getRequest().getMethodElement().getValueAsEnum() == HTTPVerbEnum.GET) {
117 					// Don't process GETs in a transaction because they'll
118 					// create their own
119 					nextResponseBundle = callback.doInTransaction(null);
120 				} else {
121 					nextResponseBundle = txTemplate.execute(callback);
122 				}
123 				caughtEx = null;
124 
125 				Entry subResponseEntry = nextResponseBundle.getEntry().get(0);
126 				resp.addEntry(subResponseEntry);
127 				/*
128 				 * If the individual entry didn't have a resource in its response, bring the sub-transaction's OperationOutcome across so the client can see it
129 				 */
130 				if (subResponseEntry.getResource() == null) {
131 					subResponseEntry.setResource(nextResponseBundle.getEntry().get(0).getResource());
132 				}
133 
134 			} catch (BaseServerResponseException e) {
135 				caughtEx = e;
136 			} catch (Throwable t) {
137 				ourLog.error("Failure during BATCH sub transaction processing", t);
138 				caughtEx = new InternalErrorException(t);
139 			}
140 
141 			if (caughtEx != null) {
142 				Entry nextEntry = resp.addEntry();
143 
144 				OperationOutcome oo = new OperationOutcome();
145 				oo.addIssue().setSeverity(IssueSeverityEnum.ERROR).setDiagnostics(caughtEx.getMessage());
146 				nextEntry.setResource(oo);
147 
148 				EntryResponse nextEntryResp = nextEntry.getResponse();
149 				nextEntryResp.setStatus(toStatusString(caughtEx.getStatusCode()));
150 			}
151 
152 		}
153 
154 		long delay = System.currentTimeMillis() - start;
155 		ourLog.info("Batch completed in {}ms", new Object[] {delay});
156 
157 		return resp;
158 	}
159 
160 	@SuppressWarnings("unchecked")
161 	private Bundle doTransaction(ServletRequestDetails theRequestDetails, Bundle theRequest, String theActionName) {
162 		BundleTypeEnum transactionType = theRequest.getTypeElement().getValueAsEnum();
163 		if (transactionType == BundleTypeEnum.BATCH) {
164 			return batch(theRequestDetails, theRequest);
165 		}
166 
167 		return doTransaction(theRequestDetails, theRequest, theActionName, transactionType);
168 	}
169 
170 	private Bundle doTransaction(ServletRequestDetails theRequestDetails, Bundle theRequest, String theActionName, BundleTypeEnum theTransactionType) {
171 		if (theTransactionType == null) {
172 			String message = "Transaction Bundle did not specify valid Bundle.type, assuming " + BundleTypeEnum.TRANSACTION.getCode();
173 			ourLog.warn(message);
174 			theTransactionType = BundleTypeEnum.TRANSACTION;
175 		}
176 		if (theTransactionType != BundleTypeEnum.TRANSACTION) {
177 			throw new InvalidRequestException("Unable to process transaction where incoming Bundle.type = " + theTransactionType.getCode());
178 		}
179 
180 		ourLog.info("Beginning {} with {} resources", theActionName, theRequest.getEntry().size());
181 
182 		long start = System.currentTimeMillis();
183 		Date updateTime = new Date();
184 
185 		Set<IdDt> allIds = new LinkedHashSet<IdDt>();
186 		Map<IdDt, IdDt> idSubstitutions = new HashMap<IdDt, IdDt>();
187 		Map<IdDt, DaoMethodOutcome> idToPersistedOutcome = new HashMap<IdDt, DaoMethodOutcome>();
188 
189 		/*
190 		 * We want to execute the transaction request bundle elements in the order
191 		 * specified by the FHIR specification (see TransactionSorter) so we save the
192 		 * original order in the request, then sort it.
193 		 *
194 		 * Entries with a type of GET are removed from the bundle so that they
195 		 * can be processed at the very end. We do this because the incoming resources
196 		 * are saved in a two-phase way in order to deal with interdependencies, and
197 		 * we want the GET processing to use the final indexing state
198 		 */
199 		Bundle response = new Bundle();
200 		List<Entry> getEntries = new ArrayList<Entry>();
201 		IdentityHashMap<Entry, Integer> originalRequestOrder = new IdentityHashMap<Entry, Integer>();
202 		for (int i = 0; i < theRequest.getEntry().size(); i++) {
203 			originalRequestOrder.put(theRequest.getEntry().get(i), i);
204 			response.addEntry();
205 			if (theRequest.getEntry().get(i).getRequest().getMethodElement().getValueAsEnum() == HTTPVerbEnum.GET) {
206 				getEntries.add(theRequest.getEntry().get(i));
207 			}
208 		}
209 		Collections.sort(theRequest.getEntry(), new TransactionSorter());
210 
211 		List<IIdType> deletedResources = new ArrayList<>();
212 		List<DeleteConflict> deleteConflicts = new ArrayList<>();
213 		Map<Entry, ResourceTable> entriesToProcess = new IdentityHashMap<>();
214 		Set<ResourceTable> nonUpdatedEntities = new HashSet<ResourceTable>();
215 		Set<ResourceTable> updatedEntities = new HashSet<>();
216 
217 		/*
218 		 * Handle: GET/PUT/POST
219 		 */
220 		TransactionTemplate txTemplate = new TransactionTemplate(myTxManager);
221 		txTemplate.execute(t->{
222 			handleTransactionWriteOperations(theRequestDetails, theRequest, theActionName, updateTime, allIds, idSubstitutions, idToPersistedOutcome, response, originalRequestOrder, deletedResources, deleteConflicts, entriesToProcess, nonUpdatedEntities, updatedEntities);
223 			return null;
224 		});
225 
226 		/*
227 		 * Loop through the request and process any entries of type GET
228 		 */
229 		for (int i = 0; i < getEntries.size(); i++) {
230 			Entry nextReqEntry = getEntries.get(i);
231 			Integer originalOrder = originalRequestOrder.get(nextReqEntry);
232 			Entry nextRespEntry = response.getEntry().get(originalOrder);
233 
234 			ServletSubRequestDetails requestDetails = new ServletSubRequestDetails();
235 			requestDetails.setServletRequest(theRequestDetails.getServletRequest());
236 			requestDetails.setRequestType(RequestTypeEnum.GET);
237 			requestDetails.setServer(theRequestDetails.getServer());
238 
239 			String url = extractTransactionUrlOrThrowException(nextReqEntry, HTTPVerbEnum.GET);
240 
241 			int qIndex = url.indexOf('?');
242 			ArrayListMultimap<String, String> paramValues = ArrayListMultimap.create();
243 			requestDetails.setParameters(new HashMap<String, String[]>());
244 			if (qIndex != -1) {
245 				String params = url.substring(qIndex);
246 				List<NameValuePair> parameters = translateMatchUrl(params);
247 				for (NameValuePair next : parameters) {
248 					paramValues.put(next.getName(), next.getValue());
249 				}
250 				for (Map.Entry<String, Collection<String>> nextParamEntry : paramValues.asMap().entrySet()) {
251 					String[] nextValue = nextParamEntry.getValue().toArray(new String[nextParamEntry.getValue().size()]);
252 					requestDetails.addParameter(nextParamEntry.getKey(), nextValue);
253 				}
254 				url = url.substring(0, qIndex);
255 			}
256 
257 			requestDetails.setRequestPath(url);
258 			requestDetails.setFhirServerBase(theRequestDetails.getFhirServerBase());
259 
260 			theRequestDetails.getServer().populateRequestDetailsFromRequestPath(requestDetails, url);
261 			BaseMethodBinding<?> method = theRequestDetails.getServer().determineResourceMethod(requestDetails, url);
262 			if (method == null) {
263 				throw new IllegalArgumentException("Unable to handle GET " + url);
264 			}
265 
266 			if (isNotBlank(nextReqEntry.getRequest().getIfMatch())) {
267 				requestDetails.addHeader(Constants.HEADER_IF_MATCH, nextReqEntry.getRequest().getIfMatch());
268 			}
269 			if (isNotBlank(nextReqEntry.getRequest().getIfNoneExist())) {
270 				requestDetails.addHeader(Constants.HEADER_IF_NONE_EXIST, nextReqEntry.getRequest().getIfNoneExist());
271 			}
272 			if (isNotBlank(nextReqEntry.getRequest().getIfNoneMatch())) {
273 				requestDetails.addHeader(Constants.HEADER_IF_NONE_MATCH, nextReqEntry.getRequest().getIfNoneMatch());
274 			}
275 
276 			if (method instanceof BaseResourceReturningMethodBinding) {
277 				try {
278 					IBaseResource resource = ((BaseResourceReturningMethodBinding) method).doInvokeServer(theRequestDetails.getServer(), requestDetails);
279 					if (paramValues.containsKey(Constants.PARAM_SUMMARY) || paramValues.containsKey(Constants.PARAM_CONTENT)) {
280 						resource = filterNestedBundle(requestDetails, resource);
281 					}
282 					nextRespEntry.setResource((IResource) resource);
283 					nextRespEntry.getResponse().setStatus(toStatusString(Constants.STATUS_HTTP_200_OK));
284 				} catch (NotModifiedException e) {
285 					nextRespEntry.getResponse().setStatus(toStatusString(Constants.STATUS_HTTP_304_NOT_MODIFIED));
286 				}
287 			} else {
288 				throw new IllegalArgumentException("Unable to handle GET " + url);
289 			}
290 
291 		}
292 
293 		for (Map.Entry<Entry, ResourceTable> nextEntry : entriesToProcess.entrySet()) {
294 			nextEntry.getKey().getResponse().setLocation(nextEntry.getValue().getIdDt().toUnqualified().getValue());
295 			nextEntry.getKey().getResponse().setEtag(nextEntry.getValue().getIdDt().getVersionIdPart());
296 		}
297 
298 		long delay = System.currentTimeMillis() - start;
299 		int numEntries = theRequest.getEntry().size();
300 		long delayPer = delay / numEntries;
301 		ourLog.info("{} completed in {}ms ({} entries at {}ms per entry)", new Object[] {theActionName, delay, numEntries, delayPer});
302 
303 		response.setType(BundleTypeEnum.TRANSACTION_RESPONSE);
304 		return response;
305 	}
306 
307 	private void handleTransactionWriteOperations(ServletRequestDetails theRequestDetails, Bundle theRequest, String theActionName, Date theUpdateTime, Set<IdDt> theAllIds, Map<IdDt, IdDt> theIdSubstitutions, Map<IdDt, DaoMethodOutcome> theIdToPersistedOutcome, Bundle theResponse, IdentityHashMap<Entry, Integer> theOriginalRequestOrder, List<IIdType> theDeletedResources, List<DeleteConflict> theDeleteConflicts, Map<Entry, ResourceTable> theEntriesToProcess, Set<ResourceTable> theNonUpdatedEntities, Set<ResourceTable> theUpdatedEntities) {
308 		/*
309 		 * Loop through the request and process any entries of type
310 		 * PUT, POST or DELETE
311 		 */
312 		for (int i = 0; i < theRequest.getEntry().size(); i++) {
313 
314 			if (i % 100 == 0) {
315 				ourLog.debug("Processed {} non-GET entries out of {}", i, theRequest.getEntry().size());
316 			}
317 
318 			Entry nextReqEntry = theRequest.getEntry().get(i);
319 			IResource res = nextReqEntry.getResource();
320 			IdDt nextResourceId = null;
321 			if (res != null) {
322 
323 				nextResourceId = res.getId();
324 
325 				if (!nextResourceId.hasIdPart()) {
326 					if (isNotBlank(nextReqEntry.getFullUrl())) {
327 						nextResourceId = new IdDt(nextReqEntry.getFullUrl());
328 					}
329 				}
330 
331 				if (nextResourceId.hasIdPart() && nextResourceId.getIdPart().matches("[a-zA-Z]+\\:.*") && !isPlaceholder(nextResourceId)) {
332 					throw new InvalidRequestException("Invalid placeholder ID found: " + nextResourceId.getIdPart() + " - Must be of the form 'urn:uuid:[uuid]' or 'urn:oid:[oid]'");
333 				}
334 
335 				if (nextResourceId.hasIdPart() && !nextResourceId.hasResourceType() && !isPlaceholder(nextResourceId)) {
336 					nextResourceId = new IdDt(toResourceName(res.getClass()), nextResourceId.getIdPart());
337 					res.setId(nextResourceId);
338 				}
339 
340 				/*
341 				 * Ensure that the bundle doesn't have any duplicates, since this causes all kinds of weirdness
342 				 */
343 				if (isPlaceholder(nextResourceId)) {
344 					if (!theAllIds.add(nextResourceId)) {
345 						throw new InvalidRequestException(getContext().getLocalizer().getMessage(BaseHapiFhirSystemDao.class, "transactionContainsMultipleWithDuplicateId", nextResourceId));
346 					}
347 				} else if (nextResourceId.hasResourceType() && nextResourceId.hasIdPart()) {
348 					IdDt nextId = nextResourceId.toUnqualifiedVersionless();
349 					if (!theAllIds.add(nextId)) {
350 						throw new InvalidRequestException(getContext().getLocalizer().getMessage(BaseHapiFhirSystemDao.class, "transactionContainsMultipleWithDuplicateId", nextId));
351 					}
352 				}
353 
354 			}
355 
356 			HTTPVerbEnum verb = nextReqEntry.getRequest().getMethodElement().getValueAsEnum();
357 			if (verb == null) {
358 				throw new InvalidRequestException(getContext().getLocalizer().getMessage(BaseHapiFhirSystemDao.class, "transactionEntryHasInvalidVerb", nextReqEntry.getRequest().getMethod()));
359 			}
360 
361 			String resourceType = res != null ? getContext().getResourceDefinition(res).getName() : null;
362 			Entry nextRespEntry = theResponse.getEntry().get(theOriginalRequestOrder.get(nextReqEntry));
363 
364 			switch (verb) {
365 				case POST: {
366 					// CREATE
367 					@SuppressWarnings("rawtypes")
368 					IFhirResourceDao resourceDao = myDaoRegistry.getResourceDao(res.getClass());
369 					res.setId((String) null);
370 					DaoMethodOutcome outcome;
371 					outcome = resourceDao.create(res, nextReqEntry.getRequest().getIfNoneExist(), false, theRequestDetails);
372 					handleTransactionCreateOrUpdateOutcome(theIdSubstitutions, theIdToPersistedOutcome, nextResourceId, outcome, nextRespEntry, resourceType, res);
373 					theEntriesToProcess.put(nextRespEntry, outcome.getEntity());
374 					if (outcome.getCreated() == false) {
375 						theNonUpdatedEntities.add(outcome.getEntity());
376 					}
377 					break;
378 				}
379 				case DELETE: {
380 					// DELETE
381 					String url = extractTransactionUrlOrThrowException(nextReqEntry, verb);
382 					UrlParts parts = UrlUtil.parseUrl(url);
383 					IFhirResourceDao<? extends IBaseResource> dao = toDao(parts, verb.getCode(), url);
384 					int status = Constants.STATUS_HTTP_204_NO_CONTENT;
385 					if (parts.getResourceId() != null) {
386 						DaoMethodOutcome outcome = dao.delete(new IdDt(parts.getResourceType(), parts.getResourceId()), theDeleteConflicts, theRequestDetails);
387 						if (outcome.getEntity() != null) {
388 							theDeletedResources.add(outcome.getId().toUnqualifiedVersionless());
389 							theEntriesToProcess.put(nextRespEntry, outcome.getEntity());
390 						}
391 					} else {
392 						DeleteMethodOutcome deleteOutcome = dao.deleteByUrl(parts.getResourceType() + '?' + parts.getParams(), theDeleteConflicts, theRequestDetails);
393 						List<ResourceTable> allDeleted = deleteOutcome.getDeletedEntities();
394 						for (ResourceTable deleted : allDeleted) {
395 							theDeletedResources.add(deleted.getIdDt().toUnqualifiedVersionless());
396 						}
397 						if (allDeleted.isEmpty()) {
398 							status = Constants.STATUS_HTTP_404_NOT_FOUND;
399 						}
400 					}
401 
402 					nextRespEntry.getResponse().setStatus(toStatusString(status));
403 					break;
404 				}
405 				case PUT: {
406 					// UPDATE
407 					@SuppressWarnings("rawtypes")
408 					IFhirResourceDao resourceDao = myDaoRegistry.getResourceDao(res.getClass());
409 
410 					DaoMethodOutcome outcome;
411 
412 					String url = extractTransactionUrlOrThrowException(nextReqEntry, verb);
413 
414 					UrlParts parts = UrlUtil.parseUrl(url);
415 					if (isNotBlank(parts.getResourceId())) {
416 						res.setId(new IdDt(parts.getResourceType(), parts.getResourceId()));
417 						outcome = resourceDao.update(res, null, false, theRequestDetails);
418 					} else {
419 						res.setId((String) null);
420 						outcome = resourceDao.update(res, parts.getResourceType() + '?' + parts.getParams(), false, theRequestDetails);
421 					}
422 
423 					if (outcome.getCreated() == Boolean.FALSE) {
424 						theUpdatedEntities.add(outcome.getEntity());
425 					}
426 
427 					handleTransactionCreateOrUpdateOutcome(theIdSubstitutions, theIdToPersistedOutcome, nextResourceId, outcome, nextRespEntry, resourceType, res);
428 					theEntriesToProcess.put(nextRespEntry, outcome.getEntity());
429 					break;
430 				}
431 				case GET:
432 					break;
433 			}
434 		}
435 
436 		/*
437 		 * Make sure that there are no conflicts from deletions. E.g. we can't delete something
438 		 * if something else has a reference to it.. Unless the thing that has a reference to it
439 		 * was also deleted as a part of this transaction, which is why we check this now at the
440 		 * end.
441 		 */
442 
443 		theDeleteConflicts.removeIf(next -> theDeletedResources.contains(next.getTargetId().toVersionless()));
444 		validateDeleteConflictsEmptyOrThrowException(theDeleteConflicts);
445 
446 		/*
447 		 * Perform ID substitutions and then index each resource we have saved
448 		 */
449 
450 		FhirTerser terser = getContext().newTerser();
451 		for (DaoMethodOutcome nextOutcome : theIdToPersistedOutcome.values()) {
452 			IResource nextResource = (IResource) nextOutcome.getResource();
453 			if (nextResource == null) {
454 				continue;
455 			}
456 
457 			// References
458 			List<BaseResourceReferenceDt> allRefs = terser.getAllPopulatedChildElementsOfType(nextResource, BaseResourceReferenceDt.class);
459 			for (BaseResourceReferenceDt nextRef : allRefs) {
460 				IdDt nextId = nextRef.getReference();
461 				if (!nextId.hasIdPart()) {
462 					continue;
463 				}
464 				if (theIdSubstitutions.containsKey(nextId)) {
465 					IdDt newId = theIdSubstitutions.get(nextId);
466 					ourLog.debug(" * Replacing resource ref {} with {}", nextId, newId);
467 					nextRef.setReference(newId);
468 				} else {
469 					ourLog.debug(" * Reference [{}] does not exist in bundle", nextId);
470 				}
471 			}
472 
473 			// URIs
474 			List<UriDt> allUris = terser.getAllPopulatedChildElementsOfType(nextResource, UriDt.class);
475 			for (UriDt nextRef : allUris) {
476 				if (nextRef instanceof IIdType) {
477 					continue; // No substitution on the resource ID itself!
478 				}
479 				IdDt nextUriString = new IdDt(nextRef.getValueAsString());
480 				if (theIdSubstitutions.containsKey(nextUriString)) {
481 					IdDt newId = theIdSubstitutions.get(nextUriString);
482 					ourLog.debug(" * Replacing resource ref {} with {}", nextUriString, newId);
483 					nextRef.setValue(newId.getValue());
484 				} else {
485 					ourLog.debug(" * Reference [{}] does not exist in bundle", nextUriString);
486 				}
487 			}
488 
489 
490 			InstantDt deletedInstantOrNull = ResourceMetadataKeyEnum.DELETED_AT.get(nextResource);
491 			Date deletedTimestampOrNull = deletedInstantOrNull != null ? deletedInstantOrNull.getValue() : null;
492 			if (theUpdatedEntities.contains(nextOutcome.getEntity())) {
493 				updateInternal(theRequestDetails, nextResource, true, false, theRequestDetails, nextOutcome.getEntity(), nextResource.getIdElement(), nextOutcome.getPreviousResource());
494 			} else if (!theNonUpdatedEntities.contains(nextOutcome.getEntity())) {
495 				updateEntity(theRequestDetails, nextResource, nextOutcome.getEntity(), deletedTimestampOrNull, true, false, theUpdateTime, false, true);
496 			}
497 		}
498 
499 		myEntityManager.flush();
500 
501 		/*
502 		 * Double check we didn't allow any duplicates we shouldn't have
503 		 */
504 		for (Entry nextEntry : theRequest.getEntry()) {
505 			if (nextEntry.getRequest().getMethodElement().getValueAsEnum() == HTTPVerbEnum.POST) {
506 				String matchUrl = nextEntry.getRequest().getIfNoneExist();
507 				if (isNotBlank(matchUrl)) {
508 					IFhirResourceDao<?> resourceDao = getDao(nextEntry.getResource().getClass());
509 					Set<Long> val = resourceDao.processMatchUrl(matchUrl);
510 					if (val.size() > 1) {
511 						throw new InvalidRequestException(
512 							"Unable to process " + theActionName + " - Request would cause multiple resources to match URL: \"" + matchUrl + "\". Does transaction request contain duplicates?");
513 					}
514 				}
515 			}
516 		}
517 
518 		for (IdDt next : theAllIds) {
519 			IdDt replacement = theIdSubstitutions.get(next);
520 			if (replacement == null) {
521 				continue;
522 			}
523 			if (replacement.equals(next)) {
524 				continue;
525 			}
526 			ourLog.debug("Placeholder resource ID \"{}\" was replaced with permanent ID \"{}\"", next, replacement);
527 		}
528 	}
529 
530 	private String extractTransactionUrlOrThrowException(Entry nextEntry, HTTPVerbEnum verb) {
531 		String url = nextEntry.getRequest().getUrl();
532 		if (isBlank(url)) {
533 			throw new InvalidRequestException(getContext().getLocalizer().getMessage(BaseHapiFhirSystemDao.class, "transactionMissingUrl", verb.name()));
534 		}
535 		return url;
536 	}
537 
538 	/**
539 	 * This method is called for nested bundles (e.g. if we received a transaction with an entry that
540 	 * was a GET search, this method is called on the bundle for the search result, that will be placed in the
541 	 * outer bundle). This method applies the _summary and _content parameters to the output of
542 	 * that bundle.
543 	 * <p>
544 	 * TODO: This isn't the most efficient way of doing this.. hopefully we can come up with something better in the future.
545 	 */
546 	private IBaseResource filterNestedBundle(RequestDetails theRequestDetails, IBaseResource theResource) {
547 		IParser p = getContext().newJsonParser();
548 		RestfulServerUtils.configureResponseParser(theRequestDetails, p);
549 		return p.parseResource(theResource.getClass(), p.encodeResourceToString(theResource));
550 	}
551 
552 	@Override
553 	public MetaDt metaGetOperation(RequestDetails theRequestDetails) {
554 		// Notify interceptors
555 		ActionRequestDetails requestDetails = new ActionRequestDetails(theRequestDetails);
556 		notifyInterceptors(RestOperationTypeEnum.META, requestDetails);
557 
558 		String sql = "SELECT d FROM TagDefinition d WHERE d.myId IN (SELECT DISTINCT t.myTagId FROM ResourceTag t)";
559 		TypedQuery<TagDefinition> q = myEntityManager.createQuery(sql, TagDefinition.class);
560 		List<TagDefinition> tagDefinitions = q.getResultList();
561 
562 		MetaDt retVal = toMetaDt(tagDefinitions);
563 
564 		return retVal;
565 	}
566 
567 	private ca.uhn.fhir.jpa.dao.IFhirResourceDao<? extends IBaseResource> toDao(UrlParts theParts, String theVerb, String theUrl) {
568 		RuntimeResourceDefinition resType;
569 		try {
570 			resType = getContext().getResourceDefinition(theParts.getResourceType());
571 		} catch (DataFormatException e) {
572 			String msg = getContext().getLocalizer().getMessage(BaseHapiFhirSystemDao.class, "transactionInvalidUrl", theVerb, theUrl);
573 			throw new InvalidRequestException(msg);
574 		}
575 		IFhirResourceDao<? extends IBaseResource> dao = null;
576 		if (resType != null) {
577 			dao = getDao(resType.getImplementingClass());
578 		}
579 		if (dao == null) {
580 			String msg = getContext().getLocalizer().getMessage(BaseHapiFhirSystemDao.class, "transactionInvalidUrl", theVerb, theUrl);
581 			throw new InvalidRequestException(msg);
582 		}
583 
584 		// if (theParts.getResourceId() == null && theParts.getParams() == null) {
585 		// String msg = getContext().getLocalizer().getMessage(BaseHapiFhirSystemDao.class, "transactionInvalidUrl", theVerb, theUrl);
586 		// throw new InvalidRequestException(msg);
587 		// }
588 
589 		return dao;
590 	}
591 
592 	protected MetaDt toMetaDt(Collection<TagDefinition> tagDefinitions) {
593 		MetaDt retVal = new MetaDt();
594 		for (TagDefinition next : tagDefinitions) {
595 			switch (next.getTagType()) {
596 				case PROFILE:
597 					retVal.addProfile(next.getCode());
598 					break;
599 				case SECURITY_LABEL:
600 					retVal.addSecurity().setSystem(next.getSystem()).setCode(next.getCode()).setDisplay(next.getDisplay());
601 					break;
602 				case TAG:
603 					retVal.addTag().setSystem(next.getSystem()).setCode(next.getCode()).setDisplay(next.getDisplay());
604 					break;
605 			}
606 		}
607 		return retVal;
608 	}
609 
610 	@Transactional(propagation = Propagation.NEVER)
611 	@Override
612 	public Bundle transaction(RequestDetails theRequestDetails, Bundle theRequest) {
613 		if (theRequestDetails != null) {
614 			ActionRequestDetails requestDetails = new ActionRequestDetails(theRequestDetails, theRequest, "Bundle", null);
615 			notifyInterceptors(RestOperationTypeEnum.TRANSACTION, requestDetails);
616 		}
617 
618 		String actionName = "Transaction";
619 		return transaction((ServletRequestDetails) theRequestDetails, theRequest, actionName);
620 	}
621 
622 	private Bundle transaction(ServletRequestDetails theRequestDetails, Bundle theRequest, String theActionName) {
623 		super.markRequestAsProcessingSubRequest(theRequestDetails);
624 		try {
625 			return doTransaction(theRequestDetails, theRequest, theActionName);
626 		} finally {
627 			super.clearRequestAsProcessingSubRequest(theRequestDetails);
628 		}
629 	}
630 
631 	private static void handleTransactionCreateOrUpdateOutcome(Map<IdDt, IdDt> idSubstitutions, Map<IdDt, DaoMethodOutcome> idToPersistedOutcome, IdDt nextResourceId, DaoMethodOutcome outcome,
632 																				  Entry newEntry, String theResourceType, IResource theRes) {
633 		IdDt newId = (IdDt) outcome.getId().toUnqualifiedVersionless();
634 		IdDt resourceId = isPlaceholder(nextResourceId) ? nextResourceId : nextResourceId.toUnqualifiedVersionless();
635 		if (newId.equals(resourceId) == false) {
636 			idSubstitutions.put(resourceId, newId);
637 			if (isPlaceholder(resourceId)) {
638 				/*
639 				 * The correct way for substitution IDs to be is to be with no resource type, but we'll accept the qualified kind too just to be lenient.
640 				 */
641 				idSubstitutions.put(new IdDt(theResourceType + '/' + resourceId.getValue()), newId);
642 			}
643 		}
644 		idToPersistedOutcome.put(newId, outcome);
645 		if (outcome.getCreated().booleanValue()) {
646 			newEntry.getResponse().setStatus(toStatusString(Constants.STATUS_HTTP_201_CREATED));
647 		} else {
648 			newEntry.getResponse().setStatus(toStatusString(Constants.STATUS_HTTP_200_OK));
649 		}
650 		newEntry.getResponse().setLastModified(ResourceMetadataKeyEnum.UPDATED.get(theRes));
651 	}
652 
653 	private static boolean isPlaceholder(IdDt theId) {
654 		if (theId.getValue() != null) {
655 			if (theId.getValue().startsWith("urn:oid:") || theId.getValue().startsWith("urn:uuid:")) {
656 				return true;
657 			}
658 		}
659 		return false;
660 	}
661 
662 	private static String toStatusString(int theStatusCode) {
663 		return Integer.toString(theStatusCode) + " " + defaultString(Constants.HTTP_STATUS_NAMES.get(theStatusCode));
664 	}
665 
666 	//@formatter:off
667 
668 	/**
669 	 * Transaction Order, per the spec:
670 	 * <p>
671 	 * Process any DELETE interactions
672 	 * Process any POST interactions
673 	 * Process any PUT interactions
674 	 * Process any GET interactions
675 	 */
676 	//@formatter:off
677 	public class TransactionSorter implements Comparator<Entry> {
678 
679 		@Override
680 		public int compare(Entry theO1, Entry theO2) {
681 			int o1 = toOrder(theO1);
682 			int o2 = toOrder(theO2);
683 
684 			return o1 - o2;
685 		}
686 
687 		private int toOrder(Entry theO1) {
688 			int o1 = 0;
689 			if (theO1.getRequest().getMethodElement().getValueAsEnum() != null) {
690 				switch (theO1.getRequest().getMethodElement().getValueAsEnum()) {
691 					case DELETE:
692 						o1 = 1;
693 						break;
694 					case POST:
695 						o1 = 2;
696 						break;
697 					case PUT:
698 						o1 = 3;
699 						break;
700 					case GET:
701 						o1 = 4;
702 						break;
703 				}
704 			}
705 			return o1;
706 		}
707 
708 	}
709 
710 }