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;
}
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);
}
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;
}
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;
}
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());
}
Aggregations