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(FhirContext theContext, ICompositeType theAttachment, String theChildName, String theChildDatatype) {
050                BaseRuntimeChildDefinition entryChild = getChild(theContext, theAttachment, theChildName);
051                List<IBase> entries = entryChild.getAccessor().getValues(theAttachment);
052                return entries
053                        .stream()
054                        .map(t -> (IPrimitiveType<T>) t)
055                        .findFirst()
056                        .orElseGet(() -> {
057                                IPrimitiveType<String> string = newPrimitive(theContext, theChildDatatype, null);
058                                entryChild.getMutator().setValue(theAttachment, string);
059                                return (IPrimitiveType<T>) string;
060                        });
061        }
062
063        public static void setUrl(FhirContext theContext, ICompositeType theAttachment, String theUrl) {
064                BaseRuntimeChildDefinition entryChild = getChild(theContext, theAttachment, "url");
065                assert entryChild != null : "Version " + theContext + " has no child " + "url";
066                String typeName = "uri";
067                if (theContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.R4)) {
068                        typeName = "url";
069                }
070                entryChild.getMutator().setValue(theAttachment, newPrimitive(theContext, typeName, theUrl));
071        }
072
073        public static void setContentType(FhirContext theContext, ICompositeType theAttachment, String theContentType) {
074                BaseRuntimeChildDefinition entryChild = getChild(theContext, theAttachment, "contentType");
075                entryChild.getMutator().setValue(theAttachment, newPrimitive(theContext, "code", theContentType));
076        }
077
078        public static void setData(FhirContext theContext, ICompositeType theAttachment, byte[] theBytes) {
079                BaseRuntimeChildDefinition entryChild = getChild(theContext, theAttachment, "data");
080                entryChild.getMutator().setValue(theAttachment, newPrimitive(theContext, "base64Binary", theBytes));
081        }
082
083        public static void setSize(FhirContext theContext, ICompositeType theAttachment, Integer theLength) {
084                BaseRuntimeChildDefinition entryChild = getChild(theContext, theAttachment, "size");
085                if (theLength == null) {
086                        entryChild.getMutator().setValue(theAttachment, null);
087                } else if (theContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.R5)){
088                        entryChild.getMutator().setValue(theAttachment, newPrimitive(theContext, "integer64", (long)theLength));
089                } else {
090                        entryChild.getMutator().setValue(theAttachment, newPrimitive(theContext, "unsignedInt", theLength));
091                }
092        }
093
094        /**
095         * This is internal API- Use with caution as it may change
096         */
097        @SuppressWarnings("unchecked")
098        static <T> IPrimitiveType<T> newPrimitive(FhirContext theContext, String theType, T theValue) {
099                BaseRuntimeElementDefinition<?> elementDefinition = theContext.getElementDefinition(theType);
100                Validate.notNull(elementDefinition, "Unknown type %s for %s", theType, theContext);
101                IPrimitiveType<T> primitive = (IPrimitiveType<T>) elementDefinition.newInstance();
102                primitive.setValue(theValue);
103                return primitive;
104        }
105
106        /**
107         * This is internal API- Use with caution as it may change
108         */
109        static BaseRuntimeChildDefinition getChild(FhirContext theContext, IBase theElement, String theName) {
110                BaseRuntimeElementCompositeDefinition<?> def = (BaseRuntimeElementCompositeDefinition<?>) theContext.getElementDefinition(theElement.getClass());
111                return def.getChildByName(theName);
112        }
113
114        public static ICompositeType newInstance(FhirContext theFhirCtx) {
115                return (ICompositeType) theFhirCtx.getElementDefinition("Attachment").newInstance();
116        }
117}