001package ca.uhn.fhir.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2018 University Health Network
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.BaseRuntimeElementDefinition;
025import ca.uhn.fhir.context.FhirContext;
026import ca.uhn.fhir.context.RuntimeResourceDefinition;
027import org.hl7.fhir.instance.model.api.IBase;
028import org.hl7.fhir.instance.model.api.IBaseBinary;
029import org.hl7.fhir.instance.model.api.IBaseReference;
030
031import java.util.List;
032
033public class BinaryUtil {
034
035        private BinaryUtil() {
036                // non instantiable
037        }
038
039        public static IBaseReference getSecurityContext(FhirContext theCtx, IBaseBinary theBinary) {
040                RuntimeResourceDefinition def = theCtx.getResourceDefinition("Binary");
041                BaseRuntimeChildDefinition child = def.getChildByName("securityContext");
042                IBaseReference retVal = null;
043                if (child != null) {
044                        List<IBase> values = child.getAccessor().getValues(theBinary);
045                        if (values.size() > 0) {
046                                retVal = (IBaseReference) values.get(0);
047                        }
048                }
049                return retVal;
050        }
051
052        public static IBaseBinary newBinary(FhirContext theCtx) {
053                return (IBaseBinary) theCtx.getResourceDefinition("Binary").newInstance();
054        }
055
056        public static void setSecurityContext(FhirContext theCtx, IBaseBinary theBinary, String theSecurityContext) {
057                RuntimeResourceDefinition def = theCtx.getResourceDefinition("Binary");
058                BaseRuntimeChildDefinition child = def.getChildByName("securityContext");
059
060                BaseRuntimeElementDefinition<?> referenceDef = theCtx.getElementDefinition("reference");
061                IBaseReference reference = (IBaseReference) referenceDef.newInstance();
062                child.getMutator().addValue(theBinary, reference);
063
064                reference.setReference(theSecurityContext);
065        }
066
067}