<commit_before>﻿angular.module('designer', ['shared', 'repository']);

angular.module('designer').controller('designerController', ['$scope', '$http', 'subscriptionRepository', function ($scope, $http, subscriptionRepository) {
    $scope.activeTab = 'report';

    $scope.init = function() {
        $scope.activeTab = 'report';
        $http.get('api/Designer/GetViewModel').
            success(function (data) {
                $scope.inputTypes = data.InputTypes;
                $scope.reportList = data.Reports;
                $scope.connections = data.Connections;
                $scope.lookupReports = data.LookupReports;
                $scope.accessLists = data.AccessLists;
                $scope.reportOwnerAccessLists = data.ReportOwnerAccessLists;
                $scope.settings = data.Settings;
                $scope.accessEditorViewModel = data.AccessEditorViewModel;
            }).
            error(function (data) {
                toastr.error("Couldn't get list of reports from server.","Error");
            });
    };
    $scope.init();

    //Globals and help functions
    var regExParameterMatch = /(@\w+)/g;
    var parameterPositionHash = [];
    function updateCollection(collection, updatedEntity, deleted) {
        var index = collection.indexOf(_.find(collection, function (entity) { return entity.Id === updatedEntity.Id }));

        if (deleted) {
            collection.splice(index, 1);
        } else {    
            if (index !== -1) {
                collection.splice(index, 1, updatedEntity);
            } else {
                collection.push(updatedEntity);
            }
        }
        $scope.$apply();
    }

    //Reports
    $scope.reportDataChanged = function () {
        createOriginalParameterBackup();

        if (!$scope.report.hasLoadedSubscriptions) {//we dont want this to run on every keyup..
            $scope.report.hasLoadedSubscriptions = true;
            var report = $scope.report;
            report.warnForParameterChanges = true;
            subscriptionRepository.allForReport(report.Id).success(function (data) {
                report.SubscriptionCount = data.length;
                report.warnForParameterChanges = data.length > 0;
            }).error(function () {
                toastr.warning('Could not load subscriptions for the report. Validation will assume there are subscriptions for your safety.');
            });
        }

        $scope.latestSql = $scope.report.Sql;
        parameterPositionHash = [];
        while (match = regExParameterMatch.exec($scope.latestSql)) {
            var existingparam = _.findWhere($scope.report.Parameters, { SqlKey: match[0] });
            if (existingparam !== undefined) {
                parameterPositionHash[match.index] = existingparam.SqlKey;
            }
        }
    };
    $scope.analyzeSQL = function () {
        var currentSQL = $scope.report.Sql;
        var match;
        var currentPosition = $('#sqlarea').prop('selectionStart');
        var foundMatches = [];

        while (match = regExParameterMatch.exec(currentSQL)) {
            var existingparam = _.findWhere($scope.report.Parameters, { SqlKey: match[0] });
            if (existingparam === undefined) {
                if (currentPosition >= match.index && currentPosition <= match.index + match[0].length) {
                    var nameOfExisting = parameterPositionHash[match.index];<commit_msg>expanded a bit on a comment<commit_after>﻿angular.module('designer', ['shared', 'repository']);

angular.module('designer').controller('designerController', ['$scope', '$http', 'subscriptionRepository', function ($scope, $http, subscriptionRepository) {
    $scope.activeTab = 'report';

    $scope.init = function() {
        $scope.activeTab = 'report';
        $http.get('api/Designer/GetViewModel').
            success(function (data) {
                $scope.inputTypes = data.InputTypes;
                $scope.reportList = data.Reports;
                $scope.connections = data.Connections;
                $scope.lookupReports = data.LookupReports;
                $scope.accessLists = data.AccessLists;
                $scope.reportOwnerAccessLists = data.ReportOwnerAccessLists;
                $scope.settings = data.Settings;
                $scope.accessEditorViewModel = data.AccessEditorViewModel;
            }).
            error(function (data) {
                toastr.error("Couldn't get list of reports from server.","Error");
            });
    };
    $scope.init();

    //Globals and help functions
    var regExParameterMatch = /(@\w+)/g;
    var parameterPositionHash = [];
    function updateCollection(collection, updatedEntity, deleted) {
        var index = collection.indexOf(_.find(collection, function (entity) { return entity.Id === updatedEntity.Id }));

        if (deleted) {
            collection.splice(index, 1);
        } else {    
            if (index !== -1) {
                collection.splice(index, 1, updatedEntity);
            } else {
                collection.push(updatedEntity);
            }
        }
        $scope.$apply();
    }

    //Reports
    $scope.reportDataChanged = function () {
        createOriginalParameterBackup();

        if (!$scope.report.hasLoadedSubscriptions) {//we dont want this to run on every keyup.. and yes i know we reload subs on every save, but i think that is correct.
            $scope.report.hasLoadedSubscriptions = true;
            var report = $scope.report;
            report.warnForParameterChanges = true;
            subscriptionRepository.allForReport(report.Id).success(function (data) {
                report.SubscriptionCount = data.length;
                report.warnForParameterChanges = data.length > 0;
            }).error(function () {
                toastr.warning('Could not load subscriptions for the report. Validation will assume there are subscriptions for your safety.');
            });
        }

        $scope.latestSql = $scope.report.Sql;
        parameterPositionHash = [];
        while (match = regExParameterMatch.exec($scope.latestSql)) {
            var existingparam = _.findWhere($scope.report.Parameters, { SqlKey: match[0] });
            if (existingparam !== undefined) {
                parameterPositionHash[match.index] = existingparam.SqlKey;
            }
        }
    };
    $scope.analyzeSQL = function () {
        var currentSQL = $scope.report.Sql;
        var match;
        var currentPosition = $('#sqlarea').prop('selectionStart');
        var foundMatches = [];

        while (match = regExParameterMatch.exec(currentSQL)) {
            var existingparam = _.findWhere($scope.report.Parameters, { SqlKey: match[0] });
            if (existingparam === undefined) {
                if (currentPosition >= match.index && currentPosition <= match.index + match[0].length) {
                    var nameOfExisting = parameterPositionHash[match.index];
{'fraction_non_alphanumeric': 0.12117714945181765, 'fraction_numerical': 0.002019619157530294, 'mean_word_length': 2.211208893006021, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '653236', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1728, 'n_words': 370}