001package ca.uhn.fhir.jpa.dao.data;
002
003import ca.uhn.fhir.jpa.model.entity.ResourceHistoryTable;
004import org.springframework.data.domain.Pageable;
005import org.springframework.data.domain.Slice;
006import org.springframework.data.jpa.repository.JpaRepository;
007import org.springframework.data.jpa.repository.Modifying;
008import org.springframework.data.jpa.repository.Query;
009import org.springframework.data.repository.query.Param;
010
011import java.util.List;
012
013/*
014 * #%L
015 * HAPI FHIR JPA Server
016 * %%
017 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
018 * %%
019 * Licensed under the Apache License, Version 2.0 (the "License");
020 * you may not use this file except in compliance with the License.
021 * You may obtain a copy of the License at
022 *
023 *      http://www.apache.org/licenses/LICENSE-2.0
024 *
025 * Unless required by applicable law or agreed to in writing, software
026 * distributed under the License is distributed on an "AS IS" BASIS,
027 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
028 * See the License for the specific language governing permissions and
029 * limitations under the License.
030 * #L%
031 */
032
033public interface IResourceHistoryTableDao extends JpaRepository<ResourceHistoryTable, Long>, IHapiFhirJpaRepository {
034
035        /**
036         * This is really only intended for unit tests - There can be many versions of resources in
037         * the real world, use a pageable query for real uses.
038         */
039        @Query("SELECT t FROM ResourceHistoryTable t WHERE t.myResourceId = :resId ORDER BY t.myResourceVersion ASC")
040        List<ResourceHistoryTable> findAllVersionsForResourceIdInOrder(@Param("resId") Long theId);
041
042
043        @Query("SELECT t FROM ResourceHistoryTable t LEFT OUTER JOIN FETCH t.myProvenance WHERE t.myResourceId = :id AND t.myResourceVersion = :version")
044        ResourceHistoryTable findForIdAndVersionAndFetchProvenance(@Param("id") long theId, @Param("version") long theVersion);
045
046        @Query("SELECT t.myId FROM ResourceHistoryTable t WHERE t.myResourceId = :resId AND t.myResourceVersion != :dontWantVersion")
047        Slice<Long> findForResourceId(Pageable thePage, @Param("resId") Long theId, @Param("dontWantVersion") Long theDontWantVersion);
048
049        @Query("" +
050                "SELECT v.myId FROM ResourceHistoryTable v " +
051                "LEFT OUTER JOIN ResourceTable t ON (v.myResourceId = t.myId) " +
052                "WHERE v.myResourceVersion != t.myVersion AND " +
053                "t.myId = :resId")
054        Slice<Long> findIdsOfPreviousVersionsOfResourceId(Pageable thePage, @Param("resId") Long theResourceId);
055
056        @Query("" +
057                "SELECT v.myId FROM ResourceHistoryTable v " +
058                "LEFT OUTER JOIN ResourceTable t ON (v.myResourceId = t.myId) " +
059                "WHERE v.myResourceVersion != t.myVersion AND " +
060                "t.myResourceType = :restype")
061        Slice<Long> findIdsOfPreviousVersionsOfResources(Pageable thePage, @Param("restype") String theResourceName);
062
063        @Query("" +
064                "SELECT v.myId FROM ResourceHistoryTable v " +
065                "LEFT OUTER JOIN ResourceTable t ON (v.myResourceId = t.myId) " +
066                "WHERE v.myResourceVersion != t.myVersion")
067        Slice<Long> findIdsOfPreviousVersionsOfResources(Pageable thePage);
068
069        @Modifying
070        @Query("UPDATE ResourceHistoryTable r SET r.myResourceVersion = :newVersion WHERE r.myResourceId = :id AND r.myResourceVersion = :oldVersion")
071        void updateVersion(@Param("id") long theId, @Param("oldVersion") long theOldVersion, @Param("newVersion") long theNewVersion);
072
073        @Modifying
074        @Query("DELETE FROM ResourceHistoryTable t WHERE t.myId = :pid")
075        void deleteByPid(@Param("pid") Long theId);
076}