-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRoleRepository.java
More file actions
109 lines (92 loc) · 4.43 KB
/
RoleRepository.java
File metadata and controls
109 lines (92 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package invite.repository;
import invite.model.Role;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryRewriter;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Repository
public interface RoleRepository extends JpaRepository<Role, Long>, QueryRewriter {
@Modifying
@Query(value = "DELETE FROM roles WHERE id = ?1", nativeQuery = true)
@Transactional(isolation = Isolation.SERIALIZABLE)
void deleteRoleById(Long id);
@Query(value = "SELECT *, (SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) as userRoleCount " +
"FROM roles r WHERE MATCH (name, description) against (?1 IN BOOLEAN MODE) AND id > 0 LIMIT ?2",
nativeQuery = true)
List<Role> search(String keyWord, int limit);
@Query(value = """
SELECT *,
(SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) as userRoleCount
FROM roles r
""",
countQuery = """
SELECT COUNT(r.id) FROM roles r
""",
queryRewriter = RoleRepository.class,
nativeQuery = true)
Page<Role> searchByPage(Pageable pageable);
@Query(value = """
SELECT *,
(SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) as userRoleCount
FROM roles r WHERE MATCH (name, description) against (?1 IN BOOLEAN MODE)
""",
countQuery = """
SELECT COUNT(r.id) FROM roles r WHERE MATCH (name, description) against (?1 IN BOOLEAN MODE)
""",
queryRewriter = RoleRepository.class,
nativeQuery = true)
Page<Role> searchByPageWithKeyword(String keyword, Pageable pageable);
@Query(value = """
SELECT *,
(SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) as userRoleCount
FROM roles r WHERE
UPPER(r.name) LIKE ?1 or UPPER(r.description) LIKE ?1
""",
countQuery = """
SELECT COUNT(r.id) FROM roles r WHERE
UPPER(r.name) LIKE ?1 or UPPER(r.description) LIKE ?1
""",
queryRewriter = RoleRepository.class,
nativeQuery = true)
Page<Role> searchByPageWithStrictSearch(String keyword, Pageable pageable);
@Query(value = """
SELECT *,
(SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) AS userRoleCount
FROM roles r WHERE r.organization_guid = ?1
""",
countQuery = """
SELECT COUNT(r.id) FROM roles r WHERE r.organization_guid = ?1
""",
queryRewriter = RoleRepository.class,
nativeQuery = true)
Page<Role> searchByPageAndOrganizationGUID(String organizationGUID, Pageable pageable);
@Query(value = """
SELECT r.id AS role_id, a.manage_id AS manage_id, a.manage_type AS manage_type
FROM applications a INNER JOIN application_usages au ON au.application_id = a.id
INNER JOIN roles r ON au.role_id = r.id WHERE r.id IN ?1
""",
nativeQuery = true)
List<Map<String, Object>> findApplications(List<Long> roleIdentifiers);
List<Role> findByApplicationUsagesApplicationManageId(String manageId);
List<Role> findByOrganizationGUIDAndApplicationUsagesApplicationManageId(String organizationGUID, String manageId);
List<Role> findByOrganizationGUID(String organizationGUID);
Optional<Role> findByName(String name);
@Override
default String rewrite(String query, Sort sort) {
Sort.Order userRoleCount = sort.getOrderFor("userRoleCount");
if (userRoleCount != null) {
//Spring cannot sort on aggregated columns
return query.replace(" order by r.userRoleCount", " order by userRoleCount");
}
return query;
}
}