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