Notification rule info; api to get targets by ids array
This commit is contained in:
parent
be5eed96dc
commit
5cd82cfda3
@ -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<NotificationRule> getNotificationRules(@RequestParam int pageSize,
|
||||
public PageData<NotificationRuleInfo> 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}")
|
||||
|
||||
@ -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<NotificationTarget> getNotificationTargetsByIds(@RequestParam("ids") UUID[] ids,
|
||||
@AuthenticationPrincipal SecurityUser user) {
|
||||
List<NotificationTargetId> 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)
|
||||
|
||||
@ -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<NotificationRuleInfo> findNotificationRules() throws Exception {
|
||||
PageLink pageLink = new PageLink(10);
|
||||
return doGetTypedWithPageLink("/api/notification/rules?", new TypeReference<PageData<NotificationRuleInfo>>() {}, pageLink);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<NotificationRuleInfo> findNotificationRulesInfosByTenantId(TenantId tenantId, PageLink pageLink);
|
||||
|
||||
PageData<NotificationRule> findNotificationRulesByTenantId(TenantId tenantId, PageLink pageLink);
|
||||
|
||||
List<NotificationRule> findNotificationRulesByTenantIdAndTriggerType(TenantId tenantId, NotificationRuleTriggerType triggerType);
|
||||
|
||||
@ -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<NotificationTarget> findNotificationTargetsByTenantId(TenantId tenantId, PageLink pageLink);
|
||||
|
||||
List<NotificationTarget> findNotificationTargetsByTenantIdAndIds(TenantId tenantId, List<NotificationTargetId> ids);
|
||||
|
||||
PageData<User> findRecipientsForNotificationTarget(TenantId tenantId, CustomerId customerId, NotificationTargetId targetId, PageLink pageLink);
|
||||
|
||||
int countRecipientsForNotificationTargetConfig(TenantId tenantId, NotificationTargetConfig targetConfig);
|
||||
|
||||
@ -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<NotificationRuleId> implements HasTenantId, HasName {
|
||||
|
||||
@ -50,6 +52,16 @@ public class NotificationRule extends BaseData<NotificationRuleId> 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() {
|
||||
|
||||
@ -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<NotificationDeliveryMethod> deliveryMethods;
|
||||
|
||||
public NotificationRuleInfo(NotificationRule rule, String templateName, List<NotificationDeliveryMethod> deliveryMethods) {
|
||||
super(rule);
|
||||
this.templateName = templateName;
|
||||
this.deliveryMethods = deliveryMethods;
|
||||
}
|
||||
|
||||
}
|
||||
@ -77,6 +77,17 @@ public class NotificationRuleEntity extends BaseSqlEntity<NotificationRule> {
|
||||
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();
|
||||
|
||||
@ -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<NotificationDeliveryMethod> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -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<NotificationRuleInfo> findNotificationRulesInfosByTenantId(TenantId tenantId, PageLink pageLink) {
|
||||
return notificationRuleDao.findInfosByTenantIdAndPageLink(tenantId, pageLink);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<NotificationRule> findNotificationRulesByTenantId(TenantId tenantId, PageLink pageLink) {
|
||||
return notificationRuleDao.findByTenantIdAndPageLink(tenantId, pageLink);
|
||||
|
||||
@ -72,6 +72,11 @@ public class DefaultNotificationTargetService extends AbstractEntityService impl
|
||||
return notificationTargetDao.findByTenantIdAndPageLink(tenantId, pageLink);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NotificationTarget> findNotificationTargetsByTenantIdAndIds(TenantId tenantId, List<NotificationTargetId> ids) {
|
||||
return notificationTargetDao.findByTenantIdAndIds(tenantId, ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<User> findRecipientsForNotificationTarget(TenantId tenantId, CustomerId customerId, NotificationTargetId targetId, PageLink pageLink) {
|
||||
NotificationTarget notificationTarget = findNotificationTargetById(tenantId, targetId);
|
||||
|
||||
@ -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<NotificationRule> {
|
||||
|
||||
PageData<NotificationRule> findByTenantIdAndPageLink(TenantId tenantId, PageLink pageLink);
|
||||
|
||||
PageData<NotificationRuleInfo> findInfosByTenantIdAndPageLink(TenantId tenantId, PageLink pageLink);
|
||||
|
||||
boolean existsByTargetId(TenantId tenantId, NotificationTargetId targetId);
|
||||
|
||||
List<NotificationRule> findByTenantIdAndTriggerType(TenantId tenantId, NotificationRuleTriggerType triggerType);
|
||||
|
||||
NotificationRuleInfo findInfoById(TenantId tenantId, NotificationRuleId id);
|
||||
|
||||
}
|
||||
|
||||
@ -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<NotificationTarget> {
|
||||
|
||||
PageData<NotificationTarget> findByTenantIdAndPageLink(TenantId tenantId, PageLink pageLink);
|
||||
|
||||
List<NotificationTarget> findByTenantIdAndIds(TenantId tenantId, List<NotificationTargetId> ids);
|
||||
|
||||
}
|
||||
|
||||
@ -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<NotificationRuleEntit
|
||||
Strings.nullToEmpty(pageLink.getTextSearch()), DaoUtil.toPageable(pageLink)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageData<NotificationRuleInfo> 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<NotificationRuleEntit
|
||||
return DaoUtil.convertDataList(notificationRuleRepository.findAllByTenantIdAndTriggerType(getId(tenantId, true), triggerType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NotificationRuleInfo findInfoById(TenantId tenantId, NotificationRuleId id) {
|
||||
NotificationRuleInfoEntity infoEntity = notificationRuleRepository.findInfoById(id.getId());
|
||||
return infoEntity != null ? infoEntity.toData() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<NotificationRuleEntity> getEntityClass() {
|
||||
return NotificationRuleEntity.class;
|
||||
|
||||
@ -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<NotificationTargetE
|
||||
Strings.nullToEmpty(pageLink.getTextSearch()), DaoUtil.toPageable(pageLink)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NotificationTarget> findByTenantIdAndIds(TenantId tenantId, List<NotificationTargetId> ids) {
|
||||
return DaoUtil.convertDataList(notificationTargetRepository.findByTenantIdAndIdIn(tenantId.getId(), DaoUtil.toUUIDs(ids)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<NotificationTargetEntity> getEntityClass() {
|
||||
return NotificationTargetEntity.class;
|
||||
|
||||
@ -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<NotificationRuleEntity, UUID> {
|
||||
|
||||
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<NotificationRuleEntity> findByTenantIdAndSearchText(@Param("tenantId") UUID tenantId,
|
||||
@ -40,4 +45,12 @@ public interface NotificationRuleRepository extends JpaRepository<NotificationRu
|
||||
|
||||
List<NotificationRuleEntity> 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<NotificationRuleInfoEntity> findInfosByTenantIdAndSearchText(@Param("tenantId") UUID tenantId,
|
||||
@Param("searchText") String searchText,
|
||||
Pageable pageable);
|
||||
|
||||
}
|
||||
|
||||
@ -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<Notification
|
||||
|
||||
Page<NotificationTargetEntity> findByTenantIdAndNameContainingIgnoreCase(UUID tenantId, String searchText, Pageable pageable);
|
||||
|
||||
List<NotificationTargetEntity> findByTenantIdAndIdIn(UUID tenantId, List<UUID> ids);
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user