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