Search in sources :

Example 1 with GwtConfigComponent

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

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

the class DeviceConfigComponents method apply.

public void apply() {
    if (!m_devConfPanel.isValid()) {
        MessageBox mb = new MessageBox();
        mb.setIcon(MessageBox.ERROR);
        mb.setMessage(MSGS.deviceConfigError());
        mb.show();
        return;
    }
    // ask for confirmation
    String componentName = m_devConfPanel.getConfiguration().getComponentName();
    String message = MSGS.deviceConfigConfirmation(componentName);
    final boolean isCloudUpdate = "CloudService".equals(componentName);
    if (isCloudUpdate) {
        message = MSGS.deviceCloudConfigConfirmation(componentName);
    }
    MessageBox.confirm(MSGS.confirm(), message, new Listener<MessageBoxEvent>() {

        public void handleEvent(MessageBoxEvent ce) {
            // if confirmed, push the update
            // if confirmed, delete
            Dialog dialog = ce.getDialog();
            if (dialog.yesText.equals(ce.getButtonClicked().getText())) {
                // mark the whole config panel dirty and for reload
                m_tabConfig.setDevice(m_selectedDevice);
                m_devConfPanel.mask(MSGS.applying());
                m_tree.mask();
                m_apply.setEnabled(false);
                m_reset.setEnabled(false);
                m_refreshButton.setEnabled(false);
                // 
                // Getting XSRF token
                gwtXSRFService.generateSecurityToken(new AsyncCallback<GwtXSRFToken>() {

                    @Override
                    public void onFailure(Throwable ex) {
                        FailureHandler.handle(ex);
                    }

                    @Override
                    public void onSuccess(GwtXSRFToken token) {
                        final GwtConfigComponent configComponent = m_devConfPanel.getUpdatedConfiguration();
                        gwtDeviceManagementService.updateComponentConfiguration(token, m_selectedDevice, configComponent, new AsyncCallback<Void>() {

                            public void onFailure(Throwable caught) {
                                FailureHandler.handle(caught);
                                m_dirty = true;
                            }

                            public void onSuccess(Void arg0) {
                                m_dirty = true;
                                if (isCloudUpdate) {
                                    refreshWhenOnline();
                                } else {
                                    refresh();
                                }
                            }
                        });
                    }
                });
            // start the configuration update
            }
        }
    });
}
Also used : MessageBoxEvent(com.extjs.gxt.ui.client.event.MessageBoxEvent) Dialog(com.extjs.gxt.ui.client.widget.Dialog) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) GwtConfigComponent(org.eclipse.kapua.app.console.shared.model.GwtConfigComponent) GwtXSRFToken(org.eclipse.kapua.app.console.shared.model.GwtXSRFToken) MessageBox(com.extjs.gxt.ui.client.widget.MessageBox)

Example 3 with GwtConfigComponent

use of org.eclipse.kapua.app.console.shared.model.GwtConfigComponent 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)

Example 4 with GwtConfigComponent

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

the class DeviceConfigComponents method initConfigPanel.

