Search in sources :

Example 11 with KapuaException

use of org.eclipse.kapua.KapuaException in project kapua by eclipse.

the class TopicInfoStoreServiceImpl method delete.

// 
// @Override
// public StorableId store(KapuaId scopeId, TopicInfoCreator creator)
// throws KapuaException
// {
// // TODO DAOs are ready, need to evaluate if this functionality
// // have to be available or not. Currently entries are added by
// // the message service directy
// throw KapuaException.internalError("Not implemented");
// }
// 
// @Override
// public StorableId update(KapuaId scopeId, TopicInfo creator)
// throws KapuaException
// {
// // TODO DAOs are ready, need to evaluate if this functionality
// // have to be available or not. Currently entries are added by
// // the message service directy
// throw KapuaException.internalError("Not implemented");
// }
@Override
public void delete(KapuaId scopeId, StorableId id) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(id, "id");
    // 
    // Check Access
    this.checkDataAccess(scopeId, Actions.delete);
    // 
    // Do the find
    AccountInfo accountInfo = getAccountServicePlan(scopeId);
    String scopeName = accountInfo.getAccount().getName();
    LocalServicePlan accountServicePlan = accountInfo.getServicePlan();
    long ttl = accountServicePlan.getDataTimeToLive() * DAY_MILLIS;
    if (!accountServicePlan.getDataStorageEnabled() || ttl == LocalServicePlan.DISABLED) {
        logger.debug("Storage not enabled for account {}, return", scopeName);
        return;
    }
    try {
        String everyIndex = EsUtils.getAnyIndexName(scopeName);
        TopicInfo topicInfo = this.find(scopeId, id);
        MessageQueryImpl mqi = new MessageQueryImpl();
        TopicMatchPredicateImpl predicate = new TopicMatchPredicateImpl(topicInfo.getFullTopicName());
        mqi.setPredicate(predicate);
        EsMessageDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.MESSAGE_TYPE_NAME).setListener(null).deleteByQuery(mqi);
        MetricInfoQueryImpl miqi = new MetricInfoQueryImpl();
        mqi.setPredicate(predicate);
        EsMetricDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.METRIC_TYPE_NAME).setListener(null).deleteByQuery(miqi);
        EsTopicDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.TOPIC_TYPE_NAME).deleteById(id.toString());
    } catch (Exception exc) {
        // CassandraUtils.handleException(e);
        throw KapuaException.internalError(exc);
    }
}
Also used : MessageQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.MessageQueryImpl) LocalServicePlan(org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan) MetricInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.MetricInfoQueryImpl) TopicMatchPredicateImpl(org.eclipse.kapua.service.datastore.internal.model.query.TopicMatchPredicateImpl) TopicInfo(org.eclipse.kapua.service.datastore.model.TopicInfo) KapuaException(org.eclipse.kapua.KapuaException)

Example 12 with KapuaException

use of org.eclipse.kapua.KapuaException in project kapua by eclipse.

the class TopicInfoStoreServiceImpl method count.

@Override
public long count(KapuaId scopeId, TopicInfoQuery query) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(query, "query");
    // 
    // Check Access
    this.checkDataAccess(scopeId, Actions.read);
    // 
    // Do the find
    AccountInfo accountInfo = getAccountServicePlan(scopeId);
    String scopeName = accountInfo.getAccount().getName();
    LocalServicePlan accountServicePlan = accountInfo.getServicePlan();
    long ttl = accountServicePlan.getDataTimeToLive() * DAY_MILLIS;
    if (!accountServicePlan.getDataStorageEnabled() || ttl == LocalServicePlan.DISABLED) {
        logger.debug("Storage not enabled for account {}, returning empty result", scopeName);
        return 0;
    }
    try {
        String everyIndex = EsUtils.getAnyIndexName(scopeName);
        long result;
        result = EsTopicDAO.connection(EsClient.getcurrent()).instance(everyIndex, EsSchema.TOPIC_TYPE_NAME).count(query);
        return result;
    } catch (Exception exc) {
        // CassandraUtils.handleException(e);
        throw KapuaException.internalError(exc);
    }
}
Also used : LocalServicePlan(org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan) KapuaException(org.eclipse.kapua.KapuaException)

