|
|
|
@ -44,8 +44,6 @@ import org.thingsboard.server.dao.timeseries.TimeseriesService; |
|
|
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
|
import org.thingsboard.server.service.entitiy.AbstractTbEntityService; |
|
|
|
import org.thingsboard.server.service.security.model.SecurityUser; |
|
|
|
import org.thingsboard.server.service.security.permission.Operation; |
|
|
|
import org.thingsboard.server.service.security.permission.Resource; |
|
|
|
|
|
|
|
import javax.annotation.Nullable; |
|
|
|
import java.util.ArrayList; |
|
|
|
@ -56,7 +54,6 @@ import java.util.concurrent.ExecutionException; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import static org.apache.commons.lang3.StringUtils.isBlank; |
|
|
|
import static org.thingsboard.server.dao.service.Validator.validateId; |
|
|
|
|
|
|
|
@Service |
|
|
|
@TbCoreComponent |
|
|
|
@ -67,16 +64,11 @@ public class DefaultTbEntityViewService extends AbstractTbEntityService implemen |
|
|
|
private final TimeseriesService tsService; |
|
|
|
|
|
|
|
@Override |
|
|
|
public EntityView save(EntityView entityView, SecurityUser user) throws ThingsboardException { |
|
|
|
public EntityView save(EntityView entityView, EntityView existingEntityView, EntityView savedEntityView, SecurityUser user) throws ThingsboardException { |
|
|
|
ActionType actionType = entityView.getId() == null ? ActionType.ADDED : ActionType.UPDATED; |
|
|
|
try { |
|
|
|
List<ListenableFuture<?>> futures = new ArrayList<>(); |
|
|
|
|
|
|
|
if (entityView.getId() == null) { |
|
|
|
accessControlService |
|
|
|
.checkPermission(user, Resource.ENTITY_VIEW, Operation.CREATE, null, entityView); |
|
|
|
} else { |
|
|
|
EntityView existingEntityView = checkEntityViewId(entityView.getId(), Operation.WRITE, user); |
|
|
|
if (existingEntityView != null) { |
|
|
|
if (existingEntityView.getKeys() != null) { |
|
|
|
if (existingEntityView.getKeys().getAttributes() != null) { |
|
|
|
futures.add(deleteAttributesFromEntityView(existingEntityView, DataConstants.CLIENT_SCOPE, existingEntityView.getKeys().getAttributes().getCs(), user)); |
|
|
|
@ -89,7 +81,6 @@ public class DefaultTbEntityViewService extends AbstractTbEntityService implemen |
|
|
|
futures.add(deleteLatestFromEntityView(existingEntityView, tsKeys, user)); |
|
|
|
} |
|
|
|
|
|
|
|
EntityView savedEntityView = checkNotNull(entityViewService.saveEntityView(entityView)); |
|
|
|
if (savedEntityView.getKeys() != null) { |
|
|
|
if (savedEntityView.getKeys().getAttributes() != null) { |
|
|
|
futures.add(copyAttributesFromEntityToEntityView(savedEntityView, DataConstants.CLIENT_SCOPE, savedEntityView.getKeys().getAttributes().getCs(), user)); |
|
|
|
@ -132,68 +123,65 @@ public class DefaultTbEntityViewService extends AbstractTbEntityService implemen |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public EntityView assignEntityViewToCustomer(TenantId tenantId, EntityViewId entityViewId, Customer customer, SecurityUser user) throws ThingsboardException { |
|
|
|
public EntityView assignEntityViewToCustomer(TenantId tenantId, EntityView savedEntityView, Customer customer, SecurityUser user) throws ThingsboardException { |
|
|
|
ActionType actionType = ActionType.ASSIGNED_TO_CUSTOMER; |
|
|
|
CustomerId customerId = customer.getId(); |
|
|
|
try { |
|
|
|
EntityView savedEntityView = checkNotNull(entityViewService.assignEntityViewToCustomer(tenantId, entityViewId, customerId)); |
|
|
|
notificationEntityService.notifyAssignOrUnassignEntityToCustomer(tenantId, entityViewId, customerId, savedEntityView, |
|
|
|
try { |
|
|
|
notificationEntityService.notifyAssignOrUnassignEntityToCustomer(tenantId, savedEntityView.getEntityId(), customerId, savedEntityView, |
|
|
|
actionType, EdgeEventActionType.ASSIGNED_TO_CUSTOMER, user, true, customerId.toString(), customer.getName()); |
|
|
|
return savedEntityView; |
|
|
|
} catch (Exception e) { |
|
|
|
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.ENTITY_VIEW), null, null, |
|
|
|
actionType, user, e, entityViewId.toString(), customerId.toString()); |
|
|
|
actionType, user, e, savedEntityView.getEntityId().toString(), customerId.toString()); |
|
|
|
throw handleException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public EntityView assignEntityViewToPublicCustomer(TenantId tenantId, CustomerId customerId, EntityViewId entityViewId, SecurityUser user) throws ThingsboardException { |
|
|
|
public EntityView assignEntityViewToPublicCustomer(TenantId tenantId, CustomerId customerId, Customer publicCustomer, |
|
|
|
EntityView savedEntityView, SecurityUser user) throws ThingsboardException { |
|
|
|
ActionType actionType = ActionType.ASSIGNED_TO_CUSTOMER; |
|
|
|
try { |
|
|
|
Customer publicCustomer = customerService.findOrCreatePublicCustomer(tenantId); |
|
|
|
EntityView savedEntityView = checkNotNull(entityViewService.assignEntityViewToCustomer(tenantId, entityViewId, publicCustomer.getId())); |
|
|
|
notificationEntityService.notifyAssignOrUnassignEntityToCustomer(tenantId, entityViewId, customerId, savedEntityView, |
|
|
|
actionType, null, user, false, entityViewId.toString(), |
|
|
|
notificationEntityService.notifyAssignOrUnassignEntityToCustomer(tenantId, savedEntityView.getEntityId(), customerId, savedEntityView, |
|
|
|
actionType, null, user, false, savedEntityView.getEntityId().toString(), |
|
|
|
publicCustomer.getId().toString(), publicCustomer.getName()); |
|
|
|
return savedEntityView; |
|
|
|
} catch (Exception e) { |
|
|
|
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.ENTITY_VIEW), null, null, |
|
|
|
actionType, user, e, entityViewId.toString()); |
|
|
|
actionType, user, e, savedEntityView.getEntityId().toString()); |
|
|
|
throw handleException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public EntityView assignEntityViewToEdge(TenantId tenantId, CustomerId customerId, EntityViewId entityViewId, Edge edge, SecurityUser user) throws ThingsboardException { |
|
|
|
public EntityView assignEntityViewToEdge(TenantId tenantId, CustomerId customerId, EntityView savedEntityView, Edge edge, SecurityUser user) throws ThingsboardException { |
|
|
|
ActionType actionType = ActionType.ASSIGNED_TO_EDGE; |
|
|
|
EdgeId edgeId = edge.getId(); |
|
|
|
try { |
|
|
|
EntityView savedEntityView = checkNotNull(entityViewService.assignEntityViewToEdge(tenantId, entityViewId, edgeId)); |
|
|
|
notificationEntityService.notifyAssignOrUnassignEntityToEdge(tenantId, entityViewId, customerId, |
|
|
|
edgeId, savedEntityView, actionType, EdgeEventActionType.ASSIGNED_TO_EDGE, user, entityViewId.toString(), |
|
|
|
notificationEntityService.notifyAssignOrUnassignEntityToEdge(tenantId, savedEntityView.getEntityId(), customerId, |
|
|
|
edgeId, savedEntityView, actionType, EdgeEventActionType.ASSIGNED_TO_EDGE, user, savedEntityView.getEntityId().toString(), |
|
|
|
edgeId.toString(), edge.getName()); |
|
|
|
return savedEntityView; |
|
|
|
} catch (Exception e) { |
|
|
|
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.DEVICE), null, null, |
|
|
|
actionType, user, e, entityViewId.toString(), edgeId.toString()); |
|
|
|
actionType, user, e, savedEntityView.getEntityId().toString(), edgeId.toString()); |
|
|
|
throw handleException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public EntityView unassignEntityViewFromEdge(TenantId tenantId, CustomerId customerId, EntityView entityView, Edge edge, SecurityUser user) throws ThingsboardException { |
|
|
|
public EntityView unassignEntityViewFromEdge(TenantId tenantId, CustomerId customerId, EntityView entityView, |
|
|
|
EntityView savedEntityView, Edge edge, SecurityUser user) throws ThingsboardException { |
|
|
|
ActionType actionType = ActionType.UNASSIGNED_FROM_EDGE; |
|
|
|
EntityViewId entityViewId = entityView.getId(); |
|
|
|
EdgeId edgeId = edge.getId(); |
|
|
|
try { |
|
|
|
EntityView savedDevice = checkNotNull(entityViewService.unassignEntityViewFromEdge(tenantId, entityViewId, edgeId)); |
|
|
|
|
|
|
|
notificationEntityService.notifyAssignOrUnassignEntityToEdge(tenantId, entityViewId, customerId, |
|
|
|
edgeId, entityView, actionType, EdgeEventActionType.UNASSIGNED_FROM_EDGE, user, entityViewId.toString(), |
|
|
|
edgeId.toString(), edge.getName()); |
|
|
|
return savedDevice; |
|
|
|
return savedEntityView; |
|
|
|
} catch (Exception e) { |
|
|
|
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.ENTITY_VIEW), null, null, |
|
|
|
actionType, user, e, entityViewId.toString(), edgeId.toString()); |
|
|
|
@ -202,16 +190,15 @@ public class DefaultTbEntityViewService extends AbstractTbEntityService implemen |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public EntityView unassignEntityViewFromCustomer(TenantId tenantId, EntityView entityView, Customer customer, SecurityUser user) throws ThingsboardException { |
|
|
|
public EntityView unassignEntityViewFromCustomer(TenantId tenantId, EntityView savedEntityView, Customer customer, SecurityUser user) throws ThingsboardException { |
|
|
|
ActionType actionType = ActionType.UNASSIGNED_FROM_CUSTOMER; |
|
|
|
try { |
|
|
|
EntityView savedEntityView = checkNotNull(entityViewService.unassignEntityViewFromCustomer(tenantId, entityView.getId())); |
|
|
|
notificationEntityService.notifyAssignOrUnassignEntityToCustomer(tenantId, entityView.getId(), customer.getId(), savedEntityView, |
|
|
|
notificationEntityService.notifyAssignOrUnassignEntityToCustomer(tenantId, savedEntityView.getEntityId(), customer.getId(), savedEntityView, |
|
|
|
actionType, EdgeEventActionType.UNASSIGNED_FROM_CUSTOMER, user, true, customer.getId().toString(), customer.getName()); |
|
|
|
return savedEntityView; |
|
|
|
} catch (Exception e) { |
|
|
|
notificationEntityService.notifyEntity(tenantId, emptyId(EntityType.ENTITY_VIEW), null, null, |
|
|
|
actionType, user, e, entityView.getId().toString()); |
|
|
|
actionType, user, e, savedEntityView.getEntityId().toString()); |
|
|
|
throw handleException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -392,18 +379,6 @@ public class DefaultTbEntityViewService extends AbstractTbEntityService implemen |
|
|
|
notificationEntityService.notifyEntity(user.getTenantId(), entityId, null, null, ActionType.TIMESERIES_DELETED, user, toException(e), keys); |
|
|
|
} |
|
|
|
|
|
|
|
private EntityView checkEntityViewId(EntityViewId entityViewId, Operation operation, SecurityUser user) throws ThingsboardException { |
|
|
|
try { |
|
|
|
validateId(entityViewId, "Incorrect entityViewId " + entityViewId); |
|
|
|
EntityView entityView = entityViewService.findEntityViewById(user.getTenantId(), entityViewId); |
|
|
|
checkNotNull(entityView, "Entity view with id [" + entityViewId + "] is not found"); |
|
|
|
accessControlService.checkPermission(user, Resource.ENTITY_VIEW, operation, entityViewId, entityView); |
|
|
|
return entityView; |
|
|
|
} catch (Exception e) { |
|
|
|
throw handleException(e, false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static Exception toException(Throwable error) { |
|
|
|
return error != null ? (Exception.class.isInstance(error) ? (Exception) error : new Exception(error)) : null; |
|
|
|
} |
|
|
|
|