Browse Source

code cleanup after review

pull/7775/head
ShvaykaD 4 years ago
parent
commit
f167c7c48b
  1. 2
      application/src/main/java/org/thingsboard/server/service/install/update/DefaultDataUpdateService.java
  2. 2
      common/dao-api/src/main/java/org/thingsboard/server/dao/entity/EntityService.java
  3. 9
      common/dao-api/src/main/java/org/thingsboard/server/dao/entity/TbEntityService.java
  4. 14
      dao/src/main/java/org/thingsboard/server/dao/alarm/BaseAlarmService.java
  5. 2
      dao/src/main/java/org/thingsboard/server/dao/asset/AssetProfileServiceImpl.java
  6. 9
      dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java
  7. 6
      dao/src/main/java/org/thingsboard/server/dao/customer/CustomerServiceImpl.java
  8. 2
      dao/src/main/java/org/thingsboard/server/dao/dashboard/DashboardServiceImpl.java
  9. 2
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileServiceImpl.java
  10. 8
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java
  11. 8
      dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java
  12. 17
      dao/src/main/java/org/thingsboard/server/dao/entity/BaseEntityService.java
  13. 8
      dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java
  14. 2
      dao/src/main/java/org/thingsboard/server/dao/ota/BaseOtaPackageService.java
  15. 2
      dao/src/main/java/org/thingsboard/server/dao/queue/BaseQueueService.java
  16. 2
      dao/src/main/java/org/thingsboard/server/dao/resource/BaseResourceService.java
  17. 2
      dao/src/main/java/org/thingsboard/server/dao/rpc/BaseRpcService.java
  18. 7
      dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java
  19. 2
      dao/src/main/java/org/thingsboard/server/dao/tenant/TenantProfileServiceImpl.java
  20. 2
      dao/src/main/java/org/thingsboard/server/dao/tenant/TenantServiceImpl.java
  21. 2
      dao/src/main/java/org/thingsboard/server/dao/usagerecord/ApiUsageStateServiceImpl.java
  22. 9
      dao/src/main/java/org/thingsboard/server/dao/user/UserServiceImpl.java
  23. 4
      dao/src/main/java/org/thingsboard/server/dao/widget/WidgetTypeServiceImpl.java
  24. 2
      dao/src/main/java/org/thingsboard/server/dao/widget/WidgetsBundleServiceImpl.java

2
application/src/main/java/org/thingsboard/server/service/install/update/DefaultDataUpdateService.java

@ -561,7 +561,7 @@ public class DefaultDataUpdateService implements DataUpdateService {
while (hasNext) {
for (Alarm alarm : alarms.getData()) {
if (alarm.getCustomerId() == null && alarm.getOriginator() != null) {
alarm.setCustomerId(entityService.fetchEntityCustomerId(tenantId, alarm.getOriginator()));
alarm.setCustomerId(entityService.fetchEntityCustomerId(tenantId, alarm.getOriginator()).get());
alarmDao.save(tenantId, alarm);
}
if (processed.incrementAndGet() % 1000 == 0) {

2
common/dao-api/src/main/java/org/thingsboard/server/dao/entity/EntityService.java

@ -29,7 +29,7 @@ public interface EntityService {
Optional<String> fetchEntityName(TenantId tenantId, EntityId entityId);
CustomerId fetchEntityCustomerId(TenantId tenantId, EntityId entityId);
Optional<CustomerId> fetchEntityCustomerId(TenantId tenantId, EntityId entityId);
long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query);

9
common/dao-api/src/main/java/org/thingsboard/server/dao/entity/TbEntityService.java

@ -15,23 +15,14 @@
*/
package org.thingsboard.server.dao.entity;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.HasId;
import org.thingsboard.server.common.data.id.TenantId;
import java.util.Optional;
import static org.thingsboard.server.common.data.id.EntityId.NULL_UUID;
public interface TbEntityService {
CustomerId NULL_CUSTOMER_ID = new CustomerId(NULL_UUID);
Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId);
default CustomerId getCustomerId(TenantId tenantId, EntityId entityId) {
return NULL_CUSTOMER_ID;
}
}

14
dao/src/main/java/org/thingsboard/server/dao/alarm/BaseAlarmService.java

@ -49,7 +49,6 @@ import org.thingsboard.server.common.data.relation.EntitySearchDirection;
import org.thingsboard.server.common.data.relation.RelationsSearchParameters;
import org.thingsboard.server.dao.entity.AbstractEntityService;
import org.thingsboard.server.dao.entity.EntityService;
import org.thingsboard.server.dao.entity.TbEntityService;
import org.thingsboard.server.dao.service.DataValidator;
import javax.annotation.Nullable;
@ -115,7 +114,7 @@ public class BaseAlarmService extends AbstractEntityService implements AlarmServ
if (alarm.getEndTs() == 0L) {
alarm.setEndTs(alarm.getStartTs());
}
alarm.setCustomerId(entityService.fetchEntityCustomerId(alarm.getTenantId(), alarm.getOriginator()));
alarm.setCustomerId(entityService.fetchEntityCustomerId(alarm.getTenantId(), alarm.getOriginator()).get());
if (alarm.getId() == null) {
Alarm existing = alarmDao.findLatestByOriginatorAndType(alarm.getTenantId(), alarm.getOriginator(), alarm.getType());
if (existing == null || existing.getStatus().isCleared()) {
@ -407,16 +406,7 @@ public class BaseAlarmService extends AbstractEntityService implements AlarmServ
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findAlarmById(tenantId, new AlarmId(entityId.getId())));
return Optional.ofNullable(findAlarmById(tenantId, new AlarmId(entityId.getId())));
}
@Override
public CustomerId getCustomerId(TenantId tenantId, EntityId entityId) {
try {
Alarm alarm = findAlarmByIdAsync(tenantId, new AlarmId(entityId.getId())).get();
return alarm != null ? alarm.getCustomerId() : TbEntityService.NULL_CUSTOMER_ID;
} catch (Exception e) {
return TbEntityService.NULL_CUSTOMER_ID;
}
}
}

2
dao/src/main/java/org/thingsboard/server/dao/asset/AssetProfileServiceImpl.java

@ -269,7 +269,7 @@ public class AssetProfileServiceImpl extends AbstractCachedEntityService<AssetPr
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findAssetProfileById(tenantId, new AssetProfileId(entityId.getId())));
return Optional.ofNullable(findAssetProfileById(tenantId, new AssetProfileId(entityId.getId())));
}
private PaginatedRemover<TenantId, AssetProfile> tenantAssetProfilesRemover =

