Fixed SonarIssues Alerts to Bootstrap Notification
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / actionPolicyDictController.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('editActionPolicyDictController', function ($scope, $modalInstance, message, UserInfoServiceDS2, Notification){
21     if(message.actionPolicyDictionaryData==null)
22         $scope.label='Add Action Dictionary',
23         $scope.choices = [];    
24      else{
25         $scope.choices = [];
26         $scope.label='Edit Action Dictionary'
27         $scope.disableCd=true;
28         var headers = message.actionPolicyDictionaryData.header;
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         /*getting user info from session*/
55         var userid = null;
56         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
57                 .then(function (response) {                     
58                         userid = response.userid;               
59          });
60         
61     $scope.editActionDict = message.actionPolicyDictionaryData;
62     $scope.saveActionDict = function(actionPolicyDictionaryData) {
63         var regex = new RegExp("^[a-zA-Z0-9_]*$");
64         if(!regex.test(actionPolicyDictionaryData.attributeName)) {
65                 Notification.error("Enter Valid Attribute Name without spaces or special characters");
66         }else{
67                 var finalData = extend(actionPolicyDictionaryData, $scope.headerDatas[0]);
68                 var uuu = "saveDictionary/action_dictionary/save_ActionDict";
69                 var postData={actionPolicyDictionaryData: finalData, userid: userid};
70                 $.ajax({
71                         type : 'POST',
72                         url : uuu,
73                         dataType: 'json',
74                         contentType: 'application/json',
75                         data: JSON.stringify(postData),
76                         success : function(data){
77                                 $scope.$apply(function(){
78                                         $scope.actionPolicyDictionaryDatas=data.actionPolicyDictionaryDatas;});
79                                 if($scope.actionPolicyDictionaryDatas == "Duplicate"){
80                                         Notification.error("ActionPolicy Dictionary exists with Same Attribute Name.")
81                                 }else{      
82                                         console.log($scope.actionPolicyDictionaryDatas);
83                                         $modalInstance.close({actionPolicyDictionaryDatas:$scope.actionPolicyDictionaryDatas});
84                                 }
85                         },
86                         error : function(data){
87                                 Notification.error("Error while saving.");
88                         }
89                 });
90         }
91     };
92
93     $scope.close = function() {
94         $modalInstance.close();
95     };
96     
97     function extend(obj, src) {
98         for (var key in src) {
99             if (src.hasOwnProperty(key)) obj[key] = src[key];
100         }
101         return obj;
102     }
103     
104     $scope.headerDatas = [{"headers" : $scope.choices}];
105     $scope.addNewChoice = function() {
106         var newItemNo = $scope.choices.length+1;
107         $scope.choices.push({'id':'choice'+newItemNo});
108     };
109     $scope.removeChoice = function() {
110         var lastItem = $scope.choices.length-1;
111         $scope.choices.splice(lastItem);
112     };
113     
114 });