Search in sources :

Example 1 with DataValidationException

use of org.thingsboard.server.dao.exception.DataValidationException in project thingsboard by thingsboard.

the class DashboardServiceImpl method unassignDashboardFromCustomer.

@Override
public Dashboard unassignDashboardFromCustomer(DashboardId dashboardId, CustomerId customerId) {
    Dashboard dashboard = findDashboardById(dashboardId);
    Customer customer = customerDao.findById(customerId.getId());
    if (customer == null) {
        throw new DataValidationException("Can't unassign dashboard from non-existent customer!");
    }
    if (dashboard.removeAssignedCustomer(customer)) {
        try {
            deleteRelation(new EntityRelation(customerId, dashboardId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD));
        } catch (ExecutionException | InterruptedException e) {
            log.warn("[{}] Failed to delete dashboard relation. Customer Id: [{}]", dashboardId, customerId);
            throw new RuntimeException(e);
        }
        return saveDashboard(dashboard);
    } else {
        return dashboard;
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with DataValidationException

use of org.thingsboard.server.dao.exception.DataValidationException in project thingsboard by thingsboard.

the class BaseComponentDescriptorService method validate.

@Override
public boolean validate(ComponentDescriptor component, JsonNode configuration) {
    JsonValidator validator = JsonSchemaFactory.byDefault().getValidator();
    try {
        if (!component.getConfigurationDescriptor().has("schema")) {
            throw new DataValidationException("Configuration descriptor doesn't contain schema property!");
        }
        JsonNode configurationSchema = component.getConfigurationDescriptor().get("schema");
        ProcessingReport report = validator.validate(configurationSchema, configuration);
        return report.isSuccess();
    } catch (ProcessingException e) {
        throw new IncorrectParameterException(e.getMessage(), e);
    }
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonValidator(com.github.fge.jsonschema.main.JsonValidator) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

Example 3 with DataValidationException

use of org.thingsboard.server.dao.exception.DataValidationException in project thingsboard by thingsboard.

the class BasePluginService method suspendPluginById.

@Override
public void suspendPluginById(PluginId pluginId) {
    PluginMetaData plugin = pluginDao.findById(pluginId);
    List<RuleMetaData> affectedRules = ruleDao.findRulesByPlugin(plugin.getApiToken()).stream().filter(rule -> rule.getState() == ComponentLifecycleState.ACTIVE).collect(Collectors.toList());
    if (affectedRules.isEmpty()) {
        updateLifeCycleState(pluginId, ComponentLifecycleState.SUSPENDED);
    } else {
        throw new DataValidationException("Can't suspend plugin that has active rules!");
    }
}
Also used : ComponentType(org.thingsboard.server.common.data.plugin.ComponentType) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Autowired(org.springframework.beans.factory.annotation.Autowired) TextPageData(org.thingsboard.server.common.data.page.TextPageData) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) ArrayList(java.util.ArrayList) Validator.validateId(org.thingsboard.server.dao.service.Validator.validateId) ComponentLifecycleState(org.thingsboard.server.common.data.plugin.ComponentLifecycleState) PluginId(org.thingsboard.server.common.data.id.PluginId) Service(org.springframework.stereotype.Service) ComponentDescriptorService(org.thingsboard.server.dao.component.ComponentDescriptorService) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) Validator(org.thingsboard.server.dao.service.Validator) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) DatabaseException(org.thingsboard.server.dao.exception.DatabaseException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) RuleDao(org.thingsboard.server.dao.rule.RuleDao) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) List(java.util.List) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData)

Example 4 with DataValidationException

use of org.thingsboard.server.dao.exception.DataValidationException 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)

Example 5 with DataValidationException

use of org.thingsboard.server.dao.exception.DataValidationException in project thingsboard by thingsboard.

the class DashboardServiceImpl method assignDashboardToCustomer.

@Override
public Dashboard assignDashboardToCustomer(DashboardId dashboardId, CustomerId customerId) {
    Dashboard dashboard = findDashboardById(dashboardId);
    Customer customer = customerDao.findById(customerId.getId());
    if (customer == null) {
        throw new DataValidationException("Can't assign dashboard to non-existent customer!");
    }
    if (!customer.getTenantId().getId().equals(dashboard.getTenantId().getId())) {
        throw new DataValidationException("Can't assign dashboard to customer from different tenant!");
    }
    if (dashboard.addAssignedCustomer(customer)) {
        try {
            createRelation(new EntityRelation(customerId, dashboardId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD));
        } catch (ExecutionException | InterruptedException e) {
            log.warn("[{}] Failed to create dashboard relation. Customer Id: [{}]", dashboardId, customerId);
            throw new RuntimeException(e);
        }
        return saveDashboard(dashboard);
    } else {
        return dashboard;
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)5 ExecutionException (java.util.concurrent.ExecutionException)2 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)2 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)2 RuleMetaData (org.thingsboard.server.common.data.rule.RuleMetaData)2 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)1 ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)1 JsonValidator (com.github.fge.jsonschema.main.JsonValidator)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Service (org.springframework.stereotype.Service)1 PluginId (org.thingsboard.server.common.data.id.PluginId)1