Fix default notification configs creation; refactoring
This commit is contained in:
parent
14b05a57bd
commit
3f531d8235
@ -187,6 +187,7 @@ public class EntityActionService {
|
|||||||
entityNode.put("configuration", "");
|
entityNode.put("configuration", "");
|
||||||
}
|
}
|
||||||
metaData.putValue("entityName", entity.getName());
|
metaData.putValue("entityName", entity.getName());
|
||||||
|
metaData.putValue("entityType", entityId.getEntityType().toString());
|
||||||
} else {
|
} else {
|
||||||
entityNode = json.createObjectNode();
|
entityNode = json.createObjectNode();
|
||||||
if (actionType == ActionType.ATTRIBUTES_UPDATED) {
|
if (actionType == ActionType.ATTRIBUTES_UPDATED) {
|
||||||
|
|||||||
@ -79,7 +79,6 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -168,19 +167,6 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[{}] Failed to update stats for notification request", requestId, e);
|
log.error("[{}] Failed to update stats for notification request", requestId, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
UserId senderId = notificationRequest.getSenderId();
|
|
||||||
if (senderId != null) {
|
|
||||||
if (stats.getErrors().isEmpty()) {
|
|
||||||
int sent = stats.getSent().values().stream().mapToInt(AtomicInteger::get).sum();
|
|
||||||
sendBasicNotification(tenantId, senderId, "Notifications sent",
|
|
||||||
"All notifications were successfully sent (" + sent + ")");
|
|
||||||
} else {
|
|
||||||
int failures = stats.getErrors().values().stream().mapToInt(Map::size).sum();
|
|
||||||
sendBasicNotification(tenantId, senderId, "Notification failure",
|
|
||||||
"Some notifications were not sent (" + failures + ")"); // TODO: 'Go to' button
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, dbCallbackExecutorService);
|
}, dbCallbackExecutorService);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -17,6 +17,7 @@ package org.thingsboard.server.service.notification.rule.trigger;
|
|||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.thingsboard.server.common.data.DataConstants;
|
import org.thingsboard.server.common.data.DataConstants;
|
||||||
|
import org.thingsboard.server.common.data.EntityType;
|
||||||
import org.thingsboard.server.common.data.audit.ActionType;
|
import org.thingsboard.server.common.data.audit.ActionType;
|
||||||
import org.thingsboard.server.common.data.id.EntityId;
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
import org.thingsboard.server.common.data.notification.info.EntityActionNotificationInfo;
|
import org.thingsboard.server.common.data.notification.info.EntityActionNotificationInfo;
|
||||||
@ -25,6 +26,7 @@ import org.thingsboard.server.common.data.notification.rule.trigger.EntityAction
|
|||||||
import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType;
|
import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType;
|
||||||
import org.thingsboard.server.common.msg.TbMsg;
|
import org.thingsboard.server.common.msg.TbMsg;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -49,7 +51,7 @@ public class EntityActionTriggerProcessor implements RuleEngineMsgNotificationRu
|
|||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return triggerConfig.getEntityType() == null || ruleEngineMsg.getOriginator().getEntityType() == triggerConfig.getEntityType();
|
return triggerConfig.getEntityType() == null || getEntityType(ruleEngineMsg) == triggerConfig.getEntityType();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -69,6 +71,11 @@ public class EntityActionTriggerProcessor implements RuleEngineMsgNotificationRu
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static EntityType getEntityType(TbMsg ruleEngineMsg) {
|
||||||
|
return Optional.ofNullable(ruleEngineMsg.getMetaData().getValue("entityType"))
|
||||||
|
.map(EntityType::valueOf).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NotificationRuleTriggerType getTriggerType() {
|
public NotificationRuleTriggerType getTriggerType() {
|
||||||
return NotificationRuleTriggerType.ENTITY_ACTION;
|
return NotificationRuleTriggerType.ENTITY_ACTION;
|
||||||
|
|||||||
@ -20,6 +20,7 @@ import lombok.Builder;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.thingsboard.server.common.data.EntityType;
|
import org.thingsboard.server.common.data.EntityType;
|
||||||
import org.thingsboard.server.common.data.id.EntityId;
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
@ -31,6 +32,8 @@ import org.thingsboard.server.common.data.notification.rule.trigger.RuleEngineCo
|
|||||||
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
|
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
|
||||||
import org.thingsboard.server.service.notification.rule.trigger.RuleEngineComponentLifecycleEventTriggerProcessor.RuleEngineComponentLifecycleEventTriggerObject;
|
import org.thingsboard.server.service.notification.rule.trigger.RuleEngineComponentLifecycleEventTriggerProcessor.RuleEngineComponentLifecycleEventTriggerObject;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -85,13 +88,11 @@ public class RuleEngineComponentLifecycleEventTriggerProcessor implements Notifi
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getErrorMsg(Exception error) {
|
private String getErrorMsg(Exception error) {
|
||||||
String errorMsg = error != null ? error.getMessage() : null;
|
if (error == null) return null;
|
||||||
errorMsg = Strings.nullToEmpty(errorMsg);
|
|
||||||
int lengthLimit = 150;
|
StringWriter sw = new StringWriter();
|
||||||
if (errorMsg.length() > lengthLimit) {
|
error.printStackTrace(new PrintWriter(sw));
|
||||||
errorMsg = StringUtils.substring(errorMsg, 0, lengthLimit + 1).trim() + "[...]";
|
return StringUtils.abbreviate(ExceptionUtils.getStackTrace(error), 200);
|
||||||
}
|
|
||||||
return errorMsg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.thingsboard.server.service.ws.notification.sub;
|
package org.thingsboard.server.service.ws.notification.sub;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@ -42,6 +43,7 @@ public class NotificationUpdate {
|
|||||||
|
|
||||||
private boolean deleted;
|
private boolean deleted;
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
public UUID getNotificationId() {
|
public UUID getNotificationId() {
|
||||||
return notificationId != null ? notificationId.getId() :
|
return notificationId != null ? notificationId.getId() :
|
||||||
notification != null ? notification.getUuidId() : null;
|
notification != null ? notification.getUuidId() : null;
|
||||||
|
|||||||
@ -232,8 +232,10 @@ public class DefaultNotificationSettingsService implements NotificationSettingsS
|
|||||||
|
|
||||||
WebDeliveryMethodNotificationTemplate webTemplate = new WebDeliveryMethodNotificationTemplate();
|
WebDeliveryMethodNotificationTemplate webTemplate = new WebDeliveryMethodNotificationTemplate();
|
||||||
ObjectNode additionalConfig = newObjectNode();
|
ObjectNode additionalConfig = newObjectNode();
|
||||||
ObjectNode iconConfig = additionalConfig.set("icon", newObjectNode());
|
ObjectNode iconConfig = newObjectNode();
|
||||||
ObjectNode buttonConfig = additionalConfig.set("actionButtonConfig", newObjectNode());
|
additionalConfig.set("icon", iconConfig);
|
||||||
|
ObjectNode buttonConfig = newObjectNode();
|
||||||
|
additionalConfig.set("actionButtonConfig", buttonConfig);
|
||||||
if (icon != null) {
|
if (icon != null) {
|
||||||
iconConfig.put("enabled", true)
|
iconConfig.put("enabled", true)
|
||||||
.put("icon", icon)
|
.put("icon", icon)
|
||||||
@ -250,12 +252,10 @@ public class DefaultNotificationSettingsService implements NotificationSettingsS
|
|||||||
buttonConfig.put("enabled", false);
|
buttonConfig.put("enabled", false);
|
||||||
}
|
}
|
||||||
webTemplate.setAdditionalConfig(additionalConfig);
|
webTemplate.setAdditionalConfig(additionalConfig);
|
||||||
|
webTemplate.setEnabled(true);
|
||||||
templateConfig.setDeliveryMethodsTemplates(Map.of(
|
templateConfig.setDeliveryMethodsTemplates(Map.of(
|
||||||
NotificationDeliveryMethod.WEB, webTemplate
|
NotificationDeliveryMethod.WEB, webTemplate
|
||||||
));
|
));
|
||||||
/*
|
|
||||||
* {"icon":{"enabled":true,"icon":"info","color":"#757575"},"actionButtonConfig":{"enabled":true,"text":"Go to device","linkType":"LINK","link":"/devices/aa4a3660-c3e9-11ed-934f-bdfa9bfa7568"}}
|
|
||||||
* */
|
|
||||||
template.setConfiguration(templateConfig);
|
template.setConfiguration(templateConfig);
|
||||||
return notificationTemplateService.saveNotificationTemplate(tenantId, template);
|
return notificationTemplateService.saveNotificationTemplate(tenantId, template);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
package org.thingsboard.server.dao.sql.notification;
|
package org.thingsboard.server.dao.sql.notification;
|
||||||
|
|
||||||
import com.datastax.oss.driver.api.core.uuid.Uuids;
|
import com.datastax.oss.driver.api.core.uuid.Uuids;
|
||||||
|
import com.google.common.base.Strings;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
@ -65,12 +66,14 @@ public class JpaNotificationDao extends JpaAbstractDao<NotificationEntity, Notif
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageData<Notification> findUnreadByRecipientIdAndPageLink(TenantId tenantId, UserId recipientId, PageLink pageLink) {
|
public PageData<Notification> findUnreadByRecipientIdAndPageLink(TenantId tenantId, UserId recipientId, PageLink pageLink) {
|
||||||
return DaoUtil.toPageData(notificationRepository.findByRecipientIdAndStatusNot(recipientId.getId(), NotificationStatus.READ, DaoUtil.toPageable(pageLink)));
|
return DaoUtil.toPageData(notificationRepository.findByRecipientIdAndStatusNot(recipientId.getId(), NotificationStatus.READ,
|
||||||
|
Strings.nullToEmpty(pageLink.getTextSearch()), DaoUtil.toPageable(pageLink)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageData<Notification> findByRecipientIdAndPageLink(TenantId tenantId, UserId recipientId, PageLink pageLink) {
|
public PageData<Notification> findByRecipientIdAndPageLink(TenantId tenantId, UserId recipientId, PageLink pageLink) {
|
||||||
return DaoUtil.toPageData(notificationRepository.findByRecipientId(recipientId.getId(), DaoUtil.toPageable(pageLink)));
|
return DaoUtil.toPageData(notificationRepository.findByRecipientId(recipientId.getId(),
|
||||||
|
Strings.nullToEmpty(pageLink.getTextSearch()), DaoUtil.toPageable(pageLink)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -26,15 +26,25 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import org.thingsboard.server.common.data.notification.NotificationStatus;
|
import org.thingsboard.server.common.data.notification.NotificationStatus;
|
||||||
import org.thingsboard.server.dao.model.sql.NotificationEntity;
|
import org.thingsboard.server.dao.model.sql.NotificationEntity;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface NotificationRepository extends JpaRepository<NotificationEntity, UUID> {
|
public interface NotificationRepository extends JpaRepository<NotificationEntity, UUID> {
|
||||||
|
|
||||||
Page<NotificationEntity> findByRecipientIdAndStatusNot(UUID recipientId, NotificationStatus status, Pageable pageable);
|
@Query("SELECT n FROM NotificationEntity n WHERE n.recipientId = :recipientId AND n.status <> :status " +
|
||||||
|
"AND (:searchText = '' OR lower(n.subject) LIKE lower(concat('%', :searchText, '%')) " +
|
||||||
|
"OR lower(n.text) LIKE lower(concat('%', :searchText, '%')))")
|
||||||
|
Page<NotificationEntity> findByRecipientIdAndStatusNot(@Param("recipientId") UUID recipientId,
|
||||||
|
@Param("status") NotificationStatus status,
|
||||||
|
@Param("searchText") String searchText,
|
||||||
|
Pageable pageable);
|
||||||
|
|
||||||
Page<NotificationEntity> findByRecipientId(UUID recipientId, Pageable pageable);
|
@Query("SELECT n FROM NotificationEntity n WHERE n.recipientId = :recipientId " +
|
||||||
|
"AND (:searchText = '' OR lower(n.subject) LIKE lower(concat('%', :searchText, '%')) " +
|
||||||
|
"OR lower(n.text) LIKE lower(concat('%', :searchText, '%')))")
|
||||||
|
Page<NotificationEntity> findByRecipientId(@Param("recipientId") UUID recipientId,
|
||||||
|
@Param("searchText") String searchText,
|
||||||
|
Pageable pageable);
|
||||||
|
|
||||||
@Modifying
|
@Modifying
|
||||||
@Transactional
|
@Transactional
|
||||||
|
|||||||
@ -41,7 +41,7 @@ public interface NotificationRequestRepository extends JpaRepository<Notificatio
|
|||||||
Page<NotificationRequestEntity> findByTenantIdAndOriginatorEntityType(UUID tenantId, EntityType originatorType, Pageable pageable);
|
Page<NotificationRequestEntity> findByTenantIdAndOriginatorEntityType(UUID tenantId, EntityType originatorType, Pageable pageable);
|
||||||
|
|
||||||
@Query(REQUEST_INFO_QUERY + " WHERE r.tenantId = :tenantId AND r.originatorEntityType = :originatorType " +
|
@Query(REQUEST_INFO_QUERY + " WHERE r.tenantId = :tenantId AND r.originatorEntityType = :originatorType " +
|
||||||
"AND (:searchText = '' OR (t.name IS NOT NULL AND lower(t.name) LIKE lower(concat('%', :searchText, '%'))))")
|
"AND (:searchText = '' OR t.name IS NOT NULL AND lower(t.name) LIKE lower(concat('%', :searchText, '%')))")
|
||||||
Page<NotificationRequestInfoEntity> findInfosByTenantIdAndOriginatorEntityTypeAndSearchText(@Param("tenantId") UUID tenantId,
|
Page<NotificationRequestInfoEntity> findInfosByTenantIdAndOriginatorEntityTypeAndSearchText(@Param("tenantId") UUID tenantId,
|
||||||
@Param("originatorType") EntityType originatorType,
|
@Param("originatorType") EntityType originatorType,
|
||||||
@Param("searchText") String searchText,
|
@Param("searchText") String searchText,
|
||||||
|
|||||||
@ -37,7 +37,7 @@ public interface NotificationRuleRepository extends JpaRepository<NotificationRu
|
|||||||
"FROM NotificationRuleEntity r INNER JOIN NotificationTemplateEntity t ON r.templateId = t.id";
|
"FROM NotificationRuleEntity r INNER JOIN NotificationTemplateEntity t ON r.templateId = t.id";
|
||||||
|
|
||||||
@Query("SELECT r FROM NotificationRuleEntity r WHERE r.tenantId = :tenantId " +
|
@Query("SELECT r FROM NotificationRuleEntity r WHERE r.tenantId = :tenantId " +
|
||||||
"AND lower(r.name) LIKE lower(concat('%', :searchText, '%')) ")
|
"AND (:searchText = '' OR lower(r.name) LIKE lower(concat('%', :searchText, '%')))")
|
||||||
Page<NotificationRuleEntity> findByTenantIdAndSearchText(@Param("tenantId") UUID tenantId,
|
Page<NotificationRuleEntity> findByTenantIdAndSearchText(@Param("tenantId") UUID tenantId,
|
||||||
@Param("searchText") String searchText,
|
@Param("searchText") String searchText,
|
||||||
Pageable pageable);
|
Pageable pageable);
|
||||||
|
|||||||
@ -32,9 +32,9 @@ import java.util.UUID;
|
|||||||
public interface NotificationTemplateRepository extends JpaRepository<NotificationTemplateEntity, UUID> {
|
public interface NotificationTemplateRepository extends JpaRepository<NotificationTemplateEntity, UUID> {
|
||||||
|
|
||||||
@Query("SELECT t FROM NotificationTemplateEntity t WHERE t.tenantId = :tenantId AND " +
|
@Query("SELECT t FROM NotificationTemplateEntity t WHERE t.tenantId = :tenantId AND " +
|
||||||
"t.notificationType IN :notificationTypes AND " +
|
"t.notificationType IN :notificationTypes " +
|
||||||
"(lower(t.name) LIKE lower(concat('%', :searchText, '%')) OR " +
|
"AND (:searchText = '' OR lower(t.name) LIKE lower(concat('%', :searchText, '%')) " +
|
||||||
"lower(t.notificationType) LIKE lower(concat('%', :searchText, '%')))")
|
"OR lower(t.notificationType) LIKE lower(concat('%', :searchText, '%')))")
|
||||||
Page<NotificationTemplateEntity> findByTenantIdAndNotificationTypesAndSearchText(@Param("tenantId") UUID tenantId,
|
Page<NotificationTemplateEntity> findByTenantIdAndNotificationTypesAndSearchText(@Param("tenantId") UUID tenantId,
|
||||||
@Param("notificationTypes") List<NotificationType> notificationTypes,
|
@Param("notificationTypes") List<NotificationType> notificationTypes,
|
||||||
@Param("searchText") String searchText,
|
@Param("searchText") String searchText,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user