001package ca.uhn.fhir.util.bundle;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
024import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
025import ca.uhn.fhir.context.FhirContext;
026import ca.uhn.fhir.util.ParametersUtil;
027import org.hl7.fhir.instance.model.api.IBase;
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}