001/*-
002 * #%L
003 * HAPI FHIR - Core Library
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.util.bundle;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.rest.api.RequestTypeEnum;
024import jakarta.annotation.Nullable;
025import org.apache.commons.lang3.Validate;
026import org.hl7.fhir.instance.model.api.IBaseResource;
027
028import java.util.Date;
029
030/**
031 * @since 8.6.0
032 */
033public class ModifiableBundleEntryParts extends BundleEntryParts {
034        private final BundleEntryMutator myBundleEntryMutator;
035        private final FhirContext myFhirContext;
036
037        public ModifiableBundleEntryParts(
038                        FhirContext theFhirContext,
039                        BundleEntryParts theBundleEntryParts,
040                        BundleEntryMutator theBundleEntryMutator) {
041                super(theBundleEntryParts);
042                myFhirContext = theFhirContext;
043                myBundleEntryMutator = theBundleEntryMutator;
044        }
045
046        /**
047         * Sets the conditional URL, following the same element selection logic as
048         * {@link #getConditionalUrl()}.
049         *
050         * @param theConditionalUrl The conditional URL. Must be in the format [resourceType]?[params]
051         */
052        public void setConditionalUrl(@Nullable String theConditionalUrl) {
053                Validate.isTrue(
054                                theConditionalUrl == null || theConditionalUrl.contains("?"),
055                                "theConditionalUrl must be null or be a valid conditional URL with format: [resourceType]?[params]");
056                switch (getMethod()) {
057                        case POST -> setRequestIfNoneExist(theConditionalUrl);
058                        case PUT, PATCH, DELETE -> setRequestUrl(theConditionalUrl);
059                }
060        }
061
062        public void setRequestUrl(String theRequestUrl) {
063                myUrl = theRequestUrl;
064                myBundleEntryMutator.setRequestUrl(theRequestUrl);
065        }
066
067        public void setFullUrl(String theFullUrl) {
068                myFullUrl = theFullUrl;
069                myBundleEntryMutator.setFullUrl(theFullUrl);
070        }
071
072        public void setResource(IBaseResource theUpdatedResource) {
073                myBundleEntryMutator.setResource(theUpdatedResource);
074        }
075
076        public void setRequestIfNoneMatch(String ifNoneMatch) {
077                myBundleEntryMutator.setRequestIfNoneMatch(ifNoneMatch);
078        }
079
080        public void setRequestIfModifiedSince(Date theModifiedSince) {
081                myBundleEntryMutator.setRequestIfModifiedSince(theModifiedSince);
082        }
083
084        public void setRequestIfMatch(String theIfMatch) {
085                myBundleEntryMutator.setRequestIfMatch(theIfMatch);
086        }
087
088        public void setRequestIfNoneExist(String theIfNoneExist) {
089                myBundleEntryMutator.setRequestIfNoneExist(theIfNoneExist);
090        }
091
092        public void setMethod(RequestTypeEnum theMethod) {
093                myMethod = theMethod;
094                myBundleEntryMutator.setMethod(theMethod);
095        }
096}