Added Policy GUI Cosmetic Fixes
[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 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     function extend(obj, src) {
76         for (var key in src) {
77             if (src.hasOwnProperty(key)) obj[key] = src[key];
78         }
79         return obj;
80     }
81     
82     $scope.saveActionPolicy = function(policy){
83         if(policy.itemContent != undefined){
84                 $scope.refreshCheck = true; 
85                 $scope.policyNavigator = policy.itemContent;
86                 policy.itemContent = "";
87         }
88         $scope.savebutton = false;
89         var uuu = "policycreation/save_policy";
90                 var postData={policyData: policy};
91                 $.ajax({
92                         type : 'POST',
93                         url : uuu,
94                         dataType: 'json',
95                         contentType: 'application/json',
96                         data: JSON.stringify(postData),
97                         success : function(data){
98                                 $scope.$apply(function(){
99                                         $scope.data=data.policyData;
100                                         if($scope.data == 'success'){
101                                                 $scope.temp.policy.readOnly = 'true';
102                                                 Notification.success("Policy Saved Successfully.");     
103                                         }else if ($scope.data == 'PolicyExists'){
104                                                 $scope.savebutton = true;
105                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
106                                         }
107                                 });
108                         },
109                         error : function(data){
110                                 Notification.error("Error Occured while saving Policy.");
111                                 $scope.savebutton = true;
112                         }
113                 });
114     };
115
116     $scope.validatePolicy = function(policy){
117         document.getElementById("validate").innerHTML = "";
118         var uuu = "policyController/validate_policy.htm";
119                 var postData={policyData: policy};
120                 $.ajax({
121                         type : 'POST',
122                         url : uuu,
123                         dataType: 'json',
124                         contentType: 'application/json',
125                         data: JSON.stringify(postData),
126                         success : function(data){
127                                 $scope.$apply(function(){
128                                         $scope.validateData = data.data.replace(/\"/g, "");
129                                                 $scope.data=data.data;
130                                                 if($scope.data == '"success"'){
131                                                         Notification.success("Validation Success.");
132                                                         $scope.savebutton = false;
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                                 $scope.savebutton = true;
146                         }
147                 });
148     };
149
150     if(!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly){
151         $scope.temp.policy.attributes = [];
152         $scope.temp.policy.attributes.push({'id':'choice'+1});
153          $scope.temp.policy.ruleAlgorithmschoices = [];
154     }else{
155            if($scope.temp.policy.attributes.length == 0){
156                    $scope.temp.policy.attributes = [];
157            }
158            if($scope.temp.policy.ruleAlgorithmschoices.length == 0){
159                    $scope.temp.policy.ruleAlgorithmschoices = [];
160            }
161    }
162   
163     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
164     $scope.addNewChoice = function() {
165         var newItemNo = $scope.temp.policy.attributes .length+1;
166         $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
167     };
168     $scope.removeChoice = function() {
169         var lastItem = $scope.temp.policy.attributes.length-1;
170         $scope.temp.policy.attributes.splice(lastItem);
171     };
172     
173    
174     $scope.ItemNo = 0;
175     $scope.ruleAlgorithmDatas = [{"ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices}];
176     
177     $scope.addNewRuleAlgorithm = function(){
178       var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length+1;
179       $scope.ItemNo = newItemNo;
180         if(newItemNo > 1){
181             var value = newItemNo-1;
182             $scope.attributeDictionaryDatas.push('A'+value);
183         }
184       $scope.temp.policy.ruleAlgorithmschoices.push({'id':'A'+newItemNo});
185
186     };
187     
188     $scope.removeRuleAlgorithm = function() {
189       var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length-1;
190       $scope.ItemNo = lastItem; 
191       $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
192     };
193     
194 }]);