[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / SafePolicyWarningDictController.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('editSafePolicyWarningController' ,  function ($scope, $modalInstance, message, PolicyAppService, UserInfoServiceDS2, Notification){
21         $scope.edit = false;
22     if(message.safePolicyWarningData==null)
23         $scope.label='Add New Safe Policy Warning'
24     else{
25         $scope.label='Edit Safe Policy Warning'
26         $scope.disableCd=true;
27         $scope.edit = true;
28     }
29             
30     PolicyAppService.getData('getDictionary/get_SafePolicyWarningDataByName').then(function (data) {
31         var j = data;
32         $scope.data = JSON.parse(j.data);
33         console.log($scope.data);
34         $scope.safePolicyWarningDatas = JSON.parse($scope.data.safePolicyWarningDatas);
35         console.log($scope.safePolicyWarningDatas);
36     }, function (error) {
37         console.log("failed");
38     });
39
40     PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
41         var j = data;
42         $scope.data = JSON.parse(j.data);
43         console.log($scope.data);
44         $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
45         console.log($scope.riskTypeDictionaryDatas);
46     }, function (error) {
47         console.log("failed");
48     });
49         
50         /*getting user info from session*/
51         var userid = null;
52         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
53                 .then(function (response) {                     
54                         userid = response.userid;               
55          });
56    
57     $scope.editSafePolicyWarning = message.safePolicyWarningData;
58     $scope.editSafePolicyWarning1 = {resource: [], type:[], service: [], closedloop: []};
59     if($scope.edit){
60         if(message.safePolicyWarningData.groupList != null){
61                 var splitValue = message.safePolicyWarningData.groupList.split(",");
62                 console.log(splitValue);
63                 $scope.splittedGroupListValues = [];
64                 var splitResource = splitValue[0].split("=");
65                 $scope.editSafePolicyWarningScope1.riskType.push(splitResource[1]);
66         }       
67     }
68     
69     $scope.saveSafePolicyWarning = function(safePolicyWarningData) {
70         var regex = new RegExp("^[a-zA-Z0-9_]*$");
71         if(!regex.test(safePolicyWarningData.name)) {
72                 Notification.error("Enter Valid Safe Policy Warning Name without spaces or special characters");
73         }else{
74                 console.log(safePolicyWarningData);
75                 var uuu = "saveDictionary/sp_dictionary/save_safePolicyWarning";
76                 var postData={safePolicyWarningData: safePolicyWarningData, userid: userid};
77                 $.ajax({
78                         type : 'POST',
79                         url : uuu,
80                         dataType: 'json',
81                         contentType: 'application/json',
82                         data: JSON.stringify(postData),
83                         success : function(data){
84                                 $scope.$apply(function(){
85                                         $scope.safePolicyWarningDatas=data.safePolicyWarningDatas;});
86                                 if($scope.safePolicyWarningDatas == "Duplicate"){
87                                         Notification.error("Safe Policy Dictionary exists with Same Name.")
88                                 }else{      
89                                         console.log($scope.safePolicyWarningDatas);
90                                         $modalInstance.close({safePolicyWarningDatas:$scope.safePolicyWarningDatas});
91                                 }
92                         },
93                         error : function(data){
94                                 alert("Error while saving.");
95                         }
96                 });
97         }
98     };
99
100     $scope.close = function() {
101         $modalInstance.close();
102     };
103 });