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.storage.interceptor; 021 022import ca.uhn.fhir.interceptor.api.Hook; 023import ca.uhn.fhir.interceptor.api.Interceptor; 024import ca.uhn.fhir.interceptor.api.Pointcut; 025 026import java.util.Set; 027 028/** 029 * This interceptor can be used on systems with 030 * {@link ca.uhn.fhir.jpa.api.config.JpaStorageSettings#setAutoCreatePlaceholderReferenceTargets(boolean) Auto-Create Placeholder Reference Targets} 031 * enabled in order to cause these placeholder resources to only be created for specific resource types. 032 * 033 * @since 8.4.0 034 */ 035@Interceptor 036public class AutoCreatePlaceholderReferenceEnabledByTypeInterceptor { 037 038 private final Set<String> myResourceTypesToAllow; 039 040 /** 041 * Constructor 042 * 043 * @param theResourceTypesToAllow The resource types to allow. For example, if you pass in the strings "Patient" 044 * and "Observation", then placeholder reference target resources will be created 045 * with these resource types but not with any other resource types. 046 */ 047 public AutoCreatePlaceholderReferenceEnabledByTypeInterceptor(String... theResourceTypesToAllow) { 048 myResourceTypesToAllow = Set.of(theResourceTypesToAllow); 049 } 050 051 /** 052 * This method will be called automatically before each auto-created placeholder 053 * reference target resource. 054 */ 055 @Hook(Pointcut.STORAGE_PRE_AUTO_CREATE_PLACEHOLDER_REFERENCE) 056 public AutoCreatePlaceholderReferenceTargetResponse autoCreatePlaceholderReferenceTarget( 057 AutoCreatePlaceholderReferenceTargetRequest theRequest) { 058 059 String resourceType = 060 theRequest.getTargetResourceToCreate().getIdElement().getResourceType(); 061 if (!myResourceTypesToAllow.contains(resourceType)) { 062 return AutoCreatePlaceholderReferenceTargetResponse.doNotCreateTarget(); 063 } 064 065 return AutoCreatePlaceholderReferenceTargetResponse.proceed(); 066 } 067}