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.dao;
021
022import ca.uhn.fhir.jpa.dao.data.ITagDefinitionDao;
023import ca.uhn.fhir.jpa.model.entity.TagDefinition;
024import ca.uhn.fhir.jpa.model.entity.TagTypeEnum;
025import ca.uhn.fhir.jpa.util.MemoryCacheService;
026import ca.uhn.fhir.rest.api.server.storage.TransactionDetails;
027import jakarta.annotation.Nonnull;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.springframework.data.domain.Pageable;
031import org.springframework.stereotype.Repository;
032import org.springframework.transaction.support.TransactionSynchronization;
033import org.springframework.transaction.support.TransactionSynchronizationManager;
034
035import java.util.HashMap;
036import java.util.List;
037
038import static org.apache.commons.lang3.StringUtils.isBlank;
039
040@Repository
041public class CacheTagDefinitionDao {
042        private static final Logger ourLog = LoggerFactory.getLogger(CacheTagDefinitionDao.class);
043
044        private final ITagDefinitionDao tagDefinitionDao;
045        private final MemoryCacheService memoryCacheService;
046
047        public CacheTagDefinitionDao(ITagDefinitionDao tagDefinitionDao, MemoryCacheService memoryCacheService) {
048                this.tagDefinitionDao = tagDefinitionDao;
049                this.memoryCacheService = memoryCacheService;
050        }
051
052        /**
053         * Returns a TagDefinition or null if the scheme, term, and label are all blank.
054         */
055        protected TagDefinition getTagOrNull(
056                        TransactionDetails transactionDetails,
057                        TagTypeEnum tagType,
058                        String scheme,
059                        String term,
060                        String label,
061                        String version,
062                        Boolean userSelected) {
063
064                if (isBlank(scheme) && isBlank(term) && isBlank(label)) {
065                        return null;
066                }
067
068                MemoryCacheService.TagDefinitionCacheKey key =
069                                toTagDefinitionMemoryCacheKey(tagType, scheme, term, version, userSelected);
070                TagDefinition tagDefinition = memoryCacheService.getIfPresent(MemoryCacheService.CacheEnum.TAG_DEFINITION, key);
071
072                if (tagDefinition == null) {
073                        HashMap<MemoryCacheService.TagDefinitionCacheKey, TagDefinition> resolvedTagDefinitions =
074                                        transactionDetails.getOrCreateUserData("resolvedTagDefinitions", HashMap::new);
075
076                        tagDefinition = resolvedTagDefinitions.get(key);
077
078                        if (tagDefinition == null) {
079                                tagDefinition = getOrCreateTag(tagType, scheme, term, label, version, userSelected);
080
081                                TransactionSynchronization sync =
082                                                new AddTagDefinitionToCacheAfterCommitSynchronization(key, tagDefinition);
083                                TransactionSynchronizationManager.registerSynchronization(sync);
084
085                                resolvedTagDefinitions.put(key, tagDefinition);
086                        }
087                }
088
089                return tagDefinition;
090        }
091
092        /**
093         * Gets or creates a TagDefinition entity.
094         */
095        private TagDefinition getOrCreateTag(
096                        TagTypeEnum tagType, String scheme, String term, String label, String version, Boolean userSelected) {
097                List<TagDefinition> result = tagDefinitionDao.findByTagTypeAndSchemeAndTermAndVersionAndUserSelected(
098                                tagType, scheme, term, version, userSelected, Pageable.ofSize(1));
099
100                if (!result.isEmpty()) {
101                        return result.get(0);
102                } else {
103                        // Create a new TagDefinition if no result is found
104                        TagDefinition newTag = new TagDefinition(tagType, scheme, term, label);
105                        newTag.setVersion(version);
106                        newTag.setUserSelected(userSelected);
107                        return tagDefinitionDao.save(newTag);
108                }
109        }
110
111        @Nonnull
112        private static MemoryCacheService.TagDefinitionCacheKey toTagDefinitionMemoryCacheKey(
113                        TagTypeEnum tagType, String scheme, String term, String version, Boolean userSelected) {
114                return new MemoryCacheService.TagDefinitionCacheKey(tagType, scheme, term, version, userSelected);
115        }
116
117        private class AddTagDefinitionToCacheAfterCommitSynchronization implements TransactionSynchronization {
118                private final TagDefinition tagDefinition;
119                private final MemoryCacheService.TagDefinitionCacheKey key;
120
121                public AddTagDefinitionToCacheAfterCommitSynchronization(
122                                MemoryCacheService.TagDefinitionCacheKey key, TagDefinition tagDefinition) {
123                        this.tagDefinition = tagDefinition;
124                        this.key = key;
125                }
126
127                @Override
128                public void afterCommit() {
129                        memoryCacheService.put(MemoryCacheService.CacheEnum.TAG_DEFINITION, key, tagDefinition);
130                }
131        }
132}