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