db5bb1b6d63e149e20884534563dfba5bb5d40f0
[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                                 }
25                                 else{
26                                         $scope.label='Edit Role Function'
27                                         $scope.disableCd=true;
28                                         var tempText = new String(message.availableRoleFunction.name);
29                                         $scope.editRoleFunction = angular.copy(message.availableRoleFunction);
30                                 }
31                                 
32                                 $scope.tempText = tempText;
33                                 $scope.isEditing = isEditing;
34                                 
35                                 $scope.saveRoleFunction = function(availableRoleFunction) {
36                                           var uuu = conf.api.saveRoleFunction;
37                                           var postData={availableRoleFunction: availableRoleFunction};
38
39                                           if(availableRoleFunction==null){
40                                                   confirmBoxService.showInformation("Please enter valid role function details.");
41                                           }
42                                           var exists = false,x;
43                                           for(x in message.availableRoleFunctions){
44                                                   console.log(message.availableRoleFunctions[x].name);
45                                                         if(message.availableRoleFunctions[x].name==availableRoleFunction.name){
46                                                                 confirmBoxService.showInformation("Role Function already exists.");
47                                                                 exists = true;
48                                                                 availableRoleFunction.name = $scope.tempText;
49                                                                 break;
50                                                         } 
51                                                         if(!isEditing){
52                                                                 if (message.availableRoleFunctions[x].code == availableRoleFunction.code) {
53                                                                         confirmBoxService.showInformation("Code already exists. Please create a role function with a different code to proceed.");
54                                                                         exists = true;
55                                                                         availableRoleFunction.name = $scope.tempText;
56                                                                         break;
57                                                                 }
58                                                         }
59                                           }
60                                           
61                                           if(!exists && availableRoleFunction.name.trim() != '' && availableRoleFunction.code.trim() != ''){
62                                       $http.post(uuu, JSON.stringify(postData)).then(function(res){
63                                           console.log("data");
64 //                                        console.log(res.data);
65 //                                        $scope.availableRoleFunctionsTemp = res.data.availableRoleFunctions;
66                                           RoleService.getRoleFunctionList().then(function(data){
67                                                         
68                                                         var j = data;
69                                                         $scope.data = JSON.parse(j.data);
70                                                         $scope.availableRoleFunctions =JSON.parse($scope.data.availableRoleFunctions);
71                                                         
72                                                         //$scope.resetMenu();
73                                                         $scope.closeThisDialog({result: true, availableRoleFunctions: $scope.availableRoleFunctions });
74                                                 },function(error){
75                                                         console.log("failed");
76                                                         //reloadPageOnce();
77                                                         $scope.closeThisDialog(true);                                           
78                                                 });
79                                           
80                                           
81                                       });                                                 
82                                                   
83                                                   
84                                                   
85                                                   
86                                                 }
87                                 };
88                                           
89                                           
90                                         
91                                 $scope.close = function() { 
92                                         this.closeThisDialog(true);
93                                 };
94 }
95 );