From 684ef4a65883cc1ea1eee91a5c7f2e0603460f24 Mon Sep 17 00:00:00 2001 From: ViacheslavKlimov Date: Fri, 11 Nov 2022 14:06:09 +0200 Subject: [PATCH] Add ability to update notification request --- .../controller/NotificationController.java | 69 ++++++++++++------- .../DefaultNotificationManager.java | 9 ++- ...aultNotificationRuleProcessingService.java | 25 +++++-- .../DefaultNotificationCommandsHandler.java | 19 +++-- .../sub/NotificationRequestUpdate.java | 1 + .../notification/NotificationApiTest.java | 6 +- .../NotificationRequestService.java | 3 + .../dao/notification/NotificationService.java | 2 +- .../notification/NotificationRequest.java | 15 +++- .../notification/NotificationRequestInfo.java | 35 ++++++++++ .../DefaultNotificationRequestService.java | 6 ++ .../DefaultNotificationService.java | 13 +--- .../dao/notification/NotificationDao.java | 2 +- .../notification/NotificationRequestDao.java | 4 ++ .../sql/notification/JpaNotificationDao.java | 4 +- .../JpaNotificationRequestDao.java | 19 +++++ .../notification/NotificationRepository.java | 17 ++++- .../rule/engine/api/NotificationManager.java | 2 +- .../rule/engine/profile/DeviceState.java | 1 - 19 files changed, 187 insertions(+), 65 deletions(-) create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequestInfo.java diff --git a/application/src/main/java/org/thingsboard/server/controller/NotificationController.java b/application/src/main/java/org/thingsboard/server/controller/NotificationController.java index a392e6ec74..40d3c47fff 100644 --- a/application/src/main/java/org/thingsboard/server/controller/NotificationController.java +++ b/application/src/main/java/org/thingsboard/server/controller/NotificationController.java @@ -37,6 +37,7 @@ import org.thingsboard.server.common.data.id.NotificationRequestId; import org.thingsboard.server.common.data.notification.Notification; import org.thingsboard.server.common.data.notification.NotificationOriginatorType; import org.thingsboard.server.common.data.notification.NotificationRequest; +import org.thingsboard.server.common.data.notification.NotificationRequestInfo; import org.thingsboard.server.common.data.notification.NotificationSeverity; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -88,34 +89,43 @@ public class NotificationController extends BaseController { @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") public NotificationRequest createNotificationRequest(@RequestBody NotificationRequest notificationRequest, @AuthenticationPrincipal SecurityUser user) throws ThingsboardException { - accessControlService.checkPermission(user, Resource.NOTIFICATION_REQUEST, Operation.CREATE, null, notificationRequest); // todo: check permission for notification target - if (notificationRequest.getId() != null) { - // TODO: think about notification request update - throw new IllegalArgumentException("Notification request cannot be changed. You can delete it and create a new one"); - } - notificationRequest.setOriginatorType(NotificationOriginatorType.USER); - notificationRequest.setOriginatorEntityId(user.getId()); - if (StringUtils.isBlank(notificationRequest.getNotificationReason())) { - notificationRequest.setNotificationReason(NotificationRequest.GENERAL_NOTIFICATION_REASON); - } - if (notificationRequest.getNotificationSeverity() == null) { - notificationRequest.setNotificationSeverity(NotificationSeverity.NORMAL); - } - if (notificationRequest.getNotificationInfo() != null && notificationRequest.getNotificationInfo().getOriginatorType() != null) { - throw new IllegalArgumentException("Unsupported notification info type"); - } - notificationRequest.setRuleId(null); - notificationRequest.setStatus(null); - try { - NotificationRequest savedNotificationRequest = notificationManager.processNotificationRequest(user.getTenantId(), notificationRequest); - logEntityAction(user, EntityType.NOTIFICATION_REQUEST, savedNotificationRequest, ActionType.ADDED); - return savedNotificationRequest; - } catch (Exception e) { - logEntityAction(user, EntityType.NOTIFICATION_REQUEST, notificationRequest, null, ActionType.ADDED, e); - throw e; + if (notificationRequest.getId() == null) { + accessControlService.checkPermission(user, Resource.NOTIFICATION_REQUEST, Operation.CREATE, null, notificationRequest); + notificationRequest.setOriginatorType(NotificationOriginatorType.USER); + notificationRequest.setOriginatorEntityId(user.getId()); + if (StringUtils.isBlank(notificationRequest.getNotificationReason())) { + notificationRequest.setNotificationReason("General"); + } + if (notificationRequest.getNotificationSeverity() == null) { + notificationRequest.setNotificationSeverity(NotificationSeverity.NORMAL); + } + if (notificationRequest.getNotificationInfo() != null && notificationRequest.getNotificationInfo().getOriginatorType() != null) { + throw new IllegalArgumentException("Unsupported notification info type"); + } + notificationRequest.setRuleId(null); + notificationRequest.setStatus(null); + return notificationManager.processNotificationRequest(user.getTenantId(), notificationRequest); + } else { + NotificationRequest existingNotificationRequest = notificationRequestService.findNotificationRequestById(user.getTenantId(), notificationRequest.getId()); + checkNotNull(existingNotificationRequest); + accessControlService.checkPermission(user, Resource.NOTIFICATION_REQUEST, Operation.WRITE, notificationRequest.getId(), notificationRequest); + + existingNotificationRequest.setNotificationReason(notificationRequest.getNotificationReason()); + existingNotificationRequest.setTextTemplate(notificationRequest.getTextTemplate()); + existingNotificationRequest.setNotificationSeverity(notificationRequest.getNotificationSeverity()); + return notificationManager.updateNotificationRequest(user.getTenantId(), existingNotificationRequest); } +// +// try { +// NotificationRequest savedNotificationRequest = ; +// logEntityAction(user, EntityType.NOTIFICATION_REQUEST, savedNotificationRequest, ActionType.ADDED); +// return savedNotificationRequest; +// } catch (Exception e) { +// logEntityAction(user, EntityType.NOTIFICATION_REQUEST, notificationRequest, null, ActionType.ADDED, e); +// throw e; +// } } @GetMapping("/notification/request/{id}") @@ -126,6 +136,15 @@ public class NotificationController extends BaseController { return notificationRequestService.findNotificationRequestById(user.getTenantId(), notificationRequestId); } + @GetMapping("/notification/request/info/{id}") + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") + public NotificationRequestInfo getNotificationRequestInfoById(@PathVariable UUID id, + @AuthenticationPrincipal SecurityUser user) { + // fixme: permission checks + NotificationRequestId notificationRequestId = new NotificationRequestId(id); + return notificationRequestService.getNotificationRequestInfoById(user.getTenantId(), notificationRequestId); + } + @GetMapping("/notification/requests") @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") public PageData getNotificationRequests(@RequestParam int pageSize, diff --git a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationManager.java b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationManager.java index 46d9604577..73926c70be 100644 --- a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationManager.java +++ b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationManager.java @@ -153,15 +153,18 @@ public class DefaultNotificationManager extends AbstractSubscriptionService impl } @Override - public void updateNotificationRequest(TenantId tenantId, NotificationRequest notificationRequest) { + public NotificationRequest updateNotificationRequest(TenantId tenantId, NotificationRequest notificationRequest) { log.debug("Updating notification request {}", notificationRequest.getId()); - notificationRequestService.saveNotificationRequest(tenantId, notificationRequest); - notificationService.updateNotificationsInfosByRequestId(tenantId, notificationRequest.getId(), notificationRequest.getNotificationInfo()); + notificationRequest = notificationRequestService.saveNotificationRequest(tenantId, notificationRequest); + notificationService.updateNotificationsByRequestId(tenantId, notificationRequest.getId(), + notificationRequest.getNotificationReason(), notificationRequest.getNotificationInfo()); onNotificationRequestUpdate(tenantId, NotificationRequestUpdate.builder() .notificationRequestId(notificationRequest.getId()) + .notificationReason(notificationRequest.getNotificationReason()) .notificationInfo(notificationRequest.getNotificationInfo()) .deleted(false) .build()); + return notificationRequest; } private Notification createNotification(User recipient, NotificationRequest notificationRequest) { diff --git a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationRuleProcessingService.java b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationRuleProcessingService.java index b103eb28f8..a0e1dc1bb1 100644 --- a/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationRuleProcessingService.java +++ b/application/src/main/java/org/thingsboard/server/service/notification/DefaultNotificationRuleProcessingService.java @@ -18,8 +18,10 @@ package org.thingsboard.server.service.notification; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.thingsboard.rule.engine.api.NotificationManager; +import org.thingsboard.rule.engine.api.util.TbNodeUtils; import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.NotificationTargetId; @@ -38,10 +40,12 @@ import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.executors.DbCallbackExecutorService; import java.util.List; +import java.util.Map; @Service @TbCoreComponent @RequiredArgsConstructor +@Slf4j public class DefaultNotificationRuleProcessingService implements NotificationRuleProcessingService { private final NotificationRuleService notificationRuleService; @@ -78,6 +82,7 @@ public class DefaultNotificationRuleProcessingService implements NotificationRul // todo: think about: what if notification rule was updated? private void onAlarmUpdate(TenantId tenantId, NotificationRuleId notificationRuleId, Alarm alarm, boolean deleted) { + log.debug("Processing alarm update ({}) with notification rule {}", alarm.getId(), notificationRuleId); List notificationRequests = notificationRequestService.findNotificationRequestsByRuleIdAndOriginatorEntityId(tenantId, notificationRuleId, alarm.getId()); NotificationRule notificationRule = notificationRuleService.findNotificationRuleById(tenantId, notificationRuleId); if (notificationRule == null) return; @@ -101,7 +106,7 @@ public class DefaultNotificationRuleProcessingService implements NotificationRul } } } else { - NotificationInfo newNotificationInfo = constructNotificationInfo(alarm, notificationRule); + NotificationInfo newNotificationInfo = constructNotificationInfo(alarm); for (NotificationRequest notificationRequest : notificationRequests) { NotificationInfo previousNotificationInfo = notificationRequest.getNotificationInfo(); if (!previousNotificationInfo.equals(newNotificationInfo)) { @@ -121,13 +126,13 @@ public class DefaultNotificationRuleProcessingService implements NotificationRul if (delayInSec > 0) { config.setSendingDelayInSec(delayInSec); } - NotificationInfo notificationInfo = constructNotificationInfo(alarm, notificationRule); + NotificationInfo notificationInfo = constructNotificationInfo(alarm); NotificationRequest notificationRequest = NotificationRequest.builder() .tenantId(tenantId) .targetId(targetId) .notificationReason("Alarm") - .textTemplate(notificationRule.getNotificationTextTemplate()) // todo: format with alarm vars + .textTemplate(formatNotificationTextTemplate(notificationRule.getNotificationTextTemplate(), alarm)) .notificationInfo(notificationInfo) .notificationSeverity(NotificationSeverity.NORMAL) // todo: from alarm severity .originatorType(NotificationOriginatorType.ALARM) @@ -138,7 +143,19 @@ public class DefaultNotificationRuleProcessingService implements NotificationRul notificationManager.processNotificationRequest(tenantId, notificationRequest); } - private NotificationInfo constructNotificationInfo(Alarm alarm, NotificationRule notificationRule) { + private String formatNotificationTextTemplate(String textTemplate, Alarm alarm) { + Map context = Map.of( // fixme: notification text is not updatable + "alarmType", alarm.getType(), + "alarmId", alarm.getId().toString(), + "alarmOriginatorEntityType", alarm.getOriginator().getEntityType().toString(), + "alarmOriginatorId", alarm.getOriginator().getId().toString(), + "alarmSeverity", alarm.getSeverity().toString(), + "alarmStatus", alarm.getStatus().toString() + ); + return TbNodeUtils.processTemplate(textTemplate, context); + } + + private NotificationInfo constructNotificationInfo(Alarm alarm) { return AlarmOriginatedNotificationInfo.builder() .alarmId(alarm.getId()) .alarmType(alarm.getType()) diff --git a/application/src/main/java/org/thingsboard/server/service/ws/notification/DefaultNotificationCommandsHandler.java b/application/src/main/java/org/thingsboard/server/service/ws/notification/DefaultNotificationCommandsHandler.java index 5dd8bd7096..e86fd58161 100644 --- a/application/src/main/java/org/thingsboard/server/service/ws/notification/DefaultNotificationCommandsHandler.java +++ b/application/src/main/java/org/thingsboard/server/service/ws/notification/DefaultNotificationCommandsHandler.java @@ -20,30 +20,29 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; +import org.thingsboard.rule.engine.api.NotificationManager; import org.thingsboard.server.common.data.id.IdBased; import org.thingsboard.server.common.data.id.NotificationId; import org.thingsboard.server.common.data.id.NotificationRequestId; import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.notification.Notification; -import org.thingsboard.server.common.data.notification.NotificationInfo; import org.thingsboard.server.common.data.notification.NotificationStatus; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.dao.notification.NotificationService; import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; import org.thingsboard.server.queue.util.TbCoreComponent; -import org.thingsboard.rule.engine.api.NotificationManager; import org.thingsboard.server.service.security.model.SecurityUser; -import org.thingsboard.server.service.ws.notification.cmd.NotificationsCountSubCmd; -import org.thingsboard.server.service.ws.notification.sub.NotificationRequestUpdate; -import org.thingsboard.server.service.ws.notification.sub.NotificationUpdate; -import org.thingsboard.server.service.ws.notification.sub.NotificationsSubscription; import org.thingsboard.server.service.subscription.TbLocalSubscriptionService; +import org.thingsboard.server.service.ws.WebSocketService; import org.thingsboard.server.service.ws.WebSocketSessionRef; import org.thingsboard.server.service.ws.notification.cmd.MarkNotificationsAsReadCmd; +import org.thingsboard.server.service.ws.notification.cmd.NotificationsCountSubCmd; import org.thingsboard.server.service.ws.notification.cmd.NotificationsSubCmd; -import org.thingsboard.server.service.ws.notification.sub.NotificationsSubscriptionUpdate; +import org.thingsboard.server.service.ws.notification.sub.NotificationRequestUpdate; +import org.thingsboard.server.service.ws.notification.sub.NotificationUpdate; import org.thingsboard.server.service.ws.notification.sub.NotificationsCountSubscription; -import org.thingsboard.server.service.ws.WebSocketService; +import org.thingsboard.server.service.ws.notification.sub.NotificationsSubscription; +import org.thingsboard.server.service.ws.notification.sub.NotificationsSubscriptionUpdate; import org.thingsboard.server.service.ws.telemetry.cmd.v2.CmdUpdate; import org.thingsboard.server.service.ws.telemetry.cmd.v2.UnsubscribeCmd; @@ -168,11 +167,11 @@ public class DefaultNotificationCommandsHandler implements NotificationCommandsH sendUpdate(subscription.getSessionId(), subscription.createFullUpdate()); } } else { - NotificationInfo notificationInfo = update.getNotificationInfo(); subscription.getLatestUnreadNotifications().values().stream() .filter(notification -> notification.getRequestId().equals(notificationRequestId)) .forEach(notification -> { - notification.setInfo(notificationInfo); + notification.setReason(update.getNotificationReason()); + notification.setInfo(update.getNotificationInfo()); sendUpdate(subscription.getSessionId(), subscription.createPartialUpdate(notification)); }); } diff --git a/application/src/main/java/org/thingsboard/server/service/ws/notification/sub/NotificationRequestUpdate.java b/application/src/main/java/org/thingsboard/server/service/ws/notification/sub/NotificationRequestUpdate.java index 36c3b2a441..05642743ee 100644 --- a/application/src/main/java/org/thingsboard/server/service/ws/notification/sub/NotificationRequestUpdate.java +++ b/application/src/main/java/org/thingsboard/server/service/ws/notification/sub/NotificationRequestUpdate.java @@ -28,6 +28,7 @@ import org.thingsboard.server.common.data.notification.NotificationInfo; @Builder public class NotificationRequestUpdate { private NotificationRequestId notificationRequestId; + private String notificationReason; private NotificationInfo notificationInfo; private boolean deleted; } diff --git a/application/src/test/java/org/thingsboard/server/service/notification/NotificationApiTest.java b/application/src/test/java/org/thingsboard/server/service/notification/NotificationApiTest.java index 3d7e55757b..cce7ff749e 100644 --- a/application/src/test/java/org/thingsboard/server/service/notification/NotificationApiTest.java +++ b/application/src/test/java/org/thingsboard/server/service/notification/NotificationApiTest.java @@ -247,7 +247,7 @@ public class NotificationApiTest extends AbstractControllerTest { @Test public void testNotificationUpdatesForUsersInTarget() throws Exception { - Map wsSessions = createUsersAndSetUpWsSessions(150); + Map wsSessions = createUsersAndSetUpWsSessions(100); wsSessions.forEach((user, wsClient) -> { wsClient.subscribeForUnreadNotifications(10); wsClient.waitForReply(true); @@ -258,14 +258,14 @@ public class NotificationApiTest extends AbstractControllerTest { NotificationTarget notificationTarget = new NotificationTarget(); UserListNotificationTargetConfig config = new UserListNotificationTargetConfig(); config.setUsersIds(wsSessions.keySet().stream().map(User::getUuidId).collect(Collectors.toList())); - notificationTarget.setName("150 users"); + notificationTarget.setName("100 users"); notificationTarget.setTenantId(tenantId); notificationTarget.setConfiguration(config); notificationTarget = saveNotificationTarget(notificationTarget); wsSessions.forEach((user, wsClient) -> wsClient.registerWaitForUpdate(2)); NotificationRequest notificationRequest = submitNotificationRequest(notificationTarget.getId(), "Hello, ${recipientEmail}"); - await().atMost(3, TimeUnit.SECONDS) + await().atMost(5, TimeUnit.SECONDS) .pollDelay(1, TimeUnit.SECONDS).pollInterval(500, TimeUnit.MILLISECONDS) .until(() -> wsSessions.values().stream() .allMatch(wsClient -> wsClient.getLastDataUpdate() != null diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestService.java index 7ae96ada79..9a5687d04c 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestService.java @@ -20,6 +20,7 @@ import org.thingsboard.server.common.data.id.NotificationRequestId; import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.notification.NotificationRequest; +import org.thingsboard.server.common.data.notification.NotificationRequestInfo; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -39,4 +40,6 @@ public interface NotificationRequestService { PageData findScheduledNotificationRequests(PageLink pageLink); + NotificationRequestInfo getNotificationRequestInfoById(TenantId tenantId, NotificationRequestId id); + } diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationService.java index 498e3dfa3f..7cc4fd9183 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationService.java @@ -38,6 +38,6 @@ public interface NotificationService { int countUnreadNotificationsByUserId(TenantId tenantId, UserId userId); - int updateNotificationsInfosByRequestId(TenantId tenantId, NotificationRequestId notificationRequestId, NotificationInfo notificationInfo); + int updateNotificationsByRequestId(TenantId tenantId, NotificationRequestId notificationRequestId, String notificationReason, NotificationInfo notificationInfo); } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequest.java b/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequest.java index 8ba27a96a8..142c3c6b9e 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequest.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequest.java @@ -61,7 +61,20 @@ public class NotificationRequest extends BaseData impleme private NotificationRequestConfig additionalConfig; private NotificationRequestStatus status; - public static final String GENERAL_NOTIFICATION_REASON = "General"; + public NotificationRequest(NotificationRequest other) { + super(other); + this.tenantId = other.tenantId; + this.targetId = other.targetId; + this.notificationReason = other.notificationReason; + this.textTemplate = other.textTemplate; + this.notificationInfo = other.notificationInfo; + this.notificationSeverity = other.notificationSeverity; + this.originatorType = other.originatorType; + this.originatorEntityId = other.originatorEntityId; + this.ruleId = other.ruleId; + this.additionalConfig = other.additionalConfig; + this.status = other.status; + } @JsonIgnore @Override diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequestInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequestInfo.java new file mode 100644 index 0000000000..53835200d7 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/notification/NotificationRequestInfo.java @@ -0,0 +1,35 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.common.data.notification; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.Map; + +@Data +@EqualsAndHashCode(callSuper = true) +public class NotificationRequestInfo extends NotificationRequest { + + private int sent; + private int read; + private Map statusesByRecipient; + + public NotificationRequestInfo(NotificationRequest notificationRequest) { + super(notificationRequest); + } + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java index af67c8ae40..be49f05c09 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java @@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.id.NotificationRequestId; import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.notification.NotificationRequest; +import org.thingsboard.server.common.data.notification.NotificationRequestInfo; import org.thingsboard.server.common.data.notification.NotificationRequestStatus; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -71,6 +72,11 @@ public class DefaultNotificationRequestService implements NotificationRequestSer return notificationRequestDao.findAllByStatus(NotificationRequestStatus.SCHEDULED, pageLink); } + @Override + public NotificationRequestInfo getNotificationRequestInfoById(TenantId tenantId, NotificationRequestId id) { + return notificationRequestDao.getNotificationRequestInfoById(tenantId, id); + } + private static class NotificationRequestValidator extends DataValidator { diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationService.java b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationService.java index 37a45a3a72..1e57ffaf5d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationService.java @@ -17,28 +17,19 @@ package org.thingsboard.server.dao.notification; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; -import org.thingsboard.server.common.data.id.AlarmId; import org.thingsboard.server.common.data.id.NotificationId; import org.thingsboard.server.common.data.id.NotificationRequestId; -import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.notification.Notification; import org.thingsboard.server.common.data.notification.NotificationInfo; -import org.thingsboard.server.common.data.notification.NotificationRequest; -import org.thingsboard.server.common.data.notification.NotificationRequestStatus; -import org.thingsboard.server.common.data.notification.NotificationSeverity; import org.thingsboard.server.common.data.notification.NotificationStatus; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.SortOrder; -import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.sql.query.EntityKeyMapping; -import java.util.List; - @Service @Slf4j @RequiredArgsConstructor @@ -83,8 +74,8 @@ public class DefaultNotificationService implements NotificationService { } @Override - public int updateNotificationsInfosByRequestId(TenantId tenantId, NotificationRequestId notificationRequestId, NotificationInfo notificationInfo) { - return notificationDao.updateInfosByRequestId(tenantId, notificationRequestId, notificationInfo); + public int updateNotificationsByRequestId(TenantId tenantId, NotificationRequestId notificationRequestId, String notificationReason, NotificationInfo notificationInfo) { + return notificationDao.updateByRequestId(tenantId, notificationRequestId, notificationReason, notificationInfo); } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationDao.java b/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationDao.java index dcfba4d704..8df17f4491 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationDao.java @@ -38,6 +38,6 @@ public interface NotificationDao extends Dao { PageData findByRequestId(TenantId tenantId, NotificationRequestId notificationRequestId, PageLink pageLink); - int updateInfosByRequestId(TenantId tenantId, NotificationRequestId notificationRequestId, NotificationInfo notificationInfo); + int updateByRequestId(TenantId tenantId, NotificationRequestId notificationRequestId, String notificationReason, NotificationInfo notificationInfo); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestDao.java b/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestDao.java index 2036105ffb..6036143f87 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRequestDao.java @@ -16,9 +16,11 @@ package org.thingsboard.server.dao.notification; import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.common.data.id.NotificationRequestId; import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.notification.NotificationRequest; +import org.thingsboard.server.common.data.notification.NotificationRequestInfo; import org.thingsboard.server.common.data.notification.NotificationRequestStatus; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -34,4 +36,6 @@ public interface NotificationRequestDao extends Dao { PageData findAllByStatus(NotificationRequestStatus status, PageLink pageLink); + NotificationRequestInfo getNotificationRequestInfoById(TenantId tenantId, NotificationRequestId id); + } diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationDao.java index cd98941011..5b4bf6eaa5 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationDao.java @@ -91,8 +91,8 @@ public class JpaNotificationDao extends JpaAbstractDao implements NotificationRequestDao { private final NotificationRequestRepository notificationRequestRepository; + private final NotificationRepository notificationRepository; @Override public PageData findByTenantIdAndPageLink(TenantId tenantId, PageLink pageLink) { @@ -58,6 +65,18 @@ public class JpaNotificationRequestDao extends JpaAbstractDao statusesByRecipient = notificationRepository.getStatusesByRecipientForRequestId(id.getId()).stream() + .collect(Collectors.toMap(r -> (String) r[0], r -> (NotificationStatus) r[1])); + notificationRequestInfo.setStatusesByRecipient(statusesByRecipient); + return notificationRequestInfo; + } + @Override protected Class getEntityClass() { return NotificationRequestEntity.class; diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRepository.java index 3f46a3dfa9..09ce53e7b8 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRepository.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRepository.java @@ -27,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional; import org.thingsboard.server.common.data.notification.NotificationStatus; import org.thingsboard.server.dao.model.sql.NotificationEntity; +import java.util.List; import java.util.UUID; @Repository @@ -50,7 +51,19 @@ public interface NotificationRepository extends JpaRepository getStatusesByRecipientForRequestId(@Param("requestId") UUID requestId); } diff --git a/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/NotificationManager.java b/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/NotificationManager.java index ac336e9fe4..dc57a876fd 100644 --- a/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/NotificationManager.java +++ b/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/NotificationManager.java @@ -29,6 +29,6 @@ public interface NotificationManager { void deleteNotificationRequest(TenantId tenantId, NotificationRequestId notificationRequestId); - void updateNotificationRequest(TenantId tenantId, NotificationRequest notificationRequest); + NotificationRequest updateNotificationRequest(TenantId tenantId, NotificationRequest notificationRequest); } diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/DeviceState.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/DeviceState.java index 1ef06547ba..5ab5adc7fc 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/DeviceState.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/DeviceState.java @@ -191,7 +191,6 @@ class DeviceState { AlarmState alarmState = alarmStates.computeIfAbsent(alarm.getId(), a -> new AlarmState(this.deviceProfile, deviceId, alarm, getOrInitPersistedAlarmState(alarm), dynamicPredicateValueCtx)); alarmState.processAckAlarm(alarmNf); - // todo: process notification rule } ctx.tellSuccess(msg); }