Remove unused vars and methods
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / ActionPolicyController.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 app.controller('actionPolicyController', ['$scope', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', function ($scope, PolicyAppService, PolicyNavigator, modalService, $modal, Notification) {
21     $("#dialog").hide();
22     
23     $scope.policyNavigator;
24     $scope.savebutton = true;
25     $scope.refreshCheck = false;
26         
27     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
28         $scope.temp.policy = {
29                         policyType : "Action"
30         }
31     }
32     
33     $scope.refresh = function(){
34         if($scope.refreshCheck){
35                 $scope.policyNavigator.refresh();
36         }
37         $scope.modal('createNewPolicy', true);
38         $scope.temp.policy = "";
39     }
40     
41     $scope.modal = function(id, hide) {
42         return $('#' + id).modal(hide ? 'hide' : 'show');
43     };
44     
45     PolicyAppService.getData('getDictionary/get_ActionPolicyDictDataByName').then(function (data) {
46         var j = data;
47         $scope.data = JSON.parse(j.data);
48         console.log($scope.data);
49         $scope.actionPolicyDictionaryDatas = JSON.parse($scope.data.actionPolicyDictionaryDatas);
50         console.log($scope.actionPolicyDictionaryDatas);
51     }, function (error) {
52         console.log("failed");
53     });
54
55     PolicyAppService.getData('getDictionary/get_AttributeDatabyAttributeName').then(function (data) {
56         var j = data;
57         $scope.data = JSON.parse(j.data);
58         console.log($scope.data);
59         $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
60         console.log($scope.attributeDictionaryDatas);
61     }, function (error) {
62         console.log("failed");
63     });
64
65     PolicyAppService.getData('get_FunctionDefinitionDataByName').then(function (data) {
66         var j = data;
67         $scope.data = JSON.parse(j.data);
68         console.log($scope.data);
69         $scope.functionDefinitionDatas = JSON.parse($scope.data.functionDefinitionDatas);
70         console.log($scope.functionDefinitionDatas);
71     }, function (error) {
72         console.log("failed");
73     });
74     
75     $scope.saveActionPolicy = 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;
123                                                 if($scope.data == '"success"'){
124                                                         Notification.success("Validation Success.");
125                                                         $scope.savebutton = false;
126                                                 }else{
127                                                         Notification.error("Validation Failed.");
128                                                         document.getElementById("validate").innerHTML = $scope.validateData;
129                                                         document.getElementById("validate").style.color = "white";
130                                                         document.getElementById("validate").style.backgroundColor = "red";
131                                                         $scope.savebutton = true;
132                                                 }
133                                                 
134                                 });
135                         },
136                         error : function(data){
137                                 Notification.error("Validation Failed.");
138                                 $scope.savebutton = true;
139                         }
140                 });
141     };
142
143     if(!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly){
144         $scope.temp.policy.attributes = [];
145         $scope.temp.policy.attributes.push({'id':'choice'+1});
146          $scope.temp.policy.ruleAlgorithmschoices = [];
147     }else{
148            if($scope.temp.policy.attributes.length == 0){
149                    $scope.temp.policy.attributes = [];
150            }
151            if($scope.temp.policy.ruleAlgorithmschoices.length == 0){
152                    $scope.temp.policy.ruleAlgorithmschoices = [];
153            }
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     
166    
167     $scope.ItemNo = 0;
168     $scope.ruleAlgorithmDatas = [{"ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices}];
169     
170     $scope.addNewRuleAlgorithm = function(){
171       var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length+1;
172       $scope.ItemNo = newItemNo;
173         if(newItemNo > 1){
174             var value = newItemNo-1;
175             $scope.attributeDictionaryDatas.push('A'+value);
176         }
177       $scope.temp.policy.ruleAlgorithmschoices.push({'id':'A'+newItemNo});
178
179     };
180     
181     $scope.removeRuleAlgorithm = function() {
182       var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length-1;
183       $scope.ItemNo = lastItem; 
184       $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
185     };
186     
187 }]);