001package ca.uhn.fhir.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
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 ca.uhn.fhir.context.RuntimeSearchParam;
027import ca.uhn.fhir.i18n.Msg;
028import org.apache.commons.lang3.Validate;
029import org.hl7.fhir.instance.model.api.IBase;
030import org.hl7.fhir.instance.model.api.IBaseResource;
031import org.hl7.fhir.instance.model.api.IPrimitiveType;
032
033import javax.annotation.Nullable;
034import java.util.ArrayList;
035import java.util.List;
036import java.util.Optional;
037
038public class SearchParameterUtil {
039
040        public static List<String> getBaseAsStrings(FhirContext theContext, IBaseResource theResource) {
041                Validate.notNull(theContext, "theContext must not be null");
042                Validate.notNull(theResource, "theResource must not be null");
043                RuntimeResourceDefinition def = theContext.getResourceDefinition(theResource);
044
045                BaseRuntimeChildDefinition base = def.getChildByName("base");
046                List<IBase> baseValues = base.getAccessor().getValues(theResource);
047                List<String> retVal = new ArrayList<>();
048                for (IBase next : baseValues) {
049                        IPrimitiveType<?> nextPrimitive = (IPrimitiveType<?>) next;
050                        retVal.add(nextPrimitive.getValueAsString());
051                }
052
053                return retVal;
054        }
055
056        /**
057         * Given the resource type, fetch its patient-based search parameter name
058         * 1. Attempt to find one called 'patient'
059         * 2. If that fails, find one called 'subject'
060         * 3. If that fails, find find by Patient Compartment.
061         * 3.1 If that returns >1 result, throw an error
062         * 3.2 If that returns 1 result, return it
063         */
064        public static Optional<RuntimeSearchParam> getOnlyPatientSearchParamForResourceType(FhirContext theFhirContext, String theResourceType) {
065                RuntimeSearchParam myPatientSearchParam = null;
066                RuntimeResourceDefinition runtimeResourceDefinition = theFhirContext.getResourceDefinition(theResourceType);
067                myPatientSearchParam = runtimeResourceDefinition.getSearchParam("patient");
068                if (myPatientSearchParam == null) {
069                        myPatientSearchParam = runtimeResourceDefinition.getSearchParam("subject");
070                        if (myPatientSearchParam == null) {
071                                myPatientSearchParam = getOnlyPatientCompartmentRuntimeSearchParam(runtimeResourceDefinition);
072                        }
073                }
074                return Optional.ofNullable(myPatientSearchParam);
075        }
076
077
078        /**
079         * Search the resource definition for a compartment named 'patient' and return its related Search Parameter.
080         */
081        public static RuntimeSearchParam getOnlyPatientCompartmentRuntimeSearchParam(FhirContext theFhirContext, String theResourceType) {
082                RuntimeResourceDefinition resourceDefinition = theFhirContext.getResourceDefinition(theResourceType);
083                return getOnlyPatientCompartmentRuntimeSearchParam(resourceDefinition);
084        }
085
086        public static RuntimeSearchParam getOnlyPatientCompartmentRuntimeSearchParam(RuntimeResourceDefinition runtimeResourceDefinition) {
087                RuntimeSearchParam patientSearchParam;
088                List<RuntimeSearchParam> searchParams = runtimeResourceDefinition.getSearchParamsForCompartmentName("Patient");
089                if (searchParams == null || searchParams.size() == 0) {
090                        String errorMessage = String.format("Resource type [%s] is not eligible for this type of export, as it contains no Patient compartment, and no `patient` or `subject` search parameter", runtimeResourceDefinition.getId());
091                        throw new IllegalArgumentException(Msg.code(1774) + errorMessage);
092                } else if (searchParams.size() == 1) {
093                        patientSearchParam = searchParams.get(0);
094                } else {
095                        String errorMessage = String.format("Resource type %s has more than one Search Param which references a patient compartment. We are unable to disambiguate which patient search parameter we should be searching by.", runtimeResourceDefinition.getId());
096                        throw new IllegalArgumentException(Msg.code(1775) + errorMessage);
097                }
098                return patientSearchParam;
099        }
100
101        public static List<RuntimeSearchParam> getAllPatientCompartmentRuntimeSearchParams(FhirContext theFhirContext, String theResourceType) {
102                RuntimeResourceDefinition runtimeResourceDefinition = theFhirContext.getResourceDefinition(theResourceType);
103                return getAllPatientCompartmentRuntimeSearchParams(runtimeResourceDefinition);
104
105        }
106
107        private static List<RuntimeSearchParam> getAllPatientCompartmentRuntimeSearchParams(RuntimeResourceDefinition theRuntimeResourceDefinition) {
108                List<RuntimeSearchParam> patient = theRuntimeResourceDefinition.getSearchParamsForCompartmentName("Patient");
109                return patient;
110        }
111
112
113        /**
114         * Return true if any search parameter in the resource can point at a patient, false otherwise
115         */
116        public static boolean isResourceTypeInPatientCompartment(FhirContext theFhirContext, String theResourceType) {
117                RuntimeResourceDefinition runtimeResourceDefinition = theFhirContext.getResourceDefinition(theResourceType);
118                return getAllPatientCompartmentRuntimeSearchParams(runtimeResourceDefinition).size() > 0;
119        }
120
121
122        @Nullable
123        public static String getCode(FhirContext theContext, IBaseResource theResource) {
124                return getStringChild(theContext, theResource, "code");
125        }
126
127        @Nullable
128        public static String getURL(FhirContext theContext, IBaseResource theResource) {
129                return getStringChild(theContext, theResource, "url");
130        }
131
132        @Nullable
133        public static String getExpression(FhirContext theFhirContext, IBaseResource theResource) {
134                return getStringChild(theFhirContext, theResource, "expression");
135        }
136
137        private static String getStringChild(FhirContext theFhirContext, IBaseResource theResource, String theChildName) {
138                Validate.notNull(theFhirContext, "theContext must not be null");
139                Validate.notNull(theResource, "theResource must not be null");
140                RuntimeResourceDefinition def = theFhirContext.getResourceDefinition(theResource);
141
142                BaseRuntimeChildDefinition base = def.getChildByName(theChildName);
143                return base
144                        .getAccessor()
145                        .getFirstValueOrNull(theResource)
146                        .map(t -> ((IPrimitiveType<?>) t))
147                        .map(t -> t.getValueAsString())
148                        .orElse(null);
149        }
150
151        public static String stripModifier(String theSearchParam) {
152                String retval;
153                int colonIndex = theSearchParam.indexOf(":");
154                if (colonIndex == -1) {
155                        retval = theSearchParam;
156                } else {
157                        retval = theSearchParam.substring(0, colonIndex);
158                }
159                return retval;
160        }
161}