001package ca.uhn.fhir.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2020 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.FhirContext;
025import ca.uhn.fhir.context.RuntimeResourceDefinition;
026import org.apache.commons.lang3.Validate;
027import org.hl7.fhir.instance.model.api.IBase;
028import org.hl7.fhir.instance.model.api.IBaseResource;
029import org.hl7.fhir.instance.model.api.IPrimitiveType;
030
031import javax.annotation.Nullable;
032import java.util.ArrayList;
033import java.util.List;
034
035public class SearchParameterUtil {
036
037        public static List<String> getBaseAsStrings(FhirContext theContext, IBaseResource theResource) {
038                Validate.notNull(theContext, "theContext must not be null");
039                Validate.notNull(theResource, "theResource must not be null");
040                RuntimeResourceDefinition def = theContext.getResourceDefinition(theResource);
041
042                BaseRuntimeChildDefinition base = def.getChildByName("base");
043                List<IBase> baseValues = base.getAccessor().getValues(theResource);
044                List<String> retVal = new ArrayList<>();
045                for (IBase next : baseValues) {
046                        IPrimitiveType<?> nextPrimitive = (IPrimitiveType<?>) next;
047                        retVal.add(nextPrimitive.getValueAsString());
048                }
049
050                return retVal;
051        }
052
053        @Nullable
054        public static String getCode(FhirContext theContext, IBaseResource theResource) {
055                return getStringChild(theContext, theResource, "code");
056        }
057
058        @Nullable
059        public static String getExpression(FhirContext theFhirContext, IBaseResource theResource) {
060                return getStringChild(theFhirContext, theResource, "expression");
061        }
062
063        private static String getStringChild(FhirContext theFhirContext, IBaseResource theResource, String theChildName) {
064                Validate.notNull(theFhirContext, "theContext must not be null");
065                Validate.notNull(theResource, "theResource must not be null");
066                RuntimeResourceDefinition def = theFhirContext.getResourceDefinition(theResource);
067
068                BaseRuntimeChildDefinition base = def.getChildByName(theChildName);
069                return base
070                        .getAccessor()
071                        .getFirstValueOrNull(theResource)
072                        .map(t -> ((IPrimitiveType<?>) t))
073                        .map(t -> t.getValueAsString())
074                        .orElse(null);
075        }
076}