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