001package ca.uhn.fhir.jpa.dao.data;
002
003import java.util.List;
004import java.util.Optional;
005
006import org.springframework.data.domain.Pageable;
007import org.springframework.data.jpa.repository.JpaRepository;
008import org.springframework.data.jpa.repository.Modifying;
009import org.springframework.data.jpa.repository.Query;
010import org.springframework.data.repository.query.Param;
011
012import ca.uhn.fhir.jpa.entity.TermConceptMap;
013
014/*
015 * #%L
016 * HAPI FHIR JPA Server
017 * %%
018 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
019 * %%
020 * Licensed under the Apache License, Version 2.0 (the "License");
021 * you may not use this file except in compliance with the License.
022 * You may obtain a copy of the License at
023 *
024 *      http://www.apache.org/licenses/LICENSE-2.0
025 *
026 * Unless required by applicable law or agreed to in writing, software
027 * distributed under the License is distributed on an "AS IS" BASIS,
028 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
029 * See the License for the specific language governing permissions and
030 * limitations under the License.
031 * #L%
032 */
033
034public interface ITermConceptMapDao extends JpaRepository<TermConceptMap, Long>, IHapiFhirJpaRepository {
035        @Query("DELETE FROM TermConceptMap cm WHERE cm.myId = :pid")
036        @Modifying
037        void deleteTermConceptMapById(@Param("pid") Long theId);
038
039        @Query("SELECT cm FROM TermConceptMap cm WHERE cm.myResourcePid = :resource_pid")
040        Optional<TermConceptMap> findTermConceptMapByResourcePid(@Param("resource_pid") Long theResourcePid);
041
042        // Keep backwards compatibility, recommend to use findTermConceptMapByUrlAndNullVersion instead
043        @Deprecated
044        @Query("SELECT cm FROM TermConceptMap cm WHERE cm.myUrl = :url and cm.myVersion is null")
045        Optional<TermConceptMap> findTermConceptMapByUrl(@Param("url") String theUrl);
046        
047        @Query("SELECT cm FROM TermConceptMap cm WHERE cm.myUrl = :url and cm.myVersion is null")
048        Optional<TermConceptMap> findTermConceptMapByUrlAndNullVersion(@Param("url") String theUrl);
049
050        // Note that last updated version is considered current version.
051        @Query(value="SELECT cm FROM TermConceptMap cm INNER JOIN ResourceTable r ON r.myId = cm.myResourcePid WHERE cm.myUrl = :url ORDER BY r.myUpdated DESC") 
052        List<TermConceptMap> getTermConceptMapEntitiesByUrlOrderByMostRecentUpdate(Pageable thePage, @Param("url") String theUrl);
053
054        @Query("SELECT cm FROM TermConceptMap cm WHERE cm.myUrl = :url AND cm.myVersion = :version")
055        Optional<TermConceptMap> findTermConceptMapByUrlAndVersion(@Param("url") String theUrl, @Param("version") String theVersion);
056}