View Javadoc
1   package ca.uhn.fhir.jpa.dao.data;
2   
3   import java.util.Collection;
4   import java.util.Date;
5   
6   import javax.persistence.TemporalType;
7   
8   import org.springframework.data.domain.Pageable;
9   import org.springframework.data.domain.Slice;
10  import org.springframework.data.jpa.repository.JpaRepository;
11  import org.springframework.data.jpa.repository.Query;
12  import org.springframework.data.jpa.repository.Temporal;
13  import org.springframework.data.repository.query.Param;
14  
15  import ca.uhn.fhir.jpa.entity.ResourceHistoryTable;
16  
17  /*
18   * #%L
19   * HAPI FHIR JPA Server
20   * %%
21   * Copyright (C) 2014 - 2018 University Health Network
22   * %%
23   * Licensed under the Apache License, Version 2.0 (the "License");
24   * you may not use this file except in compliance with the License.
25   * You may obtain a copy of the License at
26   * 
27   *      http://www.apache.org/licenses/LICENSE-2.0
28   * 
29   * Unless required by applicable law or agreed to in writing, software
30   * distributed under the License is distributed on an "AS IS" BASIS,
31   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32   * See the License for the specific language governing permissions and
33   * limitations under the License.
34   * #L%
35   */
36  
37  public interface IResourceHistoryTableDao extends JpaRepository<ResourceHistoryTable, Long> {
38  
39  	@Query("SELECT COUNT(*) FROM ResourceHistoryTable t WHERE t.myUpdated >= :cutoff")
40  	int countForAllResourceTypes(
41  		@Temporal(value = TemporalType.TIMESTAMP) @Param("cutoff") Date theCutoff
42  	);
43  
44  	@Query("SELECT COUNT(*) FROM ResourceHistoryTable t")
45  	int countForAllResourceTypes(
46  	);
47  
48  	@Query("SELECT COUNT(*) FROM ResourceHistoryTable t WHERE t.myResourceId = :id AND t.myUpdated >= :cutoff")
49  	int countForResourceInstance(
50  		@Param("id") Long theId,
51  		@Temporal(value = TemporalType.TIMESTAMP) @Param("cutoff") Date theCutoff
52  	);
53  
54  	@Query("SELECT COUNT(*) FROM ResourceHistoryTable t WHERE t.myResourceId = :id")
55  	int countForResourceInstance(
56  		@Param("id") Long theId
57  	);
58  
59  	@Query("SELECT COUNT(*) FROM ResourceHistoryTable t WHERE t.myResourceType = :type AND t.myUpdated >= :cutoff")
60  	int countForResourceType(
61  		@Param("type") String theType,
62  		@Temporal(value = TemporalType.TIMESTAMP) @Param("cutoff") Date theCutoff
63  	);
64  
65  	@Query("SELECT COUNT(*) FROM ResourceHistoryTable t WHERE t.myResourceType = :type")
66  	int countForResourceType(
67  		@Param("type") String theType
68  	);
69  
70  	@Query("SELECT t FROM ResourceHistoryTable t WHERE t.myResourceId = :id AND t.myResourceVersion = :version")
71  	ResourceHistoryTable findForIdAndVersion(@Param("id") long theId, @Param("version") long theVersion);
72  
73  	@Query("SELECT t.myId FROM ResourceHistoryTable t WHERE t.myResourceId = :resId AND t.myResourceVersion != :dontWantVersion")
74  	Slice<Long> findForResourceId(Pageable thePage, @Param("resId") Long theId, @Param("dontWantVersion") Long theDontWantVersion);
75  
76  	@Query("" +
77  		"SELECT v.myId FROM ResourceHistoryTable v " +
78  		"LEFT OUTER JOIN ResourceTable t ON (v.myResourceId = t.myId) " +
79  		"WHERE v.myResourceVersion != t.myVersion AND " +
80  		"t.myResourceType = :restype")
81  	Slice<Long> findIdsOfPreviousVersionsOfResources(Pageable thePage, @Param("restype") String theResourceName);
82  
83  	@Query("" +
84  		"SELECT v.myId FROM ResourceHistoryTable v " +
85  		"LEFT OUTER JOIN ResourceTable t ON (v.myResourceId = t.myId) " +
86  		"WHERE v.myResourceVersion != t.myVersion")
87  	Slice<Long> findIdsOfPreviousVersionsOfResources(Pageable thePage);
88  	
89  	@Query("" + 
90  		"SELECT h FROM ResourceHistoryTable h " + 
91  		"INNER JOIN ResourceTable r ON (r.myId = h.myResourceId and r.myVersion = h.myResourceVersion) " + 
92  		"WHERE r.myId in (:pids)")
93  	Collection<ResourceHistoryTable> findByResourceIds(@Param("pids") Collection<Long> pids);
94  }