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