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