Browse Source

refactoring: EntityRelationController fix bug sendRelationNotification

pull/6593/head
nickAS21 4 years ago
parent
commit
d00234b3a2
  1. 26
      application/src/main/java/org/thingsboard/server/service/entitiy/DefaultTbNotificationEntityService.java
  2. 8
      application/src/main/java/org/thingsboard/server/service/entitiy/TbNotificationEntityService.java
  3. 6
      application/src/main/java/org/thingsboard/server/service/entitiy/deviceProfile/DefaultTbDeviceProfileService.java
  4. 24
      application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/DefaultTbEntityRelationService.java

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

@ -24,6 +24,7 @@ import org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMs
import org.thingsboard.server.cluster.TbClusterService;
import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.HasName;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.alarm.Alarm;
@ -37,6 +38,7 @@ import org.thingsboard.server.common.data.id.EdgeId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.security.DeviceCredentials;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgDataType;
@ -203,12 +205,30 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS
}
@Override
public <E extends HasName, I extends EntityId> void notifyCreateOrUpdateOrDelete(I entityId, E entity, SecurityUser user,
public <E extends HasName, I extends EntityId> void notifyCreateOrUpdateOrDelete(TenantId tenantId, CustomerId customerId,
I entityId, E entity, SecurityUser user,
ActionType actionType, Exception e,
Object... additionalInfo) {
notifyEntity(user.getTenantId(), entityId, entity, null, actionType, user, e, additionalInfo);
notifyEntity(tenantId, entityId, entity, customerId, actionType, user, e, additionalInfo);
if (e == null) {
sendEntityNotificationMsg(user.getTenantId(), entityId, edgeTypeByActionType(actionType));
sendEntityNotificationMsg(tenantId, entityId, edgeTypeByActionType(actionType));
}
}
@Override
public void notifyCreateOrUpdateOrDeleteRelation(TenantId tenantId, CustomerId customerId, EntityId entityId,
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));
}
} catch (Exception e1) {
log.warn("Failed to push relation to core: {}", relation, e1);
}
}

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

@ -28,6 +28,7 @@ import org.thingsboard.server.common.data.id.EdgeId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.security.DeviceCredentials;
import org.thingsboard.server.service.security.model.SecurityUser;
@ -88,7 +89,12 @@ public interface TbNotificationEntityService {
void notifyCreateOrUpdateAlarm(Alarm alarm, ActionType actionType, SecurityUser user, Object... additionalInfo);
<E extends HasName, I extends EntityId> void notifyCreateOrUpdateOrDelete(I entityId, E entity, SecurityUser user,
<E extends HasName, I extends EntityId> 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);
}

6
application/src/main/java/org/thingsboard/server/service/entitiy/deviceProfile/DefaultTbDeviceProfileService.java

@ -60,7 +60,7 @@ public class DefaultTbDeviceProfileService extends AbstractTbEntityService imple
otaPackageStateService.update(savedDeviceProfile, isFirmwareChanged, isSoftwareChanged);
notificationEntityService.notifyCreateOrUpdateOrDelete(savedDeviceProfile.getId(), savedDeviceProfile, user, actionType, null);
notificationEntityService.notifyCreateOrUpdateOrDelete(user.getTenantId(), user.getCustomerId(), savedDeviceProfile.getId(), savedDeviceProfile, user, actionType, null);
return savedDeviceProfile;
} catch (Exception e) {
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.DEVICE_PROFILE), deviceProfile, null,
@ -77,9 +77,9 @@ public class DefaultTbDeviceProfileService extends AbstractTbEntityService imple
tbClusterService.onDeviceProfileDelete(deviceProfile, null);
tbClusterService.broadcastEntityStateChangeEvent(deviceProfile.getTenantId(), deviceProfile.getId(), ComponentLifecycleEvent.DELETED);
notificationEntityService.notifyCreateOrUpdateOrDelete(deviceProfile.getId(), deviceProfile, user, ActionType.DELETED, null, deviceProfile.getId().toString());
notificationEntityService.notifyCreateOrUpdateOrDelete(user.getTenantId(), user.getCustomerId(), deviceProfile.getId(), deviceProfile, user, ActionType.DELETED, null, deviceProfile.getId().toString());
} catch (Exception e) {
notificationEntityService.notifyCreateOrUpdateOrDelete(emptyId(EntityType.DEVICE_PROFILE), null, user, ActionType.DELETED, e, deviceProfile.getId().toString());
notificationEntityService.notifyCreateOrUpdateOrDelete(user.getTenantId(), user.getCustomerId(), emptyId(EntityType.DEVICE_PROFILE), null, user, ActionType.DELETED, e, deviceProfile.getId().toString());
throw handleException(e);
}
}

24
application/src/main/java/org/thingsboard/server/service/entitiy/entityRelation/DefaultTbEntityRelationService.java

@ -37,11 +37,15 @@ public class DefaultTbEntityRelationService extends AbstractTbEntityService impl
TenantId tenantId = user.getTenantId();
try {
relationService.saveRelation(tenantId, relation);
notificationEntityService.notifyCreateOrUpdateOrDelete(relation.getFrom(), null, user, ActionType.RELATION_ADD_OR_UPDATE, null, relation);
notificationEntityService.notifyCreateOrUpdateOrDelete(relation.getTo(), null, user, ActionType.RELATION_ADD_OR_UPDATE, null, 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);
} catch (Exception e) {
notificationEntityService.notifyCreateOrUpdateOrDelete(relation.getFrom(), null, user, ActionType.RELATION_ADD_OR_UPDATE, e, relation);
notificationEntityService.notifyCreateOrUpdateOrDelete(relation.getTo(), null, user, ActionType.RELATION_ADD_OR_UPDATE, e, relation);
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);
throw handleException(e);
}
}
@ -53,11 +57,15 @@ public class DefaultTbEntityRelationService extends AbstractTbEntityService impl
if (!found) {
throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);
}
notificationEntityService.notifyCreateOrUpdateOrDelete(relation.getFrom(), null, user, ActionType.RELATION_DELETED, null, relation);
notificationEntityService.notifyCreateOrUpdateOrDelete(relation.getTo(), null, user, ActionType.RELATION_DELETED, null, relation);
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);
} catch (Exception e) {
notificationEntityService.notifyCreateOrUpdateOrDelete(relation.getFrom(), null, user, ActionType.RELATION_DELETED, e, relation);
notificationEntityService.notifyCreateOrUpdateOrDelete(relation.getTo(), null, user, ActionType.RELATION_DELETED, e, relation);
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);
throw handleException(e);
}
}

Loading…
Cancel
Save