Search in sources :

Example 1 with GwtConfigParameter

use of org.eclipse.kapua.app.console.shared.model.GwtConfigParameter 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 2 with GwtConfigParameter

use of org.eclipse.kapua.app.console.shared.model.GwtConfigParameter in project kapua by eclipse.

the class GwtDeviceManagementServiceImpl method updateComponentConfiguration.

@Override
public void updateComponentConfiguration(GwtXSRFToken xsrfToken, GwtDevice gwtDevice, GwtConfigComponent gwtCompConfig) throws GwtKapuaException {
    // 
    // Checking validity of the given XSRF Token
    checkXSRFToken(xsrfToken);
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceConfigurationManagementService deviceConfigurationManagementService = locator.getService(DeviceConfigurationManagementService.class);
    DeviceConfigurationFactory deviceConfigurationManagementFactory = locator.getFactory(DeviceConfigurationFactory.class);
    // set name and properties
    DeviceComponentConfiguration compConfig = deviceConfigurationManagementFactory.newComponentConfigurationInstance(gwtCompConfig.getUnescapedComponentId());
    compConfig.setName(gwtCompConfig.getUnescapedComponentName());
    Map<String, Object> compProps = new HashMap<String, Object>();
    for (GwtConfigParameter gwtConfigParam : gwtCompConfig.getParameters()) {
        Object objValue;
        int cardinality = gwtConfigParam.getCardinality();
        if (cardinality == 0 || cardinality == 1 || cardinality == -1) {
            String strValue = gwtConfigParam.getValue();
            objValue = getObjectValue(gwtConfigParam, strValue);
        } else {
            String[] strValues = gwtConfigParam.getValues();
            objValue = getObjectValue(gwtConfigParam, strValues);
        }
        compProps.put(gwtConfigParam.getName(), objValue);
    }
    compConfig.setProperties(compProps);
    // execute the update
    try {
        KapuaId scopeId = KapuaEid.parseShortId(gwtDevice.getScopeId());
        KapuaId deviceId = KapuaEid.parseShortId(gwtDevice.getId());
        deviceConfigurationManagementService.put(scopeId, deviceId, compConfig, null);
        // 
        // Add an additional delay after the configuration update
        // to give the time to the device to apply the received
        // configuration
        Thread.sleep(1000);
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) HashMap(java.util.HashMap) GwtConfigParameter(org.eclipse.kapua.app.console.shared.model.GwtConfigParameter) KapuaId(org.eclipse.kapua.model.id.KapuaId) DeviceComponentConfiguration(org.eclipse.kapua.service.device.management.configuration.DeviceComponentConfiguration) DeviceConfigurationFactory(org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationFactory) DeviceConfigurationManagementService(org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService)

Example 3 with GwtConfigParameter

use of org.eclipse.kapua.app.console.shared.model.GwtConfigParameter in project kapua by eclipse.

the class DeviceConfigPanel method paintConfig.

private void paintConfig() {
    LayoutContainer lcAction = new LayoutContainer();
    lcAction.setLayout(new BorderLayout());
    lcAction.setBorders(true);
    lcAction.setSize(475, -1);
    add(lcAction);
    // center panel: action form
    BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER, .75F);
    centerData.setSplit(false);
    centerData.setMargins(new Margins(0, 0, 0, 0));
    FormData formData = new FormData("-20");
    formData.setMargins(new Margins(0, 10, 0, 0));
    if (!UserAgentUtils.isIE()) {
        m_actionFormPanel = new FormPanel();
        m_actionFormPanel.setFrame(false);
        m_actionFormPanel.setBodyBorder(false);
        m_actionFormPanel.setHeaderVisible(false);
        m_actionFormPanel.setLabelWidth(Constants.LABEL_WIDTH_CONFIG_FORM);
        m_actionFormPanel.setStyleAttribute("padding", "0px");
        m_actionFormPanel.setScrollMode(Scroll.AUTO);
        m_actionFormPanel.setLayout(new FlowLayout());
        m_actionFormPanel.addListener(Events.Render, new Listener<BaseEvent>() {

            public void handleEvent(BaseEvent be) {
                NodeList<Element> nl = m_actionFormPanel.getElement().getElementsByTagName("form");
                if (nl.getLength() > 0) {
                    Element elemForm = nl.getItem(0);
                    elemForm.setAttribute("autocomplete", "off");
                }
            }
        });
    }
    m_actionFieldSet = new FieldSet();
    m_actionFieldSet.setId("configuration-form");
    m_actionFieldSet.setBorders(false);
    m_actionFieldSet.setStyleAttribute("padding", "0px");
    m_actionFieldSet.setScrollMode(Scroll.AUTO);
    if (m_configComponent.getComponentDescription() != null && m_configComponent.getComponentDescription().trim().length() > 0) {
        m_actionFieldSet.addText(KapuaSafeHtmlUtils.htmlEscape(m_configComponent.getComponentDescription()));
    }
    FormLayout layout = new FormLayout();
    layout.setLabelWidth(Constants.LABEL_WIDTH_CONFIG_FORM);
    m_actionFieldSet.setLayout(layout);
    Field<?> field = null;
    for (GwtConfigParameter param : m_configComponent.getParameters()) {
        if (param.getCardinality() == 0 || param.getCardinality() == 1 || param.getCardinality() == -1) {
            field = paintConfigParameter(param);
        } else {
            field = paintMultiFieldConfigParameter(param);
        }
        m_actionFieldSet.add(field, formData);
    }
    if (!UserAgentUtils.isIE()) {
        m_actionFormPanel.add(m_actionFieldSet, formData);
        lcAction.add(m_actionFormPanel, centerData);
    } else {
        lcAction.add(m_actionFieldSet, centerData);
    }
}
Also used : FormData(com.extjs.gxt.ui.client.widget.layout.FormData) FormLayout(com.extjs.gxt.ui.client.widget.layout.FormLayout) FlowLayout(com.extjs.gxt.ui.client.widget.layout.FlowLayout) BorderLayoutData(com.extjs.gxt.ui.client.widget.layout.BorderLayoutData) NodeList(com.google.gwt.dom.client.NodeList) Element(com.google.gwt.dom.client.Element) BaseEvent(com.extjs.gxt.ui.client.event.BaseEvent) FieldSet(com.extjs.gxt.ui.client.widget.form.FieldSet) BorderLayout(com.extjs.gxt.ui.client.widget.layout.BorderLayout) FormPanel(com.extjs.gxt.ui.client.widget.form.FormPanel) LayoutContainer(com.extjs.gxt.ui.client.widget.LayoutContainer) GwtConfigParameter(org.eclipse.kapua.app.console.shared.model.GwtConfigParameter) Margins(com.extjs.gxt.ui.client.util.Margins)

