001package ca.uhn.fhir.jpa.search.helper;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
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.i18n.Msg;
024import ca.uhn.fhir.context.FhirContext;
025import ca.uhn.fhir.context.RuntimeResourceDefinition;
026import ca.uhn.fhir.context.RuntimeSearchParam;
027import org.springframework.beans.factory.annotation.Autowired;
028import org.springframework.stereotype.Service;
029
030import java.util.ArrayList;
031import java.util.Collection;
032import java.util.HashMap;
033import java.util.List;
034import java.util.Map;
035
036@Service
037public class SearchParamHelper {
038
039        @Autowired
040        private FhirContext myFhirContext;
041
042
043        public Collection<RuntimeSearchParam> getPatientSearchParamsForResourceType(String theResourceType) {
044                RuntimeResourceDefinition runtimeResourceDefinition = myFhirContext.getResourceDefinition(theResourceType);
045                Map<String, RuntimeSearchParam> searchParams = new HashMap<>();
046
047                RuntimeSearchParam  patientSearchParam = runtimeResourceDefinition.getSearchParam("patient");
048                if (patientSearchParam != null) {
049                        searchParams.put(patientSearchParam.getName(), patientSearchParam);
050
051                }
052                RuntimeSearchParam  subjectSearchParam = runtimeResourceDefinition.getSearchParam("subject");
053                if (subjectSearchParam != null) {
054                        searchParams.put(subjectSearchParam.getName(), subjectSearchParam);
055                }
056
057                List<RuntimeSearchParam> compartmentSearchParams = getPatientCompartmentRuntimeSearchParams(runtimeResourceDefinition);
058                compartmentSearchParams.forEach(param -> searchParams.put(param.getName(), param));
059
060                return searchParams.values();
061        }
062
063        /**
064         * Search the resource definition for a compartment named 'patient' and return its related Search Parameter.
065         */
066        public List<RuntimeSearchParam> getPatientCompartmentRuntimeSearchParams(RuntimeResourceDefinition runtimeResourceDefinition) {
067                List<RuntimeSearchParam> patientSearchParam = new ArrayList<>();
068                List<RuntimeSearchParam> searchParams = runtimeResourceDefinition.getSearchParamsForCompartmentName("Patient");
069                return searchParams;
070//              if (searchParams == null || searchParams.size() == 0) {
071//                      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", myResourceType);
072//                      throw new IllegalArgumentException(Msg.code(1264) + errorMessage);
073//              } else if (searchParams.size() == 1) {
074//                      patientSearchParam = searchParams.get(0);
075//              } else {
076//                      String errorMessage = String.format("Resource type [%s] is not eligible for Group Bulk export, as we are unable to disambiguate which patient search parameter we should be searching by.", myResourceType);
077//                      throw new IllegalArgumentException(Msg.code(1265) + errorMessage);
078//              }
079//              return patientSearchParam;
080        }
081}