Fixed Sonar issues for Console Logs
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / BRMSParamPolicyController.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 angular.module('abs').controller('brmsParamPolicyController', 
21     ['$scope', '$window', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', 
22     function ($scope, $window, PolicyAppService, PolicyNavigator, modalService, $modal, Notification) {
23     $("#dialog").hide();
24
25     $scope.policyNavigator;
26     $scope.savebutton = true;
27     $scope.refreshCheck = false;
28     
29     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
30         $scope.temp.policy = {
31             policyType : "Config",
32             configPolicyType : "BRMS_Param"
33         }
34     }
35     
36     $scope.refresh = function(){
37         if($scope.refreshCheck){
38             $scope.policyNavigator.refresh();
39         }
40         $scope.modal('createNewPolicy', true);
41         $scope.temp.policy = "";
42     };
43     
44     $scope.modal = function(id, hide) {
45         return $('#' + id).modal(hide ? 'hide' : 'show');
46     };
47     
48     $('#ttlDate').datepicker({
49         dateFormat: 'dd/mm/yy',
50         changeMonth: true,
51         changeYear: true,
52         onSelect: function(date) {
53             angular.element($('#ttlDate')).triggerHandler('input');
54         }
55     });
56     
57     $scope.validateSuccess = true;
58     var readValue = $scope.temp.policy.readOnly;
59     if(readValue){
60         $scope.validateSuccess = false;
61     }
62     
63     PolicyAppService.getData('getDictionary/get_BRMSControllerDataByName').then(function (data) {
64         var j = data;
65         $scope.data = JSON.parse(j.data);
66         $scope.brmsControllerDatas = JSON.parse($scope.data.brmsControllerDictionaryDatas);
67     });
68
69     PolicyAppService.getData('getDictionary/get_BRMSDependencyDataByName').then(function (data) {
70         var j = data;
71         $scope.data = JSON.parse(j.data);
72         $scope.brmsDependencyDatas = JSON.parse($scope.data.brmsDependencyDictionaryDatas);
73     });
74
75     PolicyAppService.getData('getDictionary/get_BRMSParamDataByName').then(function (data) {
76         var j = data;
77         $scope.data = JSON.parse(j.data);
78         $scope.brmsParamDictionaryDatas = JSON.parse($scope.data.brmsParamDictionaryDatas);
79     });
80
81     PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
82         var j = data;
83         $scope.data = JSON.parse(j.data);
84         $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
85     });
86     
87     $scope.temp.policy.dynamicLayoutMap = {};
88     $scope.addDataToFields = function(ruleName){
89         if(ruleName != null){
90             var uuu = "policyController/getBRMSTemplateData.htm";
91             var postData={policyData: ruleName};
92              $.ajax({
93                  type : 'POST',
94                  url : uuu,
95                  dataType: 'json',
96                  contentType: 'application/json',
97                  data: JSON.stringify(postData),
98                  success : function(data){
99                      $scope.$apply(function(){
100                         $scope.temp.policy.dynamicLayoutMap = data.policyData;
101                      });
102                  },
103                  error : function(data){
104                     Notification.error("Error While Retriving the Template Layout Pattren.");
105                  }
106              });
107         }   
108     };
109     
110     $scope.showbrmsrule = true;
111     
112     $scope.ShowRule = function(policy){
113         var uuu = "policyController/ViewBRMSParamPolicyRule.htm";
114         var postData={policyData: policy};
115         $.ajax({
116             type : 'POST',
117             url : uuu,
118             dataType: 'json',
119             contentType: 'application/json',
120             data: JSON.stringify(postData),
121             success : function(data){
122                 $scope.showbrmsrule = false;
123                 $scope.validateSuccess = true;
124                 $scope.$apply(function(){
125                     $scope.datarule = data.policyData;
126                 });
127             },
128             error : function(data){
129                 Notification.error("Error Occured while Showing Rule.");
130             }
131         });
132     };
133     
134     $scope.hideRule = function(){
135         $scope.showbrmsrule = true;
136         $scope.validateSuccess = false;
137         $scope.apply();
138     };
139     
140     $scope.saveBrmsParamPolicy = function(policy){
141         if(policy.itemContent != undefined){
142             $scope.refreshCheck = true; 
143             $scope.policyNavigator = policy.itemContent;
144             policy.itemContent = "";
145         }
146         $scope.savebutton = false;
147         var uuu = "policycreation/save_policy";
148         var postData={policyData: policy};
149         $.ajax({
150             type : 'POST',
151             url : uuu,
152             dataType: 'json',
153             contentType: 'application/json',
154             data: JSON.stringify(postData),
155             success : function(data){
156                 $scope.$apply(function(){
157                     $scope.data=data.policyData;
158                     if($scope.data == 'success'){
159                         $scope.temp.policy.readOnly = 'true';
160                         $scope.safetyChecker = data.policyData.split("#")[2];
161                         if ($scope.safetyChecker!=undefined) {
162                             Notification.success($scope.safetyChecker);
163                         }
164                         Notification.success("Policy Saved Successfully.");    
165                     }else if ($scope.data == 'PolicyExists'){
166                         $scope.savebutton = true;
167                         Notification.error("Policy Already Exists with Same Name in Scope.");
168                     }
169                 });
170             },
171             error : function(data){
172                 Notification.error("Error Occured while saving Policy.");
173                 $scope.savebutton = true;
174             }
175         });
176     };
177
178     $scope.validatePolicy = function(policy){
179         document.getElementById("validate").innerHTML = "";
180         var uuu = "policyController/validate_policy.htm";
181          var postData={policyData: policy};
182          $.ajax({
183              type : 'POST',
184              url : uuu,
185              dataType: 'json',
186              contentType: 'application/json',
187              data: JSON.stringify(postData),
188              success : function(data){
189                  $scope.$apply(function(){
190                      $scope.validateData = data.data.replace(/\"/g, "");
191                         $scope.data=data.data.substring(1,8);
192                          var size = data.data.length;
193                          if($scope.data == 'success'){
194                              Notification.success("Validation Success.");
195                              $scope.savebutton = false;
196                              if (size > 18){
197                                  var displayWarning = data.data.substring(19,size);
198                                  document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
199                                  document.getElementById("validate").style.color = "white";
200                                  document.getElementById("validate").style.backgroundColor = "skyblue";
201                              }    
202                              $scope.validateSuccess = false;
203                          }else{
204                              Notification.error("Validation Failed.");
205                              document.getElementById("validate").innerHTML = $scope.validateData;
206                              document.getElementById("validate").style.color = "white";
207                              document.getElementById("validate").style.backgroundColor = "red";
208                              $scope.savebutton = true;
209                          }
210                          
211                  });
212              },
213              error : function(data){
214                  Notification.error("Validation Failed.");
215                  $scope.savebutton = true;
216              }
217          });
218     };
219     
220     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
221         $scope.temp.policy.attributes = [];
222     }else{
223       if($scope.temp.policy.attributes.length == 0){
224           $scope.temp.policy.attributes = [];
225       }
226    }
227     
228     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
229     $scope.addNewChoice = function() {
230         var newItemNo = $scope.temp.policy.attributes.length+1;
231         $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
232     };
233     $scope.removeChoice = function() {
234         var lastItem = $scope.temp.policy.attributes.length-1;
235         $scope.temp.policy.attributes.splice(lastItem);
236     };
237 }]);