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