<commit_before>/* --------------------------------
 Developed by <NAME>
 Applied Information Sciences
 7/8/2014
 ---------------------------------*/

angular.module('opApp.query')
    .service('opWebFeatureService',
    function ($q, $http, opConfig, opStateService, $log) {
        'use strict';

        // Using WFS 1.1.0 for the very specific reason, that 2.0.0 always runs full table scan to get feature counts.
        // With the size of the data we are dealing with, this is not acceptable.
        // We are also being forced to use XML as JSON as of GeoServer 2.4 always includes the feature count.

        // moved these variables to multiple server variant (each function gets the server its working with)
        //this.WFS_VERSION = opConfig.server.wfsVersion;
        //this.URL = opConfig.server.url + '/wfs';
        //this.AJAX_URL = opConfig.server.ajaxUrl + '/wfs';

        /**
         * Determine all fields and their associated types for a given layer name and workspace.
         *
         * @param name
         * @param workspace
         * @returns {Promise} array of JSON objects containing name and type of fields
         */
        this.extractFieldsAndTypes = function (serverName, name, workspace) {
            var deferred = $q.defer();
            var error;

            this.describeFeatureType(serverName, name, workspace).then(
                function (result) {
                    if (result !== null) {
                        var xmlDoc = $.parseXML(result.data);

                        var nodes = xmlDoc.getElementsByTagNameNS('*', 'complexType');

                        // Verify the complexType node exists and contains an attribute
                        if (nodes && nodes.length > 0 && nodes.item(0).hasAttributes()) {
                            var typeNode = nodes.item(0);
                            // Verify that response is referencing typeName we are interested in
                            if (typeNode.attributes['name'].textContent.replace('Type', '') === name) {
                                var fields = [];

                                var elementNodes = typeNode.childNodes[1].childNodes[1].childNodes[1].childNodes;

                                for (var i = 0; i < elementNodes.length; i++) {
                                    var node = elementNodes.item(i);
                                    if (node.nodeType === 1 && node.nodeName === 'xsd:element') {
                                        var field = {};
                                        field.name = node.attributes['name'].textContent;
                                        field.type = node.attributes['type'].textContent;
                                        fields.push(field);
                                    }
                                }

                                deferred.resolve(fields);
                            }
                        }
                        else {
                            error = 'Unable to parse DescribeFeatureType response: ' + result.data;
                            $log.log(error);
                            deferred.reject(error);
                        }
                    }
                    else {
                        error = 'Null response received from DescribeFeatureType';
                        $log.log(error);
                        deferred.reject(error);
                    }
                },
                function (reason) {
                    error = 'Failure in DescribeFeatureType request for start/stop field names: ' + reason;
                    $log.log(error);
                    deferred.reject(error);
                });

            return deferred.promise;
        };

        /**
         * Retrieve the DescribeFeatureType response for specified name and workspace
         *
          * @param name
         * @param workspace
         * @returns {*}
         */
        this.describeFeatureType = function (serverName, name, workspace) {
            var server = opStateService.getServer(serverName);
            var wfsVersion = server.wfsVersion;
            var ajaxUrl = server.ajaxUrl + '/wfs';

            var deferred = $q.defer();

            var typeName = workspace + ':' + name;
            var params = { version: wfsVersion, request: 'DescribeFeatureType', typeName: typeName };

            $http.get(ajaxUrl, {params: params }).then(function (result) {
                $log.log('Successfully retrieved DescribeFeatureType result.');
                deferred.resolve(result);
            }, function (reason) {
                // error
                var error ='Error retrieving DescribeFeatureType result: ' + reason;
                $log.log(error);
                deferred.reject(error);
            });

            return deferred.promise;
        };

        /**
         * Retrieve the GetFeature response for the specified name and workspace.  Basic request can be augmented with
         * any desired parameters set in params
         * @param name
         * @param workspace
         * @param params JSON object of KVP to include in GetFeature request
         * @returns {*}
         */
        this.getFeature = function (serverName, name, workspace, params) {
            var deferred = $q.defer();

            var server = opStateService.getServer(serverName);
            var wfsVersion = server.wfsVersion;
            var ajaxUrl = server.ajaxUrl + '/wfs';

            var typeName = workspace + ':' + name;
            var serviceParams = angular.extend({ version: wfsVersion, request: 'GetFeature', typeName: typeName }, params);

            $http.get(ajaxUrl, {params: serviceParams, cache:true }).then(
                function (result) {
                    $log.log('Successfully retrieved GetFeature result.');
                    deferred.resolve(result);
                },
                function (reason) {
                    // error
                    $log.log('Error retrieving GetFeature result');
                    deferred.reject(reason);
                });

            return deferred.promise;
        };

        /**
         * Attempt to retrieve features as GML and convert into GeoJSON resolved into promise
         *
         * @param serverName server name
         * @param name layer name
         * @param workspace layer workspace
         * @param fields layer fields, must be passed so xml can be converted into 'Feature' properties
         * @param params JSON KVP of WFS parameters to be applied to the request
         * @returns {Deferred.promise|*}
         */
        this.getFeaturesAsJson = function(serverName, name, workspace, fields, params) {
            var deferred = $q.defer();
            var wfsOutputFormat = server.wfsOutputFormat;
<commit_msg>Fixed bug where I accidentally removed a line we need.<commit_after>/* --------------------------------
 Developed by <NAME>
 Applied Information Sciences
 7/8/2014
 ---------------------------------*/

angular.module('opApp.query')
    .service('opWebFeatureService',
    function ($q, $http, opConfig, opStateService, $log) {
        'use strict';

        // Using WFS 1.1.0 for the very specific reason, that 2.0.0 always runs full table scan to get feature counts.
        // With the size of the data we are dealing with, this is not acceptable.
        // We are also being forced to use XML as JSON as of GeoServer 2.4 always includes the feature count.

        // moved these variables to multiple server variant (each function gets the server its working with)
        //this.WFS_VERSION = opConfig.server.wfsVersion;
        //this.URL = opConfig.server.url + '/wfs';
        //this.AJAX_URL = opConfig.server.ajaxUrl + '/wfs';

        /**
         * Determine all fields and their associated types for a given layer name and workspace.
         *
         * @param name
         * @param workspace
         * @returns {Promise} array of JSON objects containing name and type of fields
         */
        this.extractFieldsAndTypes = function (serverName, name, workspace) {
            var deferred = $q.defer();
            var error;

            this.describeFeatureType(serverName, name, workspace).then(
                function (result) {
                    if (result !== null) {
                        var xmlDoc = $.parseXML(result.data);

                        var nodes = xmlDoc.getElementsByTagNameNS('*', 'complexType');

                        // Verify the complexType node exists and contains an attribute
                        if (nodes && nodes.length > 0 && nodes.item(0).hasAttributes()) {
                            var typeNode = nodes.item(0);
                            // Verify that response is referencing typeName we are interested in
                            if (typeNode.attributes['name'].textContent.replace('Type', '') === name) {
                                var fields = [];

                                var elementNodes = typeNode.childNodes[1].childNodes[1].childNodes[1].childNodes;

                                for (var i = 0; i < elementNodes.length; i++) {
                                    var node = elementNodes.item(i);
                                    if (node.nodeType === 1 && node.nodeName === 'xsd:element') {
                                        var field = {};
                                        field.name = node.attributes['name'].textContent;
                                        field.type = node.attributes['type'].textContent;
                                        fields.push(field);
                                    }
                                }

                                deferred.resolve(fields);
                            }
                        }
                        else {
                            error = 'Unable to parse DescribeFeatureType response: ' + result.data;
                            $log.log(error);
                            deferred.reject(error);
                        }
                    }
                    else {
                        error = 'Null response received from DescribeFeatureType';
                        $log.log(error);
                        deferred.reject(error);
                    }
                },
                function (reason) {
                    error = 'Failure in DescribeFeatureType request for start/stop field names: ' + reason;
                    $log.log(error);
                    deferred.reject(error);
                });

            return deferred.promise;
        };

        /**
         * Retrieve the DescribeFeatureType response for specified name and workspace
         *
          * @param name
         * @param workspace
         * @returns {*}
         */
        this.describeFeatureType = function (serverName, name, workspace) {
            var server = opStateService.getServer(serverName);
            var wfsVersion = server.wfsVersion;
            var ajaxUrl = server.ajaxUrl + '/wfs';

            var deferred = $q.defer();

            var typeName = workspace + ':' + name;
            var params = { version: wfsVersion, request: 'DescribeFeatureType', typeName: typeName };

            $http.get(ajaxUrl, {params: params }).then(function (result) {
                $log.log('Successfully retrieved DescribeFeatureType result.');
                deferred.resolve(result);
            }, function (reason) {
                // error
                var error ='Error retrieving DescribeFeatureType result: ' + reason;
                $log.log(error);
                deferred.reject(error);
            });

            return deferred.promise;
        };

        /**
         * Retrieve the GetFeature response for the specified name and workspace.  Basic request can be augmented with
         * any desired parameters set in params
         * @param name
         * @param workspace
         * @param params JSON object of KVP to include in GetFeature request
         * @returns {*}
         */
        this.getFeature = function (serverName, name, workspace, params) {
            var deferred = $q.defer();

            var server = opStateService.getServer(serverName);
            var wfsVersion = server.wfsVersion;
            var ajaxUrl = server.ajaxUrl + '/wfs';

            var typeName = workspace + ':' + name;
            var serviceParams = angular.extend({ version: wfsVersion, request: 'GetFeature', typeName: typeName }, params);

            $http.get(ajaxUrl, {params: serviceParams, cache:true }).then(
                function (result) {
                    $log.log('Successfully retrieved GetFeature result.');
                    deferred.resolve(result);
                },
                function (reason) {
                    // error
                    $log.log('Error retrieving GetFeature result');
                    deferred.reject(reason);
                });

            return deferred.promise;
        };

        /**
         * Attempt to retrieve features as GML and convert into GeoJSON resolved into promise
         *
         * @param serverName server name
         * @param name layer name
         * @param workspace layer workspace
         * @param fields layer fields, must be passed so xml can be converted into 'Feature' properties
         * @param params JSON KVP of WFS parameters to be applied to the request
         * @returns {Deferred.promise|*}
         */
        this.getFeaturesAsJson = function(serverName, name, workspace, fields, params) {
            var deferred = $q.defer();
            var server = opStateService.getServer(serverName);
            var wfsOutputFormat = server.wfsOutputFormat;

{'fraction_non_alphanumeric': 0.09978181818181818, 'fraction_numerical': 0.0032, 'mean_word_length': 1.3889854065323142, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6802061', 'n_tokens_mistral': 3209, 'n_tokens_neox': 3032, 'n_words': 950}