From aecaca734f1385cc54d57f297669356e72bb45ee Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Tue, 24 May 2022 23:33:39 +0300 Subject: [PATCH] refactoring: EntityRelationController fix bug testRelations --- .../controller/EntityRelationController.java | 10 ++--- .../DefaultTbNotificationEntityService.java | 23 +++++++---- .../entitiy/TbNotificationEntityService.java | 23 ++++++----- .../DefaultTbEntityRelationService.java | 41 ++++++++++--------- .../TbEntityRelationService.java | 9 +++- 5 files changed, 58 insertions(+), 48 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/EntityRelationController.java b/application/src/main/java/org/thingsboard/server/controller/EntityRelationController.java index b1b39ef5bd..54a891e1f8 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EntityRelationController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EntityRelationController.java @@ -28,7 +28,6 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; -import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityIdFactory; @@ -87,7 +86,7 @@ public class EntityRelationController extends BaseController { relation.setTypeGroup(RelationTypeGroup.COMMON); } - tbEntityRelationService.save(relation, getCurrentUser()); + tbEntityRelationService.save(getTenantId(), getCurrentUser().getCustomerId(), relation, getCurrentUser()); } @ApiOperation(value = "Delete Relation (deleteRelation)", @@ -113,7 +112,7 @@ public class EntityRelationController extends BaseController { RelationTypeGroup relationTypeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON); EntityRelation relation = new EntityRelation(fromId, toId, strRelationType, relationTypeGroup); - tbEntityRelationService.delete(relation, getCurrentUser()); + tbEntityRelationService.delete(getTenantId(), getCurrentUser().getCustomerId(), relation, getCurrentUser()); } @ApiOperation(value = "Delete Relations (deleteRelations)", @@ -129,10 +128,9 @@ public class EntityRelationController extends BaseController { EntityId entityId = EntityIdFactory.getByTypeAndId(strType, strId); checkEntityId(entityId, Operation.WRITE); try { - relationService.deleteEntityRelations(getTenantId(), entityId); - logEntityAction(entityId, null, getCurrentUser().getCustomerId(), ActionType.RELATIONS_DELETED, null); + tbEntityRelationService.deleteRelations (getTenantId(), getCurrentUser().getCustomerId(), entityId, getCurrentUser(), null); } catch (Exception e) { - logEntityAction(entityId, null, getCurrentUser().getCustomerId(), ActionType.RELATIONS_DELETED, e); + tbEntityRelationService.deleteRelations (getTenantId(), getCurrentUser().getCustomerId(), entityId, getCurrentUser(), e); throw handleException(e); } } diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java index f9ab7401fc..f9af011013 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java @@ -216,19 +216,22 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS } @Override - public void notifyCreateOrUpdateOrDeleteRelation(TenantId tenantId, CustomerId customerId, EntityId entityId, + public void notifyCreateOrUpdateOrDeleteRelation(TenantId tenantId, CustomerId customerId, EntityRelation relation, SecurityUser user, ActionType actionType, Exception e, Object... additionalInfo) { - notifyEntity(tenantId, entityId, null, customerId, actionType, user, e, additionalInfo); - try { - if (!relation.getFrom().getEntityType().equals(EntityType.EDGE) && - !relation.getTo().getEntityType().equals(EntityType.EDGE)) { - sendNotificationMsgToEdgeService(tenantId, null, null, json.writeValueAsString(relation), - EdgeEventType.RELATION, edgeTypeByActionType(actionType)); + notifyEntity(tenantId, relation.getFrom(), null, customerId, actionType, user, e, additionalInfo); + notifyEntity(tenantId, relation.getTo(), null, customerId, actionType, user, e, additionalInfo); + if (e == null) { + try { + if (!relation.getFrom().getEntityType().equals(EntityType.EDGE) && + !relation.getTo().getEntityType().equals(EntityType.EDGE)) { + sendNotificationMsgToEdgeService(tenantId, null, null, json.writeValueAsString(relation), + EdgeEventType.RELATION, edgeTypeByActionType(actionType)); + } + } catch (Exception e1) { + log.warn("Failed to push relation to core: {}", relation, e1); } - } catch (Exception e1) { - log.warn("Failed to push relation to core: {}", relation, e1); } } @@ -329,6 +332,8 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS return EdgeEventActionType.DELETED; case RELATION_ADD_OR_UPDATE: return EdgeEventActionType.RELATION_ADD_OR_UPDATE; + case RELATION_DELETED: + return EdgeEventActionType.RELATION_DELETED; default: return null; } diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java index 65d2db3fd6..97d9b0ca58 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java @@ -50,9 +50,9 @@ public interface TbNotificationEntityService { SecurityUser user, Object... additionalInfo); void notifyDeleteAlarm(TenantId tenantId, Alarm alarm, EntityId originatorId, - CustomerId customerId, ActionType actionType, - List relatedEdgeIds, - SecurityUser user, String body, Object... additionalInfo); + CustomerId customerId, ActionType actionType, + List relatedEdgeIds, + SecurityUser user, String body, Object... additionalInfo); void notifyAssignOrUnassignEntityToCustomer(TenantId tenantId, I entityId, CustomerId customerId, E entity, @@ -89,12 +89,13 @@ public interface TbNotificationEntityService { void notifyCreateOrUpdateAlarm(Alarm alarm, ActionType actionType, SecurityUser user, Object... additionalInfo); - void notifyCreateOrUpdateOrDelete(TenantId tenantId, CustomerId customerId, - I entityId, E entity, SecurityUser user, - ActionType actionType, Exception e, - Object... additionalInfo); - void notifyCreateOrUpdateOrDeleteRelation (TenantId tenantId, CustomerId customerId, EntityId entityId, - EntityRelation relation, SecurityUser user, - ActionType actionType, Exception e, - Object... additionalInfo); + void notifyCreateOrUpdateOrDelete(TenantId tenantId, CustomerId customerId, + I entityId, E entity, SecurityUser user, + ActionType actionType, Exception e, + Object... additionalInfo); + + void notifyCreateOrUpdateOrDeleteRelation(TenantId tenantId, CustomerId customerId, + EntityRelation relation, SecurityUser user, + ActionType actionType, Exception e, + Object... additionalInfo); } diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/DefaultTbEntityRelationService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/DefaultTbEntityRelationService.java index 7c4f688f8c..57fedfab73 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/DefaultTbEntityRelationService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/DefaultTbEntityRelationService.java @@ -21,6 +21,8 @@ import org.springframework.stereotype.Service; import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.queue.util.TbCoreComponent; @@ -33,40 +35,39 @@ import org.thingsboard.server.service.security.model.SecurityUser; @Slf4j public class DefaultTbEntityRelationService extends AbstractTbEntityService implements TbEntityRelationService { @Override - public void save(EntityRelation relation, SecurityUser user) throws ThingsboardException { - TenantId tenantId = user.getTenantId(); + public void save(TenantId tenantId, CustomerId customerId, EntityRelation relation, SecurityUser user) throws ThingsboardException { try { relationService.saveRelation(tenantId, relation); - notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (user.getTenantId(), user.getCustomerId(), - relation.getFrom(), relation, user, ActionType.RELATION_ADD_OR_UPDATE, null, relation); - notificationEntityService.notifyCreateOrUpdateOrDeleteRelation(user.getTenantId(), user.getCustomerId(), - relation.getTo(), relation, user, ActionType.RELATION_ADD_OR_UPDATE, null, relation); + notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId, + relation, user, ActionType.RELATION_ADD_OR_UPDATE, null, relation); } catch (Exception e) { - notificationEntityService.notifyCreateOrUpdateOrDelete(user.getTenantId(), user.getCustomerId(), - relation.getFrom(), null, user, ActionType.RELATION_ADD_OR_UPDATE, e, relation); - notificationEntityService.notifyCreateOrUpdateOrDelete(user.getTenantId(), user.getCustomerId(), - relation.getTo(), null, user, ActionType.RELATION_ADD_OR_UPDATE, e, relation); + notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId, + relation, user, ActionType.RELATION_ADD_OR_UPDATE, e, relation); throw handleException(e); } } @Override - public void delete(EntityRelation relation, SecurityUser user) throws ThingsboardException { + public void delete(TenantId tenantId, CustomerId customerId, EntityRelation relation, SecurityUser user) throws ThingsboardException { try { - Boolean found = relationService.deleteRelation(user.getTenantId(), relation.getFrom(), relation.getTo(), relation.getType(), relation.getTypeGroup()); + Boolean found = relationService.deleteRelation(tenantId, relation.getFrom(), relation.getTo(), relation.getType(), relation.getTypeGroup()); if (!found) { throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); } - notificationEntityService.notifyCreateOrUpdateOrDeleteRelation(user.getTenantId(), user.getCustomerId(), - relation.getFrom(), relation, user, ActionType.RELATION_DELETED, null, relation); - notificationEntityService.notifyCreateOrUpdateOrDeleteRelation(user.getTenantId(), user.getCustomerId(), - relation.getTo(), relation, user, ActionType.RELATION_DELETED, null, relation); + notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId, + relation, user, ActionType.RELATION_DELETED, null, relation); } catch (Exception e) { - notificationEntityService.notifyCreateOrUpdateOrDelete(user.getTenantId(), user.getCustomerId(), - relation.getFrom(), null, user, ActionType.RELATION_DELETED, e, relation); - notificationEntityService.notifyCreateOrUpdateOrDelete(user.getTenantId(), user.getCustomerId(), - relation.getTo(), null, user, ActionType.RELATION_DELETED, e, relation); + notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId, + relation, user, ActionType.RELATION_DELETED, e, relation); throw handleException(e); } } + + @Override + public void deleteRelations(TenantId tenantId, CustomerId customerId, EntityId entityId, SecurityUser user, Exception e) throws ThingsboardException { + if (e == null) { + relationService.deleteEntityRelations(tenantId, entityId); + } + notificationEntityService.notifyEntity(tenantId, entityId, null, customerId, ActionType.RELATIONS_DELETED, user, e); + } } diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/TbEntityRelationService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/TbEntityRelationService.java index 8bcb58c777..b4adcfd913 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/TbEntityRelationService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/TbEntityRelationService.java @@ -16,13 +16,18 @@ package org.thingsboard.server.service.entitiy.entityRelation; import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.id.EntityId; +import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.service.security.model.SecurityUser; public interface TbEntityRelationService { - void save(EntityRelation entity, SecurityUser user) throws ThingsboardException; + void save(TenantId tenantId, CustomerId customerId, EntityRelation entity, SecurityUser user) throws ThingsboardException; - void delete(EntityRelation entity, SecurityUser user) throws ThingsboardException; + void delete(TenantId tenantId, CustomerId customerId, EntityRelation entity, SecurityUser user) throws ThingsboardException; + + void deleteRelations (TenantId tenantId, CustomerId customerId, EntityId entityId, SecurityUser user, Exception e) throws ThingsboardException; }