001/*-
002 * #%L
003 * HAPI FHIR JPA - Search Parameters
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.interceptor.model;
021
022import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
023import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
024import jakarta.annotation.Nonnull;
025import jakarta.annotation.Nullable;
026import org.hl7.fhir.instance.model.api.IBaseResource;
027import org.hl7.fhir.instance.model.api.IIdType;
028
029import static org.apache.commons.lang3.StringUtils.isNotBlank;
030
031/**
032 * This is a model class used as a parameter for the interceptor pointcut
033 * {@link ca.uhn.fhir.interceptor.api.Pointcut#STORAGE_PARTITION_IDENTIFY_READ}.
034 */
035public class ReadPartitionIdRequestDetails extends PartitionIdRequestDetails {
036
037        private final String myResourceType;
038        private final RestOperationTypeEnum myRestOperationType;
039        private final IIdType myReadResourceId;
040
041        @Nullable
042        private final SearchParameterMap mySearchParams;
043
044        @Nullable
045        private final IBaseResource myConditionalTargetOrNull;
046
047        @Nullable
048        private final String mySearchUuid;
049
050        @Nullable
051        private final String myExtendedOperationName;
052
053        private ReadPartitionIdRequestDetails(
054                        String theResourceType,
055                        RestOperationTypeEnum theRestOperationType,
056                        IIdType theReadResourceId,
057                        @Nullable SearchParameterMap theSearchParams,
058                        @Nullable IBaseResource theConditionalTargetOrNull,
059                        @Nullable String theSearchUuid,
060                        String theExtendedOperationName) {
061                myResourceType = theResourceType;
062                myRestOperationType = theRestOperationType;
063                myReadResourceId = theReadResourceId;
064                mySearchParams = theSearchParams;
065                myConditionalTargetOrNull = theConditionalTargetOrNull;
066                mySearchUuid = theSearchUuid;
067                myExtendedOperationName = theExtendedOperationName;
068        }
069
070        @Nullable
071        public String getExtendedOperationName() {
072                return myExtendedOperationName;
073        }
074
075        @Nullable
076        public String getSearchUuid() {
077                return mySearchUuid;
078        }
079
080        public String getResourceType() {
081                return myResourceType;
082        }
083
084        public RestOperationTypeEnum getRestOperationType() {
085                return myRestOperationType;
086        }
087
088        public IIdType getReadResourceId() {
089                return myReadResourceId;
090        }
091
092        @Nullable
093        public SearchParameterMap getSearchParams() {
094                return mySearchParams;
095        }
096
097        @Nullable
098        public IBaseResource getConditionalTargetOrNull() {
099                return myConditionalTargetOrNull;
100        }
101
102        /**
103         * @param theId The resource ID (must include a resource type and ID)
104         */
105        public static ReadPartitionIdRequestDetails forRead(IIdType theId) {
106                assert isNotBlank(theId.getResourceType());
107                assert isNotBlank(theId.getIdPart());
108                return forRead(theId.getResourceType(), theId, false);
109        }
110
111        public static ReadPartitionIdRequestDetails forServerOperation(@Nonnull String theOperationName) {
112                return new ReadPartitionIdRequestDetails(
113                                null, RestOperationTypeEnum.EXTENDED_OPERATION_SERVER, null, null, null, null, theOperationName);
114        }
115
116        public static ReadPartitionIdRequestDetails forRead(
117                        String theResourceType, @Nonnull IIdType theId, boolean theIsVread) {
118                RestOperationTypeEnum op = theIsVread ? RestOperationTypeEnum.VREAD : RestOperationTypeEnum.READ;
119                return new ReadPartitionIdRequestDetails(
120                                theResourceType, op, theId.withResourceType(theResourceType), null, null, null, null);
121        }
122
123        public static ReadPartitionIdRequestDetails forSearchType(
124                        String theResourceType, SearchParameterMap theParams, IBaseResource theConditionalOperationTargetOrNull) {
125                return new ReadPartitionIdRequestDetails(
126                                theResourceType,
127                                RestOperationTypeEnum.SEARCH_TYPE,
128                                null,
129                                theParams != null ? theParams : SearchParameterMap.newSynchronous(),
130                                theConditionalOperationTargetOrNull,
131                                null,
132                                null);
133        }
134
135        public static ReadPartitionIdRequestDetails forHistory(String theResourceType, IIdType theIdType) {
136                RestOperationTypeEnum restOperationTypeEnum;
137                if (theIdType != null) {
138                        restOperationTypeEnum = RestOperationTypeEnum.HISTORY_INSTANCE;
139                } else if (theResourceType != null) {
140                        restOperationTypeEnum = RestOperationTypeEnum.HISTORY_TYPE;
141                } else {
142                        restOperationTypeEnum = RestOperationTypeEnum.HISTORY_SYSTEM;
143                }
144                return new ReadPartitionIdRequestDetails(
145                                theResourceType, restOperationTypeEnum, theIdType, null, null, null, null);
146        }
147
148        public static ReadPartitionIdRequestDetails forSearchUuid(String theUuid) {
149                return new ReadPartitionIdRequestDetails(null, RestOperationTypeEnum.GET_PAGE, null, null, null, theUuid, null);
150        }
151}