use of org.thingsboard.server.common.data.rule.RuleChainImportResult in project thingsboard by thingsboard.
the class BaseRuleChainService method importTenantRuleChains.
@Override
public List<RuleChainImportResult> importTenantRuleChains(TenantId tenantId, RuleChainData ruleChainData, boolean overwrite) {
List<RuleChainImportResult> importResults = new ArrayList<>();
setRandomRuleChainIds(ruleChainData);
resetRuleNodeIds(ruleChainData.getMetadata());
resetRuleChainMetadataTenantIds(tenantId, ruleChainData.getMetadata());
for (RuleChain ruleChain : ruleChainData.getRuleChains()) {
RuleChainImportResult importResult = new RuleChainImportResult();
ruleChain.setTenantId(tenantId);
ruleChain.setRoot(false);
if (overwrite) {
Collection<RuleChain> existingRuleChains = ruleChainDao.findByTenantIdAndTypeAndName(tenantId, Optional.ofNullable(ruleChain.getType()).orElse(RuleChainType.CORE), ruleChain.getName());
Optional<RuleChain> existingRuleChain = existingRuleChains.stream().findFirst();
if (existingRuleChain.isPresent()) {
setNewRuleChainId(ruleChain, ruleChainData.getMetadata(), ruleChain.getId(), existingRuleChain.get().getId());
ruleChain.setRoot(existingRuleChain.get().isRoot());
importResult.setUpdated(true);
}
}
try {
ruleChain = saveRuleChain(ruleChain);
} catch (Exception e) {
importResult.setError(ExceptionUtils.getRootCauseMessage(e));
}
importResult.setTenantId(tenantId);
importResult.setRuleChainId(ruleChain.getId());
importResult.setRuleChainName(ruleChain.getName());
importResults.add(importResult);
}
if (CollectionUtils.isNotEmpty(ruleChainData.getMetadata())) {
ruleChainData.getMetadata().forEach(md -> saveRuleChainMetaData(tenantId, md));
}
return importResults;
}
use of org.thingsboard.server.common.data.rule.RuleChainImportResult in project thingsboard by thingsboard.
the class RuleChainController method importRuleChains.
@ApiOperation(value = "Import Rule Chains", notes = "Imports all tenant rule chains as one JSON." + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChains/import", method = RequestMethod.POST)
@ResponseBody
public List<RuleChainImportResult> importRuleChains(@ApiParam(value = "A JSON value representing the rule chains.") @RequestBody RuleChainData ruleChainData, @ApiParam(value = "Enables overwrite for existing rule chains with the same name.") @RequestParam(required = false, defaultValue = "false") boolean overwrite) throws ThingsboardException {
try {
TenantId tenantId = getCurrentUser().getTenantId();
List<RuleChainImportResult> importResults = ruleChainService.importTenantRuleChains(tenantId, ruleChainData, overwrite);
for (RuleChainImportResult importResult : importResults) {
if (importResult.getError() == null) {
tbClusterService.broadcastEntityStateChangeEvent(importResult.getTenantId(), importResult.getRuleChainId(), importResult.isUpdated() ? ComponentLifecycleEvent.UPDATED : ComponentLifecycleEvent.CREATED);
}
}
return importResults;
} catch (Exception e) {
throw handleException(e);
}
}
Aggregations