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