Example 4 with GwtConfigParameter

use of org.eclipse.kapua.app.console.shared.model.GwtConfigParameter in project kapua by eclipse.

the class DeviceConfigPanel method getUpdatedConfiguration.

public GwtConfigComponent getUpdatedConfiguration() {
    List<Component> fields = m_actionFieldSet.getItems();
    for (int i = 0; i < fields.size(); i++) {
        if (fields.get(i) instanceof Field<?>) {
            Field<?> field = (Field<?>) fields.get(i);
            String fieldName = field.getItemId();
            GwtConfigParameter param = m_configComponent.getParameter(fieldName);
            if (param == null) {
                System.err.println(field);
            }
            if (!(field instanceof MultiField) || (field instanceof RadioGroup)) {
                // get the updated values for the single field
                String value = getUpdatedFieldConfiguration(param, field);
                param.setValue(value);
            } else {
                // iterate over the subfields and extract each value
                List<String> multiFieldValues = new ArrayList<String>();
                MultiField<?> multiField = (MultiField<?>) field;
                List<Field<?>> childFields = multiField.getAll();
                for (int j = 0; j < childFields.size(); j++) {
                    Field<?> childField = (Field<?>) childFields.get(j);
                    String value = getUpdatedFieldConfiguration(param, childField);
                    if (value != null) {
                        multiFieldValues.add(value);
                    }
                }
                param.setValues(multiFieldValues.toArray(new String[] {}));
            }
        }
    }
    return m_configComponent;
}
Also used : RadioGroup(com.extjs.gxt.ui.client.widget.form.RadioGroup) ArrayList(java.util.ArrayList) MultiField(com.extjs.gxt.ui.client.widget.form.MultiField) MultiField(com.extjs.gxt.ui.client.widget.form.MultiField) TextField(com.extjs.gxt.ui.client.widget.form.TextField) NumberField(com.extjs.gxt.ui.client.widget.form.NumberField) Field(com.extjs.gxt.ui.client.widget.form.Field) GwtConfigParameter(org.eclipse.kapua.app.console.shared.model.GwtConfigParameter) GwtConfigComponent(org.eclipse.kapua.app.console.shared.model.GwtConfigComponent) Component(com.extjs.gxt.ui.client.widget.Component)

Aggregations

GwtConfigParameter (org.eclipse.kapua.app.console.shared.model.GwtConfigParameter)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 GwtConfigComponent (org.eclipse.kapua.app.console.shared.model.GwtConfigComponent)2 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)2 KapuaId (org.eclipse.kapua.model.id.KapuaId)2 DeviceComponentConfiguration (org.eclipse.kapua.service.device.management.configuration.DeviceComponentConfiguration)2 DeviceConfigurationManagementService (org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService)2 BaseEvent (com.extjs.gxt.ui.client.event.BaseEvent)1 Margins (com.extjs.gxt.ui.client.util.Margins)1 Component (com.extjs.gxt.ui.client.widget.Component)1 LayoutContainer (com.extjs.gxt.ui.client.widget.LayoutContainer)1 Field (com.extjs.gxt.ui.client.widget.form.Field)1 FieldSet (com.extjs.gxt.ui.client.widget.form.FieldSet)1 FormPanel (com.extjs.gxt.ui.client.widget.form.FormPanel)1 MultiField (com.extjs.gxt.ui.client.widget.form.MultiField)1 NumberField (com.extjs.gxt.ui.client.widget.form.NumberField)1 RadioGroup (com.extjs.gxt.ui.client.widget.form.RadioGroup)1 TextField (com.extjs.gxt.ui.client.widget.form.TextField)1 BorderLayout (com.extjs.gxt.ui.client.widget.layout.BorderLayout)1