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