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