<commit_before>/**
 * @class Oskari.mapframework.bundle.mapwfs2.service.Mediator
 *
 * Handles Connection's IO
 */
Oskari.clazz.define('Oskari.mapframework.bundle.mapwfs2.service.Mediator',
    /**
     * @method create called automatically on construction
     * @static
     *
     * @param {Object} config
     * @param {Object} plugin
     */

    function (config, plugin) {
        this.config = config;
        this.plugin = plugin;
        this.connection = this.plugin.getConnection();
        this.cometd = this.connection.get();
        this.layerProperties = {};

        this.rootURL = location.protocol + "//" +
            this.config.hostname + this.config.port +
            this.config.contextPath;

        this.session = {
            session: jQuery.cookie('JSESSIONID') || "",
            route: jQuery.cookie('ROUTEID') || ""
        };

        this._previousTimer = null;
        this._featureUpdateFrequence = 200;
    }, {

        /**
         * @method getSessionID
         */
        getSessionID: function () {
            return this.session.session;
        },

        /**
         * @method getRootURL
         */
        getRootURL: function () {
            return this.rootURL;
        },

        /**
         * @method subscribe
         *
         * Subscribes client channels
         */
        subscribe: function () {
            var self = this;

            var channels = {
                '/wfs/properties': function () {
                    self.getWFSProperties.apply(self, arguments);
                },
                '/wfs/feature': function () {
                    self.getWFSFeature.apply(self, arguments);
                },
                '/wfs/featureGeometries': function () {
                    self.getWFSFeatureGeometries.apply(self, arguments);
                },
                '/wfs/mapClick': function () {
                    self.getWFSMapClick.apply(self, arguments);
                },
                '/wfs/filter': function () {
                    self.getWFSFilter.apply(self, arguments);
                },
                '/wfs/propertyfilter': function () {
                    self.getWFSFilter.apply(self, arguments);
                },
                '/wfs/image': function () {
                    self.getWFSImage.apply(self, arguments);
                },
                '/wfs/reset': function () {
                    self.resetWFS.apply(self, arguments);
                }
            };


            for (var c in channels) {
                this.cometd.subscribe(c, channels[c]);
            }
        },

        /**
         * @method startup
         * @param {Object} session
         *
         * Sends init information to the backend
         */
        startup: function (session) {
            var self = this;
            if (session) { // use objects session if not defined as parameter
                this.session = session;
            }

            // update session and route
            this.session.session = jQuery.cookie('JSESSIONID') || "";
            this.session.route = jQuery.cookie('ROUTEID') || "";

            var layers = this.plugin.getSandbox().findAllSelectedMapLayers(), // get array of AbstractLayer (WFS|WMS..)
                initLayers = {},
                i;
            for (i = 0; i < layers.length; ++i) {
                if (layers[i].hasFeatureData()) {
                    initLayers[layers[i].getId() + ""] = {
                        styleName: layers[i].getCurrentStyle().getName()
                    };
                }
            }

            var srs = this.plugin.getSandbox().getMap().getSrsName(),
                bbox = this.plugin.getSandbox().getMap().getExtent(),
                zoom = this.plugin.getSandbox().getMap().getZoom(),
                mapScales = this.plugin.mapModule.getMapScales(),
                grid = this.plugin.getGrid();
            if (grid === null || grid === undefined) {
                grid = {};
            }
            var tileSize = this.plugin.getTileSize();
            if (tileSize === null || tileSize === undefined) {
                tileSize = {};
            }

            this.cometd.publish('/service/wfs/init', {
                "session": this.session.session,
                "route": this.session.route,
                "language": Oskari.getLang(),
                "browser": this.session.browser,
                "browserVersion": this.session.browserVersion,
                "location": {
                    "srs": srs,
                    "bbox": [bbox.left, bbox.bottom, bbox.right, bbox.top],
                    "zoom": zoom
                },
                "grid": grid,
                "tileSize": tileSize,
                "mapSize": {
                    "width": self.plugin.getSandbox().getMap().getWidth(),
                    "height": self.plugin.getSandbox().getMap().getHeight()
                },
                "mapScales": mapScales,
                "layers": initLayers
            });
        }
    });

// receive from backend

