Browse Source

minor refactoring

pull/12536/head
YevhenBondarenko 2 years ago
parent
commit
753071ea17
  1. 24
      application/src/main/java/org/thingsboard/server/service/install/update/DefaultDataUpdateService.java
  2. 6
      dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java
  3. 5
      dao/src/test/java/org/thingsboard/server/dao/service/RuleChainServiceTest.java

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

@ -49,7 +49,7 @@ import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import static org.thingsboard.server.common.data.relation.EntityRelation.USES_TYPE;
import static org.thingsboard.server.dao.rule.BaseRuleChainService.TB_RULE_CHAIN_INPUT_NODE;
@Service
@Profile("install")
@ -98,28 +98,22 @@ public class DefaultDataUpdateService implements DataUpdateService {
protected void updateEntity(Tenant tenant) {
TenantId tenantId = tenant.getId();
try {
var inputNodes = ruleChainService.findRuleNodesByTenantIdAndType(tenantId, "org.thingsboard.rule.engine.flow.TbRuleChainInputNode");
var inputNodes = ruleChainService.findRuleNodesByTenantIdAndType(tenantId, TB_RULE_CHAIN_INPUT_NODE);
var resultFutures = inputNodes.stream().map(ruleNode -> {
try {
JsonNode id = ruleNode.getConfiguration().get("ruleChainId");
if (id != null) {
RuleChainId toRuleChainId = new RuleChainId(UUID.fromString(id.asText()));
RuleChainId fromRuleChainId = ruleNode.getRuleChainId();
var isExistFuture = relationService.checkRelationAsync(null, fromRuleChainId, toRuleChainId, USES_TYPE, RelationTypeGroup.COMMON);
Futures.transformAsync(isExistFuture, isExist -> {
if (!isExist) {
EntityRelation relation = new EntityRelation();
relation.setFrom(fromRuleChainId);
relation.setTo(toRuleChainId);
relation.setType(EntityRelation.USES_TYPE);
relation.setTypeGroup(RelationTypeGroup.COMMON);
return relationService.saveRelationAsync(tenantId, relation);
}
return Futures.immediateFuture(null);
}, executorService);
EntityRelation relation = new EntityRelation();
relation.setFrom(fromRuleChainId);
relation.setTo(toRuleChainId);
relation.setType(EntityRelation.USES_TYPE);
relation.setTypeGroup(RelationTypeGroup.COMMON);
return relationService.saveRelationAsync(tenantId, relation);
}
} catch (Exception e) {
log.error("[{}] Create relation for input node: [{}]", tenantId, ruleNode, e);
log.error("[{}] Failed to save relation for input node: [{}]", tenantId, ruleNode, e);
}
return Futures.immediateFuture(null);
}).toList();

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

@ -210,7 +210,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
List<RuleNode> existingRuleNodes = getRuleChainNodes(tenantId, ruleChainMetaData.getRuleChainId());
for (RuleNode existingNode : existingRuleNodes) {
relationService.deleteEntityRelations(tenantId, existingNode.getId());
if (existingNode.getType().equals("org.thingsboard.rule.engine.flow.TbRuleChainInputNode")) {
if (existingNode.getType().equals(TB_RULE_CHAIN_INPUT_NODE)) {
if (existingNode.getConfiguration().has("ruleChainId")) {
RuleChainId targetRuleChainId = extractRuleChainIdFromInputNode(existingNode);
var relation = createRuleChainInputRelation(ruleChainId, targetRuleChainId);
@ -241,7 +241,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
RuleNode savedNode = ruleNodeDao.save(tenantId, node);
relations.add(new EntityRelation(ruleChainMetaData.getRuleChainId(), savedNode.getId(),
EntityRelation.CONTAINS_TYPE, RelationTypeGroup.RULE_CHAIN));
if (node.getType().equals("org.thingsboard.rule.engine.flow.TbRuleChainInputNode")) {
if (node.getType().equals(TB_RULE_CHAIN_INPUT_NODE)) {
if (node.getConfiguration().has("ruleChainId")) {
RuleChainId targetRuleChainId = extractRuleChainIdFromInputNode(node);
var relation = createRuleChainInputRelation(ruleChainId, targetRuleChainId);
@ -280,7 +280,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
RuleNode targetNode = new RuleNode();
targetNode.setName(targetRuleChain != null ? targetRuleChain.getName() : "Rule Chain Input");
targetNode.setRuleChainId(ruleChainId);
targetNode.setType("org.thingsboard.rule.engine.flow.TbRuleChainInputNode");
targetNode.setType(TB_RULE_CHAIN_INPUT_NODE);
var configuration = JacksonUtil.newObjectNode();
configuration.put("ruleChainId", targetRuleChainId.getId().toString());
targetNode.setConfiguration(configuration);

5
dao/src/test/java/org/thingsboard/server/dao/service/RuleChainServiceTest.java

@ -48,6 +48,7 @@ import java.util.function.Function;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.thingsboard.server.common.data.relation.EntityRelation.USES_TYPE;
import static org.thingsboard.server.dao.rule.BaseRuleChainService.TB_RULE_CHAIN_INPUT_NODE;
/**
* Created by igor on 3/13/18.
@ -377,7 +378,7 @@ public class RuleChainServiceTest extends AbstractServiceTest {
RuleNode ruleNode = new RuleNode();
ruleNode.setName("Input node");
ruleNode.setType("org.thingsboard.rule.engine.flow.TbRuleChainInputNode");
ruleNode.setType(TB_RULE_CHAIN_INPUT_NODE);
ObjectNode configuration = JacksonUtil.newObjectNode();
configuration.put("ruleChainId", savedToRuleChain.getId().toString());
ruleNode.setConfiguration(configuration);
@ -402,7 +403,7 @@ public class RuleChainServiceTest extends AbstractServiceTest {
RuleNode newRuleNode = new RuleNode();
newRuleNode.setName("Input node");
newRuleNode.setType("org.thingsboard.rule.engine.flow.TbRuleChainInputNode");
newRuleNode.setType(TB_RULE_CHAIN_INPUT_NODE);
ObjectNode newConfiguration = JacksonUtil.newObjectNode();
configuration.put("ruleChainId", savedNewToRuleChain.getId().toString());
newRuleNode.setConfiguration(newConfiguration);

Loading…
Cancel
Save