Search in sources :

Example 1 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class AbstractKapuaConfigurableService method setConfigValues.

@Override
public void setConfigValues(KapuaId scopeId, Map<String, Object> values) throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.write, scopeId));
    KapuaTocd ocd = this.getConfigMetadata();
    validateConfigurations(this.pid, ocd, values);
    Properties props = toProperties(values);
    AndPredicate predicate = new AndPredicate().and(new AttributePredicate<String>("pid", this.pid, Operator.EQUAL)).and(new AttributePredicate<KapuaId>("scopeId", scopeId, Operator.EQUAL));
    ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
    query.setPredicate(predicate);
    ServiceConfig serviceConfig = null;
    EntityManager em = this.entityManagerFactory.createEntityManager();
    ServiceConfigListResultImpl result = ServiceConfigDAO.query(em, ServiceConfig.class, ServiceConfigImpl.class, new ServiceConfigListResultImpl(), query);
    // In not exists create then return
    if (result == null || result.getSize() == 0) {
        ServiceConfigImpl serviceConfigNew = new ServiceConfigImpl(scopeId);
        serviceConfigNew.setPid(this.pid);
        serviceConfigNew.setConfigurations(props);
        serviceConfig = this.create(em, serviceConfigNew);
        return;
    }
    // If exists update it
    serviceConfig = result.getItem(0);
    serviceConfig.setConfigurations(props);
    this.update(em, serviceConfig);
    return;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) Properties(java.util.Properties) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd)

Example 2 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class AbstractKapuaConfigurableService method getConfigValues.

@Override
public Map<String, Object> getConfigValues(KapuaId scopeId) throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.read, scopeId));
    AndPredicate predicate = new AndPredicate().and(new AttributePredicate<String>("pid", this.pid, Operator.EQUAL)).and(new AttributePredicate<KapuaId>("scopeId", scopeId, Operator.EQUAL));
    ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
    query.setPredicate(predicate);
    Properties properties = null;
    EntityManager em = this.entityManagerFactory.createEntityManager();
    ServiceConfigListResult result = ServiceConfigDAO.query(em, ServiceConfig.class, ServiceConfigImpl.class, new ServiceConfigListResultImpl(), query);
    if (result != null && result.getSize() > 0)
        properties = result.getItem(0).getConfigurations();
    KapuaTocd ocd = this.getConfigMetadata();
    return toValues(ocd, properties);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) Properties(java.util.Properties) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd)

Example 3 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class AbstractKapuaConfigurableService method getConfigMetadata.

@Override
public KapuaTocd getConfigMetadata() throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    KapuaId scopeId = KapuaSecurityUtils.getSession().getScopeId();
    authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.read, scopeId));
    try {
        TmetadataImpl metadata = readMetadata(this.pid);
        if (metadata.getOCD() != null && metadata.getOCD().size() > 0) {
            for (KapuaTocd ocd : metadata.getOCD()) {
                if (ocd.getId() != null && ocd.getId().equals(pid)) {
                    return ocd;
                }
            }
        }
        return null;
    } catch (Exception e) {
        throw KapuaConfigurationException.internalError(e);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaId(org.eclipse.kapua.model.id.KapuaId) TmetadataImpl(org.eclipse.kapua.commons.configuration.metatype.TmetadataImpl) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd) XMLStreamException(javax.xml.stream.XMLStreamException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) IOException(java.io.IOException) KapuaException(org.eclipse.kapua.KapuaException)

Example 4 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class AccountServiceTest method testCreate.

@Test
public void testCreate() 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 asserts
    assertNotNull(account);
    assertNotNull(account.getId());
    assertNotNull(account.getId().getId());
    assertTrue(account.getOptlock() >= 0);
    assertTrue(account.getScopeId().equals(scopeId));
    assertTrue(account.getName().equals(accountCreator.getOrganizationName()));
    assertNotNull(account.getOrganization());
    assertTrue(account.getOrganization().getEmail().equals(accountCreator.getOrganizationEmail()));
    assertNotNull(account.getCreatedOn());
    assertNotNull(account.getCreatedBy());
    assertNotNull(account.getModifiedOn());
    assertNotNull(account.getModifiedBy());
}
Also used : AccountCreator(org.eclipse.kapua.service.account.AccountCreator) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaEid(org.eclipse.kapua.commons.model.id.KapuaEid) AccountService(org.eclipse.kapua.service.account.AccountService) Date(java.util.Date) Test(org.junit.Test)

Example 5 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class AccountServiceTest method getTestAccountCreator.

private AccountCreator getTestAccountCreator(KapuaId scopeId, long random) {
    KapuaLocator locator = KapuaLocator.getInstance();
    long accountSerial = (new Date()).getTime();
    String testAccount = String.format("test-%d", accountSerial);
    AccountFactory accountFactory = locator.getFactory(AccountFactory.class);
    AccountCreator accountCreator = accountFactory.newAccountCreator(scopeId, testAccount);
    accountCreator.setAccountPassword("!aA1234567890");
    accountCreator.setOrganizationName(testAccount);
    accountCreator.setOrganizationEmail(String.format("theuser@%s.com", testAccount));
    return accountCreator;
}
Also used : AccountCreator(org.eclipse.kapua.service.account.AccountCreator) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) AccountFactory(org.eclipse.kapua.service.account.AccountFactory) Date(java.util.Date)

Aggregations

KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)150 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)61 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)58 KapuaId (org.eclipse.kapua.model.id.KapuaId)54 KapuaException (org.eclipse.kapua.KapuaException)52 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)42 Account (org.eclipse.kapua.service.account.Account)37 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)36 AccountService (org.eclipse.kapua.service.account.AccountService)34 Date (java.util.Date)30 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)23 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)23 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)23 Device (org.eclipse.kapua.service.device.registry.Device)22 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)21 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)17 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)13 GwtAccount (org.eclipse.kapua.app.console.shared.model.GwtAccount)11 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)10