001package ca.uhn.fhir.jpa.dao.r4;
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.instance.model.api.IIdType;
038import org.hl7.fhir.instance.model.api.IPrimitiveType;
039import org.hl7.fhir.r4.model.Patient;
040import org.springframework.beans.factory.annotation.Autowired;
041
042import javax.servlet.http.HttpServletRequest;
043import java.util.Arrays;
044import java.util.Collections;
045
046public class FhirResourceDaoPatientR4 extends BaseHapiFhirResourceDao<Patient>implements IFhirResourceDaoPatient<Patient> {
047
048        @Autowired
049        private IRequestPartitionHelperSvc myPartitionHelperSvc;
050
051        private IBundleProvider doEverythingOperation(TokenOrListParam theIds,
052                                                                                                                                 IPrimitiveType<Integer> theCount,
053                                                                                                                                 IPrimitiveType<Integer> theOffset,
054                                                                                                                                 DateRangeParam theLastUpdated,
055                                                                                                                                 SortSpec theSort,
056                                                                                                                                 StringAndListParam theContent,
057                                                                                                                                 StringAndListParam theNarrative,
058                                                                                                                                 StringAndListParam theFilter,
059                                                                                                                                 RequestDetails theRequest) {
060                SearchParameterMap paramMap = new SearchParameterMap();
061                if (theCount != null) {
062                        paramMap.setCount(theCount.getValue());
063                }
064                if (theOffset != null) {
065                        throw new IllegalArgumentException(Msg.code(1106) + "Everything operation does not support offset searching");
066                }
067                if (theContent != null) {
068                        paramMap.add(Constants.PARAM_CONTENT, theContent);
069                }
070                if (theNarrative != null) {
071                        paramMap.add(Constants.PARAM_TEXT, theNarrative);
072                }
073                paramMap.setIncludes(Collections.singleton(IResource.INCLUDE_ALL.asRecursive()));
074                paramMap.setEverythingMode(theIds != null && theIds.getValuesAsQueryTokens().size() == 1 ? EverythingModeEnum.PATIENT_INSTANCE : EverythingModeEnum.PATIENT_TYPE);
075                paramMap.setSort(theSort);
076                paramMap.setLastUpdated(theLastUpdated);
077                if (theIds != null) {
078                        if (theRequest.getParameters().containsKey("_mdm")) {
079                                String[] paramVal = theRequest.getParameters().get("_mdm");
080                                if (Arrays.asList(paramVal).contains("true")) {
081                                        theIds.getValuesAsQueryTokens().stream().forEach(param -> param.setMdmExpand(true));
082                                }
083                        }
084                        paramMap.add("_id", theIds);
085                }
086
087                if (!isPagingProviderDatabaseBacked(theRequest)) {
088                        paramMap.setLoadSynchronous(true);
089                }
090
091                RequestPartitionId requestPartitionId = myPartitionHelperSvc.determineReadPartitionForRequestForSearchType(theRequest, getResourceName(), paramMap, null);
092                return mySearchCoordinatorSvc.registerSearch(this,
093                        paramMap,
094                        getResourceName(),
095                        new CacheControlDirective().parse(theRequest.getHeaders(Constants.HEADER_CACHE_CONTROL)),
096                        theRequest,
097                        requestPartitionId);
098        }
099
100        @Override
101        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) {
102                TokenOrListParam id = new TokenOrListParam().add(new TokenParam(theId.getIdPart()));
103                return doEverythingOperation(id, theCount, theOffset, theLastUpdated, theSort, theContent, theNarrative, theFilter, theRequestDetails);
104        }
105
106        @Override
107        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) {
108                return doEverythingOperation(theId, theCount, theOffset, theLastUpdated, theSort, theContent, theNarrative, theFilter, theRequestDetails);
109        }
110
111}