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.search;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.context.RuntimeResourceDefinition;
024import ca.uhn.fhir.jpa.dao.data.IResourceSearchUrlDao;
025import ca.uhn.fhir.jpa.model.config.PartitionSettings;
026import ca.uhn.fhir.jpa.model.dao.JpaPid;
027import ca.uhn.fhir.jpa.model.entity.ResourceSearchUrlEntity;
028import ca.uhn.fhir.jpa.model.entity.ResourceTable;
029import ca.uhn.fhir.jpa.searchparam.MatchUrlService;
030import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
031import jakarta.persistence.EntityManager;
032import org.apache.commons.lang3.Validate;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035import org.springframework.stereotype.Service;
036import org.springframework.transaction.annotation.Transactional;
037
038import java.util.Collection;
039import java.util.Date;
040import java.util.stream.Collectors;
041
042/**
043 * This service ensures uniqueness of resources during create or create-on-update
044 * by storing the resource searchUrl.
045 *
046 * @see SearchUrlJobMaintenanceSvcImpl which deletes stale entities
047 */
048@Transactional
049@Service
050public class ResourceSearchUrlSvc {
051        private static final Logger ourLog = LoggerFactory.getLogger(ResourceSearchUrlSvc.class);
052        private final EntityManager myEntityManager;
053
054        private final IResourceSearchUrlDao myResourceSearchUrlDao;
055
056        private final MatchUrlService myMatchUrlService;
057
058        private final FhirContext myFhirContext;
059        private final PartitionSettings myPartitionSettings;
060
061        public ResourceSearchUrlSvc(
062                        EntityManager theEntityManager,
063                        IResourceSearchUrlDao theResourceSearchUrlDao,
064                        MatchUrlService theMatchUrlService,
065                        FhirContext theFhirContext,
066                        PartitionSettings thePartitionSettings) {
067                myEntityManager = theEntityManager;
068                myResourceSearchUrlDao = theResourceSearchUrlDao;
069                myMatchUrlService = theMatchUrlService;
070                myFhirContext = theFhirContext;
071                myPartitionSettings = thePartitionSettings;
072        }
073
074        /**
075         * Perform removal of entries older than {@code theCutoffDate} since the create operations are done.
076         */
077        public void deleteEntriesOlderThan(Date theCutoffDate) {
078                ourLog.debug("About to delete SearchUrl which are older than {}", theCutoffDate);
079                int deletedCount = myResourceSearchUrlDao.deleteAllWhereCreatedBefore(theCutoffDate);
080                ourLog.debug("Deleted {} SearchUrls", deletedCount);
081        }
082
083        /**
084         * Once a resource is updated or deleted, we can trust that future match checks will find the committed resource in the db.
085         * The use of the constraint table is done, and we can delete it to keep the table small.
086         */
087        public void deleteByResId(JpaPid theResId) {
088                myResourceSearchUrlDao.deleteByResId(theResId.getId());
089        }
090
091        /**
092         * Once a resource is updated or deleted, we can trust that future match checks will find the committed resource in the db.
093         * The use of the constraint table is done, and we can delete it to keep the table small.
094         */
095        public void deleteByResIds(Collection<JpaPid> theResId) {
096                myResourceSearchUrlDao.deleteByResIds(
097                                theResId.stream().map(JpaPid::getId).collect(Collectors.toList()));
098        }
099
100        /**
101         * @param theResourceName The resource name associated with the conditional URL
102         * @param theMatchUrl The URL parameters portion of the match URL. Should not include a leading {@literal ?}, but can include {@literal &} separators.
103         *
104         *  We store a record of match urls with res_id so a db constraint can catch simultaneous creates that slip through.
105         */
106        public void enforceMatchUrlResourceUniqueness(
107                        String theResourceName, String theMatchUrl, ResourceTable theResourceTable) {
108                Validate.notBlank(theResourceName, "theResourceName must not be blank");
109                Validate.notBlank(theMatchUrl, "theMatchUrl must not be blank");
110
111                String canonicalizedUrlForStorage = createCanonicalizedUrlForStorage(theResourceName, theMatchUrl);
112
113                ResourceSearchUrlEntity searchUrlEntity = ResourceSearchUrlEntity.from(
114                                canonicalizedUrlForStorage,
115                                theResourceTable,
116                                myPartitionSettings.isConditionalCreateDuplicateIdentifiersEnabled());
117                // calling dao.save performs a merge operation which implies a trip to
118                // the database to see if the resource exists.  Since we don't need the check, we avoid the trip by calling
119                // em.persist.
120                myEntityManager.persist(searchUrlEntity);
121        }
122
123        /**
124         * Provides a sanitized matchUrl to circumvent ordering matters.
125         */
126        private String createCanonicalizedUrlForStorage(String theResourceName, String theMatchUrl) {
127
128                RuntimeResourceDefinition resourceDef = myFhirContext.getResourceDefinition(theResourceName);
129                SearchParameterMap matchUrlSearchParameterMap = myMatchUrlService.translateMatchUrl(theMatchUrl, resourceDef);
130
131                String canonicalizedMatchUrl = matchUrlSearchParameterMap.toNormalizedQueryString(myFhirContext);
132
133                return theResourceName + canonicalizedMatchUrl;
134        }
135}