
001/*- 002 * #%L 003 * HAPI FHIR Storage api 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.jpa.util; 021 022import ca.uhn.fhir.context.FhirContext; 023import ca.uhn.fhir.context.RuntimeResourceDefinition; 024import ca.uhn.fhir.context.RuntimeSearchParam; 025import ca.uhn.fhir.i18n.Msg; 026import ca.uhn.fhir.jpa.searchparam.extractor.BaseSearchParamExtractor; 027import ca.uhn.fhir.jpa.searchparam.extractor.ISearchParamExtractor; 028import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum; 029import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException; 030import jakarta.annotation.Nonnull; 031import org.apache.commons.lang3.StringUtils; 032import org.hl7.fhir.instance.model.api.IBaseReference; 033import org.hl7.fhir.instance.model.api.IBaseResource; 034import org.hl7.fhir.r4.model.IdType; 035 036import java.util.Arrays; 037import java.util.List; 038import java.util.Optional; 039import java.util.stream.Collectors; 040 041import static org.apache.commons.lang3.StringUtils.isBlank; 042 043public class ResourceCompartmentUtil { 044 045 /** 046 * Extract, if exists, the patient compartment identity of the received resource. 047 * It must be invoked in patient compartment mode. 048 * @param theResource the resource to which extract the patient compartment identity 049 * @param theFhirContext the active FhirContext 050 * @param theSearchParamExtractor the configured search parameter extractor 051 * @return the optional patient compartment identifier 052 * @throws MethodNotAllowedException if received resource is of type "Patient" and ID is not assigned. 053 */ 054 public static Optional<String> getPatientCompartmentIdentity( 055 IBaseResource theResource, FhirContext theFhirContext, ISearchParamExtractor theSearchParamExtractor) { 056 if (theResource == null) { 057 // The resource may be null in mass ingestion mode 058 return Optional.empty(); 059 } 060 061 RuntimeResourceDefinition resourceDef = theFhirContext.getResourceDefinition(theResource); 062 List<RuntimeSearchParam> patientCompartmentSps = 063 ResourceCompartmentUtil.getPatientCompartmentSearchParams(resourceDef); 064 if (patientCompartmentSps.isEmpty()) { 065 return Optional.empty(); 066 } 067 068 if (resourceDef.getName().equals("Patient")) { 069 String compartmentIdentity = theResource.getIdElement().getIdPart(); 070 if (isBlank(compartmentIdentity)) { 071 throw new MethodNotAllowedException( 072 Msg.code(2475) + "Patient resource IDs must be client-assigned in patient compartment mode"); 073 } 074 return Optional.of(compartmentIdentity); 075 } 076 077 return getResourceCompartment("Patient", theResource, patientCompartmentSps, theSearchParamExtractor); 078 } 079 080 /** 081 * Extracts and returns an optional compartment of the received resource 082 * @param theCompartmentName the name of the compartment 083 * @param theResource source resource which compartment is extracted 084 * @param theCompartmentSps the RuntimeSearchParam list involving the searched compartment 085 * @param mySearchParamExtractor the ISearchParamExtractor to be used to extract the parameter values 086 * @return optional compartment of the received resource 087 */ 088 public static Optional<String> getResourceCompartment( 089 String theCompartmentName, 090 IBaseResource theResource, 091 List<RuntimeSearchParam> theCompartmentSps, 092 ISearchParamExtractor mySearchParamExtractor) { 093 // TODO KHS consolidate with FhirTerser.getCompartmentOwnersForResource() 094 return theCompartmentSps.stream() 095 .flatMap(param -> Arrays.stream(BaseSearchParamExtractor.splitPathsR4(param.getPath()))) 096 .filter(StringUtils::isNotBlank) 097 .map(path -> mySearchParamExtractor 098 .getPathValueExtractor(theResource, path) 099 .get()) 100 .filter(t -> !t.isEmpty()) 101 .map(t -> t.get(0)) 102 .filter(t -> t instanceof IBaseReference) 103 .map(t -> (IBaseReference) t) 104 .map(t -> t.getReferenceElement().getValue()) 105 .map(IdType::new) 106 .filter(t -> theCompartmentName.equals( 107 t.getResourceType())) // assume the compartment name matches the resource type 108 .map(IdType::getIdPart) 109 .filter(StringUtils::isNotBlank) 110 .findFirst(); 111 } 112 113 /** 114 * Returns a {@code RuntimeSearchParam} list with the parameters extracted from the received 115 * {@code RuntimeResourceDefinition}, which are of type REFERENCE and have a membership compartment 116 * for "Patient" resource 117 * @param resourceDef the RuntimeResourceDefinition providing the RuntimeSearchParam list 118 * @return the RuntimeSearchParam filtered list 119 */ 120 @Nonnull 121 public static List<RuntimeSearchParam> getPatientCompartmentSearchParams(RuntimeResourceDefinition resourceDef) { 122 return resourceDef.getSearchParams().stream() 123 .filter(param -> param.getParamType() == RestSearchParameterTypeEnum.REFERENCE) 124 .filter(param -> param.getProvidesMembershipInCompartments() != null 125 && param.getProvidesMembershipInCompartments().contains("Patient")) 126 .collect(Collectors.toList()); 127 } 128}