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