code review

This commit is contained in:
Yuriy Lytvynchuk 2022-09-21 17:55:57 +03:00
parent 370c43affb
commit 3fe6fa99aa
2 changed files with 16 additions and 25 deletions

View File

@ -40,8 +40,6 @@ import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.Edge;
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;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.EntitySearchDirection; import org.thingsboard.server.common.data.relation.EntitySearchDirection;
import org.thingsboard.server.common.data.relation.RelationTypeGroup; import org.thingsboard.server.common.data.relation.RelationTypeGroup;
@ -250,12 +248,10 @@ public abstract class TbAbstractRelationActionNode<C extends TbAbstractRelationA
break; break;
case DASHBOARD: case DASHBOARD:
DashboardService dashboardService = ctx.getDashboardService(); DashboardService dashboardService = ctx.getDashboardService();
PageData<DashboardInfo> dashboardInfoTextPageData = dashboardService.findDashboardsByTenantId(ctx.getTenantId(), new PageLink(200, 0, entitykey.getEntityName())); DashboardInfo dashboardInfo = dashboardService.findFirstDashboardInfoByTenantIdAndName(ctx.getTenantId(), entitykey.getEntityName());
for (DashboardInfo dashboardInfo : dashboardInfoTextPageData.getData()) { if (dashboardInfo != null) {
if (dashboardInfo.getTitle().equals(entitykey.getEntityName())) {
targetEntity.setEntityId(dashboardInfo.getId()); targetEntity.setEntityId(dashboardInfo.getId());
} }
}
break; break;
case USER: case USER:
UserService userService = ctx.getUserService(); UserService userService = ctx.getUserService();

View File

@ -25,71 +25,66 @@ import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.Edge;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import java.util.Optional; import java.util.Optional;
public class EntitiesByNameAndTypeLoader { public class EntitiesByNameAndTypeLoader {
public static EntityId findEntityId(TbContext ctx, EntityType entityType, String entityName) { public static EntityId findEntityId(TbContext ctx, EntityType entityType, String entityName) {
EntityId targetEntity = null; EntityId targetEntityId = null;
switch (entityType) { switch (entityType) {
case DEVICE: case DEVICE:
Device device = ctx.getDeviceService().findDeviceByTenantIdAndName(ctx.getTenantId(), entityName); Device device = ctx.getDeviceService().findDeviceByTenantIdAndName(ctx.getTenantId(), entityName);
if (device != null) { if (device != null) {
targetEntity = device.getId(); targetEntityId = device.getId();
} }
break; break;
case ASSET: case ASSET:
Asset asset = ctx.getAssetService().findAssetByTenantIdAndName(ctx.getTenantId(), entityName); Asset asset = ctx.getAssetService().findAssetByTenantIdAndName(ctx.getTenantId(), entityName);
if (asset != null) { if (asset != null) {
targetEntity = asset.getId(); targetEntityId = asset.getId();
} }
break; break;
case CUSTOMER: case CUSTOMER:
Optional<Customer> customerOptional = ctx.getCustomerService().findCustomerByTenantIdAndTitle(ctx.getTenantId(), entityName); Optional<Customer> customerOptional = ctx.getCustomerService().findCustomerByTenantIdAndTitle(ctx.getTenantId(), entityName);
if (customerOptional.isPresent()) { if (customerOptional.isPresent()) {
targetEntity = customerOptional.get().getId(); targetEntityId = customerOptional.get().getId();
} }
break; break;
case TENANT: case TENANT:
targetEntity = ctx.getTenantId(); targetEntityId = ctx.getTenantId();
break; break;
case ENTITY_VIEW: case ENTITY_VIEW:
EntityView entityView = ctx.getEntityViewService().findEntityViewByTenantIdAndName(ctx.getTenantId(), entityName); EntityView entityView = ctx.getEntityViewService().findEntityViewByTenantIdAndName(ctx.getTenantId(), entityName);
if (entityView != null) { if (entityView != null) {
targetEntity = entityView.getId(); targetEntityId = entityView.getId();
} }
break; break;
case EDGE: case EDGE:
Edge edge = ctx.getEdgeService().findEdgeByTenantIdAndName(ctx.getTenantId(), entityName); Edge edge = ctx.getEdgeService().findEdgeByTenantIdAndName(ctx.getTenantId(), entityName);
if (edge != null) { if (edge != null) {
targetEntity = edge.getId(); targetEntityId = edge.getId();
} }
break; break;
case DASHBOARD: case DASHBOARD:
PageData<DashboardInfo> dashboardInfoTextPageData = ctx.getDashboardService().findDashboardsByTenantId(ctx.getTenantId(), new PageLink(200, 0, entityName)); DashboardInfo dashboardInfo = ctx.getDashboardService().findFirstDashboardInfoByTenantIdAndName(ctx.getTenantId(), entityName);
Optional<DashboardInfo> currentDashboardInfo = dashboardInfoTextPageData.getData().stream() if (dashboardInfo != null) {
.filter(dashboardInfo -> dashboardInfo.getTitle().equals(entityName)) targetEntityId = dashboardInfo.getId();
.findFirst();
if (currentDashboardInfo.isPresent()) {
targetEntity = currentDashboardInfo.get().getId();
} }
break; break;
case USER: case USER:
User user = ctx.getUserService().findUserByEmail(ctx.getTenantId(), entityName); User user = ctx.getUserService().findUserByEmail(ctx.getTenantId(), entityName);
if (user != null) { if (user != null) {
targetEntity = user.getId(); targetEntityId = user.getId();
} }
break; break;
default: default:
throw new IllegalStateException("Unexpected entity type " + entityType.name()); throw new IllegalStateException("Unexpected entity type " + entityType.name());
} }
if (targetEntity == null) { if (targetEntityId == null) {
throw new IllegalStateException("Failed to found " + entityType.name() + " entity by name: '" + entityName + "'!"); throw new IllegalStateException("Failed to found " + entityType.name() + " entity by name: '" + entityName + "'!");
} }
return targetEntity; return targetEntityId;
} }
} }