<commit_before>'use strict';

var resourcesApp = soajsApp.components;
resourcesApp.controller('resourcesAppCtrl', ['$scope', '$http', '$timeout', '$modal', 'ngDataApi', '$cookies', 'injectFiles', 'resourceConfiguration', function ($scope, $http, $timeout, $modal, ngDataApi, $cookies, injectFiles, resourceConfiguration) {
    $scope.$parent.isUserLoggedIn();
    $scope.access = {};
    constructModulePermissions($scope, $scope.access, resourcesAppConfig.permissions);

    $scope.listResources = function(cb) {
	    $scope.oldStyle = false;
	    getEnvironment(function(){

	    	overlayLoading.show();
		    getSendDataFromServer($scope, ngDataApi, {
			    method: 'get',
			    routeName: '/dashboard/resources/list',
			    params: {
				    env: $scope.envCode
			    }
		    }, function(error, response) {
			    overlayLoading.hide();
			    if(error) {
				    $scope.displayAlert('danger', error.message);
			    }
			    else {
                    $scope.resources = { list: response };
                    $scope.resources.original = angular.copy($scope.resources.list); //keep a copy of the original resources records

                    if($scope.deployConfig && $scope.deployedServices) {
                        markDeployed();
                    }

                    groupByType();

                    if(cb) return cb();
			    }
		    });

	    });

        function groupByType() {
            $scope.resources.types = {};
            $scope.resources.list.forEach(function(oneResource) {
                if(!$scope.resources.types[oneResource.type]) {
                    $scope.resources.types[oneResource.type] = {};
                }
                if(!$scope.resources.types[oneResource.type][oneResource.category]) {
                    $scope.resources.types[oneResource.type][oneResource.category] = [];
                }

                if(oneResource.created === $scope.envCode.toUpperCase()) {
                    oneResource.allowEdit = true;
                }

                $scope.resources.types[oneResource.type][oneResource.category].push(oneResource);
            });
        }

        function getEnvironment(cb){
		    getSendDataFromServer($scope, ngDataApi, {
			    "method": "get",
			    "routeName": "/dashboard/environment",
			    "params":{
				    "code": $scope.envCode
			    }
		    }, function (error, response) {
			    if (error) {
				    $scope.$parent.displayAlert('danger', error.code, true, 'dashboard', error.message);
			    }
			    else {
				    if(response.dbs.clusters && Object.keys(response.dbs.clusters).length > 0){
				        $scope.oldStyle = true;
				    }
				    return cb();
			    }
		    });
	    }

        function markDeployed() {
            $scope.resources.list.forEach(function(oneResource) {
                if($scope.deployConfig && $scope.deployConfig[$scope.envCode.toUpperCase()]) {
                    if($scope.deployConfig[$scope.envCode.toUpperCase()][oneResource.name]) {
                        var resourceConfig = $scope.deployConfig[$scope.envCode.toUpperCase()][oneResource.name];

                        if (!resourceConfig.deploy) return;
                        if (!resourceConfig.options || !resourceConfig.options.recipe) return;

                        oneResource.canBeDeployed = true;
                        oneResource.deployOptions = $scope.deployConfig[$scope.envCode.toUpperCase()][oneResource.name].options;
                    }
                }

                for(var i = 0; i < $scope.deployedServices.length; i++) {
                    if($scope.deployedServices[i].labels && $scope.deployedServices[i].labels['soajs.resource.id'] === oneResource._id.toString()) {
                        oneResource.isDeployed = true;
                        oneResource.instance = $scope.deployedServices[i];
                        break;
                    }
                }
            });
        }
    };

    $scope.addResource = function() {
        var formConfig = angular.copy(resourcesAppConfig.form.addResource);
        formConfig.entries[0].value = formConfig.data.types;
        formConfig.entries[0].onAction = function(id, value, form) {
            form.entries[1].value = [];
            formConfig.data.categories.forEach(function(oneCategory) {
                if(oneCategory.group === value.toLowerCase()) {
                    form.entries[1].value.push(oneCategory);
                }
            });
            form.entries[1].hidden = false;
        };

        var currentScope = $scope;
        var options = {
			timeout: $timeout,
			form: formConfig,
			name: 'addResource',
			label: 'Add New Resource',
			actions: [
				{
					'type': 'submit',
					'label': 'Proceed',
					'btn': 'primary',
					'action': function (formData) {
                        $scope.manageResource({}, 'add', formData);
                        currentScope.modalInstance.dismiss('cancel');
						currentScope.form.formData = {};
					}
				},
				{
					'type': 'reset',
					'label': translation.cancel[LANG],
					'btn': 'danger',
					'action': function () {
						currentScope.modalInstance.dismiss('cancel');
						currentScope.form.formData = {};
					}
				}
			]
		};

		buildFormWithModal(currentScope, $modal, options);
    };

    $scope.manageResource = function(resource, action, settings) {
	    var currentScope = $scope;
        $modal.open({
            templateUrl: "addEditResource.tmpl",
            size: 'lg',
            backdrop: true,
            keyboard: true,
            controller: function ($scope, $modalInstance) {
                fixBackDrop();
	            
                $scope.formData = {};
                $scope.envs = [];
                $scope.message = {};
                $scope.recipes = [];
	            
                let category = (resource && Object.keys(resource).length > 0) ? resource.category: settings.category;
	            resourcesAppConfig.form.addResource.data.categories.forEach((oneCategory)=>{
		            if(oneCategory.v === category){
			            $scope.categoryLabel = oneCategory.l;
		            }
	            });
	            
	            console.log($scope.categoryLabel);
	
	            let allowEdit = ((action === 'add') || (action === 'update' && resource.created.toUpperCase() === currentScope.envCode.toUpperCase()));
	            resourceConfiguration.loadDriverSchema($scope, resource, settings, allowEdit, function(error) {
		            if (error) {
			            $scope.notsupported = true;
		            }
	            });
	            
                $scope.options = {
                    deploymentModes: [],
                    envCode: currentScope.envCode,
                    envPlatform: currentScope.envPlatform,<commit_msg>removed unneeded log message<commit_after>'use strict';

var resourcesApp = soajsApp.components;
resourcesApp.controller('resourcesAppCtrl', ['$scope', '$http', '$timeout', '$modal', 'ngDataApi', '$cookies', 'injectFiles', 'resourceConfiguration', function ($scope, $http, $timeout, $modal, ngDataApi, $cookies, injectFiles, resourceConfiguration) {
    $scope.$parent.isUserLoggedIn();
    $scope.access = {};
    constructModulePermissions($scope, $scope.access, resourcesAppConfig.permissions);

    $scope.listResources = function(cb) {
	    $scope.oldStyle = false;
	    getEnvironment(function(){

	    	overlayLoading.show();
		    getSendDataFromServer($scope, ngDataApi, {
			    method: 'get',
			    routeName: '/dashboard/resources/list',
			    params: {
				    env: $scope.envCode
			    }
		    }, function(error, response) {
			    overlayLoading.hide();
			    if(error) {
				    $scope.displayAlert('danger', error.message);
			    }
			    else {
                    $scope.resources = { list: response };
                    $scope.resources.original = angular.copy($scope.resources.list); //keep a copy of the original resources records

                    if($scope.deployConfig && $scope.deployedServices) {
                        markDeployed();
                    }

                    groupByType();

                    if(cb) return cb();
			    }
		    });

	    });

        function groupByType() {
            $scope.resources.types = {};
            $scope.resources.list.forEach(function(oneResource) {
                if(!$scope.resources.types[oneResource.type]) {
                    $scope.resources.types[oneResource.type] = {};
                }
                if(!$scope.resources.types[oneResource.type][oneResource.category]) {
                    $scope.resources.types[oneResource.type][oneResource.category] = [];
                }

                if(oneResource.created === $scope.envCode.toUpperCase()) {
                    oneResource.allowEdit = true;
                }

                $scope.resources.types[oneResource.type][oneResource.category].push(oneResource);
            });
        }

        function getEnvironment(cb){
		    getSendDataFromServer($scope, ngDataApi, {
			    "method": "get",
			    "routeName": "/dashboard/environment",
			    "params":{
				    "code": $scope.envCode
			    }
		    }, function (error, response) {
			    if (error) {
				    $scope.$parent.displayAlert('danger', error.code, true, 'dashboard', error.message);
			    }
			    else {
				    if(response.dbs.clusters && Object.keys(response.dbs.clusters).length > 0){
				        $scope.oldStyle = true;
				    }
				    return cb();
			    }
		    });
	    }

        function markDeployed() {
            $scope.resources.list.forEach(function(oneResource) {
                if($scope.deployConfig && $scope.deployConfig[$scope.envCode.toUpperCase()]) {
                    if($scope.deployConfig[$scope.envCode.toUpperCase()][oneResource.name]) {
                        var resourceConfig = $scope.deployConfig[$scope.envCode.toUpperCase()][oneResource.name];

                        if (!resourceConfig.deploy) return;
                        if (!resourceConfig.options || !resourceConfig.options.recipe) return;

                        oneResource.canBeDeployed = true;
                        oneResource.deployOptions = $scope.deployConfig[$scope.envCode.toUpperCase()][oneResource.name].options;
                    }
                }

                for(var i = 0; i < $scope.deployedServices.length; i++) {
                    if($scope.deployedServices[i].labels && $scope.deployedServices[i].labels['soajs.resource.id'] === oneResource._id.toString()) {
                        oneResource.isDeployed = true;
                        oneResource.instance = $scope.deployedServices[i];
                        break;
                    }
                }
            });
        }
    };

    $scope.addResource = function() {
        var formConfig = angular.copy(resourcesAppConfig.form.addResource);
        formConfig.entries[0].value = formConfig.data.types;
        formConfig.entries[0].onAction = function(id, value, form) {
            form.entries[1].value = [];
            formConfig.data.categories.forEach(function(oneCategory) {
                if(oneCategory.group === value.toLowerCase()) {
                    form.entries[1].value.push(oneCategory);
                }
            });
            form.entries[1].hidden = false;
        };

        var currentScope = $scope;
        var options = {
			timeout: $timeout,
			form: formConfig,
			name: 'addResource',
			label: 'Add New Resource',
			actions: [
				{
					'type': 'submit',
					'label': 'Proceed',
					'btn': 'primary',
					'action': function (formData) {
                        $scope.manageResource({}, 'add', formData);
                        currentScope.modalInstance.dismiss('cancel');
						currentScope.form.formData = {};
					}
				},
				{
					'type': 'reset',
					'label': translation.cancel[LANG],
					'btn': 'danger',
					'action': function () {
						currentScope.modalInstance.dismiss('cancel');
						currentScope.form.formData = {};
					}
				}
			]
		};

		buildFormWithModal(currentScope, $modal, options);
    };

    $scope.manageResource = function(resource, action, settings) {
	    var currentScope = $scope;
        $modal.open({
            templateUrl: "addEditResource.tmpl",
            size: 'lg',
            backdrop: true,
            keyboard: true,
            controller: function ($scope, $modalInstance) {
                fixBackDrop();
	            
                $scope.formData = {};
                $scope.envs = [];
                $scope.message = {};
                $scope.recipes = [];
	            
                let category = (resource && Object.keys(resource).length > 0) ? resource.category: settings.category;
	            resourcesAppConfig.form.addResource.data.categories.forEach((oneCategory)=>{
		            if(oneCategory.v === category){
			            $scope.categoryLabel = oneCategory.l;
		            }
	            });
	            
	            let allowEdit = ((action === 'add') || (action === 'update' && resource.created.toUpperCase() === currentScope.envCode.toUpperCase()));
	            resourceConfiguration.loadDriverSchema($scope, resource, settings, allowEdit, function(error) {
		            if (error) {
			            $scope.notsupported = true;
		            }
	            });
	            
                $scope.options = {
                    deploymentModes: [],
                    envCode: currentScope.envCode,
                    envPlatform: currentScope.envPlatform,
{'fraction_non_alphanumeric': 0.13400459021248243, 'fraction_numerical': 0.0011845709632042644, 'mean_word_length': 1.8129945855893377, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 9, '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': '3739059', 'n_tokens_mistral': 3994, 'n_tokens_neox': 3431, 'n_words': 537}