Fixed SonarIssues Alerts to Bootstrap Notification
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / FWTagListDictController.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('editFWTagListController' , function ($scope, $modalInstance, message, UserInfoServiceDS2, Notification){
21    
22         if(message.fwTagDictionaryDatas==null)
23         $scope.label='Add Tag',
24         $scope.choices = [];
25     else{
26         $scope.label='Edit Tag'
27         $scope.disableCd=true;
28         $scope.choices = [];
29         var headers = message.fwTagDictionaryDatas.tagValues;
30         var SplitChars = ',';
31         if (headers.indexOf(SplitChars) >= 0) {
32             var splitHeader = headers.split(SplitChars);
33             var singleHeader  = splitHeader;
34             for(i = 0; i < singleHeader.length; i++){
35                         var splitValue = singleHeader[i];
36                         var newItemNo = $scope.choices.length+1;
37                         $scope.choices.push({'id':'choice'+newItemNo, 'tags': splitValue });
38                  }
39         }else{ 
40              $scope.choices.push({'id':'choice'+1, 'tags': headers });
41         }
42     }
43         
44         /*getting user info from session*/
45         var userid = null;
46         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
47                 .then(function (response) {                     
48                         userid = response.userid;               
49          });
50         
51     $scope.editFWTag = message.fwTagDictionaryDatas;
52
53     $scope.saveFWTag = function(fwTagDictionaryData) {
54         var regex = new RegExp("^[a-zA-Z0-9_]*$");
55         if(!regex.test(fwTagDictionaryData.fwTagName)) {
56                 Notification.error("Enter Valid Tag Name without spaces or special characters");
57         }else{
58                 var finalData = extend(fwTagDictionaryData, $scope.attributeDatas[0]);
59                 var uuu = "saveDictionary/fw_dictionary/save_fwTag";
60                 var postData={fwTagDictionaryData: finalData, userid: userid};
61                 $.ajax({
62                         type : 'POST',
63                         url : uuu,
64                         dataType: 'json',
65                         contentType: 'application/json',
66                         data: JSON.stringify(postData),
67                         success : function(data){
68                                 $scope.$apply(function(){
69                                         $scope.fwTagDictionaryDatas=data.fwTagDictionaryDatas;});
70                                 if($scope.fwTagDictionaryDatas == "Duplicate"){
71                                         Notification.error("Tag Dictionary exists with Same Tag Name.")
72                                 }else{      
73                                         console.log($scope.fwTagDictionaryDatas);
74                                         $modalInstance.close({fwTagDictionaryDatas:$scope.fwTagDictionaryDatas});
75                                 }
76                         },
77                         error : function(data){
78                                 Notification.error("Error while saving.");
79                         }
80                 });
81         }
82     };
83     
84     function extend(obj, src) {
85         for (var key in src) {
86             if (src.hasOwnProperty(key)) obj[key] = src[key];
87         }
88         return obj;
89     }
90     
91     $scope.attributeDatas = [{"tags" : $scope.choices}];
92     $scope.addNewChoice = function() {
93       var newItemNo = $scope.choices.length+1;
94       $scope.choices.push({'id':'choice'+newItemNo});
95     };    
96     $scope.removeChoice = function() {
97       var lastItem = $scope.choices.length-1;
98       $scope.choices.splice(lastItem);
99     };
100     
101
102     $scope.close = function() {
103         $modalInstance.close();
104     };
105 });