Commit 09c8f9bbbb3f55b0e7744805c71991168b9e2a47

Authored by ShvaykaD
1 parent f026b588

Create TbCreateRelationNode

  1 +package org.thingsboard.rule.engine.action;
  2 +
  3 +import com.google.common.util.concurrent.ListenableFuture;
  4 +import lombok.extern.slf4j.Slf4j;
  5 +import org.thingsboard.rule.engine.api.*;
  6 +import org.thingsboard.rule.engine.api.util.DonAsynchron;
  7 +import org.thingsboard.rule.engine.api.util.TbNodeUtils;
  8 +import org.thingsboard.server.common.data.id.EntityId;
  9 +import org.thingsboard.server.common.data.id.EntityIdFactory;
  10 +import org.thingsboard.server.common.data.plugin.ComponentType;
  11 +import org.thingsboard.server.common.data.relation.EntityRelation;
  12 +import org.thingsboard.server.common.data.relation.EntitySearchDirection;
  13 +import org.thingsboard.server.common.data.relation.RelationTypeGroup;
  14 +import org.thingsboard.server.common.msg.TbMsg;
  15 +
  16 +import static org.thingsboard.rule.engine.api.TbRelationTypes.FAILURE;
  17 +import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS;
  18 +import static org.thingsboard.rule.engine.api.util.DonAsynchron.withCallback;
  19 +
  20 +@Slf4j
  21 +@RuleNode(
  22 + type = ComponentType.ACTION,
  23 + name = "create relation",
  24 + configClazz = TbCreateRelationNodeConfiguration.class,
  25 + nodeDescription = "Create the relation from the selected entity to originator of the message by type and direction",
  26 + nodeDetails = "If relation is already exists - send Message via <b>Success</b> chain with error message, otherwise is also used <b>Success</b> chain without error message.",
  27 + uiResources = {"static/rulenode/rulenode-core-config.js"},
  28 + configDirective = "tbFilterNodeCheckRelationConfig")
  29 +public class TbCreateRelationNode implements TbNode {
  30 +
  31 + private TbCreateRelationNodeConfiguration config;
  32 + private EntityId fromId;
  33 + private EntityId toId;
  34 +
  35 + @Override
  36 + public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
  37 + this.config = TbNodeUtils.convert(configuration, TbCreateRelationNodeConfiguration.class);
  38 + }
  39 +
  40 + @Override
  41 + public void onMsg(TbContext ctx, TbMsg msg) {
  42 + DonAsynchron.withCallback(checkRelation(ctx, msg), result -> {
  43 + if (result) {
  44 + ctx.tellNext(msg, SUCCESS, new Throwable("Relation between message originator and Entity: " + config.getEntityId() + " is already exist"));
  45 + } else {
  46 + processCreateRelation(ctx, msg);
  47 + }
  48 + }, error -> ctx.tellFailure(msg, error), ctx.getDbCallbackExecutor());
  49 + }
  50 +
  51 + @Override
  52 + public void destroy() {
  53 + }
  54 +
  55 + private ListenableFuture<Boolean> checkRelation(TbContext ctx, TbMsg msg) {
  56 + if (EntitySearchDirection.TO.name().equals(config.getDirection())) {
  57 + fromId = EntityIdFactory.getByTypeAndId(config.getEntityType(), config.getEntityId());
  58 + toId = msg.getOriginator();
  59 + } else {
  60 + fromId = EntityIdFactory.getByTypeAndId(config.getEntityType(), config.getEntityId());
  61 + toId = msg.getOriginator();
  62 + }
  63 + return ctx.getRelationService().checkRelation(ctx.getTenantId(), fromId, toId, config.getRelationType(), RelationTypeGroup.COMMON);
  64 + }
  65 +
  66 + private void processCreateRelation(TbContext ctx, TbMsg msg) {
  67 + EntityRelation entityRelation = new EntityRelation(fromId, toId, config.getRelationType(), RelationTypeGroup.COMMON);
  68 + withCallback(ctx.getRelationService().saveRelationAsync(ctx.getTenantId(), entityRelation),
  69 + filterResult -> ctx.tellNext(msg, filterResult ? SUCCESS : FAILURE), t -> ctx.tellFailure(msg, t), ctx.getDbCallbackExecutor());
  70 + }
  71 +
  72 +}
  1 +package org.thingsboard.rule.engine.action;
  2 +
  3 +import lombok.Data;
  4 +import org.thingsboard.rule.engine.api.NodeConfiguration;
  5 +import org.thingsboard.server.common.data.relation.EntitySearchDirection;
  6 +
  7 +@Data
  8 +public class TbCreateRelationNodeConfiguration implements NodeConfiguration<TbCreateRelationNodeConfiguration> {
  9 +
  10 + private String direction;
  11 + private String entityId;
  12 + private String entityType;
  13 + private String relationType;
  14 +
  15 +
  16 + @Override
  17 + public TbCreateRelationNodeConfiguration defaultConfiguration() {
  18 + TbCreateRelationNodeConfiguration configuration = new TbCreateRelationNodeConfiguration();
  19 + configuration.setDirection(EntitySearchDirection.FROM.name());
  20 + configuration.setRelationType("Contains");
  21 + return configuration;
  22 + }
  23 +}