Fixed Sonar issues for Console Logs
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / BRMSRawPolicyController.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('brmsRawPolicyController',
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_Raw"
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     PolicyAppService.getData('getDictionary/get_BRMSControllerDataByName').then(function (data) {
58     var j = data;
59     $scope.data = JSON.parse(j.data);
60     $scope.brmsControllerDatas = JSON.parse($scope.data.brmsControllerDictionaryDatas);
61     });
62
63     PolicyAppService.getData('getDictionary/get_BRMSDependencyDataByName').then(function (data) {
64     var j = data;
65     $scope.data = JSON.parse(j.data);
66     $scope.brmsDependencyDatas = JSON.parse($scope.data.brmsDependencyDictionaryDatas);
67     });
68
69     PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
70     var j = data;
71     $scope.data = JSON.parse(j.data);
72     $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
73     });
74
75     $scope.saveBrmsRawPolicy = function(policy){
76     if(policy.itemContent != undefined){
77         $scope.refreshCheck = true; 
78         $scope.policyNavigator = policy.itemContent;
79         policy.itemContent = "";
80     }
81         $scope.savebutton = false;
82         var uuu = "policycreation/save_policy";
83     var postData={policyData: policy};
84     $.ajax({
85     type : 'POST',
86     url : uuu,
87     dataType: 'json',
88     contentType: 'application/json',
89     data: JSON.stringify(postData),
90     success : function(data){
91         $scope.$apply(function(){
92         $scope.data=data.policyData;
93         if($scope.data == 'success'){
94             $scope.temp.policy.readOnly = 'true';
95             Notification.success("Policy Saved Successfully."); 
96         }else if ($scope.data == 'PolicyExists'){
97             $scope.savebutton = true;
98             Notification.error("Policy Already Exists with Same Name in Scope.");
99         }
100         });
101     },
102     error : function(data){
103         Notification.error("Error Occured while saving Policy.");
104         $scope.savebutton = true;
105     }
106     });
107     };
108     
109     $scope.validatePolicy = function(policy){
110     document.getElementById("validate").innerHTML = "";
111          var uuu = "policyController/validate_policy.htm";
112      var postData={policyData: policy};
113      $.ajax({
114      type : 'POST',
115      url : uuu,
116      dataType: 'json',
117      contentType: 'application/json',
118      data: JSON.stringify(postData),
119      success : function(data){
120          $scope.$apply(function(){
121          $scope.validateData = data.data.replace(/\"/g, "");
122             $scope.data=data.data.substring(1,8);
123              var size = data.data.length;
124              if($scope.data == 'success'){
125              Notification.success("Validation Success.");
126              $scope.savebutton = false;
127              if (size > 18){
128                  var displayWarning = data.data.substring(19,size);
129                  document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
130                  document.getElementById("validate").style.color = "white";
131                  document.getElementById("validate").style.backgroundColor = "skyblue";
132              }  
133              }else{
134              Notification.error("Validation Failed.");
135              document.getElementById("validate").innerHTML = $scope.validateData;
136              document.getElementById("validate").style.color = "white";
137              document.getElementById("validate").style.backgroundColor = "red";
138              $scope.savebutton = true;
139              }
140              
141          });
142      },
143      error : function(data){
144          Notification.error("Validation Failed.");
145      }
146      });
147     };
148     
149     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
150     $scope.temp.policy.attributes = [];
151     }else{
152     if($scope.temp.policy.attributes.length == 0){
153        $scope.temp.policy.attributes = [];
154     }
155    }
156     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
157     $scope.addNewChoice = function() {
158       var newItemNo = $scope.temp.policy.attributes.length+1;
159       $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
160     };    
161     $scope.removeChoice = function() {
162       var lastItem = $scope.temp.policy.attributes.length-1;
163       $scope.temp.policy.attributes.splice(lastItem);
164     };
165 }]);