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.data;
021
022import ca.uhn.fhir.jpa.entity.TermValueSet;
023import ca.uhn.fhir.jpa.entity.TermValueSetPreExpansionStatusEnum;
024import ca.uhn.fhir.jpa.model.dao.JpaPid;
025import ca.uhn.fhir.jpa.model.entity.IdAndPartitionId;
026import org.springframework.data.domain.Pageable;
027import org.springframework.data.domain.Slice;
028import org.springframework.data.jpa.repository.JpaRepository;
029import org.springframework.data.jpa.repository.Query;
030import org.springframework.data.repository.query.Param;
031
032import java.util.List;
033import java.util.Optional;
034
035public interface ITermValueSetDao extends JpaRepository<TermValueSet, IdAndPartitionId>, IHapiFhirJpaRepository {
036
037        @Query("SELECT vs FROM TermValueSet vs WHERE vs.myResource.myPid = :resource_pid")
038        Optional<TermValueSet> findByResourcePid(@Param("resource_pid") JpaPid theResourcePid);
039
040        // Keeping for backwards compatibility but recommend using findTermValueSetByUrlAndNullVersion instead.
041        @Deprecated
042        @Query("SELECT vs FROM TermValueSet vs WHERE vs.myUrl = :url")
043        Optional<TermValueSet> findByUrl(@Param("url") String theUrl);
044
045        @Query("SELECT vs FROM TermValueSet vs WHERE vs.myExpansionStatus = :expansion_status")
046        Slice<TermValueSet> findByExpansionStatus(
047                        Pageable pageable, @Param("expansion_status") TermValueSetPreExpansionStatusEnum theExpansionStatus);
048
049        @Query(
050                        value =
051                                        "SELECT vs FROM TermValueSet vs INNER JOIN ResourceTable r ON r = vs.myResource WHERE vs.myUrl = :url ORDER BY r.myUpdated DESC")
052        List<TermValueSet> findTermValueSetByUrl(Pageable thePage, @Param("url") String theUrl);
053
054        /**
055         * The current TermValueSet is not necessarily the last uploaded anymore, but the current VS resource
056         * is pointed by a specific ForcedId, so we locate current ValueSet as the one pointing to current VS resource
057         */
058        @Query(value = "SELECT vs FROM TermValueSet vs where vs.myResource.myFhirId = :forcedId ")
059        Optional<TermValueSet> findTermValueSetByForcedId(@Param("forcedId") String theForcedId);
060
061        @Query("SELECT vs FROM TermValueSet vs WHERE vs.myUrl = :url AND vs.myVersion IS NULL")
062        Optional<TermValueSet> findTermValueSetByUrlAndNullVersion(@Param("url") String theUrl);
063
064        @Query("SELECT vs FROM TermValueSet vs WHERE vs.myUrl = :url AND vs.myVersion = :version")
065        Optional<TermValueSet> findTermValueSetByUrlAndVersion(
066                        @Param("url") String theUrl, @Param("version") String theVersion);
067}