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