Search in sources :

Example 1 with GwtDeviceCreator

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

the class GwtDeviceServiceImpl method createDevice.

public GwtDevice createDevice(GwtXSRFToken xsrfToken, GwtDeviceCreator gwtDeviceCreator) throws GwtKapuaException {
    // 
    // Checking validity of the given XSRF Token
    checkXSRFToken(xsrfToken);
    KapuaLocator locator = KapuaLocator.getInstance();
    DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);
    DeviceFactory deviceFactory = locator.getFactory(DeviceFactory.class);
    GwtDevice gwtDevice = null;
    try {
        KapuaId scopeId = KapuaEid.parseShortId(gwtDeviceCreator.getScopeId());
        DeviceCreator deviceCreator = deviceFactory.newCreator(scopeId, gwtDeviceCreator.getClientId());
        deviceCreator.setDisplayName(gwtDeviceCreator.getDisplayName());
        deviceCreator.setCredentialsMode(DeviceCredentialsMode.valueOf(gwtDeviceCreator.getGwtCredentialsTight().name()));
        deviceCreator.setCustomAttribute1(gwtDeviceCreator.getCustomAttribute1());
        deviceCreator.setCustomAttribute2(gwtDeviceCreator.getCustomAttribute2());
        deviceCreator.setCustomAttribute3(gwtDeviceCreator.getCustomAttribute3());
        deviceCreator.setCustomAttribute4(gwtDeviceCreator.getCustomAttribute4());
        deviceCreator.setCustomAttribute5(gwtDeviceCreator.getCustomAttribute5());
        Device device = deviceRegistryService.create(deviceCreator);
        gwtDevice = KapuaGwtConverter.convert(device);
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtDevice;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) GwtDevice(org.eclipse.kapua.app.console.shared.model.GwtDevice) DeviceCreator(org.eclipse.kapua.service.device.registry.DeviceCreator) GwtDeviceCreator(org.eclipse.kapua.app.console.shared.model.GwtDeviceCreator) Device(org.eclipse.kapua.service.device.registry.Device) GwtDevice(org.eclipse.kapua.app.console.shared.model.GwtDevice) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) DeviceFactory(org.eclipse.kapua.service.device.registry.DeviceFactory) KapuaId(org.eclipse.kapua.model.id.KapuaId)

Example 2 with GwtDeviceCreator

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

the class DeviceForm method onRender.

