Search in sources :

Example 1 with KapuaIllegalArgumentException

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());
}
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) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaException(org.eclipse.kapua.KapuaException)

Aggregations

KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)1 KapuaException (org.eclipse.kapua.KapuaException)1 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)1 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)1 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)1 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)1 Account (org.eclipse.kapua.service.account.Account)1 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)1 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)1