9
dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java

@ -46,7 +46,6 @@ import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.EntitySearchDirection;
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
import org.thingsboard.server.dao.entity.TbEntityService;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover;
@ -435,13 +434,7 @@ public class BaseAssetService extends AbstractCachedEntityService<AssetCacheKey,
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findAssetById(tenantId, new AssetId(entityId.getId())));
}
@Override
public CustomerId getCustomerId(TenantId tenantId, EntityId entityId) {
Asset asset = findAssetById(tenantId, new AssetId(entityId.getId()));
return asset != null ? asset.getCustomerId() : TbEntityService.NULL_CUSTOMER_ID;
return Optional.ofNullable(findAssetById(tenantId, new AssetId(entityId.getId())));
}
}

6
dao/src/main/java/org/thingsboard/server/dao/customer/CustomerServiceImpl.java

@ -183,11 +183,7 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findCustomerById(tenantId, new CustomerId(entityId.getId())));
return Optional.ofNullable(findCustomerById(tenantId, new CustomerId(entityId.getId())));
}
@Override
public CustomerId getCustomerId(TenantId tenantId, EntityId entityId) {
return new CustomerId(entityId.getId());
}
}

2
dao/src/main/java/org/thingsboard/server/dao/dashboard/DashboardServiceImpl.java

@ -312,7 +312,7 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findDashboardById(tenantId, new DashboardId(entityId.getId())));
return Optional.ofNullable(findDashboardById(tenantId, new DashboardId(entityId.getId())));
}
private class CustomerDashboardsUnassigner extends PaginatedRemover<Customer, DashboardInfo> {

2
dao/src/main/java/org/thingsboard/server/dao/device/DeviceProfileServiceImpl.java

@ -291,7 +291,7 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findDeviceProfileById(tenantId, new DeviceProfileId(entityId.getId())));
return Optional.ofNullable(findDeviceProfileById(tenantId, new DeviceProfileId(entityId.getId())));
}
private PaginatedRemover<TenantId, DeviceProfile> tenantDeviceProfilesRemover =

8
dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java

@ -67,7 +67,6 @@ import org.thingsboard.server.dao.device.provision.ProvisionFailedException;
import org.thingsboard.server.dao.device.provision.ProvisionRequest;
import org.thingsboard.server.dao.device.provision.ProvisionResponseStatus;
import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
import org.thingsboard.server.dao.entity.TbEntityService;
import org.thingsboard.server.dao.event.EventService;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.service.DataValidator;
@ -711,12 +710,7 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findDeviceById(tenantId, new DeviceId(entityId.getId())));
return Optional.ofNullable(findDeviceById(tenantId, new DeviceId(entityId.getId())));
}
@Override
public CustomerId getCustomerId(TenantId tenantId, EntityId entityId) {
Device device = findDeviceById(tenantId, new DeviceId(entityId.getId()));
return device != null ? device.getCustomerId() : TbEntityService.NULL_CUSTOMER_ID;
}
}

8
dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java

