Search in sources :

Example 1 with RuleChainImportResult

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;
}
Also used : RuleChainImportResult(org.thingsboard.server.common.data.rule.RuleChainImportResult) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ArrayList(java.util.ArrayList) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException)

Example 2 with RuleChainImportResult

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);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChainImportResult(org.thingsboard.server.common.data.rule.RuleChainImportResult) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

RuleChainImportResult (org.thingsboard.server.common.data.rule.RuleChainImportResult)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ArrayList (java.util.ArrayList)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)1 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)1