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