Search in sources :

Example 1 with KapuaTocd

use of org.eclipse.kapua.model.config.metatype.KapuaTocd 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 KapuaTocd

use of org.eclipse.kapua.model.config.metatype.KapuaTocd 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 KapuaTocd

use of org.eclipse.kapua.model.config.metatype.KapuaTocd 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 KapuaTocd

use of org.eclipse.kapua.model.config.metatype.KapuaTocd in project kapua by eclipse.

the class GwtDeviceManagementServiceImpl method findDeviceConfigurations.

// 
// Configurations
// 
@Override
public List<GwtConfigComponent> findDeviceConfigurations(GwtDevice device) throws GwtKapuaException {
    List<GwtConfigComponent> gwtConfigs = new ArrayList<GwtConfigComponent>();
    try {
        // get the configuration
        KapuaLocator locator = KapuaLocator.getInstance();
        DeviceConfigurationManagementService deviceConfiguratiomManagementService = locator.getService(DeviceConfigurationManagementService.class);
        KapuaId scopeId = KapuaEid.parseShortId(device.getScopeId());
        KapuaId deviceId = KapuaEid.parseShortId(device.getId());
        DeviceConfiguration deviceConfigurations = deviceConfiguratiomManagementService.get(scopeId, deviceId, null, null, null);
        if (deviceConfigurations != null) {
            // sort the list alphabetically by service name
            List<DeviceComponentConfiguration> configs = deviceConfigurations.getComponentConfigurations();
            Collections.sort(configs, new Comparator<DeviceComponentConfiguration>() {

                @Override
                public int compare(DeviceComponentConfiguration arg0, DeviceComponentConfiguration arg1) {
                    String name0 = arg0.getId();
                    String name1 = arg1.getId();
                    if (name0.contains(".")) {
                        name0 = name0.substring(name0.lastIndexOf('.'));
                    }
                    if (name1.contains(".")) {
                        name1 = name1.substring(name1.lastIndexOf('.'));
                    }
                    return name0.compareTo(name1);
                }
            });
            // prepare results
            ConsoleSetting consoleConfig = ConsoleSetting.getInstance();
            List<String> serviceIgnore = consoleConfig.getList(String.class, ConsoleSettingKeys.DEVICE_CONFIGURATION_SERVICE_IGNORE);
            for (DeviceComponentConfiguration config : deviceConfigurations.getComponentConfigurations()) {
                // ignore items we want to hide
                if (serviceIgnore != null && serviceIgnore.contains(config.getId())) {
                    continue;
                }
                KapuaTocd ocd = config.getDefinition();
                if (ocd != null) {
                    GwtConfigComponent gwtConfig = new GwtConfigComponent();
                    gwtConfig.setId(config.getId());
                    gwtConfig.setName(ocd.getName());
                    gwtConfig.setDescription(ocd.getDescription());
                    if (ocd.getIcon() != null && ocd.getIcon().size() > 0) {
                        KapuaTicon icon = ocd.getIcon().get(0);
                        checkIconResource(icon);
                        gwtConfig.setComponentIcon(icon.getResource());
                    }
                    List<GwtConfigParameter> gwtParams = new ArrayList<GwtConfigParameter>();
                    gwtConfig.setParameters(gwtParams);
                    for (KapuaTad ad : ocd.getAD()) {
                        if (ad != null) {
                            GwtConfigParameter gwtParam = new GwtConfigParameter();
                            gwtParam.setId(ad.getId());
                            gwtParam.setName(ad.getName());
                            gwtParam.setDescription(ad.getDescription());
                            gwtParam.setType(GwtConfigParameterType.fromString(ad.getType().value()));
                            gwtParam.setRequired(ad.isRequired());
                            gwtParam.setCardinality(ad.getCardinality());
                            if (ad.getOption() != null && ad.getOption().size() > 0) {
                                Map<String, String> options = new HashMap<String, String>();
                                for (KapuaToption option : ad.getOption()) {
                                    options.put(option.getLabel(), option.getValue());
                                }
                                gwtParam.setOptions(options);
                            }
                            gwtParam.setMin(ad.getMin());
                            gwtParam.setMax(ad.getMax());
                            if (config.getProperties() != null) {
                                // handle the value based on the cardinality of the attribute
                                int cardinality = ad.getCardinality();
                                Object value = config.getProperties().get(ad.getId());
                                if (value != null) {
                                    if (cardinality == 0 || cardinality == 1 || cardinality == -1) {
                                        gwtParam.setValue(value.toString());
                                    } else {
                                        // this could be an array value
                                        if (value instanceof Object[]) {
                                            Object[] objValues = (Object[]) value;
                                            List<String> strValues = new ArrayList<String>();
                                            for (Object v : objValues) {
                                                if (v != null) {
                                                    strValues.add(v.toString());
                                                }
                                            }
                                            gwtParam.setValues(strValues.toArray(new String[] {}));
                                        }
                                    }
                                }
                                gwtParams.add(gwtParam);
                            }
                        }
                    }
                    gwtConfigs.add(gwtConfig);
                }
            }
        }
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtConfigs;
}
Also used : KapuaTad(org.eclipse.kapua.model.config.metatype.KapuaTad) KapuaToption(org.eclipse.kapua.model.config.metatype.KapuaToption) KapuaTicon(org.eclipse.kapua.model.config.metatype.KapuaTicon) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConsoleSetting(org.eclipse.kapua.app.console.setting.ConsoleSetting) KapuaId(org.eclipse.kapua.model.id.KapuaId) GwtConfigComponent(org.eclipse.kapua.app.console.shared.model.GwtConfigComponent) DeviceComponentConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceComponentConfiguration) DeviceConfigurationManagementService(org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtConfigParameter(org.eclipse.kapua.app.console.shared.model.GwtConfigParameter) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd) DeviceConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration)

Example 5 with KapuaTocd

use of org.eclipse.kapua.model.config.metatype.KapuaTocd in project kapua by eclipse.

the class AccountServiceTest method testConfiguration.

@Test
public void testConfiguration() throws Exception {
    // KapuaPeid accountPeid = KapuaEidGenerator.generate();//
    KapuaId scopeId = new KapuaEid(BigInteger.valueOf(1));
    KapuaLocator locator = KapuaLocator.getInstance();
    AccountService accountService = locator.getService(AccountService.class);
    KapuaTocd ocd = accountService.getConfigMetadata();
    Map<String, Object> values = accountService.getConfigValues(scopeId);
    accountService.setConfigValues(scopeId, values);
    assertTrue(null == null);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaEid(org.eclipse.kapua.commons.model.id.KapuaEid) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd) AccountService(org.eclipse.kapua.service.account.AccountService) Test(org.junit.Test)

Aggregations

KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)5 KapuaTocd (org.eclipse.kapua.model.config.metatype.KapuaTocd)5 KapuaId (org.eclipse.kapua.model.id.KapuaId)5 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)3 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)3 Properties (java.util.Properties)2 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)2 AndPredicate (org.eclipse.kapua.commons.model.query.predicate.AndPredicate)2 AttributePredicate (org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)1 KapuaException (org.eclipse.kapua.KapuaException)1 ConsoleSetting (org.eclipse.kapua.app.console.setting.ConsoleSetting)1 GwtConfigComponent (org.eclipse.kapua.app.console.shared.model.GwtConfigComponent)1 GwtConfigParameter (org.eclipse.kapua.app.console.shared.model.GwtConfigParameter)1 TmetadataImpl (org.eclipse.kapua.commons.configuration.metatype.TmetadataImpl)1 KapuaEid (org.eclipse.kapua.commons.model.id.KapuaEid)1