69 changed files with 1152 additions and 158 deletions
@ -0,0 +1,103 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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.service.sync.ie.exporting.impl; |
|||
|
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.ExportableEntity; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.NotificationRuleId; |
|||
import org.thingsboard.server.common.data.id.NotificationTargetId; |
|||
import org.thingsboard.server.common.data.id.RuleChainId; |
|||
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.NotificationRuleRecipientsConfig; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.DeviceActivityNotificationRuleTriggerConfig; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerConfig; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig; |
|||
import org.thingsboard.server.common.data.sync.ie.EntityExportData; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.sync.vc.data.EntitiesExportCtx; |
|||
|
|||
import java.util.LinkedHashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
import java.util.UUID; |
|||
import java.util.stream.Collectors; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
public class NotificationRuleExportService<I extends EntityId, E extends ExportableEntity<I>, D extends EntityExportData<E>> extends BaseEntityExportService<NotificationRuleId, NotificationRule, EntityExportData<NotificationRule>> { |
|||
|
|||
@Override |
|||
protected void setRelatedEntities(EntitiesExportCtx<?> ctx, NotificationRule notificationRule, EntityExportData<NotificationRule> exportData) { |
|||
notificationRule.setTemplateId(getExternalIdOrElseInternal(ctx, notificationRule.getTemplateId())); |
|||
|
|||
NotificationRuleTriggerConfig ruleTriggerConfig = notificationRule.getTriggerConfig(); |
|||
switch (ruleTriggerConfig.getTriggerType()) { |
|||
case DEVICE_ACTIVITY: { |
|||
DeviceActivityNotificationRuleTriggerConfig triggerConfig = (DeviceActivityNotificationRuleTriggerConfig) ruleTriggerConfig; |
|||
Set<UUID> devices = triggerConfig.getDevices(); |
|||
if (devices != null) { |
|||
triggerConfig.setDevices(toExternalIds(devices, DeviceId::new, ctx).collect(Collectors.toSet())); |
|||
} |
|||
|
|||
Set<UUID> deviceProfiles = triggerConfig.getDeviceProfiles(); |
|||
if (deviceProfiles != null) { |
|||
triggerConfig.setDeviceProfiles(toExternalIds(deviceProfiles, DeviceProfileId::new, ctx).collect(Collectors.toSet())); |
|||
} |
|||
break; |
|||
} |
|||
case RULE_ENGINE_COMPONENT_LIFECYCLE_EVENT: |
|||
RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig triggerConfig = (RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig) ruleTriggerConfig; |
|||
Set<UUID> ruleChains = triggerConfig.getRuleChains(); |
|||
if (ruleChains != null) { |
|||
triggerConfig.setRuleChains(toExternalIds(ruleChains, RuleChainId::new, ctx).collect(Collectors.toSet())); |
|||
} |
|||
break; |
|||
} |
|||
|
|||
NotificationRuleRecipientsConfig ruleRecipientsConfig = notificationRule.getRecipientsConfig(); |
|||
switch (ruleTriggerConfig.getTriggerType()) { |
|||
case ALARM: { |
|||
EscalatedNotificationRuleRecipientsConfig recipientsConfig = (EscalatedNotificationRuleRecipientsConfig) ruleRecipientsConfig; |
|||
Map<Integer, List<UUID>> escalationTable = new LinkedHashMap<>(recipientsConfig.getEscalationTable()); |
|||
escalationTable.replaceAll((delay, targets) -> { |
|||
return toExternalIds(targets, NotificationTargetId::new, ctx).collect(Collectors.toList()); |
|||
}); |
|||
recipientsConfig.setEscalationTable(escalationTable); |
|||
break; |
|||
} |
|||
default: { |
|||
DefaultNotificationRuleRecipientsConfig recipientsConfig = (DefaultNotificationRuleRecipientsConfig) ruleRecipientsConfig; |
|||
List<UUID> targets = recipientsConfig.getTargets(); |
|||
targets = toExternalIds(targets, NotificationTargetId::new, ctx).collect(Collectors.toList()); |
|||
recipientsConfig.setTargets(targets); |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Set<EntityType> getSupportedEntityTypes() { |
|||
return Set.of(EntityType.NOTIFICATION_RULE); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,55 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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.service.sync.ie.exporting.impl; |
|||
|
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.NotificationTargetId; |
|||
import org.thingsboard.server.common.data.notification.targets.NotificationTarget; |
|||
import org.thingsboard.server.common.data.notification.targets.NotificationTargetType; |
|||
import org.thingsboard.server.common.data.notification.targets.platform.CustomerUsersFilter; |
|||
import org.thingsboard.server.common.data.notification.targets.platform.PlatformUsersNotificationTargetConfig; |
|||
import org.thingsboard.server.common.data.notification.targets.platform.UsersFilter; |
|||
import org.thingsboard.server.common.data.sync.ie.EntityExportData; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.sync.vc.data.EntitiesExportCtx; |
|||
|
|||
import java.util.Set; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
public class NotificationTargetExportService extends BaseEntityExportService<NotificationTargetId, NotificationTarget, EntityExportData<NotificationTarget>> { |
|||
|
|||
@Override |
|||
protected void setRelatedEntities(EntitiesExportCtx<?> ctx, NotificationTarget notificationTarget, EntityExportData<NotificationTarget> exportData) { |
|||
if (notificationTarget.getConfiguration().getType() == NotificationTargetType.PLATFORM_USERS) { |
|||
UsersFilter usersFilter = ((PlatformUsersNotificationTargetConfig) notificationTarget.getConfiguration()).getUsersFilter(); |
|||
switch (usersFilter.getType()) { |
|||
case CUSTOMER_USERS: |
|||
CustomerUsersFilter customerUsersFilter = (CustomerUsersFilter) usersFilter; |
|||
customerUsersFilter.setCustomerId(getExternalIdOrElseInternal(ctx, new CustomerId(customerUsersFilter.getCustomerId())).getId()); |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Set<EntityType> getSupportedEntityTypes() { |
|||
return Set.of(EntityType.NOTIFICATION_TARGET); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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.service.sync.ie.exporting.impl; |
|||
|
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.id.NotificationTemplateId; |
|||
import org.thingsboard.server.common.data.notification.template.NotificationTemplate; |
|||
import org.thingsboard.server.common.data.sync.ie.EntityExportData; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.sync.vc.data.EntitiesExportCtx; |
|||
|
|||
import java.util.Set; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
public class NotificationTemplateExportService extends BaseEntityExportService<NotificationTemplateId, NotificationTemplate, EntityExportData<NotificationTemplate>> { |
|||
|
|||
@Override |
|||
protected void setRelatedEntities(EntitiesExportCtx<?> ctx, NotificationTemplate notificationTemplate, EntityExportData<NotificationTemplate> exportData) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public Set<EntityType> getSupportedEntityTypes() { |
|||
return Set.of(EntityType.NOTIFICATION_TEMPLATE); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,150 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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.service.sync.ie.importing.impl; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|||
import org.thingsboard.server.common.data.id.NotificationRuleId; |
|||
import org.thingsboard.server.common.data.id.NotificationTargetId; |
|||
import org.thingsboard.server.common.data.id.RuleChainId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.id.UUIDBased; |
|||
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.NotificationRuleRecipientsConfig; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.DeviceActivityNotificationRuleTriggerConfig; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerConfig; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.NotificationRuleTriggerType; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig; |
|||
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
|||
import org.thingsboard.server.common.data.sync.ie.EntityExportData; |
|||
import org.thingsboard.server.dao.notification.NotificationRuleService; |
|||
import org.thingsboard.server.dao.service.ConstraintValidator; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.sync.vc.data.EntitiesImportCtx; |
|||
|
|||
import java.util.LinkedHashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
import java.util.UUID; |
|||
import java.util.stream.Collectors; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class NotificationRuleImportService extends BaseEntityImportService<NotificationRuleId, NotificationRule, EntityExportData<NotificationRule>> { |
|||
|
|||
private final NotificationRuleService notificationRuleService; |
|||
|
|||
@Override |
|||
protected void setOwner(TenantId tenantId, NotificationRule notificationRule, IdProvider idProvider) { |
|||
notificationRule.setTenantId(tenantId); |
|||
} |
|||
|
|||
@Override |
|||
protected NotificationRule prepare(EntitiesImportCtx ctx, NotificationRule notificationRule, NotificationRule oldNotificationRule, EntityExportData<NotificationRule> exportData, IdProvider idProvider) { |
|||
notificationRule.setTemplateId(idProvider.getInternalId(notificationRule.getTemplateId())); |
|||
|
|||
NotificationRuleTriggerConfig ruleTriggerConfig = notificationRule.getTriggerConfig(); |
|||
NotificationRuleTriggerType triggerType = ruleTriggerConfig.getTriggerType(); |
|||
switch (triggerType) { |
|||
case DEVICE_ACTIVITY: { |
|||
DeviceActivityNotificationRuleTriggerConfig triggerConfig = (DeviceActivityNotificationRuleTriggerConfig) ruleTriggerConfig; |
|||
Set<UUID> devices = triggerConfig.getDevices(); |
|||
if (devices != null) { |
|||
triggerConfig.setDevices(devices.stream().map(DeviceId::new) |
|||
.map(idProvider::getInternalId).map(UUIDBased::getId) |
|||
.collect(Collectors.toSet())); |
|||
} |
|||
|
|||
Set<UUID> deviceProfiles = triggerConfig.getDeviceProfiles(); |
|||
if (deviceProfiles != null) { |
|||
triggerConfig.setDeviceProfiles(deviceProfiles.stream().map(DeviceProfileId::new) |
|||
.map(idProvider::getInternalId).map(UUIDBased::getId) |
|||
.collect(Collectors.toSet())); |
|||
} |
|||
break; |
|||
} |
|||
case RULE_ENGINE_COMPONENT_LIFECYCLE_EVENT: |
|||
RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig triggerConfig = (RuleEngineComponentLifecycleEventNotificationRuleTriggerConfig) ruleTriggerConfig; |
|||
Set<UUID> ruleChains = triggerConfig.getRuleChains(); |
|||
if (ruleChains != null) { |
|||
triggerConfig.setRuleChains(ruleChains.stream().map(RuleChainId::new) |
|||
.map(idProvider::getInternalId).map(UUIDBased::getId) |
|||
.collect(Collectors.toSet())); |
|||
} |
|||
break; |
|||
} |
|||
if (!triggerType.isTenantLevel()) { |
|||
throw new IllegalArgumentException("Trigger type " + triggerType + " is not available for tenants"); |
|||
} |
|||
|
|||
NotificationRuleRecipientsConfig ruleRecipientsConfig = notificationRule.getRecipientsConfig(); |
|||
switch (triggerType) { |
|||
case ALARM: { |
|||
EscalatedNotificationRuleRecipientsConfig recipientsConfig = (EscalatedNotificationRuleRecipientsConfig) ruleRecipientsConfig; |
|||
Map<Integer, List<UUID>> escalationTable = new LinkedHashMap<>(recipientsConfig.getEscalationTable()); |
|||
escalationTable.replaceAll((delay, targets) -> targets.stream() |
|||
.map(NotificationTargetId::new).map(idProvider::getInternalId) |
|||
.map(UUIDBased::getId).collect(Collectors.toList())); |
|||
recipientsConfig.setEscalationTable(escalationTable); |
|||
break; |
|||
} |
|||
default: { |
|||
DefaultNotificationRuleRecipientsConfig recipientsConfig = (DefaultNotificationRuleRecipientsConfig) ruleRecipientsConfig; |
|||
List<UUID> targets = recipientsConfig.getTargets().stream() |
|||
.map(NotificationTargetId::new).map(idProvider::getInternalId) |
|||
.map(UUIDBased::getId).collect(Collectors.toList()); |
|||
recipientsConfig.setTargets(targets); |
|||
break; |
|||
} |
|||
} |
|||
return notificationRule; |
|||
} |
|||
|
|||
@Override |
|||
protected NotificationRule saveOrUpdate(EntitiesImportCtx ctx, NotificationRule notificationRule, EntityExportData<NotificationRule> exportData, IdProvider idProvider) { |
|||
ConstraintValidator.validateFields(notificationRule); |
|||
return notificationRuleService.saveNotificationRule(ctx.getTenantId(), notificationRule); |
|||
} |
|||
|
|||
@Override |
|||
protected void onEntitySaved(User user, NotificationRule savedEntity, NotificationRule oldEntity) throws ThingsboardException { |
|||
entityActionService.logEntityAction(user, savedEntity.getId(), savedEntity, null, |
|||
oldEntity == null ? ActionType.ADDED : ActionType.UPDATED, null); |
|||
clusterService.broadcastEntityStateChangeEvent(user.getTenantId(), savedEntity.getId(), |
|||
oldEntity == null ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
|||
} |
|||
|
|||
@Override |
|||
protected NotificationRule deepCopy(NotificationRule notificationRule) { |
|||
return new NotificationRule(notificationRule); |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.NOTIFICATION_RULE; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,109 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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.service.sync.ie.importing.impl; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.apache.commons.collections.CollectionUtils; |
|||
import org.springframework.security.access.AccessDeniedException; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
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.id.UserId; |
|||
import org.thingsboard.server.common.data.notification.targets.NotificationTarget; |
|||
import org.thingsboard.server.common.data.notification.targets.NotificationTargetType; |
|||
import org.thingsboard.server.common.data.notification.targets.platform.CustomerUsersFilter; |
|||
import org.thingsboard.server.common.data.notification.targets.platform.PlatformUsersNotificationTargetConfig; |
|||
import org.thingsboard.server.common.data.notification.targets.platform.TenantAdministratorsFilter; |
|||
import org.thingsboard.server.common.data.notification.targets.platform.UserListFilter; |
|||
import org.thingsboard.server.common.data.notification.targets.platform.UsersFilter; |
|||
import org.thingsboard.server.common.data.sync.ie.EntityExportData; |
|||
import org.thingsboard.server.dao.notification.NotificationTargetService; |
|||
import org.thingsboard.server.dao.service.ConstraintValidator; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.sync.vc.data.EntitiesImportCtx; |
|||
|
|||
import java.util.stream.Collectors; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class NotificationTargetImportService extends BaseEntityImportService<NotificationTargetId, NotificationTarget, EntityExportData<NotificationTarget>> { |
|||
|
|||
private final NotificationTargetService notificationTargetService; |
|||
|
|||
@Override |
|||
protected void setOwner(TenantId tenantId, NotificationTarget notificationTarget, IdProvider idProvider) { |
|||
notificationTarget.setTenantId(tenantId); |
|||
} |
|||
|
|||
@Override |
|||
protected NotificationTarget prepare(EntitiesImportCtx ctx, NotificationTarget notificationTarget, NotificationTarget oldNotificationTarget, EntityExportData<NotificationTarget> exportData, IdProvider idProvider) { |
|||
if (notificationTarget.getConfiguration().getType() == NotificationTargetType.PLATFORM_USERS) { |
|||
UsersFilter usersFilter = ((PlatformUsersNotificationTargetConfig) notificationTarget.getConfiguration()).getUsersFilter(); |
|||
switch (usersFilter.getType()) { |
|||
case CUSTOMER_USERS: |
|||
CustomerUsersFilter customerUsersFilter = (CustomerUsersFilter) usersFilter; |
|||
customerUsersFilter.setCustomerId(idProvider.getInternalId(new CustomerId(customerUsersFilter.getCustomerId())).getId()); |
|||
break; |
|||
case USER_LIST: |
|||
UserListFilter userListFilter = (UserListFilter) usersFilter; |
|||
userListFilter.setUsersIds(userListFilter.getUsersIds().stream() |
|||
.map(UserId::new).map(idProvider::getInternalId) |
|||
.map(UUIDBased::getId).collect(Collectors.toList()) |
|||
); |
|||
break; |
|||
case TENANT_ADMINISTRATORS: |
|||
if (CollectionUtils.isNotEmpty(((TenantAdministratorsFilter) usersFilter).getTenantsIds()) || |
|||
CollectionUtils.isNotEmpty(((TenantAdministratorsFilter) usersFilter).getTenantProfilesIds())) { |
|||
throw new IllegalArgumentException("Permission denied"); |
|||
} |
|||
break; |
|||
case SYSTEM_ADMINISTRATORS: |
|||
throw new AccessDeniedException("Permission denied"); |
|||
} |
|||
} |
|||
return notificationTarget; |
|||
} |
|||
|
|||
@Override |
|||
protected NotificationTarget saveOrUpdate(EntitiesImportCtx ctx, NotificationTarget notificationTarget, EntityExportData<NotificationTarget> exportData, IdProvider idProvider) { |
|||
ConstraintValidator.validateFields(notificationTarget); |
|||
return notificationTargetService.saveNotificationTarget(ctx.getTenantId(), notificationTarget); |
|||
} |
|||
|
|||
@Override |
|||
protected void onEntitySaved(User user, NotificationTarget savedEntity, NotificationTarget oldEntity) throws ThingsboardException { |
|||
entityActionService.logEntityAction(user, savedEntity.getId(), savedEntity, null, |
|||
oldEntity == null ? ActionType.ADDED : ActionType.UPDATED, null); |
|||
} |
|||
|
|||
@Override |
|||
protected NotificationTarget deepCopy(NotificationTarget notificationTarget) { |
|||
return new NotificationTarget(notificationTarget); |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.NOTIFICATION_TARGET; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,72 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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.service.sync.ie.importing.impl; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.audit.ActionType; |
|||
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|||
import org.thingsboard.server.common.data.id.NotificationTemplateId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.notification.template.NotificationTemplate; |
|||
import org.thingsboard.server.common.data.sync.ie.EntityExportData; |
|||
import org.thingsboard.server.dao.notification.NotificationTemplateService; |
|||
import org.thingsboard.server.dao.service.ConstraintValidator; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.sync.vc.data.EntitiesImportCtx; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class NotificationTemplateImportService extends BaseEntityImportService<NotificationTemplateId, NotificationTemplate, EntityExportData<NotificationTemplate>> { |
|||
|
|||
private final NotificationTemplateService notificationTemplateService; |
|||
|
|||
@Override |
|||
protected void setOwner(TenantId tenantId, NotificationTemplate notificationTemplate, IdProvider idProvider) { |
|||
notificationTemplate.setTenantId(tenantId); |
|||
} |
|||
|
|||
@Override |
|||
protected NotificationTemplate prepare(EntitiesImportCtx ctx, NotificationTemplate notificationTemplate, NotificationTemplate oldEntity, EntityExportData<NotificationTemplate> exportData, IdProvider idProvider) { |
|||
return notificationTemplate; |
|||
} |
|||
|
|||
@Override |
|||
protected NotificationTemplate saveOrUpdate(EntitiesImportCtx ctx, NotificationTemplate notificationTemplate, EntityExportData<NotificationTemplate> exportData, IdProvider idProvider) { |
|||
ConstraintValidator.validateFields(notificationTemplate); |
|||
return notificationTemplateService.saveNotificationTemplate(ctx.getTenantId(), notificationTemplate); |
|||
} |
|||
|
|||
@Override |
|||
protected void onEntitySaved(User user, NotificationTemplate savedEntity, NotificationTemplate oldEntity) throws ThingsboardException { |
|||
entityActionService.logEntityAction(user, savedEntity.getId(), savedEntity, null, |
|||
oldEntity == null ? ActionType.ADDED : ActionType.UPDATED, null); |
|||
} |
|||
|
|||
@Override |
|||
protected NotificationTemplate deepCopy(NotificationTemplate notificationTemplate) { |
|||
return new NotificationTemplate(notificationTemplate); |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return EntityType.NOTIFICATION_TEMPLATE; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,108 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.id.UUIDBased; |
|||
import org.thingsboard.server.common.data.validation.NoXss; |
|||
|
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.IOException; |
|||
import java.util.Arrays; |
|||
import java.util.Objects; |
|||
import java.util.function.Consumer; |
|||
import java.util.function.Supplier; |
|||
|
|||
/** |
|||
* Created by ashvayka on 19.02.18. |
|||
*/ |
|||
@Slf4j |
|||
public abstract class BaseDataWithAdditionalInfo<I extends UUIDBased> extends BaseData<I> implements HasAdditionalInfo { |
|||
|
|||
public static final ObjectMapper mapper = new ObjectMapper(); |
|||
@NoXss |
|||
private transient JsonNode additionalInfo; |
|||
@JsonIgnore |
|||
private byte[] additionalInfoBytes; |
|||
|
|||
public BaseDataWithAdditionalInfo() { |
|||
super(); |
|||
} |
|||
|
|||
public BaseDataWithAdditionalInfo(I id) { |
|||
super(id); |
|||
} |
|||
|
|||
public BaseDataWithAdditionalInfo(BaseDataWithAdditionalInfo<I> baseData) { |
|||
super(baseData); |
|||
setAdditionalInfo(baseData.getAdditionalInfo()); |
|||
} |
|||
|
|||
@Override |
|||
public JsonNode getAdditionalInfo() { |
|||
return getJson(() -> additionalInfo, () -> additionalInfoBytes); |
|||
} |
|||
|
|||
public void setAdditionalInfo(JsonNode addInfo) { |
|||
setJson(addInfo, json -> this.additionalInfo = json, bytes -> this.additionalInfoBytes = bytes); |
|||
} |
|||
|
|||
@Override |
|||
public boolean equals(Object o) { |
|||
if (this == o) return true; |
|||
if (o == null || getClass() != o.getClass()) return false; |
|||
if (!super.equals(o)) return false; |
|||
BaseDataWithAdditionalInfo<?> that = (BaseDataWithAdditionalInfo<?>) o; |
|||
return Arrays.equals(additionalInfoBytes, that.additionalInfoBytes); |
|||
} |
|||
|
|||
@Override |
|||
public int hashCode() { |
|||
return Objects.hash(super.hashCode(), additionalInfoBytes); |
|||
} |
|||
|
|||
public static JsonNode getJson(Supplier<JsonNode> jsonData, Supplier<byte[]> binaryData) { |
|||
JsonNode json = jsonData.get(); |
|||
if (json != null) { |
|||
return json; |
|||
} else { |
|||
byte[] data = binaryData.get(); |
|||
if (data != null) { |
|||
try { |
|||
return mapper.readTree(new ByteArrayInputStream(data)); |
|||
} catch (IOException e) { |
|||
log.warn("Can't deserialize json data: ", e); |
|||
return null; |
|||
} |
|||
} else { |
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
|
|||
public static void setJson(JsonNode json, Consumer<JsonNode> jsonConsumer, Consumer<byte[]> bytesConsumer) { |
|||
jsonConsumer.accept(json); |
|||
try { |
|||
bytesConsumer.accept(mapper.writeValueAsBytes(json)); |
|||
} catch (JsonProcessingException e) { |
|||
log.warn("Can't serialize json data: ", e); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue