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;
021
022import ca.uhn.fhir.context.*;
023import org.apache.commons.lang3.Validate;
024import org.hl7.fhir.instance.model.api.IBase;
025import org.hl7.fhir.instance.model.api.ICompositeType;
026import org.hl7.fhir.instance.model.api.IPrimitiveType;
027
028import java.util.List;
029
030public class AttachmentUtil {
031
032        /**
033         * Fetches the base64Binary value of Attachment.data, creating it if it does not
034         * already exist.
035         */
036        public static IPrimitiveType<byte[]> getOrCreateData(FhirContext theContext, ICompositeType theAttachment) {
037                return getOrCreateChild(theContext, theAttachment, "data", "base64Binary");
038        }
039
040        public static IPrimitiveType<String> getOrCreateContentType(FhirContext theContext, ICompositeType theAttachment) {
041                return getOrCreateChild(theContext, theAttachment, "contentType", "string");
042        }
043
044        public static IPrimitiveType<String> getOrCreateUrl(FhirContext theContext, ICompositeType theAttachment) {
045                return getOrCreateChild(theContext, theAttachment, "url", "uri");
046        }
047
048        @SuppressWarnings("unchecked")
049        private static <T> IPrimitiveType<T> getOrCreateChild(
050                        FhirContext theContext, ICompositeType theAttachment, String theChildName, String theChildDatatype) {
051                BaseRuntimeChildDefinition entryChild = getChild(theContext, theAttachment, theChildName);
052                List<IBase> entries = entryChild.getAccessor().getValues(theAttachment);
053                return entries.stream().map(t -> (IPrimitiveType<T>) t).findFirst().orElseGet(() -> {
054                        IPrimitiveType<String> string = newPrimitive(theContext, theChildDatatype, null);
055                        entryChild.getMutator().setValue(theAttachment, string);
056                        return (IPrimitiveType<T>) string;
057                });
058        }
059
060        public static void setUrl(FhirContext theContext, ICompositeType theAttachment, String theUrl) {
061                BaseRuntimeChildDefinition entryChild = getChild(theContext, theAttachment, "url");
062                assert entryChild != null : "Version " + theContext + " has no child " + "url";
063                String typeName = "uri";
064                if (theContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.R4)) {
065                        typeName = "url";
066                }
067                entryChild.getMutator().setValue(theAttachment, newPrimitive(theContext, typeName, theUrl));
068        }
069
070        public static void setContentType(FhirContext theContext, ICompositeType theAttachment, String theContentType) {
071                BaseRuntimeChildDefinition entryChild = getChild(theContext, theAttachment, "contentType");
072                entryChild.getMutator().setValue(theAttachment, newPrimitive(theContext, "code", theContentType));
073        }
074
075        public static void setData(FhirContext theContext, ICompositeType theAttachment, byte[] theBytes) {
076                BaseRuntimeChildDefinition entryChild = getChild(theContext, theAttachment, "data");
077                entryChild.getMutator().setValue(theAttachment, newPrimitive(theContext, "base64Binary", theBytes));
078        }
079
080        public static void setSize(FhirContext theContext, ICompositeType theAttachment, Integer theLength) {
081                BaseRuntimeChildDefinition entryChild = getChild(theContext, theAttachment, "size");
082                if (theLength == null) {
083                        entryChild.getMutator().setValue(theAttachment, null);
084                } else if (theContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.R5)) {
085                        entryChild.getMutator().setValue(theAttachment, newPrimitive(theContext, "integer64", (long) theLength));
086                } else {
087                        entryChild.getMutator().setValue(theAttachment, newPrimitive(theContext, "unsignedInt", theLength));
088                }
089        }
090
091        /**
092         * This is internal API- Use with caution as it may change
093         */
094        @SuppressWarnings("unchecked")
095        static <T> IPrimitiveType<T> newPrimitive(FhirContext theContext, String theType, T theValue) {
096                BaseRuntimeElementDefinition<?> elementDefinition = theContext.getElementDefinition(theType);
097                Validate.notNull(elementDefinition, "Unknown type %s for %s", theType, theContext);
098                IPrimitiveType<T> primitive = (IPrimitiveType<T>) elementDefinition.newInstance();
099                primitive.setValue(theValue);
100                return primitive;
101        }
102
103        /**
104         * This is internal API- Use with caution as it may change
105         */
106        static BaseRuntimeChildDefinition getChild(FhirContext theContext, IBase theElement, String theName) {
107                BaseRuntimeElementCompositeDefinition<?> def =
108                                (BaseRuntimeElementCompositeDefinition<?>) theContext.getElementDefinition(theElement.getClass());
109                return def.getChildByName(theName);
110        }
111
112        public static ICompositeType newInstance(FhirContext theFhirCtx) {
113                return (ICompositeType) theFhirCtx.getElementDefinition("Attachment").newInstance();
114        }
115}