@SuppressWarnings("unchecked")
private void initConfigPanel() {
    m_configPanel = new ContentPanel();
    m_configPanel.setBorders(false);
    m_configPanel.setBodyBorder(false);
    m_configPanel.setHeaderVisible(false);
    m_configPanel.setStyleAttribute("background-color", "white");
    m_configPanel.setScrollMode(Scroll.AUTO);
    BorderLayout borderLayout = new BorderLayout();
    m_configPanel.setLayout(borderLayout);
    // center
    m_centerData = new BorderLayoutData(LayoutRegion.CENTER);
    m_centerData.setMargins(new Margins(0));
    // west
    BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 200);
    westData.setSplit(true);
    westData.setCollapsible(true);
    westData.setMargins(new Margins(0, 5, 0, 0));
    // loader and store
    RpcProxy<List<GwtConfigComponent>> proxy = new RpcProxy<List<GwtConfigComponent>>() {

        @Override
        protected void load(Object loadConfig, AsyncCallback<List<GwtConfigComponent>> callback) {
            if (m_selectedDevice != null && m_dirty && m_initialized) {
                if (m_selectedDevice.isOnline()) {
                    m_tree.mask(MSGS.loading());
                    gwtDeviceManagementService.findDeviceConfigurations(m_selectedDevice, callback);
                } else {
                    List<GwtConfigComponent> comps = new ArrayList<GwtConfigComponent>();
                    GwtConfigComponent comp = new GwtConfigComponent();
                    comp.setId(MSGS.deviceNoDeviceSelected());
                    comp.setName(MSGS.deviceNoComponents());
                    comp.setDescription(MSGS.deviceNoConfigSupported());
                    comps.add(comp);
                    callback.onSuccess(comps);
                }
            } else {
                List<GwtConfigComponent> comps = new ArrayList<GwtConfigComponent>();
                GwtConfigComponent comp = new GwtConfigComponent();
                comp.setId(MSGS.deviceNoDeviceSelected());
                comp.setName(MSGS.deviceNoDeviceSelected());
                comp.setDescription(MSGS.deviceNoDeviceSelected());
                comps.add(comp);
                callback.onSuccess(comps);
            }
            m_dirty = false;
        }
    };
    m_loader = new BaseTreeLoader<GwtConfigComponent>(proxy);
    m_loader.addLoadListener(new DataLoadListener());
    m_treeStore = new TreeStore<ModelData>(m_loader);
    m_tree = new TreePanel<ModelData>(m_treeStore);
    m_tree.setWidth(200);
    m_tree.setDisplayProperty("componentName");
    m_tree.setBorders(true);
    m_tree.setAutoSelect(true);
    m_tree.setStyleAttribute("background-color", "white");
    m_configPanel.add(m_tree, westData);
    // 
    // Selection Listener for the component
    // make sure the form is not dirty before switching.
    m_tree.getSelectionModel().addListener(Events.BeforeSelect, new Listener<BaseEvent>() {

        @SuppressWarnings("rawtypes")
        @Override
        public void handleEvent(BaseEvent be) {
            final BaseEvent theEvent = be;
            SelectionEvent<ModelData> se = (SelectionEvent<ModelData>) be;
            final GwtConfigComponent componentToSwitchTo = (GwtConfigComponent) se.getModel();
            if (m_devConfPanel != null && m_devConfPanel.isDirty()) {
                // cancel the event first
                theEvent.setCancelled(true);
                // need to reselect the current entry
                // as the BeforeSelect event cleared it
                // we need to do this without raising events
                TreePanelSelectionModel selectionModel = (TreePanelSelectionModel) m_tree.getSelectionModel();
                selectionModel.setFiresEvents(false);
                selectionModel.select(false, m_devConfPanel.getConfiguration());
                selectionModel.setFiresEvents(true);
                // ask for confirmation before switching
                MessageBox.confirm(MSGS.confirm(), MSGS.deviceConfigDirty(), new Listener<MessageBoxEvent>() {

                    public void handleEvent(MessageBoxEvent ce) {
                        // if confirmed, delete
                        Dialog dialog = ce.getDialog();
                        if (dialog.yesText.equals(ce.getButtonClicked().getText())) {
                            m_devConfPanel.removeFromParent();
                            m_devConfPanel = null;
                            m_tree.getSelectionModel().select(false, componentToSwitchTo);
                        }
                    }
                });
            } else {
                refreshConfigPanel(componentToSwitchTo);
                // this is needed to select the item in the Tree
                // Temporarly disable the firing of the selection events
                // to avoid an infinite loop as BeforeSelect would be invoked again.
                TreePanelSelectionModel selectionModel = (TreePanelSelectionModel) m_tree.getSelectionModel();
                selectionModel.setFiresEvents(false);
                selectionModel.select(false, componentToSwitchTo);
                // renable firing of the events
                selectionModel.setFiresEvents(true);
            }
        }
    });
    m_tree.setIconProvider(new ModelIconProvider<ModelData>() {

        public AbstractImagePrototype getIcon(ModelData model) {
            if (model instanceof GwtConfigComponent) {
                String icon = ((GwtConfigComponent) model).getComponentIcon();
                if ("CloudService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.cloud16());
                } else if ("ClockService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.clock16());
                } else if ("DataService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.databaseConnect());
                } else if ("DiagnosticsService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.diagnostics());
                } else if ("MqttDataTransport".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.mqtt());
                } else if ("PositionService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.gps16());
                } else if ("SslManagerService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.lock16());
                } else if ("WatchdogService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.dog16());
                } else if ("CommandPasswordService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.terminal());
                } else if ("VpnService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.vpn());
                } else if ("ProvisioningService".equals(icon)) {
                    return AbstractImagePrototype.create(Resources.INSTANCE.provisioning16());
                } else if ("DenaliService".equals(icon)) {
                    // WebConsole: DenaliService
                    return AbstractImagePrototype.create(Resources.INSTANCE.monitorDenali());
                } else if ("BluetoothService".equals(icon)) {
                    // Bluetooth: BluetoothService
                    return AbstractImagePrototype.create(Resources.INSTANCE.bluetooth());
                } else if (icon != null && icon.toLowerCase().startsWith("img://")) {
                    // Replace the base fake protocol with the console base URL
                    // that was not available on the server side.
                    icon = icon.replaceFirst("img://", GWT.getHostPageBaseURL());
                    return new ScaledAbstractImagePrototype(IconHelper.createPath(icon, 16, 16));
                } else {
                    return AbstractImagePrototype.create(Resources.INSTANCE.plugin16());
                }
            }
            return null;
        }
    });
}
Also used : ScaledAbstractImagePrototype(org.eclipse.kapua.app.console.client.util.ScaledAbstractImagePrototype) AbstractImagePrototype(com.google.gwt.user.client.ui.AbstractImagePrototype) ModelData(com.extjs.gxt.ui.client.data.ModelData) Listener(com.extjs.gxt.ui.client.event.Listener) KapuaLoadListener(org.eclipse.kapua.app.console.client.util.KapuaLoadListener) SelectionListener(com.extjs.gxt.ui.client.event.SelectionListener) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) ArrayList(java.util.ArrayList) RpcProxy(com.extjs.gxt.ui.client.data.RpcProxy) MessageBoxEvent(com.extjs.gxt.ui.client.event.MessageBoxEvent) BorderLayout(com.extjs.gxt.ui.client.widget.layout.BorderLayout) Dialog(com.extjs.gxt.ui.client.widget.Dialog) SelectionEvent(com.extjs.gxt.ui.client.event.SelectionEvent) Margins(com.extjs.gxt.ui.client.util.Margins) List(java.util.List) ArrayList(java.util.ArrayList) GwtConfigComponent(org.eclipse.kapua.app.console.shared.model.GwtConfigComponent) ScaledAbstractImagePrototype(org.eclipse.kapua.app.console.client.util.ScaledAbstractImagePrototype) BorderLayoutData(com.extjs.gxt.ui.client.widget.layout.BorderLayoutData) BaseEvent(com.extjs.gxt.ui.client.event.BaseEvent) TreePanelSelectionModel(com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel) ContentPanel(com.extjs.gxt.ui.client.widget.ContentPanel)

