use of org.eclipse.kapua.KapuaIllegalArgumentException 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());
}
Aggregations