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