<commit_before>/*******************************************************************************
 * Copyright (c) 2017, 2018 Eurotech and/or its affiliates and others
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Eurotech - initial API and implementation
 *******************************************************************************/
package org.eclipse.kapua.app.console.module.device.client.device.assets;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.kapua.app.console.module.api.client.messages.ConsoleMessages;
import org.eclipse.kapua.app.console.module.device.client.messages.ConsoleDeviceMessages;
import org.eclipse.kapua.app.console.module.api.client.util.Constants;
import org.eclipse.kapua.app.console.module.api.client.util.FormUtils;
import org.eclipse.kapua.app.console.module.api.client.util.UserAgentUtils;
import org.eclipse.kapua.app.console.module.device.shared.model.device.management.assets.GwtDeviceAsset;
import org.eclipse.kapua.app.console.module.device.shared.model.device.management.assets.GwtDeviceAssetChannel;
import org.eclipse.kapua.app.console.module.device.shared.model.device.management.assets.GwtDeviceAssetChannel.GwtDeviceAssetChannelMode;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.core.El;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ComponentPlugin;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;

public class DeviceAssetsPanel extends LayoutContainer {

    private static final ConsoleMessages MSGS = GWT.create(ConsoleMessages.class);
    private static final ConsoleDeviceMessages DEVICE_MSGS = GWT.create(ConsoleDeviceMessages.class);
    public static final long MAX_SAFE_INTEGER = 4503599627370496L;

    private GwtDeviceAsset asset;
    private FormPanel actionFormPanel;
    private FieldSet actionFieldSet;

    private ComponentPlugin dirtyPlugin;

    public DeviceAssetsPanel(GwtDeviceAsset asset) {
        super(new FitLayout());
        setScrollMode(Scroll.AUTO);
        setBorders(false);

        this.asset = asset;

        final DeviceAssetsPanel thePanel = this;
        dirtyPlugin = new ComponentPlugin() {

            public void init(Component component) {
                component.addListener(Events.Change, new Listener<ComponentEvent>() {

                    public void handleEvent(ComponentEvent be) {
                        El elem = be.getComponent().el().findParent(".x-form-element", 7);
                        if (elem != null) {
                            El dirtyIcon = elem.createChild("");
                            dirtyIcon.setStyleName("x-grid3-dirty-cell");
                            dirtyIcon.setStyleAttribute("top", "0");
                            dirtyIcon.setStyleAttribute("position", "absolute");
                            dirtyIcon.setSize(10, 10);
                            dirtyIcon.show();
                        }
                        thePanel.fireEvent(Events.Change);
                    }
                });
            }
        };

        paintAsset();
    }

    public boolean isValid() {
        return FormUtils.isValid(actionFieldSet);
    }

    public boolean isDirty() {
        return FormUtils.isDirty(actionFieldSet);
    }

    public GwtDeviceAsset getAsset() {
        return asset;
    }

    public GwtDeviceAsset getUpdatedAsset() {

        List<Component> fields = actionFieldSet.getItems();
        List<GwtDeviceAssetChannel> updatedChannelsList = new ArrayList<GwtDeviceAssetChannel>();
        for (int i = 0; i < fields.size(); i++) {
            if (fields.get(i) instanceof Field<?>) {
                Field<?> field = (Field<?>) fields.get(i);
                String fieldName = field.getItemId();
                GwtDeviceAssetChannel channel = asset.getChannel(fieldName);
                if (channel == null) {
                    System.err.println(field);
                }
                String value = getUpdatedChannel(channel, field);
                if (value != null) {
                    channel.setValue(value);
                    updatedChannelsList.add(channel);
                }
            }
        }
        asset.setChannels(updatedChannelsList);
        return asset;
    }