protected void onRender(Element parent, int index) {
    super.onRender(parent, index);
    FormData formData = new FormData("-20");
    m_formPanel = new FormPanel();
    m_formPanel.setFrame(false);
    m_formPanel.setBodyBorder(false);
    m_formPanel.setHeaderVisible(false);
    m_formPanel.setWidth(310);
    m_formPanel.setScrollMode(Scroll.AUTOY);
    m_formPanel.setStyleAttribute("padding-bottom", "0px");
    m_formPanel.setLayout(new FlowLayout());
    // Device general info fieldset
    FieldSet fieldSet = new FieldSet();
    FormLayout layout = new FormLayout();
    layout.setLabelWidth(Constants.LABEL_WIDTH_DEVICE_FORM);
    fieldSet.setLayout(layout);
    fieldSet.setHeading(MSGS.deviceFormFieldsetGeneralInfo());
    // Device Client ID
    clientIdLabel = new LabelField();
    clientIdLabel.setFieldLabel(MSGS.deviceFormClientID());
    clientIdLabel.setLabelSeparator(":");
    clientIdLabel.setWidth(225);
    fieldSet.add(clientIdLabel, formData);
    clientIdField = new TextField<String>();
    clientIdField.setAllowBlank(false);
    clientIdField.setName("clientID");
    clientIdField.setFieldLabel(MSGS.deviceFormClientID());
    clientIdField.setValidator(new TextFieldValidator(clientIdField, FieldType.DEVICE_CLIENT_ID));
    clientIdField.setWidth(225);
    fieldSet.add(clientIdField, formData);
    // Display name
    displayNameField = new TextField<String>();
    displayNameField.setAllowBlank(true);
    displayNameField.setName("displayName");
    displayNameField.setFieldLabel(MSGS.deviceFormDisplayName());
    displayNameField.setWidth(225);
    fieldSet.add(displayNameField, formData);
    // Device Status
    statusCombo = new SimpleComboBox<GwtDeviceQueryPredicates.GwtDeviceStatus>();
    statusCombo.setName("status");
    statusCombo.setFieldLabel(MSGS.deviceFormStatus());
    statusCombo.setEditable(false);
    statusCombo.setTriggerAction(TriggerAction.ALL);
    statusCombo.setEmptyText(MSGS.deviceFilteringPanelStatusEmptyText());
    statusCombo.add(GwtDeviceQueryPredicates.GwtDeviceStatus.ENABLED);
    statusCombo.add(GwtDeviceQueryPredicates.GwtDeviceStatus.DISABLED);
    fieldSet.add(statusCombo, formData);
    // Tag fieldset
    FieldSet fieldSetTags = new FieldSet();
    FormLayout layoutTags = new FormLayout();
    layoutTags.setLabelWidth(Constants.LABEL_WIDTH_DEVICE_FORM);
    fieldSetTags.setLayout(layoutTags);
    fieldSetTags.setHeading(MSGS.deviceFormFieldsetTags());
    ContentPanel panel = new ContentPanel();
    panel.setBorders(false);
    panel.setBodyBorder(false);
    panel.setHeaderVisible(false);
    panel.setLayout(new RowLayout(Orientation.HORIZONTAL));
    panel.setBodyStyle("background-color:transparent");
    panel.setHeight(35);
    // Device Custom attributes fieldset
    FormLayout layoutSecurityOptions = new FormLayout();
    layoutSecurityOptions.setLabelWidth(Constants.LABEL_WIDTH_DEVICE_FORM);
    FieldSet fieldSetSecurityOptions = new FieldSet();
    fieldSetSecurityOptions.setLayout(layoutSecurityOptions);
    fieldSetSecurityOptions.setHeading(MSGS.deviceFormFieldsetSecurityOptions());
    // Provisioned Credentials Tight
    credentialsTightCombo = new SimpleComboBox<String>();
    credentialsTightCombo.setName("provisionedCredentialsTight");
    credentialsTightCombo.setEditable(false);
    credentialsTightCombo.setTypeAhead(false);
    credentialsTightCombo.setAllowBlank(false);
    credentialsTightCombo.setFieldLabel(MSGS.deviceFormProvisionedCredentialsTight());
    credentialsTightCombo.setToolTip(MSGS.deviceFormProvisionedCredentialsTightTooltip());
    credentialsTightCombo.setTriggerAction(TriggerAction.ALL);
    fieldSetSecurityOptions.add(credentialsTightCombo, formData);
    credentialsTightCombo.add(GwtDeviceCredentialsTight.INHERITED.getLabel());
    credentialsTightCombo.add(GwtDeviceCredentialsTight.LOOSE.getLabel());
    credentialsTightCombo.add(GwtDeviceCredentialsTight.STRICT.getLabel());
    credentialsTightCombo.setSimpleValue(GwtDeviceCredentialsTight.INHERITED.getLabel());
    // Device User
    RpcProxy<ListLoadResult<GwtUser>> deviceUserProxy = new RpcProxy<ListLoadResult<GwtUser>>() {

        @Override
        protected void load(Object loadConfig, AsyncCallback<ListLoadResult<GwtUser>> callback) {
            gwtUserService.findAll(m_currentSession.getSelectedAccount().getId(), callback);
        }
    };
    BaseListLoader<ListLoadResult<GwtUser>> deviceUserLoader = new BaseListLoader<ListLoadResult<GwtUser>>(deviceUserProxy);
    ListStore<GwtUser> deviceUserStore = new ListStore<GwtUser>(deviceUserLoader);
    deviceUserCombo = new ComboBox<GwtUser>();
    deviceUserCombo.setName("deviceUserCombo");
    deviceUserCombo.setEditable(false);
    deviceUserCombo.setTypeAhead(false);
    deviceUserCombo.setAllowBlank(false);
    deviceUserCombo.setFieldLabel(MSGS.deviceFormDeviceUser());
    deviceUserCombo.setTriggerAction(TriggerAction.ALL);
    deviceUserCombo.setStore(deviceUserStore);
    deviceUserCombo.setDisplayField("username");
    deviceUserCombo.setValueField("id");
    fieldSetSecurityOptions.add(deviceUserCombo, formData);
    // Allow credential change
    allowCredentialsChangeCheckbox = new CheckBox();
    allowCredentialsChangeCheckbox.setName("allowNewUnprovisionedDevicesCheckbox");
    allowCredentialsChangeCheckbox.setFieldLabel(MSGS.deviceFormAllowCredentialsChange());
    allowCredentialsChangeCheckbox.setToolTip(MSGS.deviceFormAllowCredentialsChangeTooltip());
    allowCredentialsChangeCheckbox.setBoxLabel("");
    fieldSetSecurityOptions.add(allowCredentialsChangeCheckbox, formData);
    // Device Custom attributes fieldset
    FieldSet fieldSetCustomAttributes = new FieldSet();
    FormLayout layoutCustomAttributes = new FormLayout();
    layoutCustomAttributes.setLabelWidth(Constants.LABEL_WIDTH_DEVICE_FORM);
    fieldSetCustomAttributes.setLayout(layoutCustomAttributes);
    fieldSetCustomAttributes.setHeading(MSGS.deviceFormFieldsetCustomAttributes());
    // Custom Attribute #1
    customAttribute1Field = new TextField<String>();
    customAttribute1Field.setName("customAttribute1");
    customAttribute1Field.setFieldLabel("* " + MSGS.deviceFormCustomAttribute1());
    customAttribute1Field.setWidth(225);
    fieldSetCustomAttributes.add(customAttribute1Field, formData);
    // Custom Attribute #2
    customAttribute2Field = new TextField<String>();
    customAttribute2Field.setName("customAttribute2");
    customAttribute2Field.setFieldLabel("* " + MSGS.deviceFormCustomAttribute2());
    customAttribute2Field.setWidth(225);
    fieldSetCustomAttributes.add(customAttribute2Field, formData);
    // Custom Attribute #3
    customAttribute3Field = new TextField<String>();
    customAttribute3Field.setName("customAttribute3");
    customAttribute3Field.setFieldLabel("* " + MSGS.deviceFormCustomAttribute3());
    customAttribute3Field.setWidth(225);
    fieldSetCustomAttributes.add(customAttribute3Field, formData);
    // Custom Attribute #4
    customAttribute4Field = new TextField<String>();
    customAttribute4Field.setName("customAttribute4");
    customAttribute4Field.setFieldLabel("* " + MSGS.deviceFormCustomAttribute4());
    customAttribute4Field.setWidth(225);
    fieldSetCustomAttributes.add(customAttribute4Field, formData);
    // Custom Attribute #5
    customAttribute5Field = new TextField<String>();
    customAttribute5Field.setName("customAttribute5");
    customAttribute5Field.setFieldLabel("* " + MSGS.deviceFormCustomAttribute5());
    customAttribute5Field.setWidth(225);
    fieldSetCustomAttributes.add(customAttribute5Field, formData);
    // Optlock
    optlock = new NumberField();
    optlock.setName("optlock");
    optlock.setEditable(false);
    optlock.setVisible(false);
    fieldSet.add(optlock, formData);
    m_formPanel.add(fieldSet);
    m_formPanel.add(fieldSetTags);
    m_formPanel.add(fieldSetSecurityOptions);
    m_formPanel.add(fieldSetCustomAttributes);
    m_formPanel.setButtonAlign(HorizontalAlignment.CENTER);
    Button submitButton = new Button(MSGS.deviceFormSubmitButton());
    submitButton.addListener(Events.OnClick, new Listener<BaseEvent>() {

        @Override
        public void handleEvent(BaseEvent be) {
            // make sure all visible fields are valid before performing the action
            for (Field<?> field : m_formPanel.getFields()) {
                if (field.isVisible() && !field.isValid()) {
                    MessageBox.alert(MSGS.error(), MSGS.formErrors(), null);
                    return;
                }
            }
            if (m_selectedDevice == null) {
                final GwtDeviceCreator gwtDeviceCreator = new GwtDeviceCreator();
                gwtDeviceCreator.setScopeId(m_currentSession.getSelectedAccount().getId());
                gwtDeviceCreator.setClientId(clientIdField.getValue());
                gwtDeviceCreator.setDisplayName(displayNameField.getValue());
                // Security Options
                gwtDeviceCreator.setGwtCredentialsTight(credentialsTightCombo.getSimpleValue());
                gwtDeviceCreator.setGwtPreferredUserId(deviceUserCombo.getValue().getId());
                // Custom attributes
                gwtDeviceCreator.setCustomAttribute1(unescapeValue(customAttribute1Field.getValue()));
                gwtDeviceCreator.setCustomAttribute2(unescapeValue(customAttribute2Field.getValue()));
                gwtDeviceCreator.setCustomAttribute3(unescapeValue(customAttribute3Field.getValue()));
                gwtDeviceCreator.setCustomAttribute4(unescapeValue(customAttribute4Field.getValue()));
                gwtDeviceCreator.setCustomAttribute5(unescapeValue(customAttribute5Field.getValue()));
                // 
                // Getting XSRF token
                gwtXSRFService.generateSecurityToken(new AsyncCallback<GwtXSRFToken>() {

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

                    @Override
                    public void onSuccess(GwtXSRFToken token) {
                        gwtDeviceService.createDevice(token, gwtDeviceCreator, new AsyncCallback<GwtDevice>() {

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

                            public void onSuccess(final GwtDevice gwtDevice) {
                                // 
                                // Getting XSRF token
                                gwtXSRFService.generateSecurityToken(new AsyncCallback<GwtXSRFToken>() {

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

                                    @Override
                                    public void onSuccess(GwtXSRFToken token) {
                                        hide();
                                        ConsoleInfo.display(MSGS.info(), MSGS.deviceUpdateSuccess());
                                    }
                                });
                            }
                        });
                    }
                });
            } else // Edit
            {
                // General info
                m_selectedDevice.setDisplayName(unescapeValue(displayNameField.getValue()));
                m_selectedDevice.setGwtDeviceStatus(statusCombo.getSimpleValue().name());
                // Security Options
                m_selectedDevice.setCredentialsTight(GwtDeviceCredentialsTight.getEnumFromLabel(credentialsTightCombo.getSimpleValue()).name());
                m_selectedDevice.setCredentialsAllowChange(allowCredentialsChangeCheckbox.getValue());
                m_selectedDevice.setDeviceUserId(deviceUserCombo.getValue().getId());
                // Custom attributes
                m_selectedDevice.setCustomAttribute1(unescapeValue(customAttribute1Field.getValue()));
                m_selectedDevice.setCustomAttribute2(unescapeValue(customAttribute2Field.getValue()));
                m_selectedDevice.setCustomAttribute3(unescapeValue(customAttribute3Field.getValue()));
                m_selectedDevice.setCustomAttribute4(unescapeValue(customAttribute4Field.getValue()));
                m_selectedDevice.setCustomAttribute5(unescapeValue(customAttribute5Field.getValue()));
                // Optlock
                m_selectedDevice.setOptlock(optlock.getValue().intValue());
                // 
                // Getting XSRF token
                gwtXSRFService.generateSecurityToken(new AsyncCallback<GwtXSRFToken>() {

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

                    @Override
                    public void onSuccess(GwtXSRFToken token) {
                        gwtDeviceService.updateAttributes(token, m_selectedDevice, new AsyncCallback<GwtDevice>() {

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

                            public void onSuccess(GwtDevice gwtDevice) {
                                // 
                                // Getting XSRF token
                                gwtXSRFService.generateSecurityToken(new AsyncCallback<GwtXSRFToken>() {

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

                                    @Override
                                    public void onSuccess(GwtXSRFToken token) {
                                        hide();
                                        ConsoleInfo.display(MSGS.info(), m_selectedDevice == null ? MSGS.deviceCreationSuccess() : MSGS.deviceUpdateSuccess());
                                    }
                                });
                            }
                        });
                    }
                });
            }
        }
    });
    Button cancelButton = new Button(MSGS.deviceFormCancelButton());
    cancelButton.addListener(Events.OnClick, new Listener<BaseEvent>() {

        @Override
        public void handleEvent(BaseEvent be) {
            hide();
        }
    });
    m_formPanel.addButton(submitButton);
    m_formPanel.addButton(cancelButton);
    add(m_formPanel);
    // Hide components according to NEW/EDIT mode
    makeNewEditAppearance();
    // Populate fields if we are in EDIT mode
    if (m_selectedDevice != null) {
        populateFields();
    }
}
Also used : FlowLayout(com.extjs.gxt.ui.client.widget.layout.FlowLayout) GwtDeviceCreator(org.eclipse.kapua.app.console.shared.model.GwtDeviceCreator) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) BaseListLoader(com.extjs.gxt.ui.client.data.BaseListLoader) GwtXSRFToken(org.eclipse.kapua.app.console.shared.model.GwtXSRFToken) RpcProxy(com.extjs.gxt.ui.client.data.RpcProxy) ListStore(com.extjs.gxt.ui.client.store.ListStore) Field(com.extjs.gxt.ui.client.widget.form.Field) TextField(com.extjs.gxt.ui.client.widget.form.TextField) LabelField(com.extjs.gxt.ui.client.widget.form.LabelField) NumberField(com.extjs.gxt.ui.client.widget.form.NumberField) FieldSet(com.extjs.gxt.ui.client.widget.form.FieldSet) GwtDevice(org.eclipse.kapua.app.console.shared.model.GwtDevice) Button(com.extjs.gxt.ui.client.widget.button.Button) RowLayout(com.extjs.gxt.ui.client.widget.layout.RowLayout) LabelField(com.extjs.gxt.ui.client.widget.form.LabelField) FormData(com.extjs.gxt.ui.client.widget.layout.FormData) FormLayout(com.extjs.gxt.ui.client.widget.layout.FormLayout) BaseEvent(com.extjs.gxt.ui.client.event.BaseEvent) NumberField(com.extjs.gxt.ui.client.widget.form.NumberField) ContentPanel(com.extjs.gxt.ui.client.widget.ContentPanel) ListLoadResult(com.extjs.gxt.ui.client.data.ListLoadResult) FormPanel(com.extjs.gxt.ui.client.widget.form.FormPanel) CheckBox(com.extjs.gxt.ui.client.widget.form.CheckBox) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) TextFieldValidator(org.eclipse.kapua.app.console.client.util.TextFieldValidator)

