Showing
4 changed files
with
137 additions
and
69 deletions
... | ... | @@ -44,14 +44,15 @@ public class AuditLogController extends BaseController { |
44 | 44 | @PathVariable("customerId") String strCustomerId, |
45 | 45 | @RequestParam int pageSize, |
46 | 46 | @RequestParam int page, |
47 | + @RequestParam(required = false) String textSearch, | |
48 | + @RequestParam(required = false) String sortProperty, | |
49 | + @RequestParam(required = false) String sortOrder, | |
47 | 50 | @RequestParam(required = false) Long startTime, |
48 | - @RequestParam(required = false) Long endTime, | |
49 | - @RequestParam(required = false, defaultValue = "false") boolean ascOrder) throws ThingsboardException { | |
51 | + @RequestParam(required = false) Long endTime) throws ThingsboardException { | |
50 | 52 | try { |
51 | 53 | checkParameter("CustomerId", strCustomerId); |
52 | 54 | TenantId tenantId = getCurrentUser().getTenantId(); |
53 | - TimePageLink pageLink = createTimePageLink(pageSize, page, "", | |
54 | - "createdTime", ascOrder ? "asc" : "desc", startTime, endTime); | |
55 | + TimePageLink pageLink = createTimePageLink(pageSize, page, textSearch, sortProperty, sortOrder, startTime, endTime); | |
55 | 56 | return checkNotNull(auditLogService.findAuditLogsByTenantIdAndCustomerId(tenantId, new CustomerId(UUID.fromString(strCustomerId)), pageLink)); |
56 | 57 | } catch (Exception e) { |
57 | 58 | throw handleException(e); |
... | ... | @@ -65,15 +66,15 @@ public class AuditLogController extends BaseController { |
65 | 66 | @PathVariable("userId") String strUserId, |
66 | 67 | @RequestParam int pageSize, |
67 | 68 | @RequestParam int page, |
69 | + @RequestParam(required = false) String textSearch, | |
70 | + @RequestParam(required = false) String sortProperty, | |
71 | + @RequestParam(required = false) String sortOrder, | |
68 | 72 | @RequestParam(required = false) Long startTime, |
69 | - @RequestParam(required = false) Long endTime, | |
70 | - @RequestParam(required = false, defaultValue = "false") boolean ascOrder) throws ThingsboardException { | |
73 | + @RequestParam(required = false) Long endTime) throws ThingsboardException { | |
71 | 74 | try { |
72 | 75 | checkParameter("UserId", strUserId); |
73 | 76 | TenantId tenantId = getCurrentUser().getTenantId(); |
74 | - TimePageLink pageLink = createTimePageLink(pageSize, page, "", | |
75 | - "createdTime", ascOrder ? "asc" : "desc", startTime, endTime); | |
76 | - return checkNotNull(auditLogService.findAuditLogsByTenantIdAndUserId(tenantId, new UserId(UUID.fromString(strUserId)), pageLink)); | |
77 | + TimePageLink pageLink = createTimePageLink(pageSize, page, textSearch, sortProperty, sortOrder, startTime, endTime); return checkNotNull(auditLogService.findAuditLogsByTenantIdAndUserId(tenantId, new UserId(UUID.fromString(strUserId)), pageLink)); | |
77 | 78 | } catch (Exception e) { |
78 | 79 | throw handleException(e); |
79 | 80 | } |
... | ... | @@ -87,16 +88,16 @@ public class AuditLogController extends BaseController { |
87 | 88 | @PathVariable("entityId") String strEntityId, |
88 | 89 | @RequestParam int pageSize, |
89 | 90 | @RequestParam int page, |
91 | + @RequestParam(required = false) String textSearch, | |
92 | + @RequestParam(required = false) String sortProperty, | |
93 | + @RequestParam(required = false) String sortOrder, | |
90 | 94 | @RequestParam(required = false) Long startTime, |
91 | - @RequestParam(required = false) Long endTime, | |
92 | - @RequestParam(required = false, defaultValue = "false") boolean ascOrder) throws ThingsboardException { | |
95 | + @RequestParam(required = false) Long endTime) throws ThingsboardException { | |
93 | 96 | try { |
94 | 97 | checkParameter("EntityId", strEntityId); |
95 | 98 | checkParameter("EntityType", strEntityType); |
96 | 99 | TenantId tenantId = getCurrentUser().getTenantId(); |
97 | - TimePageLink pageLink = createTimePageLink(pageSize, page, "", | |
98 | - "createdTime", ascOrder ? "asc" : "desc", startTime, endTime); | |
99 | - return checkNotNull(auditLogService.findAuditLogsByTenantIdAndEntityId(tenantId, EntityIdFactory.getByTypeAndId(strEntityType, strEntityId), pageLink)); | |
100 | + TimePageLink pageLink = createTimePageLink(pageSize, page, textSearch, sortProperty, sortOrder, startTime, endTime); return checkNotNull(auditLogService.findAuditLogsByTenantIdAndEntityId(tenantId, EntityIdFactory.getByTypeAndId(strEntityType, strEntityId), pageLink)); | |
100 | 101 | } catch (Exception e) { |
101 | 102 | throw handleException(e); |
102 | 103 | } |
... | ... | @@ -108,13 +109,14 @@ public class AuditLogController extends BaseController { |
108 | 109 | public PageData<AuditLog> getAuditLogs( |
109 | 110 | @RequestParam int pageSize, |
110 | 111 | @RequestParam int page, |
112 | + @RequestParam(required = false) String textSearch, | |
113 | + @RequestParam(required = false) String sortProperty, | |
114 | + @RequestParam(required = false) String sortOrder, | |
111 | 115 | @RequestParam(required = false) Long startTime, |
112 | - @RequestParam(required = false) Long endTime, | |
113 | - @RequestParam(required = false, defaultValue = "false") boolean ascOrder) throws ThingsboardException { | |
116 | + @RequestParam(required = false) Long endTime) throws ThingsboardException { | |
114 | 117 | try { |
115 | 118 | TenantId tenantId = getCurrentUser().getTenantId(); |
116 | - TimePageLink pageLink = createTimePageLink(pageSize, page, "", | |
117 | - "createdTime", ascOrder ? "asc" : "desc", startTime, endTime); | |
119 | + TimePageLink pageLink = createTimePageLink(pageSize, page, textSearch, sortProperty, sortOrder, startTime, endTime); | |
118 | 120 | return checkNotNull(auditLogService.findAuditLogsByTenantId(tenantId, pageLink)); |
119 | 121 | } catch (Exception e) { |
120 | 122 | throw handleException(e); | ... | ... |
... | ... | @@ -15,10 +15,84 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao.sql.audit; |
17 | 17 | |
18 | -import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | |
19 | -import org.springframework.data.repository.CrudRepository; | |
18 | +import org.springframework.data.domain.Page; | |
19 | +import org.springframework.data.domain.Pageable; | |
20 | +import org.springframework.data.jpa.repository.Query; | |
21 | +import org.springframework.data.repository.PagingAndSortingRepository; | |
22 | +import org.springframework.data.repository.query.Param; | |
23 | +import org.thingsboard.server.common.data.EntityType; | |
20 | 24 | import org.thingsboard.server.dao.model.sql.AuditLogEntity; |
21 | 25 | |
22 | -public interface AuditLogRepository extends CrudRepository<AuditLogEntity, String>, JpaSpecificationExecutor<AuditLogEntity> { | |
26 | +public interface AuditLogRepository extends PagingAndSortingRepository<AuditLogEntity, String> { | |
27 | + | |
28 | + @Query("SELECT a FROM AuditLogEntity a WHERE " + | |
29 | + "a.tenantId = :tenantId " + | |
30 | + "AND (:startId IS NULL OR a.id >= :startId) " + | |
31 | + "AND (:endId IS NULL OR a.id <= :endId) " + | |
32 | + "AND (LOWER(a.entityType) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
33 | + "OR LOWER(a.entityName) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
34 | + "OR LOWER(a.userName) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
35 | + "OR LOWER(a.actionType) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
36 | + "OR LOWER(a.actionStatus) LIKE LOWER(CONCAT(:textSearch, '%')))" | |
37 | + ) | |
38 | + Page<AuditLogEntity> findByTenantId( | |
39 | + @Param("tenantId") String tenantId, | |
40 | + @Param("textSearch") String textSearch, | |
41 | + @Param("startId") String startId, | |
42 | + @Param("endId") String endId, | |
43 | + Pageable pageable); | |
44 | + | |
45 | + @Query("SELECT a FROM AuditLogEntity a WHERE " + | |
46 | + "a.tenantId = :tenantId " + | |
47 | + "AND a.entityType = :entityType AND a.entityId = :entityId " + | |
48 | + "AND (:startId IS NULL OR a.id >= :startId) " + | |
49 | + "AND (:endId IS NULL OR a.id <= :endId) " + | |
50 | + "AND (LOWER(a.entityName) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
51 | + "OR LOWER(a.userName) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
52 | + "OR LOWER(a.actionType) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
53 | + "OR LOWER(a.actionStatus) LIKE LOWER(CONCAT(:textSearch, '%')))" | |
54 | + ) | |
55 | + Page<AuditLogEntity> findAuditLogsByTenantIdAndEntityId(@Param("tenantId") String tenantId, | |
56 | + @Param("entityType") EntityType entityType, | |
57 | + @Param("entityId") String entityId, | |
58 | + @Param("textSearch") String textSearch, | |
59 | + @Param("startId") String startId, | |
60 | + @Param("endId") String endId, | |
61 | + Pageable pageable); | |
62 | + | |
63 | + @Query("SELECT a FROM AuditLogEntity a WHERE " + | |
64 | + "a.tenantId = :tenantId " + | |
65 | + "AND a.customerId = :customerId " + | |
66 | + "AND (:startId IS NULL OR a.id >= :startId) " + | |
67 | + "AND (:endId IS NULL OR a.id <= :endId) " + | |
68 | + "AND (LOWER(a.entityType) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
69 | + "OR LOWER(a.entityName) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
70 | + "OR LOWER(a.userName) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
71 | + "OR LOWER(a.actionType) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
72 | + "OR LOWER(a.actionStatus) LIKE LOWER(CONCAT(:textSearch, '%')))" | |
73 | + ) | |
74 | + Page<AuditLogEntity> findAuditLogsByTenantIdAndCustomerId(@Param("tenantId") String tenantId, | |
75 | + @Param("customerId") String customerId, | |
76 | + @Param("textSearch") String textSearch, | |
77 | + @Param("startId") String startId, | |
78 | + @Param("endId") String endId, | |
79 | + Pageable pageable); | |
80 | + | |
81 | + @Query("SELECT a FROM AuditLogEntity a WHERE " + | |
82 | + "a.tenantId = :tenantId " + | |
83 | + "AND a.userId = :userId " + | |
84 | + "AND (:startId IS NULL OR a.id >= :startId) " + | |
85 | + "AND (:endId IS NULL OR a.id <= :endId) " + | |
86 | + "AND (LOWER(a.entityType) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
87 | + "OR LOWER(a.entityName) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
88 | + "OR LOWER(a.actionType) LIKE LOWER(CONCAT(:textSearch, '%'))" + | |
89 | + "OR LOWER(a.actionStatus) LIKE LOWER(CONCAT(:textSearch, '%')))" | |
90 | + ) | |
91 | + Page<AuditLogEntity> findAuditLogsByTenantIdAndUserId(@Param("tenantId") String tenantId, | |
92 | + @Param("userId") String userId, | |
93 | + @Param("textSearch") String textSearch, | |
94 | + @Param("startId") String startId, | |
95 | + @Param("endId") String endId, | |
96 | + Pageable pageable); | |
23 | 97 | |
24 | 98 | } | ... | ... |
... | ... | @@ -19,13 +19,8 @@ import com.google.common.util.concurrent.ListenableFuture; |
19 | 19 | import com.google.common.util.concurrent.ListeningExecutorService; |
20 | 20 | import com.google.common.util.concurrent.MoreExecutors; |
21 | 21 | import org.springframework.beans.factory.annotation.Autowired; |
22 | -import org.springframework.data.domain.PageRequest; | |
23 | -import org.springframework.data.domain.Pageable; | |
24 | -import org.springframework.data.domain.Sort; | |
25 | -import org.springframework.data.jpa.domain.Specification; | |
26 | 22 | import org.springframework.data.repository.CrudRepository; |
27 | 23 | import org.springframework.stereotype.Component; |
28 | -import org.thingsboard.server.common.data.UUIDConverter; | |
29 | 24 | import org.thingsboard.server.common.data.audit.AuditLog; |
30 | 25 | import org.thingsboard.server.common.data.id.CustomerId; |
31 | 26 | import org.thingsboard.server.common.data.id.EntityId; |
... | ... | @@ -36,18 +31,16 @@ import org.thingsboard.server.dao.DaoUtil; |
36 | 31 | import org.thingsboard.server.dao.audit.AuditLogDao; |
37 | 32 | import org.thingsboard.server.dao.model.sql.AuditLogEntity; |
38 | 33 | import org.thingsboard.server.dao.sql.JpaAbstractDao; |
39 | -import org.thingsboard.server.dao.sql.JpaAbstractSearchTimeDao; | |
40 | 34 | import org.thingsboard.server.dao.util.SqlDao; |
41 | 35 | |
42 | 36 | import javax.annotation.PreDestroy; |
43 | -import javax.persistence.criteria.Predicate; | |
44 | -import java.util.ArrayList; | |
45 | -import java.util.List; | |
37 | +import java.util.Objects; | |
46 | 38 | import java.util.UUID; |
47 | 39 | import java.util.concurrent.Executors; |
48 | 40 | |
49 | -import static org.springframework.data.jpa.domain.Specifications.where; | |
50 | -import static org.thingsboard.server.dao.model.ModelConstants.ID_PROPERTY; | |
41 | +import static org.thingsboard.server.common.data.UUIDConverter.fromTimeUUID; | |
42 | +import static org.thingsboard.server.dao.DaoUtil.endTimeToId; | |
43 | +import static org.thingsboard.server.dao.DaoUtil.startTimeToId; | |
51 | 44 | |
52 | 45 | @Component |
53 | 46 | @SqlDao |
... | ... | @@ -83,53 +76,52 @@ public class JpaAuditLogDao extends JpaAbstractDao<AuditLogEntity, AuditLog> imp |
83 | 76 | |
84 | 77 | @Override |
85 | 78 | public PageData<AuditLog> findAuditLogsByTenantIdAndEntityId(UUID tenantId, EntityId entityId, TimePageLink pageLink) { |
86 | - return findAuditLogs(tenantId, entityId, null, null, pageLink); | |
79 | + return DaoUtil.toPageData( | |
80 | + auditLogRepository | |
81 | + .findAuditLogsByTenantIdAndEntityId( | |
82 | + fromTimeUUID(tenantId), | |
83 | + entityId.getEntityType(), | |
84 | + fromTimeUUID(entityId.getId()), | |
85 | + Objects.toString(pageLink.getTextSearch(), ""), | |
86 | + startTimeToId(pageLink.getStartTime()), | |
87 | + endTimeToId(pageLink.getEndTime()), | |
88 | + DaoUtil.toPageable(pageLink))); | |
87 | 89 | } |
88 | 90 | |
89 | 91 | @Override |
90 | 92 | public PageData<AuditLog> findAuditLogsByTenantIdAndCustomerId(UUID tenantId, CustomerId customerId, TimePageLink pageLink) { |
91 | - return findAuditLogs(tenantId, null, customerId, null, pageLink); | |
93 | + return DaoUtil.toPageData( | |
94 | + auditLogRepository | |
95 | + .findAuditLogsByTenantIdAndCustomerId( | |
96 | + fromTimeUUID(tenantId), | |
97 | + fromTimeUUID(customerId.getId()), | |
98 | + Objects.toString(pageLink.getTextSearch(), ""), | |
99 | + startTimeToId(pageLink.getStartTime()), | |
100 | + endTimeToId(pageLink.getEndTime()), | |
101 | + DaoUtil.toPageable(pageLink))); | |
92 | 102 | } |
93 | 103 | |
94 | 104 | @Override |
95 | 105 | public PageData<AuditLog> findAuditLogsByTenantIdAndUserId(UUID tenantId, UserId userId, TimePageLink pageLink) { |
96 | - return findAuditLogs(tenantId, null, null, userId, pageLink); | |
106 | + return DaoUtil.toPageData( | |
107 | + auditLogRepository | |
108 | + .findAuditLogsByTenantIdAndUserId( | |
109 | + fromTimeUUID(tenantId), | |
110 | + fromTimeUUID(userId.getId()), | |
111 | + Objects.toString(pageLink.getTextSearch(), ""), | |
112 | + startTimeToId(pageLink.getStartTime()), | |
113 | + endTimeToId(pageLink.getEndTime()), | |
114 | + DaoUtil.toPageable(pageLink))); | |
97 | 115 | } |
98 | 116 | |
99 | 117 | @Override |
100 | 118 | public PageData<AuditLog> findAuditLogsByTenantId(UUID tenantId, TimePageLink pageLink) { |
101 | - return findAuditLogs(tenantId, null, null, null, pageLink); | |
102 | - } | |
103 | - | |
104 | - private PageData<AuditLog> findAuditLogs(UUID tenantId, EntityId entityId, CustomerId customerId, UserId userId, TimePageLink pageLink) { | |
105 | - Specification<AuditLogEntity> timeSearchSpec = JpaAbstractSearchTimeDao.getTimeSearchPageSpec(pageLink, "id"); | |
106 | - Specification<AuditLogEntity> fieldsSpec = getEntityFieldsSpec(tenantId, entityId, customerId, userId); | |
107 | - Pageable pageable = DaoUtil.toPageable(pageLink); | |
108 | - return DaoUtil.toPageData(auditLogRepository.findAll(where(timeSearchSpec).and(fieldsSpec), pageable)); | |
109 | - } | |
110 | - | |
111 | - private Specification<AuditLogEntity> getEntityFieldsSpec(UUID tenantId, EntityId entityId, CustomerId customerId, UserId userId) { | |
112 | - return (root, criteriaQuery, criteriaBuilder) -> { | |
113 | - List<Predicate> predicates = new ArrayList<>(); | |
114 | - if (tenantId != null) { | |
115 | - Predicate tenantIdPredicate = criteriaBuilder.equal(root.get("tenantId"), UUIDConverter.fromTimeUUID(tenantId)); | |
116 | - predicates.add(tenantIdPredicate); | |
117 | - } | |
118 | - if (entityId != null) { | |
119 | - Predicate entityTypePredicate = criteriaBuilder.equal(root.get("entityType"), entityId.getEntityType()); | |
120 | - predicates.add(entityTypePredicate); | |
121 | - Predicate entityIdPredicate = criteriaBuilder.equal(root.get("entityId"), UUIDConverter.fromTimeUUID(entityId.getId())); | |
122 | - predicates.add(entityIdPredicate); | |
123 | - } | |
124 | - if (customerId != null) { | |
125 | - Predicate tenantIdPredicate = criteriaBuilder.equal(root.get("customerId"), UUIDConverter.fromTimeUUID(customerId.getId())); | |
126 | - predicates.add(tenantIdPredicate); | |
127 | - } | |
128 | - if (userId != null) { | |
129 | - Predicate tenantIdPredicate = criteriaBuilder.equal(root.get("userId"), UUIDConverter.fromTimeUUID(userId.getId())); | |
130 | - predicates.add(tenantIdPredicate); | |
131 | - } | |
132 | - return criteriaBuilder.and(predicates.toArray(new Predicate[]{})); | |
133 | - }; | |
119 | + return DaoUtil.toPageData( | |
120 | + auditLogRepository.findByTenantId( | |
121 | + fromTimeUUID(tenantId), | |
122 | + Objects.toString(pageLink.getTextSearch(), ""), | |
123 | + startTimeToId(pageLink.getStartTime()), | |
124 | + endTimeToId(pageLink.getEndTime()), | |
125 | + DaoUtil.toPageable(pageLink))); | |
134 | 126 | } |
135 | 127 | } | ... | ... |