Example 13 with KapuaException

use of org.eclipse.kapua.KapuaException in project kapua by eclipse.

the class AccountServiceImpl method findChildAccountsTrusted.

private List<Account> findChildAccountsTrusted(KapuaId accountId) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(accountId, "accountId");
    ArgumentValidator.notNull(accountId.getId(), "accountId.id");
    // 
    // Do the find
    List<Account> accounts = null;
    EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
    try {
        TypedQuery<Account> q = em.createNamedQuery("Account.findChildAccounts", Account.class);
        q.setParameter("scopeId", accountId);
        accounts = q.getResultList();
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return accounts;
}
Also used : Account(org.eclipse.kapua.service.account.Account) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaException(org.eclipse.kapua.KapuaException)

Example 14 with KapuaException

use of org.eclipse.kapua.KapuaException in project kapua by eclipse.

the class AccountServiceImpl method findById.

/**
 * Find an account without authorization.
 *
 * @param accountId
 * @return
 * @throws KapuaException
 */
private Account findById(KapuaId accountId) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(accountId, "accountId");
    // 
    // Do the find
    Account account = null;
    EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
    try {
        account = AccountDAO.find(em, accountId);
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return account;
}
Also used : Account(org.eclipse.kapua.service.account.Account) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaException(org.eclipse.kapua.KapuaException)

Example 15 with KapuaException

use of org.eclipse.kapua.KapuaException in project kapua by eclipse.

the class AccountServiceImpl method update.

@Override
public Account update(Account account) throws KapuaException {
    // 
    // Validation of the fields
    ArgumentValidator.notNull(account.getId(), "id");
    ArgumentValidator.notNull(account.getId().getId(), "id.id");
    ArgumentValidator.notEmptyOrNull(account.getName(), "accountName");
    ArgumentValidator.notNull(account.getOrganization(), "organization");
    ArgumentValidator.match(account.getOrganization().getEmail(), ArgumentValidator.EMAIL_REGEXP, "organizationEmail");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.write, account.getScopeId()));
    // 
    // Update the Account
    EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
    try {
        Account oldAccount = AccountDAO.find(em, account.getId());
        if (oldAccount == null) {
            throw new KapuaEntityNotFoundException(Account.TYPE, account.getId());
        }
        // Verify unchanged parent account ID and parent account path
        if (!oldAccount.getScopeId().equals(account.getScopeId())) {
            throw new KapuaAccountException(KapuaAccountErrorCodes.ILLEGAL_ARGUMENT, null, "scopeId");
        }
        if (oldAccount.getParentAccountPath().compareTo(account.getParentAccountPath()) != 0) {
            throw new KapuaAccountException(KapuaAccountErrorCodes.ILLEGAL_ARGUMENT, null, "parentAccountPath");
        }
        if (oldAccount.getName().compareTo(account.getName()) != 0) {
            throw new KapuaAccountException(KapuaAccountErrorCodes.ILLEGAL_ARGUMENT, null, "accountName");
        }
        // Update
        em.beginTransaction();
        AccountDAO.update(em, account);
        em.commit();
    } catch (Exception e) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return find(account.getScopeId(), account.getId());
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaException(org.eclipse.kapua.KapuaException)

Aggregations

KapuaException (org.eclipse.kapua.KapuaException)99 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)53 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)50 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)50 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)48 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)48 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)19 LocalServicePlan (org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan)17 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)12 Date (java.util.Date)11 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)10 Account (org.eclipse.kapua.service.account.Account)9 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)8 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)8 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)8 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)8 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)8 IOException (java.io.IOException)7 TranslatorException (org.eclipse.kapua.translator.exception.TranslatorException)7 UnknownHostException (java.net.UnknownHostException)6