use of org.eclipse.kapua.model.config.metatype.KapuaTad in project kapua by eclipse.
the class AbstractKapuaConfigurableService method toValues.
private static Map<String, Object> toValues(KapuaTocd ocd, Properties props) throws KapuaException {
List<KapuaTad> ads = ocd.getAD();
Map<String, Object> values = new HashMap<String, Object>();
for (KapuaTad ad : ads) {
String valueStr = props == null ? ad.getDefault() : props.getProperty(ad.getId(), ad.getDefault());
Object value = StringUtil.stringToValue(ad.getType().value(), valueStr);
values.put(ad.getId(), value);
}
return values;
}
use of org.eclipse.kapua.model.config.metatype.KapuaTad in project kapua by eclipse.
the class AbstractKapuaConfigurableService method validateConfigurations.
private static void validateConfigurations(String pid, KapuaTocd ocd, Map<String, Object> updatedProps) throws KapuaException {
if (ocd != null) {
// build a map of all the attribute definitions
Map<String, KapuaTad> attrDefs = new HashMap<String, KapuaTad>();
List<KapuaTad> defs = ocd.getAD();
for (KapuaTad def : defs) {
attrDefs.put(def.getId(), def);
}
// and validate them against the definition
for (Entry<String, Object> property : updatedProps.entrySet()) {
String key = property.getKey();
KapuaTad attrDef = attrDefs.get(key);
// is attribute undefined?
if (attrDef == null) {
// just accept them.
continue;
}
// validate the attribute value
Object objectValue = property.getValue();
String stringValue = StringUtil.valueToString(objectValue);
if (stringValue != null) {
ValueTokenizer tokenizer = new ValueTokenizer(stringValue);
String result = tokenizer.validate(attrDef);
if (result != null && !result.isEmpty()) {
throw new KapuaConfigurationException(KapuaConfigurationErrorCodes.ATTRIBUTE_INVALID, attrDef.getId() + ": " + result);
}
}
}
// make sure all required properties are set
if (ocd != null) {
for (KapuaTad attrDef : ocd.getAD()) {
// to the required attributes make sure a value is defined.
if (attrDef.isRequired()) {
if (updatedProps.get(attrDef.getId()) == null) {
// exception.
throw new KapuaConfigurationException(KapuaConfigurationErrorCodes.REQUIRED_ATTRIBUTE_MISSING, attrDef.getId());
}
}
}
}
}
}
use of org.eclipse.kapua.model.config.metatype.KapuaTad in project kapua by eclipse.
the class TranslatorAppConfigurationKapuaKura method translate.
private KapuaTocd translate(KapuaTocd kapuaDefinition) {
TocdImpl definition = new TocdImpl();
definition.setId(kapuaDefinition.getId());
definition.setName(kapuaDefinition.getName());
definition.setDescription(kapuaDefinition.getDescription());
for (KapuaTad kapuaAd : kapuaDefinition.getAD()) {
TadImpl ad = new TadImpl();
ad.setCardinality(kapuaAd.getCardinality());
ad.setDefault(ad.getDefault());
ad.setDescription(kapuaAd.getDescription());
ad.setId(kapuaAd.getId());
ad.setMax(kapuaAd.getMax());
ad.setMin(kapuaAd.getMin());
ad.setName(kapuaAd.getName());
ad.setType(kapuaAd.getType());
ad.setRequired(kapuaAd.isRequired());
for (KapuaToption kuraToption : kapuaAd.getOption()) {
ToptionImpl kapuaToption = new ToptionImpl();
kapuaToption.setLabel(kuraToption.getLabel());
kapuaToption.setValue(kuraToption.getValue());
ad.addOption(kapuaToption);
}
for (Entry<QName, String> entry : kapuaAd.getOtherAttributes().entrySet()) {
ad.putOtherAttribute(entry.getKey(), entry.getValue());
}
definition.addAD(ad);
}
for (KapuaTicon kapuaIcon : kapuaDefinition.getIcon()) {
KapuaTicon icon = new TiconImpl();
icon.setResource(kapuaIcon.getResource());
icon.setSize(kapuaIcon.getSize());
definition.addIcon(icon);
}
for (Object kapuaAny : kapuaDefinition.getAny()) {
definition.addAny(kapuaAny);
}
for (Entry<QName, String> entry : kapuaDefinition.getOtherAttributes().entrySet()) {
definition.putOtherAttribute(entry.getKey(), entry.getValue());
}
return definition;
}
use of org.eclipse.kapua.model.config.metatype.KapuaTad in project kapua by eclipse.
the class TranslatorAppConfigurationKuraKapua method translate.
private KapuaTocd translate(KapuaTocd kuraDefinition) {
TocdImpl definition = new TocdImpl();
definition.setId(kuraDefinition.getId());
definition.setName(kuraDefinition.getName());
definition.setDescription(kuraDefinition.getDescription());
for (KapuaTad kuraAd : kuraDefinition.getAD()) {
TadImpl ad = new TadImpl();
ad.setCardinality(kuraAd.getCardinality());
ad.setDefault(ad.getDefault());
ad.setDescription(kuraAd.getDescription());
ad.setId(kuraAd.getId());
ad.setMax(kuraAd.getMax());
ad.setMin(kuraAd.getMin());
ad.setName(kuraAd.getName());
ad.setType(kuraAd.getType());
ad.setRequired(kuraAd.isRequired());
for (KapuaToption kuraToption : kuraAd.getOption()) {
ToptionImpl kapuaToption = new ToptionImpl();
kapuaToption.setLabel(kuraToption.getLabel());
kapuaToption.setValue(kuraToption.getValue());
ad.addOption(kapuaToption);
}
for (Entry<QName, String> entry : kuraAd.getOtherAttributes().entrySet()) {
ad.putOtherAttribute(entry.getKey(), entry.getValue());
}
definition.addAD(ad);
}
for (KapuaTicon kuraIcon : kuraDefinition.getIcon()) {
KapuaTicon icon = new TiconImpl();
icon.setResource(kuraIcon.getResource());
icon.setSize(kuraIcon.getSize());
definition.addIcon(icon);
}
for (Object kuraAny : kuraDefinition.getAny()) {
definition.addAny(kuraAny);
}
for (Entry<QName, String> entry : kuraDefinition.getOtherAttributes().entrySet()) {
definition.putOtherAttribute(entry.getKey(), entry.getValue());
}
return definition;
}
use of org.eclipse.kapua.model.config.metatype.KapuaTad 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;
}
Aggregations