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.BaseRuntimeChildDefinition;
023import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
024import ca.uhn.fhir.context.FhirContext;
025import ca.uhn.fhir.util.ParametersUtil;
026import org.hl7.fhir.instance.model.api.IBase;
027import org.hl7.fhir.instance.model.api.IBaseResource;
028import org.hl7.fhir.instance.model.api.IPrimitiveType;
029
030import java.util.Date;
031
032public class BundleEntryMutator {
033        private final IBase myEntry;
034        private final BaseRuntimeChildDefinition myRequestChildDef;
035        private final BaseRuntimeElementCompositeDefinition<?> myRequestChildContentsDef;
036        private final FhirContext myFhirContext;
037        private final BaseRuntimeElementCompositeDefinition<?> myEntryDefinition;
038
039        public BundleEntryMutator(
040                        FhirContext theFhirContext,
041                        IBase theEntry,
042                        BaseRuntimeChildDefinition theRequestChildDef,
043                        BaseRuntimeElementCompositeDefinition<?> theChildContentsDef,
044                        BaseRuntimeElementCompositeDefinition<?> theEntryDefinition) {
045                myFhirContext = theFhirContext;
046                myEntry = theEntry;
047                myRequestChildDef = theRequestChildDef;
048                myRequestChildContentsDef = theChildContentsDef;
049                myEntryDefinition = theEntryDefinition;
050        }
051
052        void setRequestUrl(FhirContext theFhirContext, String theRequestUrl) {
053                BaseRuntimeChildDefinition requestUrlChildDef = myRequestChildContentsDef.getChildByName("url");
054                IPrimitiveType<?> url = ParametersUtil.createUri(theFhirContext, theRequestUrl);
055                for (IBase nextRequest : myRequestChildDef.getAccessor().getValues(myEntry)) {
056                        requestUrlChildDef.getMutator().addValue(nextRequest, url);
057                }
058        }
059
060        @SuppressWarnings("unchecked")
061        public void setFullUrl(String theFullUrl) {
062                IPrimitiveType<String> value = (IPrimitiveType<String>)
063                                myFhirContext.getElementDefinition("uri").newInstance();
064                value.setValue(theFullUrl);
065
066                BaseRuntimeChildDefinition fullUrlChild = myEntryDefinition.getChildByName("fullUrl");
067                fullUrlChild.getMutator().setValue(myEntry, value);
068        }
069
070        public void setResource(IBaseResource theUpdatedResource) {
071                BaseRuntimeChildDefinition resourceChild = myEntryDefinition.getChildByName("resource");
072                resourceChild.getMutator().setValue(myEntry, theUpdatedResource);
073        }
074
075        public void setRequestIfNoneMatch(FhirContext theFhirContext, String theIfNoneMatch) {
076                BaseRuntimeChildDefinition requestUrlChildDef = myRequestChildContentsDef.getChildByName("ifNoneMatch");
077                IPrimitiveType<?> url = ParametersUtil.createString(theFhirContext, theIfNoneMatch);
078                for (IBase nextRequest : myRequestChildDef.getAccessor().getValues(myEntry)) {
079                        requestUrlChildDef.getMutator().addValue(nextRequest, url);
080                }
081        }
082
083        public void setRequestIfModifiedSince(FhirContext theFhirContext, Date theIfModifiedSince) {
084                BaseRuntimeChildDefinition requestUrlChildDef = myRequestChildContentsDef.getChildByName("ifModifiedSince");
085                IPrimitiveType<?> url = ParametersUtil.createInstant(theFhirContext, theIfModifiedSince);
086                for (IBase nextRequest : myRequestChildDef.getAccessor().getValues(myEntry)) {
087                        requestUrlChildDef.getMutator().addValue(nextRequest, url);
088                }
089        }
090
091        public void setRequestIfMatch(FhirContext theFhirContext, String theIfMatch) {
092                BaseRuntimeChildDefinition requestUrlChildDef = myRequestChildContentsDef.getChildByName("ifMatch");
093                IPrimitiveType<?> url = ParametersUtil.createString(theFhirContext, theIfMatch);
094                for (IBase nextRequest : myRequestChildDef.getAccessor().getValues(myEntry)) {
095                        requestUrlChildDef.getMutator().addValue(nextRequest, url);
096                }
097        }
098
099        public void setRequestIfNoneExist(FhirContext theFhirContext, String theIfNoneExist) {
100                BaseRuntimeChildDefinition requestUrlChildDef = myRequestChildContentsDef.getChildByName("ifNoneExist");
101                IPrimitiveType<?> url = ParametersUtil.createString(theFhirContext, theIfNoneExist);
102                for (IBase nextRequest : myRequestChildDef.getAccessor().getValues(myEntry)) {
103                        requestUrlChildDef.getMutator().addValue(nextRequest, url);
104                }
105        }
106}