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