001package ca.uhn.fhir.jpa.dao.dstu3;
002
003/*
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.i18n.Msg;
024import ca.uhn.fhir.interceptor.model.RequestPartitionId;
025import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoPatient;
026import ca.uhn.fhir.jpa.dao.BaseHapiFhirResourceDao;
027import ca.uhn.fhir.jpa.partition.IRequestPartitionHelperSvc;
028import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
029import ca.uhn.fhir.jpa.searchparam.SearchParameterMap.EverythingModeEnum;
030import ca.uhn.fhir.model.api.IResource;
031import ca.uhn.fhir.rest.api.CacheControlDirective;
032import ca.uhn.fhir.rest.api.Constants;
033import ca.uhn.fhir.rest.api.SortSpec;
034import ca.uhn.fhir.rest.api.server.IBundleProvider;
035import ca.uhn.fhir.rest.api.server.RequestDetails;
036import ca.uhn.fhir.rest.param.*;
037import org.hl7.fhir.dstu3.model.Patient;
038import org.hl7.fhir.instance.model.api.IIdType;
039import org.hl7.fhir.instance.model.api.IPrimitiveType;
040import org.springframework.beans.factory.annotation.Autowired;
041
042import javax.servlet.http.HttpServletRequest;
043import java.util.Arrays;
044import java.util.Collections;
045
046public class FhirResourceDaoPatientDstu3 extends BaseHapiFhirResourceDao<Patient> implements IFhirResourceDaoPatient<Patient> {
047
048        @Autowired
049        private IRequestPartitionHelperSvc myPartitionHelperSvc;
050
051        private IBundleProvider doEverythingOperation(TokenOrListParam theId, IPrimitiveType<Integer> theCount, IPrimitiveType<Integer> theOffset, DateRangeParam theLastUpdated, SortSpec theSort, StringAndListParam theContent, StringAndListParam theNarrative, StringAndListParam theFilter, RequestDetails theRequest) {
052                SearchParameterMap paramMap = new SearchParameterMap();
053                if (theCount != null) {
054                        paramMap.setCount(theCount.getValue());
055                }
056                if (theOffset != null) {
057                        throw new IllegalArgumentException(Msg.code(1082) + "Everything operation does not support offset searching");
058                }
059                if (theContent != null) {
060                        paramMap.add(Constants.PARAM_CONTENT, theContent);
061                }
062                if (theNarrative != null) {
063                        paramMap.add(Constants.PARAM_TEXT, theNarrative);
064                }
065                paramMap.setIncludes(Collections.singleton(IResource.INCLUDE_ALL.asRecursive()));
066                paramMap.setEverythingMode(theId != null ? EverythingModeEnum.PATIENT_INSTANCE : EverythingModeEnum.PATIENT_TYPE);
067                paramMap.setSort(theSort);
068                paramMap.setLastUpdated(theLastUpdated);
069                if (theId != null) {
070                        if (theRequest.getParameters().containsKey("_mdm")) {
071                                String[] paramVal = theRequest.getParameters().get("_mdm");
072                                if (Arrays.asList(paramVal).contains("true")) {
073                                        theId.getValuesAsQueryTokens().stream().forEach(param -> param.setMdmExpand(true));
074                                }
075                        }
076                        paramMap.add("_id", theId);
077                }
078
079                if (!isPagingProviderDatabaseBacked(theRequest)) {
080                        paramMap.setLoadSynchronous(true);
081                }
082
083                RequestPartitionId requestPartitionId = myPartitionHelperSvc.determineReadPartitionForRequestForSearchType(theRequest, getResourceName(), paramMap, null);
084                return mySearchCoordinatorSvc.registerSearch(this, paramMap, getResourceName(), new CacheControlDirective().parse(theRequest.getHeaders(Constants.HEADER_CACHE_CONTROL)), theRequest, requestPartitionId);
085        }
086
087        @Override
088        public IBundleProvider patientInstanceEverything(HttpServletRequest theServletRequest, IIdType theId, IPrimitiveType<Integer> theCount, IPrimitiveType<Integer> theOffset, DateRangeParam theLastUpdated, SortSpec theSort, StringAndListParam theContent, StringAndListParam theNarrative, StringAndListParam theFilter, RequestDetails theRequestDetails) {
089                TokenOrListParam id = new TokenOrListParam().addOr(new TokenParam(theId.getIdPart()));
090                return doEverythingOperation(id, theCount, theOffset, theLastUpdated, theSort, theContent, theNarrative, theFilter, theRequestDetails);
091        }
092
093        @Override
094        public IBundleProvider patientTypeEverything(HttpServletRequest theServletRequest, IPrimitiveType<Integer> theCount, IPrimitiveType<Integer> theOffset, DateRangeParam theLastUpdated, SortSpec theSort, StringAndListParam theContent, StringAndListParam theNarrative, StringAndListParam theFilter, RequestDetails theRequestDetails, TokenOrListParam theId) {
095                return doEverythingOperation(theId, theCount, theOffset, theLastUpdated, theSort, theContent, theNarrative, theFilter, theRequestDetails);
096        }
097}