Search in sources :

Example 1 with KapuaEntityNotFoundException

use of org.eclipse.kapua.KapuaEntityNotFoundException 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)

Example 2 with KapuaEntityNotFoundException

use of org.eclipse.kapua.KapuaEntityNotFoundException in project kapua by eclipse.

the class UserServiceImpl method update.

@Override
public User update(User user) throws KapuaException {
    // Validation of the fields
    ArgumentValidator.notNull(user.getId().getId(), "id");
    ArgumentValidator.notNull(user.getScopeId().getId(), "accountId");
    ArgumentValidator.notEmptyOrNull(user.getName(), "name");
    ArgumentValidator.match(user.getName(), ArgumentValidator.NAME_REGEXP, "name");
    // ArgumentValidator.match(user.getRawPassword(), ArgumentValidator.PASSWORD_REGEXP, "rawPassword");
    ArgumentValidator.match(user.getEmail(), ArgumentValidator.EMAIL_REGEXP, "email");
    validateSystemUser(user.getName());
    // 
    // Check Access
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(UserDomain.USER, Actions.write, user.getScopeId()));
    // 
    // Do update
    User userUpdated = null;
    EntityManager em = UserEntityManagerFactory.getInstance().createEntityManager();
    try {
        User currentUser = UserDAO.find(em, user.getId());
        if (currentUser == null) {
            throw new KapuaEntityNotFoundException(User.TYPE, user.getId());
        }
        em.beginTransaction();
        UserDAO.update(em, user);
        em.commit();
        userUpdated = UserDAO.find(em, user.getId());
    } catch (Exception pe) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(pe);
    } finally {
        em.close();
    }
    return userUpdated;
}
Also used : EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) User(org.eclipse.kapua.service.user.User) 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) KapuaException(org.eclipse.kapua.KapuaException)

Example 3 with KapuaEntityNotFoundException

use of org.eclipse.kapua.KapuaEntityNotFoundException in project kapua by eclipse.

the class TranslatorLifeAppsKuraKapua method translate.

@Override
public KapuaAppsMessage translate(KuraAppsMessage kuraAppsMessage) throws KapuaException {
    KapuaAppsMessage kapuaAppsMessage = new KapuaAppsMessageImpl();
    kapuaAppsMessage.setChannel(translate(kuraAppsMessage.getChannel()));
    kapuaAppsMessage.setPayload(translate(kuraAppsMessage.getPayload()));
    KapuaLocator locator = KapuaLocator.getInstance();
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.findByName(kuraAppsMessage.getChannel().getScope());
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    Device device = deviceRegistryService.findByClientId(account.getId(), kuraAppsMessage.getChannel().getClientId());
    if (device == null) {
        throw new KapuaEntityNotFoundException(Device.class.toString(), kuraAppsMessage.getChannel().getClientId());
    }
    kapuaAppsMessage.setDeviceId(device.getId());
    kapuaAppsMessage.setScopeId(account.getId());
    kapuaAppsMessage.setCapturedOn(kuraAppsMessage.getPayload().getTimestamp());
    kapuaAppsMessage.setSentOn(kuraAppsMessage.getPayload().getTimestamp());
    kapuaAppsMessage.setReceivedOn(kuraAppsMessage.getTimestamp());
    kapuaAppsMessage.setPosition(TranslatorKuraKapuaUtils.translate(kuraAppsMessage.getPayload().getPosition()));
    return kapuaAppsMessage;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) KapuaAppsMessageImpl(org.eclipse.kapua.message.internal.device.lifecycle.KapuaAppsMessageImpl) Device(org.eclipse.kapua.service.device.registry.Device) KapuaAppsMessage(org.eclipse.kapua.message.device.lifecycle.KapuaAppsMessage) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) AccountService(org.eclipse.kapua.service.account.AccountService) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException)

Example 4 with KapuaEntityNotFoundException

use of org.eclipse.kapua.KapuaEntityNotFoundException in project kapua by eclipse.

the class DeviceConnectionServiceImpl method update.

@Override
public DeviceConnection update(DeviceConnection deviceConnection) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(deviceConnection, "deviceConnection");
    ArgumentValidator.notNull(deviceConnection.getId(), "deviceConnection.id");
    ArgumentValidator.notNull(deviceConnection.getScopeId(), "deviceConnection.scopeId");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(DeviceConnectionDomain.DEVICE_CONNECTION, Actions.write, deviceConnection.getScopeId()));
    // 
    // Do update
    DeviceConnection deviceConnectionUpdated = null;
    EntityManager em = DeviceEntityManagerFactory.getEntityManager();
    try {
        if (DeviceConnectionDAO.find(em, deviceConnection.getId()) == null) {
            throw new KapuaEntityNotFoundException(DeviceConnection.TYPE, deviceConnection.getId());
        }
        em.beginTransaction();
        deviceConnectionUpdated = DeviceConnectionDAO.update(em, deviceConnection);
        em.commit();
    } catch (Exception e) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return deviceConnectionUpdated;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) DeviceConnection(org.eclipse.kapua.service.device.registry.connection.DeviceConnection) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaException(org.eclipse.kapua.KapuaException)

Example 5 with KapuaEntityNotFoundException

use of org.eclipse.kapua.KapuaEntityNotFoundException in project kapua by eclipse.

the class CredentialServiceImpl method update.

@Override
public Credential update(Credential credential) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(credential, "credentialCreator");
    ArgumentValidator.notNull(credential.getId(), "credentialCreator.id");
    ArgumentValidator.notNull(credential.getScopeId(), "credentialCreator.scopeId");
    ArgumentValidator.notNull(credential.getUserId(), "credentialCreator.userId");
    ArgumentValidator.notNull(credential.getCredentialType(), "credentialCreator.credentialType");
    ArgumentValidator.notEmptyOrNull(credential.getCredentialKey(), "credentialCreator.credentialKey");
    // 
    // Check access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(CredentialDomain.CREDENTIAL, Actions.write, credential.getScopeId()));
    // 
    // Do update
    Credential credentialUpdated = null;
    EntityManager em = AuthenticationEntityManagerFactory.getEntityManager();
    try {
        Credential currentUser = CredentialDAO.find(em, credential.getId());
        if (currentUser == null) {
            throw new KapuaEntityNotFoundException(Credential.TYPE, credential.getId());
        }
        // Passing attributes??
        em.beginTransaction();
        CredentialDAO.update(em, credential);
        em.commit();
        credentialUpdated = CredentialDAO.find(em, credential.getId());
    } catch (Exception pe) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(pe);
    } finally {
        em.close();
    }
    return credentialUpdated;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Credential(org.eclipse.kapua.service.authentication.credential.Credential) 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) KapuaException(org.eclipse.kapua.KapuaException)

Aggregations

KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)21 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)17 KapuaException (org.eclipse.kapua.KapuaException)13 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)13 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)12 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)12 Account (org.eclipse.kapua.service.account.Account)7 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)6 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)5 Device (org.eclipse.kapua.service.device.registry.Device)5 AccountService (org.eclipse.kapua.service.account.AccountService)4 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)4 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 KapuaUnauthenticatedException (org.eclipse.kapua.KapuaUnauthenticatedException)3 FileItem (org.apache.commons.fileupload.FileItem)2 FileUploadException (org.apache.commons.fileupload.FileUploadException)2 User (org.eclipse.kapua.service.user.User)2 Date (java.util.Date)1 StringTokenizer (java.util.StringTokenizer)1