Fixed SonarIssues Alerts to Bootstrap Notification
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / FWTagPickerListDictController.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('editFWTagPickerController' ,  function ($scope, $modalInstance, message, UserInfoServiceDS2, Notification, PolicyAppService){
21     if(message.fwTagPickerDictionaryData==null)
22         $scope.label='Add TagPicker',
23         $scope.choices = [];
24     else{
25         $scope.label='Edit TagPicker'
26         $scope.disableCd=true;
27         $scope.choices = [];
28         var headers = message.fwTagPickerDictionaryData.tagValues;
29         var SplitChars = '#';
30         if (headers.indexOf(SplitChars) >= 0) {
31             var splitHeader = headers.split(SplitChars);
32             var singleHeader  = splitHeader;
33             var splitEqual = ':';
34             for(i = 0; i < singleHeader.length; i++){
35                  if (singleHeader[i].indexOf(splitEqual) >= 0) {
36                         var splitValue = singleHeader[i].split(splitEqual);
37                         var key  = splitValue[0];
38                         var value = splitValue[1];
39                         var newItemNo = $scope.choices.length+1;
40                         $scope.choices.push({'id':'choice'+newItemNo, 'option': key , 'number' : value });
41                  }
42             }
43         }else{
44                  var splitEqual = ':';
45              if (headers.indexOf(splitEqual) >= 0) {
46                  var splitValue = headers.split(splitEqual);
47                  var key  = splitValue[0];
48                  var value = splitValue[1];
49                  $scope.choices.push({'id':'choice'+1, 'option': key , 'number' : value });
50              }
51         }
52     }
53
54                 
55     PolicyAppService.getData('getDictionary/get_TagListData').then(function (data) {
56         var j = data;
57         $scope.data = JSON.parse(j.data);
58         console.log($scope.data);
59         $scope.fwTagDictionaryDatas = JSON.parse($scope.data.fwTagDictionaryDatas);
60         console.log($scope.fwTagDictionaryDatas);
61     }, function (error) {
62         console.log("failed");
63     });
64
65     PolicyAppService.getData('getDictionary/get_TagNameByName').then(function (data) {
66         var j = data;
67         $scope.data = JSON.parse(j.data);
68         console.log($scope.data);
69         $scope.fwTagDictionaryKeyDatas = JSON.parse($scope.data.fwTagDictionaryDatas);
70         console.log($scope.fwTagDictionaryKeyDatas);
71     }, function (error) {
72         console.log("failed");
73     });
74                 
75         
76         $scope.fwTagDictionaryDataValues = [];
77         $scope.getTagListValues = function(key){
78                 for (var i = 0; i < $scope.fwTagDictionaryDatas.length; ++i) {
79             var obj = $scope.fwTagDictionaryDatas[i];
80             if (obj.fwTagName == key){
81                 var splitValue = obj.tagValues.split(',');
82                 for (var j = 0; j < splitValue.length; ++j) {
83                         $scope.fwTagDictionaryDataValues.push(splitValue[j].split(',')[0]);
84                 }
85             }
86         }
87         };
88         
89         /*getting user info from session*/
90         var userid = null;
91         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
92                 .then(function (response) {                     
93                         userid = response.userid;               
94          });
95         
96     $scope.editFWTagPicker = message.fwTagPickerDictionaryData;
97
98     $scope.saveFWTagPicker = function(fwTagPickerDictionaryData) {
99         var regex = new RegExp("^[a-zA-Z0-9_]*$");
100         if(!regex.test(fwTagPickerDictionaryData.tagPickerName)) {
101                 Notification.error("Enter Valid TagPicker Name without spaces or special characters");
102         }else{
103                 var finalData = extend(fwTagPickerDictionaryData, $scope.actions[0]);
104                 var uuu = "saveDictionary/fw_dictionary/save_fwTagPicker";
105                 var postData={fwTagPickerDictionaryData: finalData, userid: userid};
106                 $.ajax({
107                         type : 'POST',
108                         url : uuu,
109                         dataType: 'json',
110                         contentType: 'application/json',
111                         data: JSON.stringify(postData),
112                         success : function(data){
113                                 $scope.$apply(function(){
114                                         $scope.fwTagPickerDictionaryDatas=data.fwTagPickerDictionaryDatas;});
115                                 if($scope.fwTagPickerDictionaryDatas == "Duplicate"){
116                                         Notification.error("TagPicker Dictionary exists with TagPicker Name.")
117                                 }else{      
118                                         console.log($scope.fwTagPickerDictionaryDatas);
119                                         $modalInstance.close({fwTagPickerDictionaryDatas:$scope.fwTagPickerDictionaryDatas});
120                                 }       
121
122                         },
123                         error : function(data){
124                                 Notification.error("Error while saving.");
125                         }
126                 });
127         }
128     };
129
130     $scope.close = function() {
131         $modalInstance.close();
132     };
133     
134     function extend(obj, src) {
135         for (var key in src) {
136             if (src.hasOwnProperty(key)) obj[key] = src[key];
137         }
138         return obj;
139     }
140     
141     $scope.actions = [{"tags" : $scope.choices}];
142     $scope.addNewChoice = function() {
143         var newItemNo = $scope.choices.length+1;
144         $scope.choices.push({'id':'choice'+newItemNo});
145     };
146     $scope.removeChoice = function() {
147         var lastItem = $scope.choices.length-1;
148         $scope.choices.splice(lastItem);
149     };
150
151 });