use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.
the class GwtAccountServiceImpl method create.
public GwtAccount create(GwtXSRFToken xsrfToken, GwtAccountCreator gwtAccountCreator) throws GwtKapuaException {
//
// Checking validity of the given XSRF Token
checkXSRFToken(xsrfToken);
GwtAccount gwtAccount = null;
KapuaId parentAccountId = KapuaEid.parseShortId(gwtAccountCreator.getParentAccountId());
try {
KapuaLocator locator = KapuaLocator.getInstance();
AccountFactory accountFactory = locator.getFactory(AccountFactory.class);
AccountCreator accountCreator = accountFactory.newAccountCreator(parentAccountId, gwtAccountCreator.getAccountName());
accountCreator.setAccountPassword(gwtAccountCreator.getAccountPassword());
accountCreator.setOrganizationName(gwtAccountCreator.getOrganizationName());
accountCreator.setOrganizationPersonName(gwtAccountCreator.getOrganizationPersonName());
accountCreator.setOrganizationEmail(gwtAccountCreator.getOrganizationEmail());
accountCreator.setOrganizationPhoneNumber(gwtAccountCreator.getOrganizationPhoneNumber());
accountCreator.setOrganizationAddressLine1(gwtAccountCreator.getOrganizationAddressLine1());
accountCreator.setOrganizationAddressLine2(gwtAccountCreator.getOrganizationAddressLine2());
accountCreator.setOrganizationCity(gwtAccountCreator.getOrganizationCity());
accountCreator.setOrganizationZipPostCode(gwtAccountCreator.getOrganizationZipPostCode());
accountCreator.setOrganizationStateProvinceCounty(gwtAccountCreator.getOrganizationStateProvinceCounty());
accountCreator.setOrganizationCountry(gwtAccountCreator.getOrganizationCountry());
// create the Account
AccountService accountService = locator.getService(AccountService.class);
Account account = accountService.create(accountCreator);
// convert to GwtAccount and return
gwtAccount = KapuaGwtConverter.convert(account);
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
}
return gwtAccount;
}
use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.
the class GwtAccountServiceImpl method updateAccountProperties.
public GwtAccount updateAccountProperties(GwtXSRFToken xsrfToken, GwtAccount gwtAccount) throws GwtKapuaException {
//
// Checking validity of the given XSRF Token
checkXSRFToken(xsrfToken);
GwtAccount gwtAccountUpdated = null;
KapuaId scopeId = KapuaEid.parseShortId(gwtAccount.getId());
try {
KapuaLocator locator = KapuaLocator.getInstance();
AccountService accountService = locator.getService(AccountService.class);
Account account = accountService.find(scopeId);
// update properties
Properties property = account.getEntityProperties();
if (property == null) {
property = new Properties();
}
if (gwtAccount.getUnescapedDashboardPreferredTopic() != null) {
property.put("topic", gwtAccount.getUnescapedDashboardPreferredTopic());
}
if (gwtAccount.getUnescapedDashboardPreferredMetric() != null) {
property.put("metric", gwtAccount.getUnescapedDashboardPreferredMetric());
}
account.setEntityProperties(property);
account = accountService.update(account);
// convert to GwtAccount and return
gwtAccountUpdated = KapuaGwtConverter.convert(account);
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
}
return gwtAccountUpdated;
}
use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.
the class AccountServiceImpl method create.
@Override
public Account create(AccountCreator accountCreator) throws KapuaException {
//
// Validation of the fields
ArgumentValidator.notNull(accountCreator, "accountCreator");
ArgumentValidator.notEmptyOrNull(accountCreator.getName(), "name");
ArgumentValidator.notEmptyOrNull(accountCreator.getAccountPassword(), "accountPassword");
ArgumentValidator.notEmptyOrNull(accountCreator.getOrganizationName(), "organizationName");
ArgumentValidator.notEmptyOrNull(accountCreator.getOrganizationEmail(), "organizationEmail");
ArgumentValidator.notNull(accountCreator.getScopeId(), "scopeId");
ArgumentValidator.notNull(accountCreator.getScopeId().getId(), "scopeId.id");
ArgumentValidator.match(accountCreator.getAccountPassword(), ArgumentValidator.PASSWORD_REGEXP, "accountPassword");
ArgumentValidator.match(accountCreator.getOrganizationEmail(), 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, accountCreator.getScopeId()));
// Check if the parent account exists
if (findById(accountCreator.getScopeId()) == null) {
throw new KapuaIllegalArgumentException("scopeId", "parent account does not exist");
}
//
// Create the account
Account account = null;
EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
try {
em.beginTransaction();
account = AccountDAO.create(em, accountCreator);
em.persist(account);
// Set the parent account path
String parentAccountPath = AccountDAO.find(em, accountCreator.getScopeId()).getParentAccountPath() + "/" + account.getId();
account.setParentAccountPath(parentAccountPath);
AccountDAO.update(em, account);
em.commit();
} catch (Exception pe) {
em.rollback();
throw KapuaExceptionUtils.convertPersistenceException(pe);
} finally {
em.close();
}
// to make sure we get all the latest info/version
return find(account.getScopeId(), account.getId());
}
use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.
the class AccountServiceImpl method findChildsRecursively.
@Override
public AccountListResult findChildsRecursively(KapuaId id) throws KapuaException {
//
// Validation of the fields
ArgumentValidator.notNull(id, "scopeId");
ArgumentValidator.notNull(id.getId(), "scopeId.id");
//
// Make sure account exists
Account account = findById(id);
if (account == null) {
throw new KapuaEntityNotFoundException(Account.TYPE, id);
}
//
// 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.read, account.getId()));
AccountListResult result = null;
EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
try {
TypedQuery<Account> q;
q = em.createNamedQuery("Account.findChildAccountsRecursive", Account.class);
q.setParameter("parentAccountPath", account.getParentAccountPath() + "/%");
result = new AccountListResultImpl();
result.addItems(q.getResultList());
} catch (Exception e) {
em.rollback();
throw KapuaExceptionUtils.convertPersistenceException(e);
} finally {
em.close();
}
return result;
}
use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.
the class AccountServiceImpl method delete.
@Override
public void delete(KapuaId scopeId, KapuaId accountId) throws KapuaException {
//
// Validation of the fields
ArgumentValidator.notNull(accountId, "id");
ArgumentValidator.notNull(scopeId, "id.id");
//
// Check Access
Actions action = Actions.write;
KapuaLocator locator = KapuaLocator.getInstance();
AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
authorizationService.checkPermission(permissionFactory.newPermission(AccountDomain.ACCOUNT, action, scopeId));
// Check if it has children
if (this.findChildAccountsTrusted(accountId).size() > 0) {
throw new KapuaAccountException(KapuaAccountErrorCodes.OPERATION_NOT_ALLOWED, null, "This account cannot be deleted. Delete its child first.");
}
//
// Delete the Account
EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
try {
// Entity needs to be loaded in the context of the same EntityManger to be able to delete it afterwards
Account accountx = AccountDAO.find(em, accountId);
if (accountx == null) {
throw new KapuaEntityNotFoundException(Account.TYPE, accountId);
}
// do not allow deletion of the kapua admin account
SystemSetting settings = SystemSetting.getInstance();
if (settings.getString(SystemSettingKey.SYS_PROVISION_ACCOUNT_NAME).equals(accountx.getName())) {
throw new KapuaIllegalAccessException(action.name());
}
if (settings.getString(SystemSettingKey.SYS_ADMIN_ACCOUNT).equals(accountx.getName())) {
throw new KapuaIllegalAccessException(action.name());
}
em.beginTransaction();
AccountDAO.delete(em, accountId);
em.commit();
} catch (Exception e) {
em.rollback();
throw KapuaExceptionUtils.convertPersistenceException(e);
} finally {
em.close();
}
}
Aggregations