use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class AccountServiceTest method testUpdate.
@Test
public void testUpdate() throws Exception {
// KapuaPeid accountPeid = KapuaEidGenerator.generate();//
KapuaId scopeId = new KapuaEid(BigInteger.valueOf(1));
KapuaLocator locator = KapuaLocator.getInstance();
long accountSerial = (new Date()).getTime();
AccountCreator accountCreator = this.getTestAccountCreator(scopeId, accountSerial);
AccountService accountService = locator.getService(AccountService.class);
Account account = accountService.create(accountCreator);
account = accountService.find(account.getScopeId(), account.getId());
Organization org = account.getOrganization();
org.setAddressLine1("5th evenue, NY");
account.setOrganization(org);
Account accountUpd = accountService.update(account);
//
// Account asserts
assertNotNull(accountUpd);
assertNotNull(accountUpd.getId());
assertNotNull(accountUpd.getId().getId());
assertTrue(accountUpd.getOptlock() >= 0);
assertTrue(accountUpd.getId().equals(account.getId()));
assertTrue(accountUpd.getScopeId().equals(account.getScopeId()));
assertTrue(accountUpd.getName().equals(account.getName()));
assertNotNull(accountUpd.getOrganization());
assertTrue(accountUpd.getOrganization().getAddressLine1().equals(org.getAddressLine1()));
assertNotNull(accountUpd.getCreatedOn().equals(account.getCreatedOn()));
assertNotNull(accountUpd.getCreatedBy().equals(account.getCreatedBy()));
assertNotNull(account.getModifiedOn());
assertNotNull(account.getModifiedBy());
}
use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class AccountServiceImpl method find.
@Override
public Account find(KapuaId id) throws KapuaException {
//
// Validation of the fields
ArgumentValidator.notNull(id, "id");
ArgumentValidator.notNull(id.getId(), "id.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, id));
// Make sure account exists
return findById(id);
}
use of org.eclipse.kapua.locator.KapuaLocator 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.locator.KapuaLocator in project kapua by eclipse.
the class AccountServiceImpl method query.
@Override
public KapuaListResultImpl<Account> query(KapuaQuery<Account> query) throws KapuaException {
ArgumentValidator.notNull(query, "query");
ArgumentValidator.notNull(query.getScopeId(), "query.scopeId");
//
// 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, query.getScopeId()));
//
// Do count
KapuaListResultImpl<Account> result = null;
EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
try {
result = AccountDAO.query(em, query);
} catch (Exception e) {
throw KapuaExceptionUtils.convertPersistenceException(e);
} finally {
em.close();
}
return result;
}
use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class TranslatorAppCommandKapuaKura method translate.
@Override
public KuraRequestMessage translate(CommandRequestMessage kapuaMessage) throws KapuaException {
//
// Kura channel
KapuaLocator locator = KapuaLocator.getInstance();
AccountService accountService = locator.getService(AccountService.class);
Account account = accountService.find(kapuaMessage.getScopeId());
DeviceRegistryService deviceService = locator.getService(DeviceRegistryService.class);
Device device = deviceService.find(kapuaMessage.getScopeId(), kapuaMessage.getDeviceId());
KuraRequestChannel kuraRequestChannel = translate(kapuaMessage.getChannel());
kuraRequestChannel.setScope(account.getName());
kuraRequestChannel.setClientId(device.getClientId());
//
// Kura payload
KuraRequestPayload kuraPayload = translate(kapuaMessage.getPayload());
// return Kura Message
return new KuraRequestMessage(kuraRequestChannel, kapuaMessage.getReceivedOn(), kuraPayload);
}
Aggregations