001package ca.uhn.fhir.jpa.dao.data;
002
003import ca.uhn.fhir.jpa.model.entity.ForcedId;
004import org.springframework.data.jpa.repository.JpaRepository;
005import org.springframework.data.jpa.repository.Modifying;
006import org.springframework.data.jpa.repository.Query;
007import org.springframework.data.repository.query.Param;
008
009import java.util.Collection;
010import java.util.List;
011import java.util.Optional;
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 IForcedIdDao extends JpaRepository<ForcedId, Long>, IHapiFhirJpaRepository {
034
035        @Query("SELECT f FROM ForcedId f WHERE myResourcePid IN (:resource_pids)")
036        List<ForcedId> findAllByResourcePid(@Param("resource_pids") List<Long> theResourcePids);
037
038        @Query("SELECT f FROM ForcedId f WHERE f.myResourcePid = :resource_pid")
039        Optional<ForcedId> findByResourcePid(@Param("resource_pid") Long theResourcePid);
040
041        @Modifying
042        @Query("DELETE FROM ForcedId t WHERE t.myId = :pid")
043        void deleteByPid(@Param("pid") Long theId);
044
045        /**
046         * This method returns a Collection where each row is an element in the collection. Each element in the collection
047         * is an object array, where the order matters (the array represents columns returned by the query). Be careful if you change this query in any way.
048         */
049        @Query("" +
050                "SELECT " +
051                "   f.myResourceType, f.myResourcePid, f.myForcedId, t.myDeleted " +
052                "FROM ForcedId f " +
053                "JOIN ResourceTable t ON t.myId = f.myResourcePid " +
054                "WHERE f.myResourceType = :resource_type AND f.myForcedId IN ( :forced_id )")
055        Collection<Object[]> findAndResolveByForcedIdWithNoType(@Param("resource_type") String theResourceType, @Param("forced_id") Collection<String> theForcedIds);
056
057        /**
058         * This method returns a Collection where each row is an element in the collection. Each element in the collection
059         * is an object array, where the order matters (the array represents columns returned by the query). Be careful if you change this query in any way.
060         */
061        @Query("" +
062                "SELECT " +
063                "   f.myResourceType, f.myResourcePid, f.myForcedId, t.myDeleted " +
064                "FROM ForcedId f " +
065                "JOIN ResourceTable t ON t.myId = f.myResourcePid " +
066                "WHERE f.myResourceType = :resource_type AND f.myForcedId IN ( :forced_id ) AND f.myPartitionIdValue IN :partition_id")
067        Collection<Object[]> findAndResolveByForcedIdWithNoTypeInPartition(@Param("resource_type") String theResourceType, @Param("forced_id") Collection<String> theForcedIds, @Param("partition_id") Collection<Integer> thePartitionId);
068
069
070        /**
071         * This method returns a Collection where each row is an element in the collection. Each element in the collection
072         * is an object array, where the order matters (the array represents columns returned by the query). Be careful if you change this query in any way.
073         */
074        @Query("" +
075                "SELECT " +
076                "   f.myResourceType, f.myResourcePid, f.myForcedId, t.myDeleted " +
077                "FROM ForcedId f " +
078                "JOIN ResourceTable t ON t.myId = f.myResourcePid " +
079                "WHERE f.myResourceType = :resource_type AND f.myForcedId IN ( :forced_id ) AND f.myPartitionIdValue IS NULL")
080        Collection<Object[]> findAndResolveByForcedIdWithNoTypeInPartitionNull(@Param("resource_type") String theResourceType, @Param("forced_id") Collection<String> theForcedIds);
081
082
083        /**
084         * This method returns a Collection where each row is an element in the collection. Each element in the collection
085         * is an object array, where the order matters (the array represents columns returned by the query). Be careful if you change this query in any way.
086         */
087        @Query("" +
088                "SELECT " +
089                "   f.myResourceType, f.myResourcePid, f.myForcedId, t.myDeleted " +
090                "FROM ForcedId f " +
091                "JOIN ResourceTable t ON t.myId = f.myResourcePid " +
092                "WHERE f.myResourceType = :resource_type AND f.myForcedId IN ( :forced_id ) AND (f.myPartitionIdValue IS NULL OR f.myPartitionIdValue IN :partition_id)")
093        Collection<Object[]> findAndResolveByForcedIdWithNoTypeInPartitionIdOrNullPartitionId(@Param("resource_type") String theNextResourceType, @Param("forced_id") Collection<String> theNextIds, @Param("forced_id") List<Integer> thePartitionIdsWithoutDefault);
094}