412415006c0c814db439b3437503f0837a422e98
[portal.git] / ecomp-portal-FE-common / client / app / views / role / rolefunctionpopupController.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 app.controller('rolefunctionpopupController',function($scope, confirmBoxService, message, $http,RoleService, conf, isEditing) {
21                                         if (message.availableRoleFunction == null) {
22                                                 $scope.label = 'Add Role Function';
23                                                 var tempText = "";
24                                         } else {
25                                                 $scope.label = 'Edit Role Function'
26                                                 $scope.disableCd = true;
27                                                 var tempText = new String(message.availableRoleFunction.name);
28                                                 $scope.editRoleFunction = angular.copy(message.availableRoleFunction);
29                                         }
30
31                                         $scope.tempText = tempText;
32                                         $scope.isEditing = isEditing;
33
34                                         $scope.saveRoleFunction = function(availableRoleFunction) {
35                                                 confirmBoxService.confirm(
36                                                                                 "You are about to Create the role function "+ availableRoleFunction.name+ ". Do you want to continue?")
37                                                                 .then(function(confirmed) {
38                                                                                         if (confirmed) {
39
40                                                                                                 var uuu = conf.api.saveRoleFunction;
41                                                                                                 var postData = availableRoleFunction;
42
43                                                                                                 if (availableRoleFunction == null) {
44                                                                                                         confirmBoxService.showInformation("Please enter valid role function details.");
45                                                                                                 }
46                                                                                                 var exists = false, x;
47                                                                                                 for (x in message.availableRoleFunctions) {
48                                                                                                         console.log(message.availableRoleFunctions[x].name);
49                                                                                                         if (message.availableRoleFunctions[x].name == availableRoleFunction.name) {
50                                                                                                                 confirmBoxService.showInformation("Role Function already exists.");
51                                                                                                                 exists = true;
52                                                                                                                 availableRoleFunction.name = $scope.tempText;
53                                                                                                                 break;
54                                                                                                         }
55                                                                                                         if (!isEditing) {
56                                                                                                                 if (message.availableRoleFunctions[x].code == availableRoleFunction.code) {
57                                                                                                                         confirmBoxService.showInformation("Code already exists. Please create a role function with a different code to proceed.");
58                                                                                                                         exists = true;
59                                                                                                                         availableRoleFunction.name = $scope.tempText;
60                                                                                                                         break;
61                                                                                                                 }
62                                                                                                         }
63                                                                                                 }
64
65                                                                                                 if (!exists&& availableRoleFunction.name.trim() != ''&& availableRoleFunction.code.trim() != '') {
66                                                                                                         $http.post(uuu,JSON.stringify(postData)).then(function(res) {
67                                                                                                                                                 $scope.availableRoleFunctionsTemp = res.data.availableRoleFunctions;
68                                                                                                                                                 RoleService.getRoleFunctionList().then(
69                                                                                                                                                                                 function(data) {
70
71                                                                                                                                                                                         var j = data;
72                                                                                                                                                                                         $scope.data = JSON.parse(j.data);
73                                                                                                                                                                                         $scope.availableRoleFunctions = JSON.parse($scope.data.availableRoleFunctions);
74
75                                                                                                                                                                                         // $scope.resetMenu();
76                                                                                                                                                                                         /*$scope.closeThisDialog({
77                                                                                                                                                                                                                 result : true,
78                                                                                                                                                                                                                 availableRoleFunctions : $scope.availableRoleFunctions
79                                                                                                                                                                                                         });*/
80                                                                                                                                                                                         $scope.$dismiss({result : true,availableRoleFunctions : $scope.availableRoleFunctions});
81                                                                                                                                                                                 },
82                                                                                                                                                                                 function(error) {
83                                                                                                                                                                                         console.log("failed");
84                                                                                                                                                                                         // reloadPageOnce();
85                                                                                                                                                                                         //$scope.closeThisDialog(true);
86                                                                                                                                                                                         $scope.$dismiss('cancel');
87                                                                                                                                                                                 });
88
89                                                                                                                                         });
90
91                                                                                                 }
92                                                                                         };
93
94                                                                                         $scope.close = function() {
95                                                                                                 this.closeThisDialog(true);
96                                                                                         };
97                                                                                 }
98
99                                                                 );
100                                         }
101                                 });