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