Commit d44ab0827719639fdef556faac254e6ba64fc554
1 parent
9d23930b
Check target Rule Nodes/Rule Chains existence when fetching Rule Node/Rule Chain relations.
Showing
1 changed file
with
32 additions
and
2 deletions
@@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j; | @@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j; | ||
20 | import org.apache.commons.lang3.StringUtils; | 20 | import org.apache.commons.lang3.StringUtils; |
21 | import org.springframework.beans.factory.annotation.Autowired; | 21 | import org.springframework.beans.factory.annotation.Autowired; |
22 | import org.springframework.stereotype.Service; | 22 | import org.springframework.stereotype.Service; |
23 | +import org.thingsboard.server.common.data.BaseData; | ||
23 | import org.thingsboard.server.common.data.EntityType; | 24 | import org.thingsboard.server.common.data.EntityType; |
24 | import org.thingsboard.server.common.data.Tenant; | 25 | import org.thingsboard.server.common.data.Tenant; |
25 | import org.thingsboard.server.common.data.id.EntityId; | 26 | import org.thingsboard.server.common.data.id.EntityId; |
@@ -274,14 +275,43 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC | @@ -274,14 +275,43 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC | ||
274 | public List<RuleNode> getRuleChainNodes(TenantId tenantId, RuleChainId ruleChainId) { | 275 | public List<RuleNode> getRuleChainNodes(TenantId tenantId, RuleChainId ruleChainId) { |
275 | Validator.validateId(ruleChainId, "Incorrect rule chain id for search request."); | 276 | Validator.validateId(ruleChainId, "Incorrect rule chain id for search request."); |
276 | List<EntityRelation> relations = getRuleChainToNodeRelations(tenantId, ruleChainId); | 277 | List<EntityRelation> relations = getRuleChainToNodeRelations(tenantId, ruleChainId); |
277 | - List<RuleNode> ruleNodes = relations.stream().map(relation -> ruleNodeDao.findById(tenantId, relation.getTo().getId())).collect(Collectors.toList()); | 278 | + List<RuleNode> ruleNodes = new ArrayList<>(); |
279 | + for (EntityRelation relation : relations) { | ||
280 | + RuleNode ruleNode = ruleNodeDao.findById(tenantId, relation.getTo().getId()); | ||
281 | + if (ruleNode != null) { | ||
282 | + ruleNodes.add(ruleNode); | ||
283 | + } else { | ||
284 | + relationService.deleteRelation(tenantId, relation); | ||
285 | + } | ||
286 | + } | ||
278 | return ruleNodes; | 287 | return ruleNodes; |
279 | } | 288 | } |
280 | 289 | ||
281 | @Override | 290 | @Override |
282 | public List<EntityRelation> getRuleNodeRelations(TenantId tenantId, RuleNodeId ruleNodeId) { | 291 | public List<EntityRelation> getRuleNodeRelations(TenantId tenantId, RuleNodeId ruleNodeId) { |
283 | Validator.validateId(ruleNodeId, "Incorrect rule node id for search request."); | 292 | Validator.validateId(ruleNodeId, "Incorrect rule node id for search request."); |
284 | - return relationService.findByFrom(tenantId, ruleNodeId, RelationTypeGroup.RULE_NODE); | 293 | + List<EntityRelation> relations = relationService.findByFrom(tenantId, ruleNodeId, RelationTypeGroup.RULE_NODE); |
294 | + List<EntityRelation> validRelations = new ArrayList<>(); | ||
295 | + for (EntityRelation relation : relations) { | ||
296 | + boolean valid = true; | ||
297 | + EntityType toType = relation.getTo().getEntityType(); | ||
298 | + if (toType == EntityType.RULE_NODE || toType == EntityType.RULE_CHAIN) { | ||
299 | + BaseData entity; | ||
300 | + if (relation.getTo().getEntityType() == EntityType.RULE_NODE) { | ||
301 | + entity = ruleNodeDao.findById(tenantId, relation.getTo().getId()); | ||
302 | + } else { | ||
303 | + entity = ruleChainDao.findById(tenantId, relation.getTo().getId()); | ||
304 | + } | ||
305 | + if (entity == null) { | ||
306 | + relationService.deleteRelation(tenantId, relation); | ||
307 | + valid = false; | ||
308 | + } | ||
309 | + } | ||
310 | + if (valid) { | ||
311 | + validRelations.add(relation); | ||
312 | + } | ||
313 | + } | ||
314 | + return validRelations; | ||
285 | } | 315 | } |
286 | 316 | ||
287 | @Override | 317 | @Override |