@ -53,7 +53,6 @@ import org.thingsboard.server.common.data.relation.EntitySearchDirection;
import org.thingsboard.server.common.data.rule.RuleChain;
import org.thingsboard.server.common.data.rule.RuleNode;
import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
import org.thingsboard.server.dao.entity.TbEntityService;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.relation.RelationService;
import org.thingsboard.server.dao.rule.RuleChainService;
@ -524,12 +523,7 @@ public class EdgeServiceImpl extends AbstractCachedEntityService<EdgeCacheKey, E
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findEdgeById(tenantId, new EdgeId(entityId.getId())));
return Optional.ofNullable(findEdgeById(tenantId, new EdgeId(entityId.getId())));
}
@Override
public CustomerId getCustomerId(TenantId tenantId, EntityId entityId) {
Edge edge = findEdgeById(tenantId, new EdgeId(entityId.getId()));
return edge != null ? edge.getCustomerId() : TbEntityService.NULL_CUSTOMER_ID;
}
}

17
dao/src/main/java/org/thingsboard/server/dao/entity/BaseEntityService.java

@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.thingsboard.server.common.data.HasCustomerId;
import org.thingsboard.server.common.data.HasName;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EntityId;
@ -35,6 +36,7 @@ import org.thingsboard.server.dao.exception.IncorrectParameterException;
import java.util.Optional;
import static org.thingsboard.server.common.data.id.EntityId.NULL_UUID;
import static org.thingsboard.server.dao.service.Validator.validateId;
/**
@ -46,6 +48,7 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId ";
public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId ";
public static final CustomerId NULL_CUSTOMER_ID = new CustomerId(NULL_UUID);
@Autowired
private EntityQueryDao entityQueryDao;
@ -80,17 +83,25 @@ public class BaseEntityService extends AbstractEntityService implements EntitySe
HasId<?> hasId = hasIdOpt.get();
if (hasId instanceof HasName) {
HasName hasName = (HasName) hasId;
return Optional.of(hasName.getName());
return Optional.ofNullable(hasName.getName());
}
}
return Optional.empty();
}
@Override
public CustomerId fetchEntityCustomerId(TenantId tenantId, EntityId entityId) {
public Optional<CustomerId> fetchEntityCustomerId(TenantId tenantId, EntityId entityId) {
log.trace("Executing fetchEntityCustomerId [{}]", entityId);
TbEntityService tbEntityService = entityServiceBeanFactory.getServiceByEntityType(entityId.getEntityType());
return tbEntityService.getCustomerId(tenantId, entityId);
Optional<HasId<?>> hasIdOpt = tbEntityService.fetchEntity(tenantId, entityId);
if (hasIdOpt.isPresent()) {
HasId<?> hasId = hasIdOpt.get();
if (hasId instanceof HasCustomerId) {
HasCustomerId hasCustomerId = (HasCustomerId) hasId;
return Optional.ofNullable(hasCustomerId.getCustomerId());
}
}
return Optional.of(NULL_CUSTOMER_ID);
}
private static void validateEntityCountQuery(EntityCountQuery query) {

8
dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewServiceImpl.java

@ -43,7 +43,6 @@ import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.EntitySearchDirection;
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
import org.thingsboard.server.dao.entity.TbEntityService;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover;
@ -414,12 +413,7 @@ public class EntityViewServiceImpl extends AbstractCachedEntityService<EntityVie
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findEntityViewById(tenantId, new EntityViewId(entityId.getId())));
return Optional.ofNullable(findEntityViewById(tenantId, new EntityViewId(entityId.getId())));
}
@Override
public CustomerId getCustomerId(TenantId tenantId, EntityId entityId) {
EntityView entityView = findEntityViewById(tenantId, new EntityViewId(entityId.getId()));
return entityView != null ? entityView.getCustomerId() : TbEntityService.NULL_CUSTOMER_ID;
}
}

2
dao/src/main/java/org/thingsboard/server/dao/ota/BaseOtaPackageService.java

@ -238,7 +238,7 @@ public class BaseOtaPackageService extends AbstractCachedEntityService<OtaPackag
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findOtaPackageInfoById(tenantId, new OtaPackageId(entityId.getId())));
return Optional.ofNullable(findOtaPackageInfoById(tenantId, new OtaPackageId(entityId.getId())));
}
}

2
dao/src/main/java/org/thingsboard/server/dao/queue/BaseQueueService.java

@ -124,7 +124,7 @@ public class BaseQueueService extends AbstractEntityService implements QueueServ
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findQueueById(tenantId, new QueueId(entityId.getId())));
return Optional.ofNullable(findQueueById(tenantId, new QueueId(entityId.getId())));
}
private PaginatedRemover<TenantId, Queue> tenantQueuesRemover =

2
dao/src/main/java/org/thingsboard/server/dao/resource/BaseResourceService.java

@ -138,7 +138,7 @@ public class BaseResourceService implements ResourceService {
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findResourceInfoById(tenantId, new TbResourceId(entityId.getId())));
return Optional.ofNullable(findResourceInfoById(tenantId, new TbResourceId(entityId.getId())));
}
@Override

2
dao/src/main/java/org/thingsboard/server/dao/rpc/BaseRpcService.java

@ -99,7 +99,7 @@ public class BaseRpcService implements RpcService {
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findById(tenantId, new RpcId(entityId.getId())));
return Optional.ofNullable(findById(tenantId, new RpcId(entityId.getId())));
}
private PaginatedRemover<TenantId, Rpc> tenantRpcRemover =

7
dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java

@ -762,9 +762,10 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return EntityType.RULE_NODE.equals(entityId.getEntityType()) ?
Optional.of(findRuleNodeById(tenantId, new RuleNodeId(entityId.getId()))) :
Optional.of(findRuleChainById(tenantId, new RuleChainId(entityId.getId())));
HasId<?> hasId = EntityType.RULE_NODE.equals(entityId.getEntityType()) ?
findRuleNodeById(tenantId, new RuleNodeId(entityId.getId())) :
findRuleChainById(tenantId, new RuleChainId(entityId.getId()));
return Optional.ofNullable(hasId);
}
private List<EntityRelation> getRuleChainToNodeRelations(TenantId tenantId, RuleChainId ruleChainId) {

2
dao/src/main/java/org/thingsboard/server/dao/tenant/TenantProfileServiceImpl.java

@ -209,7 +209,7 @@ public class TenantProfileServiceImpl extends AbstractCachedEntityService<Tenant
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findTenantProfileById(tenantId, new TenantProfileId(entityId.getId())));
return Optional.ofNullable(findTenantProfileById(tenantId, new TenantProfileId(entityId.getId())));
}
private final PaginatedRemover<String, TenantProfile> tenantProfilesRemover =

2
dao/src/main/java/org/thingsboard/server/dao/tenant/TenantServiceImpl.java

@ -262,6 +262,6 @@ public class TenantServiceImpl extends AbstractCachedEntityService<TenantId, Ten
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findTenantById(new TenantId(entityId.getId())));
return Optional.ofNullable(findTenantById(new TenantId(entityId.getId())));
}
}

2
dao/src/main/java/org/thingsboard/server/dao/usagerecord/ApiUsageStateServiceImpl.java

@ -165,6 +165,6 @@ public class ApiUsageStateServiceImpl extends AbstractEntityService implements A
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findApiUsageStateById(tenantId, new ApiUsageStateId(entityId.getId())));
return Optional.ofNullable(findApiUsageStateById(tenantId, new ApiUsageStateId(entityId.getId())));
}
}

9
dao/src/main/java/org/thingsboard/server/dao/user/UserServiceImpl.java

@ -41,7 +41,6 @@ import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.security.UserCredentials;
import org.thingsboard.server.common.data.security.event.UserCredentialsInvalidationEvent;
import org.thingsboard.server.dao.entity.AbstractEntityService;
import org.thingsboard.server.dao.entity.TbEntityService;
import org.thingsboard.server.dao.exception.IncorrectParameterException;
import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover;
@ -403,13 +402,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findUserById(tenantId, new UserId(entityId.getId())));
}
@Override
public CustomerId getCustomerId(TenantId tenantId, EntityId entityId) {
User user = findUserById(tenantId, new UserId(entityId.getId()));
return user != null ? user.getCustomerId() : TbEntityService.NULL_CUSTOMER_ID;
return Optional.ofNullable(findUserById(tenantId, new UserId(entityId.getId())));
}
}

4
dao/src/main/java/org/thingsboard/server/dao/widget/WidgetTypeServiceImpl.java

@ -117,7 +117,7 @@ public class WidgetTypeServiceImpl implements WidgetTypeService {
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
findWidgetTypeById(tenantId, new WidgetTypeId(entityId.getId()));
return Optional.of(findWidgetTypeById(tenantId, new WidgetTypeId(entityId.getId())));
return Optional.ofNullable(findWidgetTypeById(tenantId, new WidgetTypeId(entityId.getId())));
}
}

2
dao/src/main/java/org/thingsboard/server/dao/widget/WidgetsBundleServiceImpl.java

@ -156,7 +156,7 @@ public class WidgetsBundleServiceImpl implements WidgetsBundleService {
@Override
public Optional<HasId<?>> fetchEntity(TenantId tenantId, EntityId entityId) {
return Optional.of(findWidgetsBundleById(tenantId, new WidgetsBundleId(entityId.getId())));
return Optional.ofNullable(findWidgetsBundleById(tenantId, new WidgetsBundleId(entityId.getId())));
}
private PaginatedRemover<TenantId, WidgetsBundle> tenantWidgetsBundleRemover =

Loading…
Cancel
Save