Search in sources :

Example 11 with IncorrectParameterException

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

the class BasePluginService method savePlugin.

@Override
public PluginMetaData savePlugin(PluginMetaData plugin) {
    pluginValidator.validate(plugin);
    if (plugin.getTenantId() == null) {
        log.trace("Save system plugin metadata with predefined id {}", SYSTEM_TENANT);
        plugin.setTenantId(SYSTEM_TENANT);
    }
    if (plugin.getId() != null) {
        PluginMetaData oldVersion = pluginDao.findById(plugin.getId());
        if (plugin.getState() == null) {
            plugin.setState(oldVersion.getState());
        } else if (plugin.getState() != oldVersion.getState()) {
            throw new IncorrectParameterException("Use Activate/Suspend method to control state of the plugin!");
        }
    } else {
        if (plugin.getState() == null) {
            plugin.setState(ComponentLifecycleState.SUSPENDED);
        } else if (plugin.getState() != ComponentLifecycleState.SUSPENDED) {
            throw new IncorrectParameterException("Use Activate/Suspend method to control state of the plugin!");
        }
    }
    ComponentDescriptor descriptor = componentDescriptorService.findByClazz(plugin.getClazz());
    if (descriptor == null) {
        throw new IncorrectParameterException("Plugin descriptor not found!");
    } else if (!ComponentType.PLUGIN.equals(descriptor.getType())) {
        throw new IncorrectParameterException("Plugin class is actually " + descriptor.getType() + "!");
    }
    PluginMetaData savedPlugin = pluginDao.findByApiToken(plugin.getApiToken());
    if (savedPlugin != null && (plugin.getId() == null || !savedPlugin.getId().getId().equals(plugin.getId().getId()))) {
        throw new IncorrectParameterException("API token is already reserved!");
    }
    if (!componentDescriptorService.validate(descriptor, plugin.getConfiguration())) {
        throw new IncorrectParameterException("Filters configuration is not valid!");
    }
    return pluginDao.save(plugin);
}
Also used : IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData)

Example 12 with IncorrectParameterException

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

the class CustomerServiceImpl method findOrCreatePublicCustomer.

@Override
public Customer findOrCreatePublicCustomer(TenantId tenantId) {
    log.trace("Executing findOrCreatePublicCustomer, tenantId [{}]", tenantId);
    Validator.validateId(tenantId, INCORRECT_CUSTOMER_ID + tenantId);
    Optional<Customer> publicCustomerOpt = customerDao.findCustomersByTenantIdAndTitle(tenantId.getId(), PUBLIC_CUSTOMER_TITLE);
    if (publicCustomerOpt.isPresent()) {
        return publicCustomerOpt.get();
    } else {
        Customer publicCustomer = new Customer();
        publicCustomer.setTenantId(tenantId);
        publicCustomer.setTitle(PUBLIC_CUSTOMER_TITLE);
        try {
            publicCustomer.setAdditionalInfo(new ObjectMapper().readValue("{ \"isPublic\": true }", JsonNode.class));
        } catch (IOException e) {
            throw new IncorrectParameterException("Unable to create public customer.", e);
        }
        return customerDao.save(publicCustomer);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 13 with IncorrectParameterException

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

the class CustomerServiceImpl method deleteCustomer.

@Override
public void deleteCustomer(CustomerId customerId) {
    log.trace("Executing deleteCustomer [{}]", customerId);
    Validator.validateId(customerId, INCORRECT_CUSTOMER_ID + customerId);
    Customer customer = findCustomerById(customerId);
    if (customer == null) {
        throw new IncorrectParameterException("Unable to delete non-existent customer.");
    }
    dashboardService.unassignCustomerDashboards(customerId);
    assetService.unassignCustomerAssets(customer.getTenantId(), customerId);
    deviceService.unassignCustomerDevices(customer.getTenantId(), customerId);
    userService.deleteCustomerUsers(customer.getTenantId(), customerId);
    deleteEntityRelations(customerId);
    customerDao.removeById(customerId.getId());
}
Also used : Customer(org.thingsboard.server.common.data.Customer) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException)

Aggregations

IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Customer (org.thingsboard.server.common.data.Customer)4 ComponentDescriptor (org.thingsboard.server.common.data.plugin.ComponentDescriptor)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)2 UserCredentials (org.thingsboard.server.common.data.security.UserCredentials)2 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)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 IOException (java.io.IOException)1 AdminSettings (org.thingsboard.server.common.data.AdminSettings)1 Device (org.thingsboard.server.common.data.Device)1 User (org.thingsboard.server.common.data.User)1 Asset (org.thingsboard.server.common.data.asset.Asset)1 AssetId (org.thingsboard.server.common.data.id.AssetId)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 RuleMetaData (org.thingsboard.server.common.data.rule.RuleMetaData)1