Oskari.clazz.category('Oskari.mapframework.bundle.mapwfs2.service.Mediator', 'getters', {
    /**
     * @method getWFSProperties
     * @param {Object} data
     *
     * Creates WFSPropertiesEvent
     */
    getWFSProperties: function (data) {
        var layer = this.plugin.getSandbox().findMapLayerFromSelectedMapLayers(data.data.layerId);
        if (layer.getLayerType() != 'analysis') {
            var oldFields = layer.getFields();
            var oldLocales = layer.getLocales();
            if (oldFields.length > 0 && !this.plugin.isArrayEqual(data.data.fields, oldFields) && !this.plugin.isArrayEqual(data.data.locales, oldLocales)) {
                this.plugin.mapMoveHandler();
            }

            layer.setFields(data.data.fields);
            layer.setLocales(data.data.locales);

        }

        var self = this;
        if (this._propertyTimer) {
            clearTimeout(this._propertyTimer);
            this._propertyTimer = null;
        }
        this._propertyTimer = setTimeout(function () {
            var event = self.plugin.getSandbox().getEventBuilder("WFSPropertiesEvent")(layer);
            self.plugin.getSandbox().notifyAll(event);
        }, this._featureUpdateFrequence);
    },

    /**
     * @method getWFSFeature
     * @param {Object} data
     *
     * Creates WFSFeatureEvent
     */
    getWFSFeature: function (data) {
        var layer = this.plugin.getSandbox().findMapLayerFromSelectedMapLayers(data.data.layerId);
        if (data.data.feature != "empty" && data.data.feature != "max") {
            layer.setActiveFeature(data.data.feature);
        }

        var self = this;
        if (this._featureTimer) {
            clearTimeout(this._featureTimer);
            this._featureTimer = null;
        }
        this._featureTimer = setTimeout(function () {
            var event = self.plugin.getSandbox().getEventBuilder("WFSFeatureEvent")(
                layer,
                data.data.feature
            );
            self.plugin.getSandbox().notifyAll(event);
        }, this._featureUpdateFrequence);
    },

    /**
     * @method getWFSMapClick
     * @param {Object} data
     *<commit_msg>[AH-1966] Testing version of the feature data table.<commit_after>/**
 * @class Oskari.mapframework.bundle.mapwfs2.service.Mediator
 *
 * Handles Connection's IO
 */
Oskari.clazz.define('Oskari.mapframework.bundle.mapwfs2.service.Mediator',
    /**
     * @method create called automatically on construction
     * @static
     *
     * @param {Object} config
     * @param {Object} plugin
     */

    function (config, plugin) {
        this.config = config;
        this.plugin = plugin;
        this.connection = this.plugin.getConnection();
        this.cometd = this.connection.get();
        this.layerProperties = {};

        this.rootURL = location.protocol + "//" +
            this.config.hostname + this.config.port +
            this.config.contextPath;

        this.session = {
            session: jQuery.cookie('JSESSIONID') || "",
            route: jQuery.cookie('ROUTEID') || ""
        };

        this._previousTimer = null;
        this._featureUpdateFrequence = 200;
    }, {

        /**
         * @method getSessionID
         */
        getSessionID: function () {
            return this.session.session;
        },

        /**
         * @method getRootURL
         */
        getRootURL: function () {
            return this.rootURL;
        },

        /**
         * @method subscribe
         *
         * Subscribes client channels
         */
        subscribe: function () {
            var self = this;

            var channels = {
                '/wfs/properties': function () {
                    self.getWFSProperties.apply(self, arguments);
                },
                '/wfs/feature': function () {
                    self.getWFSFeature.apply(self, arguments);
                },
                '/wfs/featureGeometries': function () {
                    self.getWFSFeatureGeometries.apply(self, arguments);
                },
                '/wfs/mapClick': function () {
                    self.getWFSMapClick.apply(self, arguments);
                },
                '/wfs/filter': function () {
                    self.getWFSFilter.apply(self, arguments);
                },
                '/wfs/propertyfilter': function () {
                    self.getWFSFilter.apply(self, arguments);
                },
                '/wfs/image': function () {
                    self.getWFSImage.apply(self, arguments);
                },
                '/wfs/reset': function () {
                    self.resetWFS.apply(self, arguments);
                }
            };


            for (var c in channels) {
                this.cometd.subscribe(c, channels[c]);
            }
        },

        /**
         * @method startup
         * @param {Object} session
         *
         * Sends init information to the backend
         */
        startup: function (session) {
            var self = this;
            if (session) { // use objects session if not defined as parameter
                this.session = session;
            }

            // update session and route
            this.session.session = jQuery.cookie('JSESSIONID') || "";
            this.session.route = jQuery.cookie('ROUTEID') || "";

            var layers = this.plugin.getSandbox().findAllSelectedMapLayers(), // get array of AbstractLayer (WFS|WMS..)
                initLayers = {},
                i;
            for (i = 0; i < layers.length; ++i) {
                if (layers[i].hasFeatureData()) {
                    initLayers[layers[i].getId() + ""] = {
                        styleName: layers[i].getCurrentStyle().getName()
                    };
                }
            }

            var srs = this.plugin.getSandbox().getMap().getSrsName(),
                bbox = this.plugin.getSandbox().getMap().getExtent(),
                zoom = this.plugin.getSandbox().getMap().getZoom(),
                mapScales = this.plugin.mapModule.getMapScales(),
                grid = this.plugin.getGrid();
            if (grid === null || grid === undefined) {
                grid = {};
            }
            var tileSize = this.plugin.getTileSize();
            if (tileSize === null || tileSize === undefined) {
                tileSize = {};
            }

            this.cometd.publish('/service/wfs/init', {
                "session": this.session.session,
                "route": this.session.route,
                "language": Oskari.getLang(),
                "browser": this.session.browser,
                "browserVersion": this.session.browserVersion,
                "location": {
                    "srs": srs,
                    "bbox": [bbox.left, bbox.bottom, bbox.right, bbox.top],
                    "zoom": zoom
                },
                "grid": grid,
                "tileSize": tileSize,
                "mapSize": {
                    "width": self.plugin.getSandbox().getMap().getWidth(),
                    "height": self.plugin.getSandbox().getMap().getHeight()
                },
                "mapScales": mapScales,
                "layers": initLayers
            });
        }
    });

// receive from backend

Oskari.clazz.category('Oskari.mapframework.bundle.mapwfs2.service.Mediator', 'getters', {
    /**
     * @method getWFSProperties
     * @param {Object} data
     *
     * Creates WFSPropertiesEvent
     */
    getWFSProperties: function (data) {
        var layer = this.plugin.getSandbox().findMapLayerFromSelectedMapLayers(data.data.layerId);
        if (layer.getLayerType() != 'analysis') {
            var oldFields = layer.getFields();
            var oldLocales = layer.getLocales();
            if (oldFields.length > 0 && !this.plugin.isArrayEqual(data.data.fields, oldFields) && !this.plugin.isArrayEqual(data.data.locales, oldLocales)) {
                this.plugin.mapMoveHandler();
            }

            layer.setFields(data.data.fields);
            layer.setLocales(data.data.locales);

        }

        var self = this;
        if (this._propertyTimer) {
            clearTimeout(this._propertyTimer);
            this._propertyTimer = null;
        }
        this._propertyTimer = setTimeout(function () {
            var event = self.plugin.getSandbox().getEventBuilder("WFSPropertiesEvent")(layer);
            self.plugin.getSandbox().notifyAll(event);
        }, this._featureUpdateFrequence);
    },

    /**
     * @method getWFSFeature
     * @param {Object} data
     *
     * Creates WFSFeatureEvent
     */
    getWFSFeature: function (data) {
        var layer = this.plugin.getSandbox().findMapLayerFromSelectedMapLayers(data.data.layerId);
console.log(":::");
console.log(data);
        if (data.data.feature != "empty" && data.data.feature != "max") {
            layer.setActiveFeature(data.data.feature);
        }

        var self = this;
        if (this._featureTimer) {
            clearTimeout(this._featureTimer);
            this._featureTimer = null;
        }
        this._featureTimer = setTimeout(function () {
            var event = self.plugin.getSandbox().getEventBuilder("WFSFeatureEvent")(
                layer,
                data.data.feature
            );
            self.plugin.getSandbox().notifyAll(event);
        }, this._featureUpdateFrequence);
    },

    /**
     * @method getWFSMapClick
     * @param {Object} data
     *
{'fraction_non_alphanumeric': 0.12747388675096208, 'fraction_numerical': 0.0013743815283122594, 'mean_word_length': 1.5074086836664369, 'pattern_counts': {'":': 33, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7054128', 'n_tokens_mistral': 4005, 'n_tokens_neox': 3799, 'n_words': 649}