Fixed SonarIssues Alerts to Bootstrap Notification
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / FWParentListDictController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20 app.controller('editFWParentListController' ,  function ($scope, $modalInstance, message, UserInfoServiceDS2, PolicyAppService, Notification){
21     if(message.fwDictListDictionaryData==null){
22         $scope.slchoices = [];
23         $scope.alchoices = [];
24         $scope.label='Add Parent List'
25     }else{
26         $scope.label='Edit Parent List'
27         $scope.disableCd=true;
28         $scope.slchoices = [];
29         $scope.alchoices = [];
30         var slList = message.fwDictListDictionaryData.serviceList;
31         var alList = message.fwDictListDictionaryData.addressList;    
32         var splitEqual = ',';
33         if(slList != null){
34                 if (slList.indexOf(splitEqual) >= 0) {
35                         var splitValue = slList.split(splitEqual);
36                         for(i = 0; i < splitValue.length; i++){
37                                 var key  = splitValue[i];
38                                 $scope.slchoices.push({'id':'choice'+i+1, 'option': key});
39                         }
40                 }else{
41                         var key  = slList;
42                         $scope.slchoices.push({'id':'choice'+1, 'option': key});
43                 }
44         }
45         if(alList != null){
46                 if (alList.indexOf(splitEqual) >= 0) {
47                         var splitALValue = alList.split(splitEqual);
48                         for(i = 0; i < splitALValue.length; i++){
49                                 var key  = splitALValue[i];
50                                 $scope.alchoices.push({'id':'choice'+i+1, 'option': key});
51                         }
52                 }else{
53                         var key  = alList;
54                         $scope.alchoices.push({'id':'choice'+1, 'option': key});
55                 }
56         }
57     }
58
59     PolicyAppService.getData('getDictionary/get_ServiceListDictionaryDataByName').then(function (data) {
60         var j = data;
61         $scope.data = JSON.parse(j.data);
62         console.log($scope.data);
63         $scope.serviceListDictionaryDatas = JSON.parse($scope.data.serviceListDictionaryDatas);
64         console.log($scope.serviceListDictionaryDatas);
65     }, function (error) {
66         console.log("failed");
67     });
68
69     PolicyAppService.getData('getDictionary/get_AddressGroupDictionaryDataByName').then(function (data) {
70         var j = data;
71         $scope.data = JSON.parse(j.data);
72         console.log($scope.data);
73         $scope.addressGroupDictionaryDatas = JSON.parse($scope.data.addressGroupDictionaryDatas);
74         console.log($scope.addressGroupDictionaryDatas);
75     }, function (error) {
76         console.log("failed");
77     });
78         
79         /*getting user info from session*/
80         var userid = null;
81         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
82                 .then(function (response) {                     
83                         userid = response.userid;               
84          });
85         
86     $scope.editParentList = message.fwDictListDictionaryData;
87
88     $scope.saveFWParentList = function(fwDictListDictionaryData) {
89         var regex = new RegExp("^[a-zA-Z0-9_]*$");
90         if(!regex.test(fwDictListDictionaryData.parentItemName)) {
91                 Notification.error("Enter Valid Parent List Name without spaces or special characters");
92         }else{
93                 var addSLData = extend(fwDictListDictionaryData, $scope.attributeDatas[0]);
94                 var finalData = extend(addSLData, $scope.attributeALDatas[0]);
95                 var uuu = "saveDictionary/fw_dictionary/save_FWDictionaryList";
96                 var postData={fwDictListDictionaryData: finalData, userid: userid};
97                 $.ajax({
98                         type : 'POST',
99                         url : uuu,
100                         dataType: 'json',
101                         contentType: 'application/json',
102                         data: JSON.stringify(postData),
103                         success : function(data){
104                                 $scope.$apply(function(){
105                                         $scope.fwDictListDictionaryDatas=data.fwDictListDictionaryDatas;});
106                                 if($scope.fwDictListDictionaryDatas == "Duplicate"){
107                                         Notification.error("FW DictionaryList Dictionary exists with Same  Name.")
108                                 }else{      
109                                         console.log($scope.fwDictListDictionaryDatas);
110                                         $modalInstance.close({fwDictListDictionaryDatas:$scope.fwDictListDictionaryDatas});
111                                 }
112                         },
113                         error : function(data){
114                                 Notification("Error while saving.");
115                         }
116                 });
117         }
118     };
119
120     $scope.close = function() {
121         $modalInstance.close();
122     };
123     
124     function extend(obj, src) {
125         for (var key in src) {
126             if (src.hasOwnProperty(key)) obj[key] = src[key];
127         }
128         return obj;
129     }
130     
131  
132     $scope.attributeDatas = [{"attributes" : $scope.slchoices}];
133     $scope.addServiceGroupNewChoice = function() {
134         var newItemNo = $scope.slchoices.length+1;
135         $scope.slchoices.push({'id':'choice'+newItemNo});
136     };
137     $scope.removeServiceGroupChoice = function() {
138         var lastItem = $scope.slchoices.length-1;
139         $scope.slchoices.splice(lastItem);
140     };
141     
142     $scope.attributeALDatas = [{"alAttributes" : $scope.alchoices}];
143     $scope.addAddressGroupNewChoice = function() {
144         var newItemNo = $scope.alchoices.length+1;
145         $scope.alchoices.push({'id':'choice'+newItemNo});
146     };
147     $scope.removeAddressGroupChoice = function() {
148         var lastItem = $scope.alchoices.length-1;
149         $scope.alchoices.splice(lastItem);
150     };
151 });