    private String getUpdatedChannel(GwtDeviceAssetChannel channel, Field<?> field) {
        try {
            if (!field.isDirty()) {
                return null;
            }
            switch (channel.getTypeEnum()) {
            case BOOLEAN:
                RadioGroup radioGroup = (RadioGroup) field;
                Radio radio = radioGroup.getValue();
                String booleanValue = radio.getItemId();
                return booleanValue;
            case LONG:
                NumberField longField = (NumberField) field;
                Number longNumber = longField.getValue();
                if (longNumber != null) {
                    return String.valueOf(longNumber.longValue());
                } else {
                    return null;
                }
            case DOUBLE:
                NumberField doubleField = (NumberField) field;
                Number doubleNumber = doubleField.getValue();
                if (doubleNumber != null) {
                    return String.valueOf(doubleNumber.doubleValue());
                } else {<commit_msg>Removed unecessary if check in DeviceAssetsPanel (#1890)<commit_after>/*******************************************************************************
 * Copyright (c) 2017, 2018 Eurotech and/or its affiliates and others
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Eurotech - initial API and implementation
 *******************************************************************************/
package org.eclipse.kapua.app.console.module.device.client.device.assets;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.kapua.app.console.module.api.client.messages.ConsoleMessages;
import org.eclipse.kapua.app.console.module.device.client.messages.ConsoleDeviceMessages;
import org.eclipse.kapua.app.console.module.api.client.util.Constants;
import org.eclipse.kapua.app.console.module.api.client.util.FormUtils;
import org.eclipse.kapua.app.console.module.api.client.util.UserAgentUtils;
import org.eclipse.kapua.app.console.module.device.shared.model.device.management.assets.GwtDeviceAsset;
import org.eclipse.kapua.app.console.module.device.shared.model.device.management.assets.GwtDeviceAssetChannel;
import org.eclipse.kapua.app.console.module.device.shared.model.device.management.assets.GwtDeviceAssetChannel.GwtDeviceAssetChannelMode;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.core.El;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ComponentPlugin;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;

public class DeviceAssetsPanel extends LayoutContainer {

    private static final ConsoleMessages MSGS = GWT.create(ConsoleMessages.class);
    private static final ConsoleDeviceMessages DEVICE_MSGS = GWT.create(ConsoleDeviceMessages.class);
    public static final long MAX_SAFE_INTEGER = 4503599627370496L;

    private GwtDeviceAsset asset;
    private FormPanel actionFormPanel;
    private FieldSet actionFieldSet;

    private ComponentPlugin dirtyPlugin;

    public DeviceAssetsPanel(GwtDeviceAsset asset) {
        super(new FitLayout());
        setScrollMode(Scroll.AUTO);
        setBorders(false);

        this.asset = asset;

        final DeviceAssetsPanel thePanel = this;
        dirtyPlugin = new ComponentPlugin() {

            public void init(Component component) {
                component.addListener(Events.Change, new Listener<ComponentEvent>() {

                    public void handleEvent(ComponentEvent be) {
                        El elem = be.getComponent().el().findParent(".x-form-element", 7);
                        if (elem != null) {
                            El dirtyIcon = elem.createChild("");
                            dirtyIcon.setStyleName("x-grid3-dirty-cell");
                            dirtyIcon.setStyleAttribute("top", "0");
                            dirtyIcon.setStyleAttribute("position", "absolute");
                            dirtyIcon.setSize(10, 10);
                            dirtyIcon.show();
                        }
                        thePanel.fireEvent(Events.Change);
                    }
                });
            }
        };

        paintAsset();
    }

    public boolean isValid() {
        return FormUtils.isValid(actionFieldSet);
    }

    public boolean isDirty() {
        return FormUtils.isDirty(actionFieldSet);
    }

    public GwtDeviceAsset getAsset() {
        return asset;
    }

    public GwtDeviceAsset getUpdatedAsset() {

        List<Component> fields = actionFieldSet.getItems();
        List<GwtDeviceAssetChannel> updatedChannelsList = new ArrayList<GwtDeviceAssetChannel>();
        for (int i = 0; i < fields.size(); i++) {
            if (fields.get(i) instanceof Field<?>) {
                Field<?> field = (Field<?>) fields.get(i);
                String fieldName = field.getItemId();
                GwtDeviceAssetChannel channel = asset.getChannel(fieldName);
                if (channel == null) {
                    System.err.println(field);
                }
                String value = getUpdatedChannel(channel, field);
                if (value != null) {
                    channel.setValue(value);
                    updatedChannelsList.add(channel);
                }
            }
        }
        asset.setChannels(updatedChannelsList);
        return asset;
    }

    private String getUpdatedChannel(GwtDeviceAssetChannel channel, Field<?> field) {
        try {
            switch (channel.getTypeEnum()) {
            case BOOLEAN:
                RadioGroup radioGroup = (RadioGroup) field;
                Radio radio = radioGroup.getValue();
                String booleanValue = radio.getItemId();
                return booleanValue;
            case LONG:
                NumberField longField = (NumberField) field;
                Number longNumber = longField.getValue();
                if (longNumber != null) {
                    return String.valueOf(longNumber.longValue());
                } else {
                    return null;
                }
            case DOUBLE:
                NumberField doubleField = (NumberField) field;
                Number doubleNumber = doubleField.getValue();
                if (doubleNumber != null) {
                    return String.valueOf(doubleNumber.doubleValue());
                } else {
{'fraction_non_alphanumeric': 0.12588862559241706, 'fraction_numerical': 0.005627962085308057, 'mean_word_length': 2.5539473684210527, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '383091', 'n_tokens_mistral': 3793, 'n_tokens_neox': 3635, 'n_words': 658}