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.UUID;
import java.util.concurrent.ExecutionException; 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 @Service
@Profile("install") @Profile("install")
@ -98,28 +98,22 @@ public class DefaultDataUpdateService implements DataUpdateService {
protected void updateEntity(Tenant tenant) { protected void updateEntity(Tenant tenant) {
TenantId tenantId = tenant.getId(); TenantId tenantId = tenant.getId();
try { 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 -> { var resultFutures = inputNodes.stream().map(ruleNode -> {
try { try {
JsonNode id = ruleNode.getConfiguration().get("ruleChainId"); JsonNode id = ruleNode.getConfiguration().get("ruleChainId");
if (id != null) { if (id != null) {
RuleChainId toRuleChainId = new RuleChainId(UUID.fromString(id.asText())); RuleChainId toRuleChainId = new RuleChainId(UUID.fromString(id.asText()));
RuleChainId fromRuleChainId = ruleNode.getRuleChainId(); RuleChainId fromRuleChainId = ruleNode.getRuleChainId();
var isExistFuture = relationService.checkRelationAsync(null, fromRuleChainId, toRuleChainId, USES_TYPE, RelationTypeGroup.COMMON); EntityRelation relation = new EntityRelation();
Futures.transformAsync(isExistFuture, isExist -> { relation.setFrom(fromRuleChainId);
if (!isExist) { relation.setTo(toRuleChainId);
EntityRelation relation = new EntityRelation(); relation.setType(EntityRelation.USES_TYPE);
relation.setFrom(fromRuleChainId); relation.setTypeGroup(RelationTypeGroup.COMMON);
relation.setTo(toRuleChainId); return relationService.saveRelationAsync(tenantId, relation);
relation.setType(EntityRelation.USES_TYPE);
relation.setTypeGroup(RelationTypeGroup.COMMON);
return relationService.saveRelationAsync(tenantId, relation);
}
return Futures.immediateFuture(null);
}, executorService);
} }
} catch (Exception e) { } 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); return Futures.immediateFuture(null);
}).toList(); }).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()); List<RuleNode> existingRuleNodes = getRuleChainNodes(tenantId, ruleChainMetaData.getRuleChainId());
for (RuleNode existingNode : existingRuleNodes) { for (RuleNode existingNode : existingRuleNodes) {
relationService.deleteEntityRelations(tenantId, existingNode.getId()); 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")) { if (existingNode.getConfiguration().has("ruleChainId")) {
RuleChainId targetRuleChainId = extractRuleChainIdFromInputNode(existingNode); RuleChainId targetRuleChainId = extractRuleChainIdFromInputNode(existingNode);
var relation = createRuleChainInputRelation(ruleChainId, targetRuleChainId); var relation = createRuleChainInputRelation(ruleChainId, targetRuleChainId);
@ -241,7 +241,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
RuleNode savedNode = ruleNodeDao.save(tenantId, node); RuleNode savedNode = ruleNodeDao.save(tenantId, node);
relations.add(new EntityRelation(ruleChainMetaData.getRuleChainId(), savedNode.getId(), relations.add(new EntityRelation(ruleChainMetaData.getRuleChainId(), savedNode.getId(),
EntityRelation.CONTAINS_TYPE, RelationTypeGroup.RULE_CHAIN)); 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")) { if (node.getConfiguration().has("ruleChainId")) {
RuleChainId targetRuleChainId = extractRuleChainIdFromInputNode(node); RuleChainId targetRuleChainId = extractRuleChainIdFromInputNode(node);
var relation = createRuleChainInputRelation(ruleChainId, targetRuleChainId); var relation = createRuleChainInputRelation(ruleChainId, targetRuleChainId);
@ -280,7 +280,7 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
RuleNode targetNode = new RuleNode(); RuleNode targetNode = new RuleNode();
targetNode.setName(targetRuleChain != null ? targetRuleChain.getName() : "Rule Chain Input"); targetNode.setName(targetRuleChain != null ? targetRuleChain.getName() : "Rule Chain Input");
targetNode.setRuleChainId(ruleChainId); targetNode.setRuleChainId(ruleChainId);
targetNode.setType("org.thingsboard.rule.engine.flow.TbRuleChainInputNode"); targetNode.setType(TB_RULE_CHAIN_INPUT_NODE);
var configuration = JacksonUtil.newObjectNode(); var configuration = JacksonUtil.newObjectNode();
configuration.put("ruleChainId", targetRuleChainId.getId().toString()); configuration.put("ruleChainId", targetRuleChainId.getId().toString());
targetNode.setConfiguration(configuration); 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.assertj.core.api.Assertions.assertThatThrownBy;
import static org.thingsboard.server.common.data.relation.EntityRelation.USES_TYPE; 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. * Created by igor on 3/13/18.
@ -377,7 +378,7 @@ public class RuleChainServiceTest extends AbstractServiceTest {
RuleNode ruleNode = new RuleNode(); RuleNode ruleNode = new RuleNode();
ruleNode.setName("Input node"); ruleNode.setName("Input node");
ruleNode.setType("org.thingsboard.rule.engine.flow.TbRuleChainInputNode"); ruleNode.setType(TB_RULE_CHAIN_INPUT_NODE);
ObjectNode configuration = JacksonUtil.newObjectNode(); ObjectNode configuration = JacksonUtil.newObjectNode();
configuration.put("ruleChainId", savedToRuleChain.getId().toString()); configuration.put("ruleChainId", savedToRuleChain.getId().toString());
ruleNode.setConfiguration(configuration); ruleNode.setConfiguration(configuration);
@ -402,7 +403,7 @@ public class RuleChainServiceTest extends AbstractServiceTest {
RuleNode newRuleNode = new RuleNode(); RuleNode newRuleNode = new RuleNode();
newRuleNode.setName("Input node"); newRuleNode.setName("Input node");
newRuleNode.setType("org.thingsboard.rule.engine.flow.TbRuleChainInputNode"); newRuleNode.setType(TB_RULE_CHAIN_INPUT_NODE);
ObjectNode newConfiguration = JacksonUtil.newObjectNode(); ObjectNode newConfiguration = JacksonUtil.newObjectNode();
configuration.put("ruleChainId", savedNewToRuleChain.getId().toString()); configuration.put("ruleChainId", savedNewToRuleChain.getId().toString());
newRuleNode.setConfiguration(newConfiguration); newRuleNode.setConfiguration(newConfiguration);

Loading…
Cancel
Save