001/*-
002 * #%L
003 * HAPI FHIR - Core Library
004 * %%
005 * Copyright (C) 2014 - 2023 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
030public class BundleEntryMutator {
031        private final IBase myEntry;
032        private final BaseRuntimeChildDefinition myRequestChildDef;
033        private final BaseRuntimeElementCompositeDefinition<?> myRequestChildContentsDef;
034        private final FhirContext myFhirContext;
035        private final BaseRuntimeElementCompositeDefinition<?> myEntryDefinition;
036
037        public BundleEntryMutator(FhirContext theFhirContext, IBase theEntry, BaseRuntimeChildDefinition theRequestChildDef, BaseRuntimeElementCompositeDefinition<?> theChildContentsDef, BaseRuntimeElementCompositeDefinition<?> theEntryDefinition) {
038                myFhirContext = theFhirContext;
039                myEntry = theEntry;
040                myRequestChildDef = theRequestChildDef;
041                myRequestChildContentsDef = theChildContentsDef;
042                myEntryDefinition = theEntryDefinition;
043        }
044
045        void setRequestUrl(FhirContext theFhirContext, String theRequestUrl) {
046                BaseRuntimeChildDefinition requestUrlChildDef = myRequestChildContentsDef.getChildByName("url");
047                IPrimitiveType<?> url = ParametersUtil.createUri(theFhirContext, theRequestUrl);
048                for (IBase nextRequest : myRequestChildDef.getAccessor().getValues(myEntry)) {
049                        requestUrlChildDef.getMutator().addValue(nextRequest, url);
050                }
051        }
052
053        @SuppressWarnings("unchecked")
054        public void setFullUrl(String theFullUrl) {
055                IPrimitiveType<String> value = (IPrimitiveType<String>) myFhirContext.getElementDefinition("uri").newInstance();
056                value.setValue(theFullUrl);
057
058                BaseRuntimeChildDefinition fullUrlChild = myEntryDefinition.getChildByName("fullUrl");
059                fullUrlChild.getMutator().setValue(myEntry, value);
060        }
061
062        public void setResource(IBaseResource theUpdatedResource) {
063                BaseRuntimeChildDefinition resourceChild = myEntryDefinition.getChildByName("resource");
064                resourceChild.getMutator().setValue(myEntry, theUpdatedResource);
065        }
066}