Aggregations

GwtConfigComponent (org.eclipse.kapua.app.console.shared.model.GwtConfigComponent)4 ArrayList (java.util.ArrayList)3 MessageBoxEvent (com.extjs.gxt.ui.client.event.MessageBoxEvent)2 Dialog (com.extjs.gxt.ui.client.widget.Dialog)2 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)2 GwtConfigParameter (org.eclipse.kapua.app.console.shared.model.GwtConfigParameter)2 ModelData (com.extjs.gxt.ui.client.data.ModelData)1 RpcProxy (com.extjs.gxt.ui.client.data.RpcProxy)1 BaseEvent (com.extjs.gxt.ui.client.event.BaseEvent)1 Listener (com.extjs.gxt.ui.client.event.Listener)1 SelectionEvent (com.extjs.gxt.ui.client.event.SelectionEvent)1 SelectionListener (com.extjs.gxt.ui.client.event.SelectionListener)1 Margins (com.extjs.gxt.ui.client.util.Margins)1 Component (com.extjs.gxt.ui.client.widget.Component)1 ContentPanel (com.extjs.gxt.ui.client.widget.ContentPanel)1 MessageBox (com.extjs.gxt.ui.client.widget.MessageBox)1 Field (com.extjs.gxt.ui.client.widget.form.Field)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