Search in sources :

Example 1 with EntityManager

use of org.eclipse.kapua.commons.jpa.EntityManager in project kapua by eclipse.

the class AbstractKapuaConfigurableService method setConfigValues.

@Override
public void setConfigValues(KapuaId scopeId, Map<String, Object> values) throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.write, scopeId));
    KapuaTocd ocd = this.getConfigMetadata();
    validateConfigurations(this.pid, ocd, values);
    Properties props = toProperties(values);
    AndPredicate predicate = new AndPredicate().and(new AttributePredicate<String>("pid", this.pid, Operator.EQUAL)).and(new AttributePredicate<KapuaId>("scopeId", scopeId, Operator.EQUAL));
    ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
    query.setPredicate(predicate);
    ServiceConfig serviceConfig = null;
    EntityManager em = this.entityManagerFactory.createEntityManager();
    ServiceConfigListResultImpl result = ServiceConfigDAO.query(em, ServiceConfig.class, ServiceConfigImpl.class, new ServiceConfigListResultImpl(), query);
    // In not exists create then return
    if (result == null || result.getSize() == 0) {
        ServiceConfigImpl serviceConfigNew = new ServiceConfigImpl(scopeId);
        serviceConfigNew.setPid(this.pid);
        serviceConfigNew.setConfigurations(props);
        serviceConfig = this.create(em, serviceConfigNew);
        return;
    }
    // If exists update it
    serviceConfig = result.getItem(0);
    serviceConfig.setConfigurations(props);
    this.update(em, serviceConfig);
    return;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) Properties(java.util.Properties) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd)

Example 2 with EntityManager

use of org.eclipse.kapua.commons.jpa.EntityManager in project kapua by eclipse.

the class AbstractKapuaConfigurableService method getConfigValues.

@Override
public Map<String, Object> getConfigValues(KapuaId scopeId) throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.read, scopeId));
    AndPredicate predicate = new AndPredicate().and(new AttributePredicate<String>("pid", this.pid, Operator.EQUAL)).and(new AttributePredicate<KapuaId>("scopeId", scopeId, Operator.EQUAL));
    ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
    query.setPredicate(predicate);
    Properties properties = null;
    EntityManager em = this.entityManagerFactory.createEntityManager();
    ServiceConfigListResult result = ServiceConfigDAO.query(em, ServiceConfig.class, ServiceConfigImpl.class, new ServiceConfigListResultImpl(), query);
    if (result != null && result.getSize() > 0)
        properties = result.getItem(0).getConfigurations();
    KapuaTocd ocd = this.getConfigMetadata();
    return toValues(ocd, properties);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) Properties(java.util.Properties) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd)

Example 3 with EntityManager

use of org.eclipse.kapua.commons.jpa.EntityManager 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 4 with EntityManager

use of org.eclipse.kapua.commons.jpa.EntityManager 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 5 with EntityManager

use of org.eclipse.kapua.commons.jpa.EntityManager 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

EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)55 KapuaException (org.eclipse.kapua.KapuaException)53 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)47 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)43 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)43 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)36 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)16 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)9 Account (org.eclipse.kapua.service.account.Account)8 SimpleSqlScriptExecutor (org.eclipse.kapua.commons.jpa.SimpleSqlScriptExecutor)5 User (org.eclipse.kapua.service.user.User)5 Credential (org.eclipse.kapua.service.authentication.credential.Credential)3 DeviceConnection (org.eclipse.kapua.service.device.registry.connection.DeviceConnection)3 Properties (java.util.Properties)2 AndPredicate (org.eclipse.kapua.commons.model.query.predicate.AndPredicate)2 AttributePredicate (org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)2 KapuaTocd (org.eclipse.kapua.model.config.metatype.KapuaTocd)2 KapuaId (org.eclipse.kapua.model.id.KapuaId)2 Role (org.eclipse.kapua.service.authorization.role.Role)2 UserPermission (org.eclipse.kapua.service.authorization.user.permission.UserPermission)2