Skip to content

Commit 4d1e07c

Browse files
committed
Read only optimalisations
1 parent 7dbc443 commit 4d1e07c

14 files changed

+56
-44
lines changed

server/src/main/java/invite/api/APITokenController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public APITokenController(APITokenRepository apiTokenRepository) {
4444
}
4545

4646
@GetMapping("")
47+
@Transactional(readOnly = true)
4748
public ResponseEntity<List<APIToken>> apiTokensByInstitution(@Parameter(hidden = true) User user) {
4849
LOG.debug(String.format("GET /tokens for user %s", user.getEduPersonPrincipalName()));
4950
UserPermissions.assertAuthority(user, Authority.INVITER);
@@ -54,6 +55,7 @@ public ResponseEntity<List<APIToken>> apiTokensByInstitution(@Parameter(hidden =
5455
}
5556

5657
@GetMapping("generate-token")
58+
@Transactional(readOnly = true)
5759
public ResponseEntity<Map<String, String>> generateToken(@Parameter(hidden = true) User user,
5860
@Parameter(hidden = true) HttpServletRequest request) {
5961
LOG.debug(String.format("GET /tokens/generateToken for user %s", user.getEduPersonPrincipalName()));

server/src/main/java/invite/api/InvitationController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ public ResponseEntity<Map<String, Integer>> resendInvitation(@PathVariable("id")
214214
}
215215

216216
@GetMapping("public")
217+
@Transactional(readOnly = true)
217218
public ResponseEntity<Invitation> getInvitation(@RequestParam("hash") String hash) {
218219
LOG.debug(String.format("getInvitation with hash %s", hash));
219220
Invitation invitation = invitationRepository.findByHash(hash).orElseThrow(() -> new NotFoundException("Invitation not found"));
@@ -225,6 +226,7 @@ public ResponseEntity<Invitation> getInvitation(@RequestParam("hash") String has
225226
}
226227

227228
@GetMapping("all")
229+
@Transactional(readOnly = true)
228230
public ResponseEntity<List<Invitation>> all(@Parameter(hidden = true) User user) {
229231
LOG.debug("GET /all invitations");
230232
UserPermissions.assertAuthority(user, Authority.SUPER_USER);
@@ -420,6 +422,7 @@ public ResponseEntity<List<Invitation>> byRole(@PathVariable("roleId") Long role
420422
}
421423

422424
@GetMapping("search")
425+
@Transactional(readOnly = true)
423426
public ResponseEntity<Page<Map<String, Object>>> search(@Parameter(hidden = true) User user,
424427
@RequestParam(value = "roleId", required = false) Long roleId,
425428
@RequestParam(value = "query", required = false, defaultValue = "") String query,

server/src/main/java/invite/api/ManageController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public ResponseEntity<Map<String, Object>> organizationGUIDValidation(@Parameter
9090
}
9191

9292
@GetMapping("applications")
93+
@Transactional(readOnly = true)
9394
public ResponseEntity<Map<String, List<Map<String, Object>>>> applications(@Parameter(hidden = true) User user) {
9495
LOG.debug(String.format("GET /manage/applications for user %s", user.getEduPersonPrincipalName()));
9596

server/src/main/java/invite/api/Pagination.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Pagination {
1212
private Pagination() {
1313
}
1414

15-
static <T> ResponseEntity<Page<T>> of(Page<Map<String, Object>> page, List<T> content) {
15+
static <T> ResponseEntity<Page<T>> of(Page<?> page, List<T> content) {
1616
return ResponseEntity.ok(new PageImpl<>(content, page.getPageable(), page.getTotalElements()));
1717
}
1818
}

server/src/main/java/invite/api/RoleController.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public RoleController(RoleRepository roleRepository,
8080
}
8181

8282
@GetMapping("")
83+
@Transactional(readOnly = true)
8384
public ResponseEntity<Page<Role>> rolesByApplication(@Parameter(hidden = true) User user,
8485
@RequestParam(value = "force", required = false, defaultValue = "true") boolean force,
8586
@RequestParam(value = "query", required = false, defaultValue = "") String query,
@@ -89,7 +90,7 @@ public ResponseEntity<Page<Role>> rolesByApplication(@Parameter(hidden = true) U
8990
@RequestParam(value = "sortDirection", required = false, defaultValue = "ASC") String sortDirection) {
9091
LOG.debug(String.format("/roles for user %s", user.getEduPersonPrincipalName()));
9192

92-
Page<Map<String, Object>> rolesPage;
93+
Page<Role> rolesPage;
9394
if (user.isSuperUser()) {
9495
if (force) {
9596
Pageable pageable = PageRequest.of(0, Integer.MAX_VALUE);
@@ -106,13 +107,14 @@ public ResponseEntity<Page<Role>> rolesByApplication(@Parameter(hidden = true) U
106107
rolesPage = roleRepository.searchByPageAndOrganizationGUID(user.getOrganizationGUID(), pageable);
107108

108109
}
109-
List<Long> roleIdentifiers = rolesPage.getContent().stream().map(m -> (Long) m.get("id")).toList();
110+
List<Long> roleIdentifiers = rolesPage.getContent().stream().map(role -> role.getId()).toList();
110111
List<Map<String, Object>> applications = roleRepository.findApplications(roleIdentifiers);
111112
List<Role> roles = manage.addManageMetaData(this.roleFromQuery(rolesPage, applications));
112113
return Pagination.of(rolesPage, roles);
113114
}
114115

115116
@GetMapping("{id}")
117+
@Transactional(readOnly = true)
116118
public ResponseEntity<Role> role(@PathVariable("id") Long id, @Parameter(hidden = true) User user) {
117119
LOG.debug(String.format("/role/%s for user %s", id, user.getEduPersonPrincipalName()));
118120

@@ -123,6 +125,7 @@ public ResponseEntity<Role> role(@PathVariable("id") Long id, @Parameter(hidden
123125
}
124126

125127
@GetMapping("/application/{manageId}")
128+
@Transactional(readOnly = true)
126129
public ResponseEntity<List<Role>> rolesPerApplicationId(@PathVariable("manageId") String manageId, @Parameter(hidden = true) User user) {
127130
LOG.debug(String.format("/rolesPerApplicationId for user %s", user.getEduPersonPrincipalName()));
128131

@@ -248,17 +251,8 @@ private ResponseEntity<Role> saveOrUpdate(Role role, User user) {
248251
}
249252

250253
//See RoleRepository#searchByPage
251-
private List<Role> roleFromQuery(Page<Map<String, Object>> rolesPage, List<Map<String, Object>> applications) {
252-
List<Role> roles = rolesPage.getContent().stream().map(m -> new Role(
253-
(Long) m.get("id"),
254-
(String) m.get("name"),
255-
(String) m.get("description"),
256-
(Long) m.get("userRoleCount"),
257-
(Integer) m.get("defaultExpiryDays"),
258-
(boolean) m.get("enforceEmailEquality"),
259-
(boolean) m.get("eduIDOnly"),
260-
(boolean) m.get("overrideSettingsAllowed")
261-
)).toList();
254+
private List<Role> roleFromQuery(Page<Role> rolesPage, List<Map<String, Object>> applications) {
255+
List<Role> roles = rolesPage.getContent();
262256

263257
//Now add all applications, note that we need to preserve ordering of the roles
264258
Map<Long, List<Map<String, Object>>> applicationGroupedByRoleId =

server/src/main/java/invite/api/SystemController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public ResponseEntity<List<UserRole>> expiryUserRoles(@Parameter(hidden = true)
8989
}
9090

9191
@GetMapping("/unknown-roles")
92+
@Transactional(readOnly = true)
9293
public ResponseEntity<List<Role>> unknownRoles(@Parameter(hidden = true) User user) {
9394
LOG.debug("/unknown-roles");
9495

server/src/main/java/invite/api/UserController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public ResponseEntity<Config> config(User user,
108108
}
109109

110110
@GetMapping("me")
111+
@Transactional(readOnly = true)
111112
public ResponseEntity<User> me(@Parameter(hidden = true) User user) {
112113
LOG.debug(String.format("/me for user %s", user.getEduPersonPrincipalName()));
113114
List<Role> roles = user.getUserRoles().stream().map(UserRole::getRole).toList();
@@ -116,6 +117,7 @@ public ResponseEntity<User> me(@Parameter(hidden = true) User user) {
116117
}
117118

118119
@GetMapping("other/{id}")
120+
@Transactional(readOnly = true)
119121
public ResponseEntity<User> details(@PathVariable("id") Long id, @Parameter(hidden = true) User user) {
120122
LOG.debug(String.format("/other/%s for user %s", id, user.getEduPersonPrincipalName()));
121123

@@ -134,6 +136,7 @@ public ResponseEntity<User> details(@PathVariable("id") Long id, @Parameter(hidd
134136
}
135137

136138
@GetMapping("search")
139+
@Transactional(readOnly = true)
137140
public ResponseEntity<Page<Map<String, Object>>> search(@Parameter(hidden = true) User user,
138141
@RequestParam(value = "force", required = false, defaultValue = "true") boolean force,
139142
@RequestParam(value = "query", required = false, defaultValue = "") String query,
@@ -152,6 +155,7 @@ public ResponseEntity<Page<Map<String, Object>>> search(@Parameter(hidden = true
152155
}
153156

154157
@GetMapping("search-by-application")
158+
@Transactional(readOnly = true)
155159
public ResponseEntity<Page<UserRoles>> searchByApplication(@Parameter(hidden = true) User user,
156160
@RequestParam(value = "query", required = false, defaultValue = "") String query,
157161
@RequestParam(value = "pageNumber", required = false, defaultValue = "0") int pageNumber,

server/src/main/java/invite/api/UserRoleController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public UserRoleController(UserRoleRepository userRoleRepository,
7777
}
7878

7979
@GetMapping("roles/{roleId}")
80+
@Transactional(readOnly = true)
8081
public ResponseEntity<List<UserRole>> byRole(@PathVariable("roleId") Long roleId,
8182
@Parameter(hidden = true) User user) {
8283
LOG.debug(String.format("GET user_roles/roles/%s for user %s", roleId, user.getEduPersonPrincipalName()));
@@ -85,6 +86,7 @@ public ResponseEntity<List<UserRole>> byRole(@PathVariable("roleId") Long roleId
8586
}
8687

8788
@GetMapping("managers/{roleId}")
89+
@Transactional(readOnly = true)
8890
public ResponseEntity<List<String>> managersByRole(@PathVariable("roleId") Long roleId,
8991
@Parameter(hidden = true) User user) {
9092
LOG.debug(String.format("GET user_roles/managers/%s for user %s", roleId, user.getEduPersonPrincipalName()));
@@ -95,6 +97,7 @@ public ResponseEntity<List<String>> managersByRole(@PathVariable("roleId") Long
9597
}
9698

9799
@GetMapping("/consequences/{roleId}")
100+
@Transactional(readOnly = true)
98101
public ResponseEntity<List<Map<String, Object>>> consequencesDeleteRole(@PathVariable("roleId") Long roleId,
99102
@Parameter(hidden = true) User user) {
100103
Role role = roleRepository.findById(roleId).orElseThrow(() -> new NotFoundException("Role not found"));
@@ -110,6 +113,7 @@ public ResponseEntity<List<Map<String, Object>>> consequencesDeleteRole(@PathVar
110113
}
111114

112115
@GetMapping("/search/{roleId}/{guests}")
116+
@Transactional(readOnly = true)
113117
public ResponseEntity<Page<?>> searchPaginated(@PathVariable("roleId") Long roleId,
114118
@PathVariable("guests") boolean guests,
115119
@RequestParam(value = "query", required = false, defaultValue = "") String query,
@@ -187,7 +191,6 @@ public ResponseEntity<User> userRoleProvisioning(@Validated @RequestBody UserRol
187191
return ResponseEntity.status(201).body(user);
188192
}
189193

190-
191194
@PutMapping("")
192195
public ResponseEntity<Map<String, Integer>> updateUserRoleExpirationDate(@Validated @RequestBody UpdateUserRole updateUserRole,
193196
@Parameter(hidden = true) User user) {

server/src/main/java/invite/api/UserRoleOperations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.apache.commons.logging.Log;
77
import org.apache.commons.logging.LogFactory;
88
import org.springframework.http.ResponseEntity;
9+
import org.springframework.transaction.annotation.Transactional;
910

1011
import java.util.List;
1112

server/src/main/java/invite/repository/RoleRepository.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ public interface RoleRepository extends JpaRepository<Role, Long>, QueryRewriter
2222
List<Role> search(String keyWord, int limit);
2323

2424
@Query(value = """
25-
SELECT r.id as id, r.name as name, r.description as description,
26-
r.default_expiry_days as defaultExpiryDays,
27-
r.enforce_email_equality as enforceEmailEquality,
28-
r.edu_id_only as eduIDOnly,
29-
r.override_settings_allowed as overrideSettingsAllowed,
25+
SELECT *,
3026
(SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) as userRoleCount
3127
FROM roles r
3228
""",
@@ -35,14 +31,10 @@ SELECT COUNT(r.id) FROM roles r
3531
""",
3632
queryRewriter = RoleRepository.class,
3733
nativeQuery = true)
38-
Page<Map<String, Object>> searchByPage(Pageable pageable);
34+
Page<Role> searchByPage(Pageable pageable);
3935

4036
@Query(value = """
41-
SELECT r.id as id, r.name as name, r.description as description,
42-
r.default_expiry_days as defaultExpiryDays,
43-
r.enforce_email_equality as enforceEmailEquality,
44-
r.edu_id_only as eduIDOnly,
45-
r.override_settings_allowed as overrideSettingsAllowed,
37+
SELECT *,
4638
(SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) as userRoleCount
4739
FROM roles r WHERE MATCH (name, description) against (?1 IN BOOLEAN MODE)
4840
""",
@@ -51,15 +43,11 @@ SELECT COUNT(r.id) FROM roles r WHERE MATCH (name, description) against (?1 IN B
5143
""",
5244
queryRewriter = RoleRepository.class,
5345
nativeQuery = true)
54-
Page<Map<String, Object>> searchByPageWithKeyword(String keyword, Pageable pageable);
46+
Page<Role> searchByPageWithKeyword(String keyword, Pageable pageable);
5547

5648

5749
@Query(value = """
58-
SELECT r.id AS id, r.name AS name, r.description AS description,
59-
r.default_expiry_days as defaultExpiryDays,
60-
r.enforce_email_equality as enforceEmailEquality,
61-
r.edu_id_only as eduIDOnly,
62-
r.override_settings_allowed as overrideSettingsAllowed,
50+
SELECT *,
6351
(SELECT COUNT(*) FROM user_roles ur WHERE ur.role_id=r.id) AS userRoleCount
6452
FROM roles r WHERE r.organization_guid = ?1
6553
""",
@@ -68,7 +56,7 @@ SELECT COUNT(r.id) FROM roles r WHERE r.organization_guid = ?1
6856
""",
6957
queryRewriter = RoleRepository.class,
7058
nativeQuery = true)
71-
Page<Map<String, Object>> searchByPageAndOrganizationGUID(String organizationGUID, Pageable pageable);
59+
Page<Role> searchByPageAndOrganizationGUID(String organizationGUID, Pageable pageable);
7260

7361
@Query(value = """
7462
SELECT r.id AS role_id, a.manage_id AS manage_id, a.manage_type AS manage_type

0 commit comments

Comments
 (0)