Aggregations

GwtDevice (org.eclipse.kapua.app.console.shared.model.GwtDevice)2 GwtDeviceCreator (org.eclipse.kapua.app.console.shared.model.GwtDeviceCreator)2 BaseListLoader (com.extjs.gxt.ui.client.data.BaseListLoader)1 ListLoadResult (com.extjs.gxt.ui.client.data.ListLoadResult)1 RpcProxy (com.extjs.gxt.ui.client.data.RpcProxy)1 BaseEvent (com.extjs.gxt.ui.client.event.BaseEvent)1 ListStore (com.extjs.gxt.ui.client.store.ListStore)1 ContentPanel (com.extjs.gxt.ui.client.widget.ContentPanel)1 Button (com.extjs.gxt.ui.client.widget.button.Button)1 CheckBox (com.extjs.gxt.ui.client.widget.form.CheckBox)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 LabelField (com.extjs.gxt.ui.client.widget.form.LabelField)1 NumberField (com.extjs.gxt.ui.client.widget.form.NumberField)1 TextField (com.extjs.gxt.ui.client.widget.form.TextField)1 FlowLayout (com.extjs.gxt.ui.client.widget.layout.FlowLayout)1 FormData (com.extjs.gxt.ui.client.widget.layout.FormData)1 FormLayout (com.extjs.gxt.ui.client.widget.layout.FormLayout)1 RowLayout (com.extjs.gxt.ui.client.widget.layout.RowLayout)1