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.dao.data;
021
022import ca.uhn.fhir.jpa.entity.TermCodeSystem;
023import ca.uhn.fhir.jpa.entity.TermCodeSystemVersion;
024import org.springframework.data.jpa.repository.JpaRepository;
025import org.springframework.data.jpa.repository.Modifying;
026import org.springframework.data.jpa.repository.Query;
027import org.springframework.data.repository.query.Param;
028
029import java.util.List;
030
031public interface ITermCodeSystemVersionDao extends JpaRepository<TermCodeSystemVersion, Long>, IHapiFhirJpaRepository {
032
033        @Modifying
034        @Query("DELETE FROM TermCodeSystemVersion csv WHERE csv.myCodeSystem = :cs")
035        void deleteForCodeSystem(@Param("cs") TermCodeSystem theCodeSystem);
036
037        @Query("SELECT myId FROM TermCodeSystemVersion WHERE myCodeSystemPid = :codesystem_pid order by myId")
038        List<Long> findSortedPidsByCodeSystemPid(@Param("codesystem_pid") Long theCodeSystemPid);
039
040        @Query(
041                        "SELECT cs FROM TermCodeSystemVersion cs WHERE cs.myCodeSystemPid = :codesystem_pid AND cs.myCodeSystemVersionId = :codesystem_version_id")
042        TermCodeSystemVersion findByCodeSystemPidAndVersion(
043                        @Param("codesystem_pid") Long theCodeSystemPid,
044                        @Param("codesystem_version_id") String theCodeSystemVersionId);
045
046        @Query(
047                        "SELECT tcsv FROM TermCodeSystemVersion tcsv INNER JOIN FETCH TermCodeSystem tcs on tcs.myPid = tcsv.myCodeSystemPid "
048                                        + "WHERE tcs.myCodeSystemUri = :code_system_uri AND tcsv.myCodeSystemVersionId = :codesystem_version_id")
049        TermCodeSystemVersion findByCodeSystemUriAndVersion(
050                        @Param("code_system_uri") String theCodeSystemUri,
051                        @Param("codesystem_version_id") String theCodeSystemVersionId);
052
053        @Query(
054                        "SELECT cs FROM TermCodeSystemVersion cs WHERE cs.myCodeSystemPid = :codesystem_pid AND cs.myCodeSystemVersionId IS NULL")
055        TermCodeSystemVersion findByCodeSystemPidVersionIsNull(@Param("codesystem_pid") Long theCodeSystemPid);
056
057        @Query("SELECT cs FROM TermCodeSystemVersion cs WHERE cs.myResourcePid = :resource_id")
058        List<TermCodeSystemVersion> findByCodeSystemResourcePid(@Param("resource_id") Long theCodeSystemResourcePid);
059
060        @Query(
061                        "SELECT cs FROM TermCodeSystemVersion cs WHERE cs.myCodeSystemHavingThisVersionAsCurrentVersionIfAny.myResource.myId = :resource_id")
062        TermCodeSystemVersion findCurrentVersionForCodeSystemResourcePid(
063                        @Param("resource_id") Long theCodeSystemResourcePid);
064}