Merge pull request #7598 from YuriyLytvynchuk/feature/change_originator_node_entity_source_fix

[3.4.2] Fix: delete cases from EntitiesByNameAndTypeLoader
This commit is contained in:
Andrew Shvayka 2022-11-16 16:38:05 +02:00 committed by GitHub
commit e0d972799b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 46 deletions

View File

@ -127,6 +127,7 @@ public class TbChangeOriginatorNode extends TbAbstractTransformNode {
log.error("EntityNamePattern not specified for type [{}]", conf.getEntityType()); log.error("EntityNamePattern not specified for type [{}]", conf.getEntityType());
throw new IllegalArgumentException("Wrong config for [{}] in TbChangeOriginatorNode!" + ENTITY_SOURCE); throw new IllegalArgumentException("Wrong config for [{}] in TbChangeOriginatorNode!" + ENTITY_SOURCE);
} }
EntitiesByNameAndTypeLoader.checkEntityType(EntityType.valueOf(conf.getEntityType()));
} }
} }

View File

@ -16,75 +16,52 @@
package org.thingsboard.rule.engine.util; package org.thingsboard.rule.engine.util;
import org.thingsboard.rule.engine.api.TbContext; import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.DashboardInfo;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.EntityView; import org.thingsboard.server.common.data.SearchTextBasedWithAdditionalInfo;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset;
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 java.util.Optional; import java.util.List;
public class EntitiesByNameAndTypeLoader { public class EntitiesByNameAndTypeLoader {
private static final List<EntityType> AVAILABLE_ENTITY_TYPES = List.of(
EntityType.DEVICE,
EntityType.ASSET,
EntityType.ENTITY_VIEW,
EntityType.EDGE,
EntityType.USER);
public static EntityId findEntityId(TbContext ctx, EntityType entityType, String entityName) { public static EntityId findEntityId(TbContext ctx, EntityType entityType, String entityName) {
EntityId targetEntityId = null; SearchTextBasedWithAdditionalInfo<? extends EntityId> targetEntity;
switch (entityType) { switch (entityType) {
case DEVICE: case DEVICE:
Device device = ctx.getDeviceService().findDeviceByTenantIdAndName(ctx.getTenantId(), entityName); targetEntity = ctx.getDeviceService().findDeviceByTenantIdAndName(ctx.getTenantId(), entityName);
if (device != null) {
targetEntityId = device.getId();
}
break; break;
case ASSET: case ASSET:
Asset asset = ctx.getAssetService().findAssetByTenantIdAndName(ctx.getTenantId(), entityName); targetEntity = ctx.getAssetService().findAssetByTenantIdAndName(ctx.getTenantId(), entityName);
if (asset != null) {
targetEntityId = asset.getId();
}
break;
case CUSTOMER:
Optional<Customer> customerOptional = ctx.getCustomerService().findCustomerByTenantIdAndTitle(ctx.getTenantId(), entityName);
if (customerOptional.isPresent()) {
targetEntityId = customerOptional.get().getId();
}
break;
case TENANT:
targetEntityId = ctx.getTenantId();
break; break;
case ENTITY_VIEW: case ENTITY_VIEW:
EntityView entityView = ctx.getEntityViewService().findEntityViewByTenantIdAndName(ctx.getTenantId(), entityName); targetEntity = ctx.getEntityViewService().findEntityViewByTenantIdAndName(ctx.getTenantId(), entityName);
if (entityView != null) {
targetEntityId = entityView.getId();
}
break; break;
case EDGE: case EDGE:
Edge edge = ctx.getEdgeService().findEdgeByTenantIdAndName(ctx.getTenantId(), entityName); targetEntity = ctx.getEdgeService().findEdgeByTenantIdAndName(ctx.getTenantId(), entityName);
if (edge != null) {
targetEntityId = edge.getId();
}
break;
case DASHBOARD:
DashboardInfo dashboardInfo = ctx.getDashboardService().findFirstDashboardInfoByTenantIdAndName(ctx.getTenantId(), entityName);
if (dashboardInfo != null) {
targetEntityId = dashboardInfo.getId();
}
break; break;
case USER: case USER:
User user = ctx.getUserService().findUserByEmail(ctx.getTenantId(), entityName); targetEntity = ctx.getUserService().findUserByTenantIdAndEmail(ctx.getTenantId(), entityName);
if (user != null) {
targetEntityId = user.getId();
}
break; break;
default: default:
throw new IllegalStateException("Unexpected entity type " + entityType.name()); throw new IllegalStateException("Unexpected entity type " + entityType.name());
} }
if (targetEntityId == null) { if (targetEntity == 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 targetEntityId; return targetEntity.getId();
}
public static void checkEntityType(EntityType entityType) {
if (!AVAILABLE_ENTITY_TYPES.contains(entityType)) {
throw new IllegalStateException("Unexpected entity type " + entityType.name());
}
} }
} }