|
|
@ -15,29 +15,16 @@ |
|
|
*/ |
|
|
*/ |
|
|
package org.thingsboard.server.dao.entity; |
|
|
package org.thingsboard.server.dao.entity; |
|
|
|
|
|
|
|
|
import com.google.common.base.Function; |
|
|
|
|
|
import com.google.common.util.concurrent.Futures; |
|
|
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
|
|
|
import com.google.common.util.concurrent.MoreExecutors; |
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.util.CollectionUtils; |
|
|
import org.springframework.util.CollectionUtils; |
|
|
import org.thingsboard.server.common.data.HasCustomerId; |
|
|
import org.thingsboard.server.common.data.HasCustomerId; |
|
|
import org.thingsboard.server.common.data.HasName; |
|
|
import org.thingsboard.server.common.data.HasName; |
|
|
import org.thingsboard.server.common.data.id.AlarmId; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.AssetId; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.CustomerId; |
|
|
import org.thingsboard.server.common.data.id.CustomerId; |
|
|
import org.thingsboard.server.common.data.id.DashboardId; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.DeviceId; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.EdgeId; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
import org.thingsboard.server.common.data.id.EntityViewId; |
|
|
import org.thingsboard.server.common.data.id.HasId; |
|
|
import org.thingsboard.server.common.data.id.OtaPackageId; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.RuleChainId; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.TbResourceId; |
|
|
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
import org.thingsboard.server.common.data.id.UserId; |
|
|
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
import org.thingsboard.server.common.data.query.EntityCountQuery; |
|
|
import org.thingsboard.server.common.data.query.EntityCountQuery; |
|
|
import org.thingsboard.server.common.data.query.EntityData; |
|
|
import org.thingsboard.server.common.data.query.EntityData; |
|
|
@ -45,20 +32,11 @@ import org.thingsboard.server.common.data.query.EntityDataPageLink; |
|
|
import org.thingsboard.server.common.data.query.EntityDataQuery; |
|
|
import org.thingsboard.server.common.data.query.EntityDataQuery; |
|
|
import org.thingsboard.server.common.data.query.EntityFilterType; |
|
|
import org.thingsboard.server.common.data.query.EntityFilterType; |
|
|
import org.thingsboard.server.common.data.query.RelationsQueryFilter; |
|
|
import org.thingsboard.server.common.data.query.RelationsQueryFilter; |
|
|
import org.thingsboard.server.dao.alarm.AlarmService; |
|
|
|
|
|
import org.thingsboard.server.dao.asset.AssetService; |
|
|
|
|
|
import org.thingsboard.server.dao.customer.CustomerService; |
|
|
|
|
|
import org.thingsboard.server.dao.dashboard.DashboardService; |
|
|
|
|
|
import org.thingsboard.server.dao.device.DeviceService; |
|
|
|
|
|
import org.thingsboard.server.dao.entityview.EntityViewService; |
|
|
|
|
|
import org.thingsboard.server.dao.exception.IncorrectParameterException; |
|
|
import org.thingsboard.server.dao.exception.IncorrectParameterException; |
|
|
import org.thingsboard.server.dao.ota.OtaPackageService; |
|
|
|
|
|
import org.thingsboard.server.dao.resource.ResourceService; |
|
|
|
|
|
import org.thingsboard.server.dao.rule.RuleChainService; |
|
|
|
|
|
import org.thingsboard.server.dao.tenant.TenantService; |
|
|
|
|
|
import org.thingsboard.server.dao.user.UserService; |
|
|
|
|
|
|
|
|
|
|
|
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; |
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
|
|
|
|
import static org.thingsboard.server.common.data.id.EntityId.NULL_UUID; |
|
|
import static org.thingsboard.server.dao.service.Validator.validateId; |
|
|
import static org.thingsboard.server.dao.service.Validator.validateId; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -70,42 +48,13 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe |
|
|
|
|
|
|
|
|
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; |
|
|
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; |
|
|
public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; |
|
|
public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; |
|
|
|
|
|
public static final CustomerId NULL_CUSTOMER_ID = new CustomerId(NULL_UUID); |
|
|
@Autowired |
|
|
|
|
|
private AssetService assetService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private DeviceService deviceService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private EntityViewService entityViewService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private TenantService tenantService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private CustomerService customerService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private UserService userService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private DashboardService dashboardService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private AlarmService alarmService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private RuleChainService ruleChainService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private EntityQueryDao entityQueryDao; |
|
|
private EntityQueryDao entityQueryDao; |
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private ResourceService resourceService; |
|
|
EntityServiceBeanFactory entityServiceBeanFactory; |
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private OtaPackageService otaPackageService; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query) { |
|
|
public long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query) { |
|
|
@ -125,100 +74,34 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe |
|
|
return this.entityQueryDao.findEntityDataByQuery(tenantId, customerId, query); |
|
|
return this.entityQueryDao.findEntityDataByQuery(tenantId, customerId, query); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//TODO: 3.1 Remove this from project.
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public ListenableFuture<String> fetchEntityNameAsync(TenantId tenantId, EntityId entityId) { |
|
|
public Optional<String> fetchEntityName(TenantId tenantId, EntityId entityId) { |
|
|
log.trace("Executing fetchEntityNameAsync [{}]", entityId); |
|
|
log.trace("Executing fetchEntityName [{}]", entityId); |
|
|
ListenableFuture<String> entityName; |
|
|
TbEntityService tbEntityService = entityServiceBeanFactory.getServiceByEntityType(entityId.getEntityType()); |
|
|
ListenableFuture<? extends HasName> hasName; |
|
|
Optional<HasId<?>> hasIdOpt = tbEntityService.fetchEntity(tenantId, entityId); |
|
|
switch (entityId.getEntityType()) { |
|
|
if (hasIdOpt.isPresent()) { |
|
|
case ASSET: |
|
|
HasId<?> hasId = hasIdOpt.get(); |
|
|
hasName = assetService.findAssetByIdAsync(tenantId, new AssetId(entityId.getId())); |
|
|
if (hasId instanceof HasName) { |
|
|
break; |
|
|
HasName hasName = (HasName) hasId; |
|
|
case DEVICE: |
|
|
return Optional.ofNullable(hasName.getName()); |
|
|
hasName = deviceService.findDeviceByIdAsync(tenantId, new DeviceId(entityId.getId())); |
|
|
} |
|
|
break; |
|
|
|
|
|
case ENTITY_VIEW: |
|
|
|
|
|
hasName = entityViewService.findEntityViewByIdAsync(tenantId, new EntityViewId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case TENANT: |
|
|
|
|
|
hasName = tenantService.findTenantByIdAsync(tenantId, TenantId.fromUUID(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case CUSTOMER: |
|
|
|
|
|
hasName = customerService.findCustomerByIdAsync(tenantId, new CustomerId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case USER: |
|
|
|
|
|
hasName = userService.findUserByIdAsync(tenantId, new UserId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case DASHBOARD: |
|
|
|
|
|
hasName = dashboardService.findDashboardInfoByIdAsync(tenantId, new DashboardId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case ALARM: |
|
|
|
|
|
hasName = alarmService.findAlarmByIdAsync(tenantId, new AlarmId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case RULE_CHAIN: |
|
|
|
|
|
hasName = ruleChainService.findRuleChainByIdAsync(tenantId, new RuleChainId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case EDGE: |
|
|
|
|
|
hasName = edgeService.findEdgeByIdAsync(tenantId, new EdgeId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case TB_RESOURCE: |
|
|
|
|
|
hasName = resourceService.findResourceInfoByIdAsync(tenantId, new TbResourceId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case OTA_PACKAGE: |
|
|
|
|
|
hasName = otaPackageService.findOtaPackageInfoByIdAsync(tenantId, new OtaPackageId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
throw new IllegalStateException("Not Implemented!"); |
|
|
|
|
|
} |
|
|
} |
|
|
entityName = Futures.transform(hasName, (Function<HasName, String>) hasName1 -> hasName1 != null ? hasName1.getName() : null, MoreExecutors.directExecutor()); |
|
|
return Optional.empty(); |
|
|
return entityName; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public CustomerId fetchEntityCustomerId(TenantId tenantId, EntityId entityId) { |
|
|
public CustomerId fetchEntityCustomerId(TenantId tenantId, EntityId entityId) { |
|
|
log.trace("Executing fetchEntityCustomerId [{}]", entityId); |
|
|
log.trace("Executing fetchEntityCustomerId [{}]", entityId); |
|
|
HasCustomerId hasCustomerId = null; |
|
|
TbEntityService tbEntityService = entityServiceBeanFactory.getServiceByEntityType(entityId.getEntityType()); |
|
|
switch (entityId.getEntityType()) { |
|
|
Optional<HasId<?>> hasIdOpt = tbEntityService.fetchEntity(tenantId, entityId); |
|
|
case TENANT: |
|
|
if (hasIdOpt.isPresent()) { |
|
|
case RULE_CHAIN: |
|
|
HasId<?> hasId = hasIdOpt.get(); |
|
|
case RULE_NODE: |
|
|
if (hasId instanceof HasCustomerId) { |
|
|
case DASHBOARD: |
|
|
HasCustomerId hasCustomerId = (HasCustomerId) hasId; |
|
|
case WIDGETS_BUNDLE: |
|
|
return hasCustomerId.getCustomerId(); |
|
|
case WIDGET_TYPE: |
|
|
} |
|
|
case TENANT_PROFILE: |
|
|
|
|
|
case DEVICE_PROFILE: |
|
|
|
|
|
case ASSET_PROFILE: |
|
|
|
|
|
case API_USAGE_STATE: |
|
|
|
|
|
case TB_RESOURCE: |
|
|
|
|
|
case OTA_PACKAGE: |
|
|
|
|
|
break; |
|
|
|
|
|
case CUSTOMER: |
|
|
|
|
|
hasCustomerId = () -> new CustomerId(entityId.getId()); |
|
|
|
|
|
break; |
|
|
|
|
|
case USER: |
|
|
|
|
|
hasCustomerId = userService.findUserById(tenantId, new UserId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case ASSET: |
|
|
|
|
|
hasCustomerId = assetService.findAssetById(tenantId, new AssetId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case DEVICE: |
|
|
|
|
|
hasCustomerId = deviceService.findDeviceById(tenantId, new DeviceId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case ALARM: |
|
|
|
|
|
try { |
|
|
|
|
|
hasCustomerId = alarmService.findAlarmByIdAsync(tenantId, new AlarmId(entityId.getId())).get(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
|
|
|
case ENTITY_VIEW: |
|
|
|
|
|
hasCustomerId = entityViewService.findEntityViewById(tenantId, new EntityViewId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
case EDGE: |
|
|
|
|
|
hasCustomerId = edgeService.findEdgeById(tenantId, new EdgeId(entityId.getId())); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
return hasCustomerId != null ? hasCustomerId.getCustomerId() : new CustomerId(NULL_UUID); |
|
|
return NULL_CUSTOMER_ID; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static void validateEntityCountQuery(EntityCountQuery query) { |
|
|
private static void validateEntityCountQuery(EntityCountQuery query) { |
|
|
|