Search in sources :

Example 26 with RuleMetaData

use of org.thingsboard.server.common.data.rule.RuleMetaData in project thingsboard by thingsboard.

the class RuleController method suspendRuleById.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/rule/{ruleId}/suspend", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void suspendRuleById(@PathVariable(RULE_ID) String strRuleId) throws ThingsboardException {
    checkParameter(RULE_ID, strRuleId);
    try {
        RuleId ruleId = new RuleId(toUUID(strRuleId));
        RuleMetaData rule = checkRule(ruleService.findRuleById(ruleId));
        ruleService.suspendRuleById(ruleId);
        actorService.onRuleStateChange(rule.getTenantId(), rule.getId(), ComponentLifecycleEvent.SUSPENDED);
        logEntityAction(rule.getId(), rule, null, ActionType.SUSPENDED, null, strRuleId);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE), null, null, ActionType.SUSPENDED, e, strRuleId);
        throw handleException(e);
    }
}
Also used : RuleId(org.thingsboard.server.common.data.id.RuleId) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 27 with RuleMetaData

use of org.thingsboard.server.common.data.rule.RuleMetaData in project thingsboard by thingsboard.

the class RuleController method saveRule.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/rule", method = RequestMethod.POST)
@ResponseBody
public RuleMetaData saveRule(@RequestBody RuleMetaData source) throws ThingsboardException {
    try {
        boolean created = source.getId() == null;
        source.setTenantId(getCurrentUser().getTenantId());
        RuleMetaData rule = checkNotNull(ruleService.saveRule(source));
        actorService.onRuleStateChange(rule.getTenantId(), rule.getId(), created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED);
        logEntityAction(rule.getId(), rule, null, created ? ActionType.ADDED : ActionType.UPDATED, null);
        return rule;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE), source, null, source.getId() == null ? ActionType.ADDED : ActionType.UPDATED, e);
        throw handleException(e);
    }
}
Also used : RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 28 with RuleMetaData

use of org.thingsboard.server.common.data.rule.RuleMetaData in project thingsboard by thingsboard.

the class BaseRuleService method updateLifeCycleState.

private void updateLifeCycleState(RuleId ruleId, ComponentLifecycleState state) {
    Validator.validateId(ruleId, "Incorrect rule id for state change request.");
    RuleMetaData rule = ruleDao.findById(ruleId);
    if (rule != null) {
        rule.setState(state);
        validateRuleAndPluginState(rule);
        ruleDao.save(rule);
    } else {
        throw new DatabaseException("Plugin not found!");
    }
}
Also used : RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) DatabaseException(org.thingsboard.server.dao.exception.DatabaseException)

Example 29 with RuleMetaData

use of org.thingsboard.server.common.data.rule.RuleMetaData in project thingsboard by thingsboard.

the class BaseRuleService method findAllTenantRulesByTenantId.

@Override
public List<RuleMetaData> findAllTenantRulesByTenantId(TenantId tenantId) {
    log.trace("Executing findAllTenantRulesByTenantId, tenantId [{}]", tenantId);
    Validator.validateId(tenantId, "Incorrect tenantId " + tenantId);
    List<RuleMetaData> rules = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(300);
    TextPageData<RuleMetaData> pageData = null;
    do {
        pageData = findAllTenantRulesByTenantIdAndPageLink(tenantId, pageLink);
        rules.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    return rules;
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) ArrayList(java.util.ArrayList) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData)

Example 30 with RuleMetaData

use of org.thingsboard.server.common.data.rule.RuleMetaData in project thingsboard by thingsboard.

the class BasePluginService method checkRulesAndDelete.

private void checkRulesAndDelete(UUID pluginId) {
    PluginMetaData plugin = pluginDao.findById(pluginId);
    List<RuleMetaData> affectedRules = ruleDao.findRulesByPlugin(plugin.getApiToken());
    if (affectedRules.isEmpty()) {
        pluginDao.deleteById(pluginId);
    } else {
        throw new DataValidationException("Plugin deletion will affect existing rules!");
    }
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData)

Aggregations

RuleMetaData (org.thingsboard.server.common.data.rule.RuleMetaData)43 Test (org.junit.Test)20 TenantId (org.thingsboard.server.common.data.id.TenantId)12 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)12 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)10 RuleId (org.thingsboard.server.common.data.id.RuleId)8 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)7 ArrayList (java.util.ArrayList)6 UUID (java.util.UUID)5 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)4 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)3 ActorRef (akka.actor.ActorRef)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 List (java.util.List)2 Slf4j (lombok.extern.slf4j.Slf4j)2 RuleActorMetaData (org.thingsboard.server.actors.rule.RuleActorMetaData)2 SimpleRuleActorChain (org.thingsboard.server.actors.rule.SimpleRuleActorChain)2 PageDataIterable (org.thingsboard.server.common.data.page.PageDataIterable)2