001/* 002 * #%L 003 * HAPI FHIR JPA Server 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.cache; 021 022import ca.uhn.fhir.jpa.config.util.ResourceTypeUtil; 023import ca.uhn.fhir.jpa.dao.data.IResourceTypeDao; 024import ca.uhn.fhir.jpa.dao.tx.IHapiTransactionService; 025import ca.uhn.fhir.jpa.model.entity.ResourceTypeEntity; 026import ca.uhn.fhir.jpa.util.MemoryCacheService; 027import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 028import jakarta.annotation.PostConstruct; 029import org.apache.commons.collections4.CollectionUtils; 030import org.slf4j.Logger; 031import org.springframework.dao.DataIntegrityViolationException; 032import org.springframework.stereotype.Service; 033import org.springframework.transaction.annotation.Propagation; 034 035import java.util.List; 036 037import static org.slf4j.LoggerFactory.getLogger; 038 039@Service 040public class ResourceTypeCacheSvcImpl implements IResourceTypeCacheSvc { 041 private static final Logger ourLog = getLogger(ResourceTypeCacheSvcImpl.class); 042 043 private final IHapiTransactionService myTransactionService; 044 private final IResourceTypeDao myResourceTypeDao; 045 private final MemoryCacheService myMemoryCacheService; 046 047 public ResourceTypeCacheSvcImpl( 048 IHapiTransactionService theTransactionService, 049 IResourceTypeDao theResourceTypeDao, 050 MemoryCacheService theMemoryCacheService) { 051 myTransactionService = theTransactionService; 052 myResourceTypeDao = theResourceTypeDao; 053 myMemoryCacheService = theMemoryCacheService; 054 } 055 056 @PostConstruct 057 public void start() { 058 initResourceTypes(); 059 ourLog.info( 060 "Resource type cache size: {}", 061 myMemoryCacheService.getEstimatedSize(MemoryCacheService.CacheEnum.RES_TYPE_TO_RES_TYPE_ID)); 062 } 063 064 @Override 065 public short getResourceTypeId(String theResType) { 066 Short resTypeId = myMemoryCacheService.get( 067 MemoryCacheService.CacheEnum.RES_TYPE_TO_RES_TYPE_ID, theResType, this::lookupResourceTypeId); 068 069 if (resTypeId == null) { 070 ourLog.info("Creating a new Resource Type [{}]", theResType); 071 ResourceTypeEntity entity = createResourceType(theResType); 072 resTypeId = entity.getResourceTypeId(); 073 addToCache(theResType, resTypeId); 074 } 075 return resTypeId; 076 } 077 078 @Override 079 public void addToCache(String theResType, Short theResTypeId) { 080 myMemoryCacheService.putAfterCommit( 081 MemoryCacheService.CacheEnum.RES_TYPE_TO_RES_TYPE_ID, theResType, theResTypeId); 082 } 083 084 protected void initResourceTypes() { 085 myTransactionService 086 .withSystemRequestOnDefaultPartition() 087 .withPropagation(Propagation.REQUIRES_NEW) 088 .execute(() -> { 089 List<ResourceTypeEntity> resTypeEntities = myResourceTypeDao.findAll(); 090 if (CollectionUtils.isEmpty(resTypeEntities)) { 091 List<ResourceTypeEntity> newEntities = ResourceTypeUtil.generateResourceTypes().stream() 092 .map(r -> { 093 ResourceTypeEntity entity = new ResourceTypeEntity(); 094 entity.setResourceType(r); 095 return entity; 096 }) 097 .toList(); 098 099 myResourceTypeDao.saveAll(newEntities); 100 ourLog.info("Table HFJ_RESOURCE_TYPE is populated with {} entries.", newEntities.size()); 101 resTypeEntities = newEntities; 102 } 103 104 // Populate the cache 105 resTypeEntities.forEach(r -> addToCache(r.getResourceType(), r.getResourceTypeId())); 106 return null; 107 }); 108 } 109 110 protected ResourceTypeEntity createResourceType(String theResourceType) { 111 return myTransactionService 112 .withSystemRequestOnDefaultPartition() 113 .withPropagation(Propagation.REQUIRES_NEW) 114 .execute(() -> { 115 ResourceTypeEntity resTypeEntity = new ResourceTypeEntity(); 116 resTypeEntity.setResourceType(theResourceType); 117 try { 118 resTypeEntity = myResourceTypeDao.save(resTypeEntity); 119 myResourceTypeDao.flush(); 120 } catch (DataIntegrityViolationException e) { 121 if (e.getMessage().contains("Value too long for column")) { 122 throw new InternalErrorException("Resource type name is too long: " + theResourceType, e); 123 } 124 // This can happen if the resource type already exists in the database 125 ourLog.info("Resource type already exists: {}", theResourceType); 126 resTypeEntity = myResourceTypeDao.findByResourceType(theResourceType); 127 } 128 129 return resTypeEntity; 130 }); 131 } 132 133 private Short lookupResourceTypeId(String theResourceType) { 134 return myTransactionService 135 .withSystemRequestOnDefaultPartition() 136 .execute(() -> myResourceTypeDao.findResourceIdByType(theResourceType)); 137 } 138}