001/*-
002 * #%L
003 * HAPI FHIR Storage api
004 * %%
005 * Copyright (C) 2014 - 2025 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.replacereferences;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.interceptor.model.RequestPartitionId;
024import ca.uhn.fhir.model.api.IProvenanceAgent;
025import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
026import org.hl7.fhir.instance.model.api.IIdType;
027
028import java.util.List;
029import javax.annotation.Nonnull;
030
031import static ca.uhn.fhir.rest.server.provider.ProviderConstants.OPERATION_REPLACE_REFERENCES_PARAM_SOURCE_REFERENCE_ID;
032import static ca.uhn.fhir.rest.server.provider.ProviderConstants.OPERATION_REPLACE_REFERENCES_PARAM_TARGET_REFERENCE_ID;
033import static org.apache.commons.lang3.StringUtils.isBlank;
034
035public class ReplaceReferencesRequest {
036        /**
037         * Unqualified source id
038         */
039        @Nonnull
040        public final IIdType sourceId;
041
042        /**
043         * Unqualified target id
044         */
045        @Nonnull
046        public final IIdType targetId;
047
048        public final int resourceLimit;
049
050        public final RequestPartitionId partitionId;
051
052        /**
053         * Indicates whether a Provenance resource should be created after the operation.
054         * This flag is not exposed to FHIR clients invoking the $hapi.fhir.replace-references
055         * operation; it is used internally. It is used to avoid creating a
056         * Provenance when the $replace-references is executed as part of another
057         * operation (such as $merge), and the Provenance needs to be created after
058         * the outer operation completes.
059         */
060        public final boolean createProvenance;
061
062        /**
063         * The list of agents to be used in the Provenance resource.
064         */
065        public final List<IProvenanceAgent> provenanceAgents;
066
067        public ReplaceReferencesRequest(
068                        @Nonnull IIdType theSourceId,
069                        @Nonnull IIdType theTargetId,
070                        int theResourceLimit,
071                        RequestPartitionId thePartitionId,
072                        boolean theCreateProvenance,
073                        List<IProvenanceAgent> theProvenanceAgents) {
074                sourceId = theSourceId.toUnqualifiedVersionless();
075                targetId = theTargetId.toUnqualifiedVersionless();
076                resourceLimit = theResourceLimit;
077                partitionId = thePartitionId;
078                createProvenance = theCreateProvenance;
079                provenanceAgents = theProvenanceAgents;
080        }
081
082        public void validateOrThrowInvalidParameterException() {
083                if (isBlank(sourceId.getResourceType())) {
084                        throw new InvalidRequestException(
085                                        Msg.code(2585) + "'" + OPERATION_REPLACE_REFERENCES_PARAM_SOURCE_REFERENCE_ID
086                                                        + "' must be a resource type qualified id");
087                }
088
089                if (isBlank(targetId.getResourceType())) {
090                        throw new InvalidRequestException(
091                                        Msg.code(2586) + "'" + OPERATION_REPLACE_REFERENCES_PARAM_TARGET_REFERENCE_ID
092                                                        + "' must be a resource type qualified id");
093                }
094
095                if (!targetId.getResourceType().equals(sourceId.getResourceType())) {
096                        throw new InvalidRequestException(
097                                        Msg.code(2587) + "Source and target id parameters must be for the same resource type");
098                }
099        }
100}