From 5cd82cfda38d8e4c878770a2de44c8be3de4d7fe Mon Sep 17 00:00:00 2001 From: ViacheslavKlimov Date: Mon, 30 Jan 2023 15:39:20 +0200 Subject: [PATCH] Notification rule info; api to get targets by ids array --- .../NotificationRuleController.java | 9 ++-- .../NotificationTargetController.java | 11 ++++ .../notification/NotificationRuleApiTest.java | 41 +++++++++++++- .../notification/NotificationRuleService.java | 5 ++ .../NotificationTargetService.java | 4 ++ .../notification/rule/NotificationRule.java | 12 +++++ .../rule/NotificationRuleInfo.java | 39 ++++++++++++++ .../dao/model/sql/NotificationRuleEntity.java | 11 ++++ .../model/sql/NotificationRuleInfoEntity.java | 53 +++++++++++++++++++ .../DefaultNotificationRuleService.java | 11 ++++ .../DefaultNotificationTargetService.java | 5 ++ .../dao/notification/NotificationRuleDao.java | 6 +++ .../notification/NotificationTargetDao.java | 5 ++ .../notification/JpaNotificationRuleDao.java | 15 ++++++ .../JpaNotificationTargetDao.java | 9 ++++ .../NotificationRuleRepository.java | 13 +++++ .../NotificationTargetRepository.java | 3 ++ 17 files changed, 247 insertions(+), 5 deletions(-) create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/NotificationRuleInfo.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/model/sql/NotificationRuleInfoEntity.java diff --git a/application/src/main/java/org/thingsboard/server/controller/NotificationRuleController.java b/application/src/main/java/org/thingsboard/server/controller/NotificationRuleController.java index cca5fd433e..dc69ede817 100644 --- a/application/src/main/java/org/thingsboard/server/controller/NotificationRuleController.java +++ b/application/src/main/java/org/thingsboard/server/controller/NotificationRuleController.java @@ -31,6 +31,7 @@ import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.notification.rule.NotificationRule; +import org.thingsboard.server.common.data.notification.rule.NotificationRuleInfo; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; @@ -62,21 +63,21 @@ public class NotificationRuleController extends BaseController { @GetMapping("/rule/{id}") @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") - public NotificationRule getNotificationRuleById(@PathVariable UUID id) throws ThingsboardException { + public NotificationRuleInfo getNotificationRuleById(@PathVariable UUID id) throws ThingsboardException { NotificationRuleId notificationRuleId = new NotificationRuleId(id); - return checkEntityId(notificationRuleId, notificationRuleService::findNotificationRuleById, Operation.READ); + return checkEntityId(notificationRuleId, notificationRuleService::findNotificationRuleInfoById, Operation.READ); } @GetMapping("/rules") @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") - public PageData getNotificationRules(@RequestParam int pageSize, + public PageData getNotificationRules(@RequestParam int pageSize, @RequestParam int page, @RequestParam(required = false) String textSearch, @RequestParam(required = false) String sortProperty, @RequestParam(required = false) String sortOrder, @AuthenticationPrincipal SecurityUser user) throws ThingsboardException { PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); - return notificationRuleService.findNotificationRulesByTenantId(user.getTenantId(), pageLink); + return notificationRuleService.findNotificationRulesInfosByTenantId(user.getTenantId(), pageLink); } @DeleteMapping("/rule/{id}") diff --git a/application/src/main/java/org/thingsboard/server/controller/NotificationTargetController.java b/application/src/main/java/org/thingsboard/server/controller/NotificationTargetController.java index 6bd656bca3..e2ae5031d6 100644 --- a/application/src/main/java/org/thingsboard/server/controller/NotificationTargetController.java +++ b/application/src/main/java/org/thingsboard/server/controller/NotificationTargetController.java @@ -45,7 +45,10 @@ import org.thingsboard.server.service.security.permission.Operation; import org.thingsboard.server.service.security.permission.Resource; import javax.validation.Valid; +import java.util.Arrays; +import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; import static org.thingsboard.server.controller.ControllerConstants.SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH; @@ -120,6 +123,14 @@ public class NotificationTargetController extends BaseController { return recipients; } + @GetMapping(value = "/targets", params = {"ids"}) + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") + public List getNotificationTargetsByIds(@RequestParam("ids") UUID[] ids, + @AuthenticationPrincipal SecurityUser user) { + List targetsIds = Arrays.stream(ids).map(NotificationTargetId::new).collect(Collectors.toList()); + return notificationTargetService.findNotificationTargetsByTenantIdAndIds(user.getTenantId(), targetsIds); + } + @ApiOperation(value = "Get notification targets (getNotificationTargets)", notes = "Fetch the page of notification targets owned by sysadmin or tenant." + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH) diff --git a/application/src/test/java/org/thingsboard/server/service/notification/NotificationRuleApiTest.java b/application/src/test/java/org/thingsboard/server/service/notification/NotificationRuleApiTest.java index 1a5fdba0be..da9acc9a1e 100644 --- a/application/src/test/java/org/thingsboard/server/service/notification/NotificationRuleApiTest.java +++ b/application/src/test/java/org/thingsboard/server/service/notification/NotificationRuleApiTest.java @@ -15,6 +15,7 @@ */ package org.thingsboard.server.service.notification; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.BooleanNode; import org.junit.Before; @@ -40,20 +41,25 @@ import org.thingsboard.server.common.data.device.profile.SimpleAlarmConditionSpe import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.notification.Notification; import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; +import org.thingsboard.server.common.data.notification.NotificationRequestInfo; import org.thingsboard.server.common.data.notification.NotificationType; import org.thingsboard.server.common.data.notification.info.AlarmNotificationInfo; import org.thingsboard.server.common.data.notification.rule.DefaultNotificationRuleRecipientsConfig; import org.thingsboard.server.common.data.notification.rule.EscalatedNotificationRuleRecipientsConfig; import org.thingsboard.server.common.data.notification.rule.NotificationRule; +import org.thingsboard.server.common.data.notification.rule.NotificationRuleInfo; import org.thingsboard.server.common.data.notification.rule.trigger.AlarmNotificationRuleTriggerConfig; import org.thingsboard.server.common.data.notification.rule.trigger.EntityActionNotificationRuleTriggerConfig; import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; import org.thingsboard.server.common.data.notification.targets.NotificationTarget; import org.thingsboard.server.common.data.notification.template.NotificationTemplate; +import org.thingsboard.server.common.data.page.PageData; +import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.query.BooleanFilterPredicate; import org.thingsboard.server.common.data.query.EntityKeyValueType; import org.thingsboard.server.common.data.query.FilterPredicateValue; import org.thingsboard.server.common.data.security.Authority; +import org.thingsboard.server.dao.alarm.AlarmService; import org.thingsboard.server.dao.service.DaoSqlTest; import org.thingsboard.server.service.telemetry.AlarmSubscriptionService; @@ -79,6 +85,9 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { @SpyBean private AlarmSubscriptionService alarmSubscriptionService; + @SpyBean + private AlarmService alarmService; + @Before public void beforeEach() throws Exception { loginTenantAdmin(); @@ -183,7 +192,8 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { .set("bool", BooleanNode.TRUE); doPost("/api/plugins/telemetry/" + device.getId() + "/" + DataConstants.SHARED_SCOPE, attr); - verify(alarmSubscriptionService, timeout(2000)).createOrUpdateAlarm(argThat(alarm -> alarm.getType().equals(alarmType))); + await().atMost(2, TimeUnit.SECONDS) + .until(() -> alarmSubscriptionService.findLatestByOriginatorAndType(tenantId, device.getId(), alarmType).get() != null); Alarm alarm = alarmSubscriptionService.findLatestByOriginatorAndType(tenantId, device.getId(), alarmType).get(); long ts = System.currentTimeMillis(); @@ -278,6 +288,31 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { */ } + @Test + public void testNotificationRuleInfo() throws Exception { + NotificationDeliveryMethod[] deliveryMethods = {NotificationDeliveryMethod.PUSH, NotificationDeliveryMethod.EMAIL}; + NotificationTemplate template = createNotificationTemplate(NotificationType.ENTITY_ACTION, "Subject", "Text", deliveryMethods); + + NotificationRule rule = new NotificationRule(); + rule.setName("Test"); + rule.setTemplateId(template.getId()); + + rule.setTriggerType(NotificationRuleTriggerType.ENTITY_ACTION); + EntityActionNotificationRuleTriggerConfig triggerConfig = new EntityActionNotificationRuleTriggerConfig(); + rule.setTriggerConfig(triggerConfig); + + DefaultNotificationRuleRecipientsConfig recipientsConfig = new DefaultNotificationRuleRecipientsConfig(); + recipientsConfig.setTriggerType(NotificationRuleTriggerType.ENTITY_ACTION); + recipientsConfig.setTargets(List.of(createNotificationTarget(tenantAdminUserId).getUuidId())); + rule.setRecipientsConfig(recipientsConfig); + rule = saveNotificationRule(rule); + + NotificationRuleInfo ruleInfo = findNotificationRules().getData().get(0); + assertThat(ruleInfo.getId()).isEqualTo(ruleInfo.getId()); + assertThat(ruleInfo.getTemplateName()).isEqualTo(template.getName()); + assertThat(ruleInfo.getDeliveryMethods()).containsOnly(deliveryMethods); + } + private DeviceProfile createDeviceProfileWithAlarmRules(NotificationRuleId notificationRuleId, String alarmType) { DeviceProfile deviceProfile = createDeviceProfile("For notification rule test"); deviceProfile.setTenantId(tenantId); @@ -318,4 +353,8 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { return doPost("/api/notification/rule", notificationRule, NotificationRule.class); } + private PageData findNotificationRules() throws Exception { + PageLink pageLink = new PageLink(10); + return doGetTypedWithPageLink("/api/notification/rules?", new TypeReference>() {}, pageLink); + } } diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRuleService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRuleService.java index b8f60f704a..39fec2595b 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRuleService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationRuleService.java @@ -18,6 +18,7 @@ package org.thingsboard.server.dao.notification; import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.notification.rule.NotificationRule; +import org.thingsboard.server.common.data.notification.rule.NotificationRuleInfo; import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -30,6 +31,10 @@ public interface NotificationRuleService { NotificationRule findNotificationRuleById(TenantId tenantId, NotificationRuleId id); + NotificationRuleInfo findNotificationRuleInfoById(TenantId tenantId, NotificationRuleId id); + + PageData findNotificationRulesInfosByTenantId(TenantId tenantId, PageLink pageLink); + PageData findNotificationRulesByTenantId(TenantId tenantId, PageLink pageLink); List findNotificationRulesByTenantIdAndTriggerType(TenantId tenantId, NotificationRuleTriggerType triggerType); diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationTargetService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationTargetService.java index 89199333b4..0791f46884 100644 --- a/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationTargetService.java +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/notification/NotificationTargetService.java @@ -24,6 +24,8 @@ import org.thingsboard.server.common.data.notification.targets.NotificationTarge import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; +import java.util.List; + public interface NotificationTargetService { NotificationTarget saveNotificationTarget(TenantId tenantId, NotificationTarget notificationTarget); @@ -32,6 +34,8 @@ public interface NotificationTargetService { PageData findNotificationTargetsByTenantId(TenantId tenantId, PageLink pageLink); + List findNotificationTargetsByTenantIdAndIds(TenantId tenantId, List ids); + PageData findRecipientsForNotificationTarget(TenantId tenantId, CustomerId customerId, NotificationTargetId targetId, PageLink pageLink); int countRecipientsForNotificationTargetConfig(TenantId tenantId, NotificationTargetConfig targetConfig); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/NotificationRule.java b/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/NotificationRule.java index 1a570c2b20..e6e096454b 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/NotificationRule.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/NotificationRule.java @@ -18,6 +18,7 @@ package org.thingsboard.server.common.data.notification.rule; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; import org.thingsboard.server.common.data.BaseData; import org.thingsboard.server.common.data.HasName; import org.thingsboard.server.common.data.HasTenantId; @@ -33,6 +34,7 @@ import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; @Data +@NoArgsConstructor @EqualsAndHashCode(callSuper = true) public class NotificationRule extends BaseData implements HasTenantId, HasName { @@ -50,6 +52,16 @@ public class NotificationRule extends BaseData implements Ha @Valid private NotificationRuleRecipientsConfig recipientsConfig; // todo: add pg_tgrm index (but index is 2.5x size of the column) + public NotificationRule(NotificationRule other) { + super(other); + this.tenantId = other.tenantId; + this.name = other.name; + this.templateId = other.templateId; + this.triggerType = other.triggerType; + this.triggerConfig = other.triggerConfig; + this.recipientsConfig = other.recipientsConfig; + } + @JsonIgnore @AssertTrue(message = "trigger type not matching") public boolean isValid() { diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/NotificationRuleInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/NotificationRuleInfo.java new file mode 100644 index 0000000000..12ae232d7d --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/notification/rule/NotificationRuleInfo.java @@ -0,0 +1,39 @@ +/** + * 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.rule; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; + +import java.util.List; + +@Data +@NoArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class NotificationRuleInfo extends NotificationRule { + + private String templateName; + private List deliveryMethods; + + public NotificationRuleInfo(NotificationRule rule, String templateName, List deliveryMethods) { + super(rule); + this.templateName = templateName; + this.deliveryMethods = deliveryMethods; + } + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/NotificationRuleEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/NotificationRuleEntity.java index bca0627f76..a012fcf49d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/model/sql/NotificationRuleEntity.java +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/NotificationRuleEntity.java @@ -77,6 +77,17 @@ public class NotificationRuleEntity extends BaseSqlEntity { setRecipientsConfig(toJson(notificationRule.getRecipientsConfig())); } + public NotificationRuleEntity(NotificationRuleEntity other) { + this.id = other.id; + this.createdTime = other.createdTime; + this.tenantId = other.tenantId; + this.name = other.name; + this.templateId = other.templateId; + this.triggerType = other.triggerType; + this.triggerConfig = other.triggerConfig; + this.recipientsConfig = other.recipientsConfig; + } + @Override public NotificationRule toData() { NotificationRule notificationRule = new NotificationRule(); diff --git a/dao/src/main/java/org/thingsboard/server/dao/model/sql/NotificationRuleInfoEntity.java b/dao/src/main/java/org/thingsboard/server/dao/model/sql/NotificationRuleInfoEntity.java new file mode 100644 index 0000000000..2ed1e20f44 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/model/sql/NotificationRuleInfoEntity.java @@ -0,0 +1,53 @@ +/** + * 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.dao.model.sql; + +import com.fasterxml.jackson.databind.JsonNode; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; +import org.thingsboard.server.common.data.notification.rule.NotificationRule; +import org.thingsboard.server.common.data.notification.rule.NotificationRuleInfo; +import org.thingsboard.server.common.data.notification.template.NotificationTemplateConfig; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Data +@EqualsAndHashCode(callSuper = true) +public class NotificationRuleInfoEntity extends NotificationRuleEntity { + + private String templateName; + private JsonNode templateConfig; + + public NotificationRuleInfoEntity(NotificationRuleEntity ruleEntity, String templateName, Object templateConfig) { + super(ruleEntity); + this.templateName = templateName; + this.templateConfig = (JsonNode) templateConfig; + } + + @Override + public NotificationRuleInfo toData() { + NotificationRule rule = super.toData(); + List deliveryMethods = fromJson(templateConfig, NotificationTemplateConfig.class) + .getDeliveryMethodsTemplates().entrySet().stream() + .filter(entry -> entry.getValue().isEnabled()) + .map(Map.Entry::getKey).collect(Collectors.toList()); + return new NotificationRuleInfo(rule, templateName, deliveryMethods); + } + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRuleService.java b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRuleService.java index 5a6d1e58b6..76c563f7e8 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRuleService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRuleService.java @@ -20,6 +20,7 @@ import org.springframework.stereotype.Service; import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.notification.rule.NotificationRule; +import org.thingsboard.server.common.data.notification.rule.NotificationRuleInfo; import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -63,6 +64,16 @@ public class DefaultNotificationRuleService extends AbstractCachedEntityService< return notificationRuleDao.findById(tenantId, id.getId()); } + @Override + public NotificationRuleInfo findNotificationRuleInfoById(TenantId tenantId, NotificationRuleId id) { + return notificationRuleDao.findInfoById(tenantId, id); + } + + @Override + public PageData findNotificationRulesInfosByTenantId(TenantId tenantId, PageLink pageLink) { + return notificationRuleDao.findInfosByTenantIdAndPageLink(tenantId, pageLink); + } + @Override public PageData findNotificationRulesByTenantId(TenantId tenantId, PageLink pageLink) { return notificationRuleDao.findByTenantIdAndPageLink(tenantId, pageLink); diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationTargetService.java b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationTargetService.java index 1aab941d4d..10395ba18e 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationTargetService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationTargetService.java @@ -72,6 +72,11 @@ public class DefaultNotificationTargetService extends AbstractEntityService impl return notificationTargetDao.findByTenantIdAndPageLink(tenantId, pageLink); } + @Override + public List findNotificationTargetsByTenantIdAndIds(TenantId tenantId, List ids) { + return notificationTargetDao.findByTenantIdAndIds(tenantId, ids); + } + @Override public PageData findRecipientsForNotificationTarget(TenantId tenantId, CustomerId customerId, NotificationTargetId targetId, PageLink pageLink) { NotificationTarget notificationTarget = findNotificationTargetById(tenantId, targetId); diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRuleDao.java b/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRuleDao.java index baf34b308c..0bc1d61010 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRuleDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationRuleDao.java @@ -15,9 +15,11 @@ */ package org.thingsboard.server.dao.notification; +import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.NotificationTargetId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.notification.rule.NotificationRule; +import org.thingsboard.server.common.data.notification.rule.NotificationRuleInfo; import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -29,8 +31,12 @@ public interface NotificationRuleDao extends Dao { PageData findByTenantIdAndPageLink(TenantId tenantId, PageLink pageLink); + PageData findInfosByTenantIdAndPageLink(TenantId tenantId, PageLink pageLink); + boolean existsByTargetId(TenantId tenantId, NotificationTargetId targetId); List findByTenantIdAndTriggerType(TenantId tenantId, NotificationRuleTriggerType triggerType); + NotificationRuleInfo findInfoById(TenantId tenantId, NotificationRuleId id); + } diff --git a/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationTargetDao.java b/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationTargetDao.java index 0bcae5c456..89b49bea61 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationTargetDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/notification/NotificationTargetDao.java @@ -15,14 +15,19 @@ */ package org.thingsboard.server.dao.notification; +import org.thingsboard.server.common.data.id.NotificationTargetId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.notification.targets.NotificationTarget; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.dao.Dao; +import java.util.List; + public interface NotificationTargetDao extends Dao { PageData findByTenantIdAndPageLink(TenantId tenantId, PageLink pageLink); + List findByTenantIdAndIds(TenantId tenantId, List ids); + } diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationRuleDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationRuleDao.java index 8341002623..4176db9c8d 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationRuleDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationRuleDao.java @@ -20,14 +20,17 @@ import lombok.RequiredArgsConstructor; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.id.NotificationRuleId; import org.thingsboard.server.common.data.id.NotificationTargetId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.notification.rule.NotificationRule; +import org.thingsboard.server.common.data.notification.rule.NotificationRuleInfo; import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.dao.DaoUtil; import org.thingsboard.server.dao.model.sql.NotificationRuleEntity; +import org.thingsboard.server.dao.model.sql.NotificationRuleInfoEntity; import org.thingsboard.server.dao.notification.NotificationRuleDao; import org.thingsboard.server.dao.sql.JpaAbstractDao; import org.thingsboard.server.dao.util.SqlDao; @@ -50,6 +53,12 @@ public class JpaNotificationRuleDao extends JpaAbstractDao findInfosByTenantIdAndPageLink(TenantId tenantId, PageLink pageLink) { + return DaoUtil.pageToPageData(notificationRuleRepository.findInfosByTenantIdAndSearchText(getId(tenantId, true), + Strings.nullToEmpty(pageLink.getTextSearch()), DaoUtil.toPageable(pageLink))).mapData(NotificationRuleInfoEntity::toData); + } + @Override public boolean existsByTargetId(TenantId tenantId, NotificationTargetId targetId) { return notificationRuleRepository.existsByRecipientsConfigContaining(targetId.getId().toString()); @@ -60,6 +69,12 @@ public class JpaNotificationRuleDao extends JpaAbstractDao getEntityClass() { return NotificationRuleEntity.class; diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationTargetDao.java b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationTargetDao.java index 789574c3d0..b8577fdfc5 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationTargetDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/JpaNotificationTargetDao.java @@ -20,7 +20,9 @@ import lombok.RequiredArgsConstructor; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.id.NotificationTargetId; import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.id.UUIDBased; import org.thingsboard.server.common.data.notification.targets.NotificationTarget; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; @@ -30,7 +32,9 @@ import org.thingsboard.server.dao.notification.NotificationTargetDao; import org.thingsboard.server.dao.sql.JpaAbstractDao; import org.thingsboard.server.dao.util.SqlDao; +import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; import static org.thingsboard.server.dao.DaoUtil.getId; @@ -47,6 +51,11 @@ public class JpaNotificationTargetDao extends JpaAbstractDao findByTenantIdAndIds(TenantId tenantId, List ids) { + return DaoUtil.convertDataList(notificationTargetRepository.findByTenantIdAndIdIn(tenantId.getId(), DaoUtil.toUUIDs(ids))); + } + @Override protected Class getEntityClass() { return NotificationTargetEntity.class; diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRuleRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRuleRepository.java index 27c7753ede..275924600a 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRuleRepository.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationRuleRepository.java @@ -21,8 +21,10 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import org.thingsboard.server.common.data.notification.rule.NotificationRuleInfo; import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; import org.thingsboard.server.dao.model.sql.NotificationRuleEntity; +import org.thingsboard.server.dao.model.sql.NotificationRuleInfoEntity; import java.util.List; import java.util.UUID; @@ -30,6 +32,9 @@ import java.util.UUID; @Repository public interface NotificationRuleRepository extends JpaRepository { + String RULE_INFO_QUERY = "SELECT new org.thingsboard.server.dao.model.sql.NotificationRuleInfoEntity(r, t.name, t.configuration) " + + "FROM NotificationRuleEntity r INNER JOIN NotificationTemplateEntity t ON r.templateId = t.id"; + @Query("SELECT r FROM NotificationRuleEntity r WHERE r.tenantId = :tenantId " + "AND lower(r.name) LIKE lower(concat('%', :searchText, '%')) ") Page findByTenantIdAndSearchText(@Param("tenantId") UUID tenantId, @@ -40,4 +45,12 @@ public interface NotificationRuleRepository extends JpaRepository findAllByTenantIdAndTriggerType(UUID tenantId, NotificationRuleTriggerType triggerType); + @Query(RULE_INFO_QUERY + " WHERE r.id = :id") + NotificationRuleInfoEntity findInfoById(@Param("id") UUID id); + + @Query(RULE_INFO_QUERY + " WHERE r.tenantId = :tenantId AND lower(r.name) LIKE lower(concat('%', :searchText, '%'))") + Page findInfosByTenantIdAndSearchText(@Param("tenantId") UUID tenantId, + @Param("searchText") String searchText, + Pageable pageable); + } diff --git a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationTargetRepository.java b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationTargetRepository.java index a558ad5163..3156d355fa 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationTargetRepository.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sql/notification/NotificationTargetRepository.java @@ -23,6 +23,7 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import org.thingsboard.server.dao.model.sql.NotificationTargetEntity; +import java.util.List; import java.util.UUID; @Repository @@ -30,4 +31,6 @@ public interface NotificationTargetRepository extends JpaRepository findByTenantIdAndNameContainingIgnoreCase(UUID tenantId, String searchText, Pageable pageable); + List findByTenantIdAndIdIn(UUID tenantId, List ids); + }