bda90af92b57e8e9b2cc9d1bed4f412c6ed04f68
[portal.git] / ecomp-portal-FE-common / client / app / views / role / rolefunctionpopupController.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 app.controller('rolefunctionpopupController',function($scope, confirmBoxService, message, $http, $modalInstance, RoleService, conf, isEditing) {
39         if (message.availableRoleFunction == null) {
40                 $scope.label = 'Add Role Function';
41                 var tempText = "";
42                 $scope.selectedAppId = message.appId;
43                 $scope.defaultAction = "*";
44                 $scope.disableTypeAction = true;        
45                  $scope.ngRepeatDemo = [
46                         {id: 'menuradiobutton1', value: 'menu', labelvalue: 'menu'},
47                         {id: 'urlradiobutton2', value: 'url', labelvalue: 'url'},
48                         {id: 'otherradiobutton3', value: 'other', labelvalue: 'other'}
49                     ]
50                  $scope.selectedvalueradioButtonGroup = {
51                                 type: 'menu'
52                             }
53                  $scope.editRoleFunction = {
54                                 action: '*'
55                             }
56         } else {
57                  $scope.ngRepeatDemo = [
58                         {id: 'menuradiobutton1', value: 'menu', labelvalue: 'menu'},
59                         {id: 'urlradiobutton2', value: 'url', labelvalue: 'url'},
60                         {id: 'otherradiobutton3', value: 'other', labelvalue: 'other'}
61                     ]
62                 $scope.label = 'Edit Role Function'
63                 $scope.disableCd = true;
64                 $scope.disableTypeAction = false;
65                 var tempText = new String(message.availableRoleFunction.name);
66                 $scope.editRoleFunction = angular.copy(message.availableRoleFunction);
67                 if($scope.editRoleFunction.type.includes('menu')||$scope.editRoleFunction.type.includes('url')){
68                          $scope.selectedvalueradioButtonGroup = {
69                                         type: $scope.editRoleFunction.type
70                                     }
71                 } else{
72                          $scope.selectedvalueradioButtonGroup = {
73                                         type: 'other'
74                                     }
75                 }
76                 $scope.selectedAppId = message.appId;
77         }
78         $scope.tempText = tempText;
79         $scope.isEditing = isEditing;
80
81         $scope.saveRoleFunction = function(availableRoleFunction, type) {
82                 if(type !== 'other'){
83                         if (!availableRoleFunction.hasOwnProperty('type')) {
84                                 availableRoleFunction['type'] = type ; 
85                         } else{
86                                 availableRoleFunction.type = type ; 
87                         }
88                 }
89                 if(/[^a-zA-Z0-9\-\.\_]/.test(availableRoleFunction.type)){
90                         confirmBoxService.showInformation('Type can only contain alphanumeric characters, dots(.) and underscores(_)').then(isConfirmed => {});
91                         return;
92                 }               
93                 if(availableRoleFunction.action !== '*' && /[^a-zA-Z0-9\-\.\_]/.test(availableRoleFunction.action)){
94                         confirmBoxService.showInformation('Action can only contain alphanumeric characters, hyphens(-), dots(.) and underscores(_) and single asterisk character(*)').then(isConfirmed => {});
95                         return;
96                 }
97                 if(/[^a-zA-Z0-9\-\:\_\./*]/.test(availableRoleFunction.code)){
98                         confirmBoxService.showInformation('Instance can only contain alphanumeric characters, hyphens(-), dots(.), colons(:), forwardSlash(/) , asterisk(*) and underscores(_)').then(isConfirmed => {});
99                         return;
100                 }
101                 if(/[^a-zA-Z0-9\-\_ \.]/.test(availableRoleFunction.name)){
102                         confirmBoxService.showInformation('Name can only contain alphanumeric characters, spaces, hyphens(-), dots(.) and underscores(_)').then(isConfirmed => {});
103                         return;
104                 }
105                 confirmBoxService.confirm(
106                                 "You are about to Create the role function "+ availableRoleFunction.name+ ". Do you want to continue?")
107                                 .then(function(confirmed) {
108                                         if (confirmed) {
109                                                         var uuu = conf.api.saveRoleFunction.replace(':appId', $scope.selectedAppId);
110                                                         var postData = availableRoleFunction;
111                                                 if (availableRoleFunction == null) {
112                                                         confirmBoxService.showInformation("Please enter valid role function details.");
113                                                 }
114                                                 var exists = false, x;
115                                                 for (x in message.availableRoleFunctions) {
116                                                         console.log(message.availableRoleFunctions[x].name);
117                                                         if (message.availableRoleFunctions[x].type == availableRoleFunction.type
118                                                                         && message.availableRoleFunctions[x].code == availableRoleFunction.code
119                                                                         && message.availableRoleFunctions[x].action == availableRoleFunction.action
120                                                                         && message.availableRoleFunctions[x].name == availableRoleFunction.name) {
121                                                                 confirmBoxService.showInformation("Role Function already exists.");
122                                                                 exists = true;
123                                                                 availableRoleFunction.name = $scope.tempText;
124                                                                 break;
125                                                         }
126                                                         if (!isEditing) {
127                                                                 if (message.availableRoleFunctions[x].type == availableRoleFunction.type
128                                                                                 && message.availableRoleFunctions[x].code == availableRoleFunction.code
129                                                                                 && message.availableRoleFunctions[x].action == availableRoleFunction.action
130                                                                         ) {
131                                                                         confirmBoxService.showInformation("Please make sure code, type and action is unique. Please create a role function with a different code or type or action to proceed.");
132                                                                         exists = true;
133                                                                         availableRoleFunction.name = $scope.tempText;
134                                                                         break;
135                                                                 }
136                                                         }
137                                                 }
138
139                                                 if (!exists&& availableRoleFunction.name.trim() != ''&& availableRoleFunction.code.trim() != '') {
140                                                         $http.post(uuu,JSON.stringify(postData)).then(function(res) {
141                                                                 if(res.data.status == 'OK'){
142                                                                         confirmBoxService.showInformation(res.data.message).then(isConfirmed => {});
143                                                                 } else{
144                                                                         confirmBoxService.showInformation('Error:' + res.data.message).then(isConfirmed => {});
145                                                                 }
146                                                                 $scope.availableRoleFunctionsTemp = res.data.availableRoleFunctions;
147                                                                 $modalInstance.close(true);
148                                                         });
149
150                                                 }
151                                         };
152
153                                         $scope.close = function() {
154                                                 this.closeThisDialog(true);
155                                         };
156                                 }
157
158                                 );
159         }
160 });