Browse Source

refactoring: EntityRelationController fix bug testRelations

pull/6593/head
nickAS21 4 years ago
parent
commit
aecaca734f
  1. 10
      application/src/main/java/org/thingsboard/server/controller/EntityRelationController.java
  2. 23
      application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java
  3. 23
      application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java
  4. 41
      application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/DefaultTbEntityRelationService.java
  5. 9
      application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/TbEntityRelationService.java

10
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.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; 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.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.EntityIdFactory; import org.thingsboard.server.common.data.id.EntityIdFactory;
@ -87,7 +86,7 @@ public class EntityRelationController extends BaseController {
relation.setTypeGroup(RelationTypeGroup.COMMON); relation.setTypeGroup(RelationTypeGroup.COMMON);
} }
tbEntityRelationService.save(relation, getCurrentUser()); tbEntityRelationService.save(getTenantId(), getCurrentUser().getCustomerId(), relation, getCurrentUser());
} }
@ApiOperation(value = "Delete Relation (deleteRelation)", @ApiOperation(value = "Delete Relation (deleteRelation)",
@ -113,7 +112,7 @@ public class EntityRelationController extends BaseController {
RelationTypeGroup relationTypeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON); RelationTypeGroup relationTypeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON);
EntityRelation relation = new EntityRelation(fromId, toId, strRelationType, relationTypeGroup); EntityRelation relation = new EntityRelation(fromId, toId, strRelationType, relationTypeGroup);
tbEntityRelationService.delete(relation, getCurrentUser()); tbEntityRelationService.delete(getTenantId(), getCurrentUser().getCustomerId(), relation, getCurrentUser());
} }
@ApiOperation(value = "Delete Relations (deleteRelations)", @ApiOperation(value = "Delete Relations (deleteRelations)",
@ -129,10 +128,9 @@ public class EntityRelationController extends BaseController {
EntityId entityId = EntityIdFactory.getByTypeAndId(strType, strId); EntityId entityId = EntityIdFactory.getByTypeAndId(strType, strId);
checkEntityId(entityId, Operation.WRITE); checkEntityId(entityId, Operation.WRITE);
try { try {
relationService.deleteEntityRelations(getTenantId(), entityId); tbEntityRelationService.deleteRelations (getTenantId(), getCurrentUser().getCustomerId(), entityId, getCurrentUser(), null);
logEntityAction(entityId, null, getCurrentUser().getCustomerId(), ActionType.RELATIONS_DELETED, null);
} catch (Exception e) { } catch (Exception e) {
logEntityAction(entityId, null, getCurrentUser().getCustomerId(), ActionType.RELATIONS_DELETED, e); tbEntityRelationService.deleteRelations (getTenantId(), getCurrentUser().getCustomerId(), entityId, getCurrentUser(), e);
throw handleException(e); throw handleException(e);
} }
} }

23
application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java

@ -216,19 +216,22 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS
} }
@Override @Override
public void notifyCreateOrUpdateOrDeleteRelation(TenantId tenantId, CustomerId customerId, EntityId entityId, public void notifyCreateOrUpdateOrDeleteRelation(TenantId tenantId, CustomerId customerId,
EntityRelation relation, SecurityUser user, EntityRelation relation, SecurityUser user,
ActionType actionType, Exception e, ActionType actionType, Exception e,
Object... additionalInfo) { Object... additionalInfo) {
notifyEntity(tenantId, entityId, null, customerId, actionType, user, e, additionalInfo); notifyEntity(tenantId, relation.getFrom(), null, customerId, actionType, user, e, additionalInfo);
try { notifyEntity(tenantId, relation.getTo(), null, customerId, actionType, user, e, additionalInfo);
if (!relation.getFrom().getEntityType().equals(EntityType.EDGE) && if (e == null) {
!relation.getTo().getEntityType().equals(EntityType.EDGE)) { try {
sendNotificationMsgToEdgeService(tenantId, null, null, json.writeValueAsString(relation), if (!relation.getFrom().getEntityType().equals(EntityType.EDGE) &&
EdgeEventType.RELATION, edgeTypeByActionType(actionType)); !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; return EdgeEventActionType.DELETED;
case RELATION_ADD_OR_UPDATE: case RELATION_ADD_OR_UPDATE:
return EdgeEventActionType.RELATION_ADD_OR_UPDATE; return EdgeEventActionType.RELATION_ADD_OR_UPDATE;
case RELATION_DELETED:
return EdgeEventActionType.RELATION_DELETED;
default: default:
return null; return null;
} }

23
application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java

@ -50,9 +50,9 @@ public interface TbNotificationEntityService {
SecurityUser user, Object... additionalInfo); SecurityUser user, Object... additionalInfo);
void notifyDeleteAlarm(TenantId tenantId, Alarm alarm, EntityId originatorId, void notifyDeleteAlarm(TenantId tenantId, Alarm alarm, EntityId originatorId,
CustomerId customerId, ActionType actionType, CustomerId customerId, ActionType actionType,
List<EdgeId> relatedEdgeIds, List<EdgeId> relatedEdgeIds,
SecurityUser user, String body, Object... additionalInfo); SecurityUser user, String body, Object... additionalInfo);
<E extends HasName, I extends EntityId> void notifyAssignOrUnassignEntityToCustomer(TenantId tenantId, I entityId, <E extends HasName, I extends EntityId> void notifyAssignOrUnassignEntityToCustomer(TenantId tenantId, I entityId,
CustomerId customerId, E entity, CustomerId customerId, E entity,
@ -89,12 +89,13 @@ public interface TbNotificationEntityService {
void notifyCreateOrUpdateAlarm(Alarm alarm, ActionType actionType, SecurityUser user, Object... additionalInfo); void notifyCreateOrUpdateAlarm(Alarm alarm, ActionType actionType, SecurityUser user, Object... additionalInfo);
<E extends HasName, I extends EntityId> void notifyCreateOrUpdateOrDelete(TenantId tenantId, CustomerId customerId, <E extends HasName, I extends EntityId> void notifyCreateOrUpdateOrDelete(TenantId tenantId, CustomerId customerId,
I entityId, E entity, SecurityUser user, I entityId, E entity, SecurityUser user,
ActionType actionType, Exception e, ActionType actionType, Exception e,
Object... additionalInfo); Object... additionalInfo);
void notifyCreateOrUpdateOrDeleteRelation (TenantId tenantId, CustomerId customerId, EntityId entityId,
EntityRelation relation, SecurityUser user, void notifyCreateOrUpdateOrDeleteRelation(TenantId tenantId, CustomerId customerId,
ActionType actionType, Exception e, EntityRelation relation, SecurityUser user,
Object... additionalInfo); ActionType actionType, Exception e,
Object... additionalInfo);
} }

41
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.audit.ActionType;
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
import org.thingsboard.server.common.data.exception.ThingsboardException; 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.id.TenantId;
import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
@ -33,40 +35,39 @@ import org.thingsboard.server.service.security.model.SecurityUser;
@Slf4j @Slf4j
public class DefaultTbEntityRelationService extends AbstractTbEntityService implements TbEntityRelationService { public class DefaultTbEntityRelationService extends AbstractTbEntityService implements TbEntityRelationService {
@Override @Override
public void save(EntityRelation relation, SecurityUser user) throws ThingsboardException { public void save(TenantId tenantId, CustomerId customerId, EntityRelation relation, SecurityUser user) throws ThingsboardException {
TenantId tenantId = user.getTenantId();
try { try {
relationService.saveRelation(tenantId, relation); relationService.saveRelation(tenantId, relation);
notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (user.getTenantId(), user.getCustomerId(), notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId,
relation.getFrom(), relation, user, ActionType.RELATION_ADD_OR_UPDATE, null, relation); 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);
} catch (Exception e) { } catch (Exception e) {
notificationEntityService.notifyCreateOrUpdateOrDelete(user.getTenantId(), user.getCustomerId(), notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId,
relation.getFrom(), null, user, ActionType.RELATION_ADD_OR_UPDATE, e, relation); relation, 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);
throw handleException(e); throw handleException(e);
} }
} }
@Override @Override
public void delete(EntityRelation relation, SecurityUser user) throws ThingsboardException { public void delete(TenantId tenantId, CustomerId customerId, EntityRelation relation, SecurityUser user) throws ThingsboardException {
try { 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) { if (!found) {
throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);
} }
notificationEntityService.notifyCreateOrUpdateOrDeleteRelation(user.getTenantId(), user.getCustomerId(), notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId,
relation.getFrom(), relation, user, ActionType.RELATION_DELETED, null, relation); relation, user, ActionType.RELATION_DELETED, null, relation);
notificationEntityService.notifyCreateOrUpdateOrDeleteRelation(user.getTenantId(), user.getCustomerId(),
relation.getTo(), relation, user, ActionType.RELATION_DELETED, null, relation);
} catch (Exception e) { } catch (Exception e) {
notificationEntityService.notifyCreateOrUpdateOrDelete(user.getTenantId(), user.getCustomerId(), notificationEntityService.notifyCreateOrUpdateOrDeleteRelation (tenantId, customerId,
relation.getFrom(), null, user, ActionType.RELATION_DELETED, e, relation); relation, user, ActionType.RELATION_DELETED, e, relation);
notificationEntityService.notifyCreateOrUpdateOrDelete(user.getTenantId(), user.getCustomerId(),
relation.getTo(), null, user, ActionType.RELATION_DELETED, e, relation);
throw handleException(e); 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);
}
} }

9
application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/TbEntityRelationService.java

@ -16,13 +16,18 @@
package org.thingsboard.server.service.entitiy.entityRelation; package org.thingsboard.server.service.entitiy.entityRelation;
import org.thingsboard.server.common.data.exception.ThingsboardException; 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.common.data.relation.EntityRelation;
import org.thingsboard.server.service.security.model.SecurityUser; import org.thingsboard.server.service.security.model.SecurityUser;
public interface TbEntityRelationService { 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;
} }

Loading…
Cancel
Save