001/*
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.dao;
021
022import ca.uhn.fhir.jpa.api.dao.IFhirResourceDaoComposition;
023import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
024import ca.uhn.fhir.model.api.IResource;
025import ca.uhn.fhir.rest.api.SortSpec;
026import ca.uhn.fhir.rest.api.server.IBundleProvider;
027import ca.uhn.fhir.rest.api.server.RequestDetails;
028import ca.uhn.fhir.rest.param.DateRangeParam;
029import ca.uhn.fhir.rest.param.StringParam;
030import jakarta.servlet.http.HttpServletRequest;
031import org.hl7.fhir.instance.model.api.IBaseResource;
032import org.hl7.fhir.instance.model.api.IIdType;
033import org.hl7.fhir.instance.model.api.IPrimitiveType;
034
035import java.util.Collections;
036
037public class JpaResourceDaoComposition<T extends IBaseResource> extends BaseHapiFhirResourceDao<T>
038                implements IFhirResourceDaoComposition<T> {
039
040        @Override
041        public IBundleProvider getDocumentForComposition(
042                        HttpServletRequest theServletRequest,
043                        IIdType theId,
044                        IPrimitiveType<Integer> theCount,
045                        IPrimitiveType<Integer> theOffset,
046                        DateRangeParam theLastUpdate,
047                        SortSpec theSort,
048                        RequestDetails theRequestDetails) {
049                SearchParameterMap paramMap = new SearchParameterMap();
050                if (theCount != null) {
051                        paramMap.setCount(theCount.getValue());
052                }
053                if (theOffset != null) {
054                        paramMap.setOffset(theOffset.getValue());
055                }
056                paramMap.setIncludes(Collections.singleton(IResource.INCLUDE_ALL.asRecursive()));
057                paramMap.setSort(theSort);
058                paramMap.setLastUpdated(theLastUpdate);
059                if (theId != null) {
060                        paramMap.add("_id", new StringParam(theId.getIdPart()));
061                }
062                return search(paramMap, theRequestDetails);
063        }
064}