001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2023 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.custom;
021
022import org.springframework.stereotype.Component;
023
024import java.util.Collection;
025import java.util.List;
026import javax.persistence.EntityManager;
027import javax.persistence.PersistenceContext;
028
029@Component
030// Don't change the name of this class.  Spring Data requires the name to match.
031// See https://stackoverflow.com/questions/11880924/how-to-add-custom-method-to-spring-data-jpa
032public class IForcedIdDaoImpl implements IForcedIdQueries {
033
034        @PersistenceContext
035        private EntityManager myEntityManager;
036
037        /**
038         * This method returns a Collection where each row is an element in the collection. Each element in the collection
039         * is an object array, where the order matters (the array represents columns returned by the query).
040         * Deleted resources are not filtered.
041         */
042        public Collection<Object[]> findAndResolveByForcedIdWithNoTypeIncludeDeleted(
043                        String theResourceType, Collection<String> theForcedIds) {
044                return findAndResolveByForcedIdWithNoType(theResourceType, theForcedIds, false);
045        }
046
047        /**
048         * This method returns a Collection where each row is an element in the collection. Each element in the collection
049         * is an object array, where the order matters (the array represents columns returned by the query).
050         * Deleted resources are optionally filtered. Be careful if you change this query in any way.
051         */
052        public Collection<Object[]> findAndResolveByForcedIdWithNoType(
053                        String theResourceType, Collection<String> theForcedIds, boolean theExcludeDeleted) {
054                String query = "" + "SELECT "
055                                + "   f.myResourceType, f.myResourcePid, f.myForcedId, t.myDeleted "
056                                + "FROM ForcedId f "
057                                + "JOIN ResourceTable t ON t.myId = f.myResourcePid "
058                                + "WHERE f.myResourceType = :resource_type AND f.myForcedId IN ( :forced_id )";
059
060                if (theExcludeDeleted) {
061                        query += " AND t.myDeleted IS NULL";
062                }
063
064                return myEntityManager
065                                .createQuery(query)
066                                .setParameter("resource_type", theResourceType)
067                                .setParameter("forced_id", theForcedIds)
068                                .getResultList();
069        }
070
071        /**
072         * This method returns a Collection where each row is an element in the collection. Each element in the collection
073         * is an object array, where the order matters (the array represents columns returned by the query).
074         * Deleted resources are optionally filtered. Be careful if you change this query in any way.
075         */
076        public Collection<Object[]> findAndResolveByForcedIdWithNoTypeInPartition(
077                        String theResourceType,
078                        Collection<String> theForcedIds,
079                        Collection<Integer> thePartitionId,
080                        boolean theExcludeDeleted) {
081                String query = "" + "SELECT "
082                                + "   f.myResourceType, f.myResourcePid, f.myForcedId, t.myDeleted "
083                                + "FROM ForcedId f "
084                                + "JOIN ResourceTable t ON t.myId = f.myResourcePid "
085                                + "WHERE f.myResourceType = :resource_type AND f.myForcedId IN ( :forced_id ) AND f.myPartitionIdValue IN ( :partition_id )";
086
087                if (theExcludeDeleted) {
088                        query += " AND t.myDeleted IS NULL";
089                }
090
091                return myEntityManager
092                                .createQuery(query)
093                                .setParameter("resource_type", theResourceType)
094                                .setParameter("forced_id", theForcedIds)
095                                .setParameter("partition_id", thePartitionId)
096                                .getResultList();
097        }
098
099        /**
100         * This method returns a Collection where each row is an element in the collection. Each element in the collection
101         * is an object array, where the order matters (the array represents columns returned by the query).
102         * Deleted resources are optionally filtered. Be careful if you change this query in any way.
103         */
104        public Collection<Object[]> findAndResolveByForcedIdWithNoTypeInPartitionNull(
105                        String theResourceType, Collection<String> theForcedIds, boolean theExcludeDeleted) {
106                String query = "" + "SELECT "
107                                + "   f.myResourceType, f.myResourcePid, f.myForcedId, t.myDeleted "
108                                + "FROM ForcedId f "
109                                + "JOIN ResourceTable t ON t.myId = f.myResourcePid "
110                                + "WHERE f.myResourceType = :resource_type AND f.myForcedId IN ( :forced_id ) AND f.myPartitionIdValue IS NULL";
111
112                if (theExcludeDeleted) {
113                        query += " AND t.myDeleted IS NULL";
114                }
115
116                return myEntityManager
117                                .createQuery(query)
118                                .setParameter("resource_type", theResourceType)
119                                .setParameter("forced_id", theForcedIds)
120                                .getResultList();
121        }
122
123        /**
124         * This method returns a Collection where each row is an element in the collection. Each element in the collection
125         * is an object array, where the order matters (the array represents columns returned by the query).
126         * Deleted resources are optionally filtered. Be careful if you change this query in any way.
127         */
128        public Collection<Object[]> findAndResolveByForcedIdWithNoTypeInPartitionIdOrNullPartitionId(
129                        String theResourceType,
130                        Collection<String> theForcedIds,
131                        List<Integer> thePartitionIdsWithoutDefault,
132                        boolean theExcludeDeleted) {
133                String query = "" + "SELECT "
134                                + "   f.myResourceType, f.myResourcePid, f.myForcedId, t.myDeleted "
135                                + "FROM ForcedId f "
136                                + "JOIN ResourceTable t ON t.myId = f.myResourcePid "
137                                + "WHERE f.myResourceType = :resource_type AND f.myForcedId IN ( :forced_id ) AND (f.myPartitionIdValue IS NULL OR f.myPartitionIdValue IN ( :partition_id ))";
138
139                if (theExcludeDeleted) {
140                        query += " AND t.myDeleted IS NULL";
141                }
142
143                return myEntityManager
144                                .createQuery(query)
145                                .setParameter("resource_type", theResourceType)
146                                .setParameter("forced_id", theForcedIds)
147                                .setParameter("partition_id", thePartitionIdsWithoutDefault)
148                                .getResultList();
149        }
150}