001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 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.entity.ResourceSearchUrlEntity;
027import ca.uhn.fhir.jpa.model.entity.ResourceTable;
028import ca.uhn.fhir.jpa.searchparam.MatchUrlService;
029import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
030import jakarta.persistence.EntityManager;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033import org.springframework.stereotype.Service;
034import org.springframework.transaction.annotation.Transactional;
035
036import java.util.Date;
037
038/**
039 * This service ensures uniqueness of resources during create or create-on-update
040 * by storing the resource searchUrl.
041 *
042 * @see SearchUrlJobMaintenanceSvcImpl which deletes stale entities
043 */
044@Transactional
045@Service
046public class ResourceSearchUrlSvc {
047        private static final Logger ourLog = LoggerFactory.getLogger(ResourceSearchUrlSvc.class);
048        private final EntityManager myEntityManager;
049
050        private final IResourceSearchUrlDao myResourceSearchUrlDao;
051
052        private final MatchUrlService myMatchUrlService;
053
054        private final FhirContext myFhirContext;
055        private final PartitionSettings myPartitionSettings;
056
057        public ResourceSearchUrlSvc(
058                        EntityManager theEntityManager,
059                        IResourceSearchUrlDao theResourceSearchUrlDao,
060                        MatchUrlService theMatchUrlService,
061                        FhirContext theFhirContext,
062                        PartitionSettings thePartitionSettings) {
063                myEntityManager = theEntityManager;
064                myResourceSearchUrlDao = theResourceSearchUrlDao;
065                myMatchUrlService = theMatchUrlService;
066                myFhirContext = theFhirContext;
067                myPartitionSettings = thePartitionSettings;
068        }
069
070        /**
071         * Perform removal of entries older than {@code theCutoffDate} since the create operations are done.
072         */
073        public void deleteEntriesOlderThan(Date theCutoffDate) {
074                ourLog.debug("About to delete SearchUrl which are older than {}", theCutoffDate);
075                int deletedCount = myResourceSearchUrlDao.deleteAllWhereCreatedBefore(theCutoffDate);
076                ourLog.debug("Deleted {} SearchUrls", deletedCount);
077        }
078
079        /**
080         * Once a resource is updated or deleted, we can trust that future match checks will find the committed resource in the db.
081         * The use of the constraint table is done, and we can delete it to keep the table small.
082         */
083        public void deleteByResId(long theResId) {
084                myResourceSearchUrlDao.deleteByResId(theResId);
085        }
086
087        /**
088         *  We store a record of match urls with res_id so a db constraint can catch simultaneous creates that slip through.
089         */
090        public void enforceMatchUrlResourceUniqueness(
091                        String theResourceName, String theMatchUrl, ResourceTable theResourceTable) {
092                String canonicalizedUrlForStorage = createCanonicalizedUrlForStorage(theResourceName, theMatchUrl);
093
094                ResourceSearchUrlEntity searchUrlEntity = ResourceSearchUrlEntity.from(
095                                canonicalizedUrlForStorage,
096                                theResourceTable,
097                                myPartitionSettings.isConditionalCreateDuplicateIdentifiersEnabled());
098                // calling dao.save performs a merge operation which implies a trip to
099                // the database to see if the resource exists.  Since we don't need the check, we avoid the trip by calling
100                // em.persist.
101                myEntityManager.persist(searchUrlEntity);
102        }
103
104        /**
105         * Provides a sanitized matchUrl to circumvent ordering matters.
106         */
107        private String createCanonicalizedUrlForStorage(String theResourceName, String theMatchUrl) {
108
109                RuntimeResourceDefinition resourceDef = myFhirContext.getResourceDefinition(theResourceName);
110                SearchParameterMap matchUrlSearchParameterMap = myMatchUrlService.translateMatchUrl(theMatchUrl, resourceDef);
111
112                String canonicalizedMatchUrl = matchUrlSearchParameterMap.toNormalizedQueryString(myFhirContext);
113
114                return theResourceName + canonicalizedMatchUrl;
115        }
116}