b456c57a02984c2704dee5539c3118b3c9abbc72
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / PSGroupPolicyScopeDictController.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('editPSGroupPolicyScopeController' ,  function ($scope, $modalInstance, message, PolicyAppService, UserInfoServiceDS2, Notification){
21         $scope.edit = false;
22     if(message.groupPolicyScopeListData==null)
23         $scope.label='Add New Group Policy Scope'
24     else{
25         $scope.label='Edit Group Policy Scope'
26         $scope.disableCd=true;
27         $scope.edit = true;
28     }
29                 
30     PolicyAppService.getData('getDictionary/get_PSServiceDataByName').then(function (data) {
31         var j = data;
32         $scope.data = JSON.parse(j.data);
33         console.log($scope.data);
34         $scope.psServiceDictionaryDatas = JSON.parse($scope.data.psServiceDictionaryDatas);
35         console.log($scope.psServiceDictionaryDatas);
36     }, function (error) {
37         console.log("failed");
38     });
39
40     PolicyAppService.getData('getDictionary/get_PSTypeDataByName').then(function (data) {
41         var j = data;
42         $scope.data = JSON.parse(j.data);
43         console.log($scope.data);
44         $scope.psTypeDictionaryDatas = JSON.parse($scope.data.psTypeDictionaryDatas);
45         console.log($scope.psTypeDictionaryDatas);
46     }, function (error) {
47         console.log("failed");
48     });
49
50     PolicyAppService.getData('getDictionary/get_PSResourceDataByName').then(function (data) {
51         var j = data;
52         $scope.data = JSON.parse(j.data);
53         console.log($scope.data);
54         $scope.psResourceDictionaryDatas = JSON.parse($scope.data.psResourceDictionaryDatas);
55         console.log($scope.psResourceDictionaryDatas);
56     }, function (error) {
57         console.log("failed");
58     });
59
60     PolicyAppService.getData('getDictionary/get_PSClosedLoopDataByName').then(function (data) {
61         var j = data;
62         $scope.data = JSON.parse(j.data);
63         console.log($scope.data);
64         $scope.psClosedLoopDictionaryDatas = JSON.parse($scope.data.psClosedLoopDictionaryDatas);
65         console.log($scope.psClosedLoopDictionaryDatas);
66     }, function (error) {
67         console.log("failed");
68     });
69         
70         /*getting user info from session*/
71         var userid = null;
72         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
73                 .then(function (response) {                     
74                         userid = response.userid;               
75          });
76    
77     $scope.editPSGroupPolicyScope = message.groupPolicyScopeListData;
78     $scope.editPSGroupPolicyScope1 = {resource: [], type:[], service: [], closedloop: []};
79     if($scope.edit){
80         if(message.groupPolicyScopeListData.groupList != null){
81                 var splitValue = message.groupPolicyScopeListData.groupList.split(",");
82                 console.log(splitValue);
83                 $scope.splittedGroupListValues = [];
84                 var splitResource = splitValue[0].split("=");
85                 $scope.editPSGroupPolicyScope1.resource.push(splitResource[1]);
86                 var splitType = splitValue[1].split("=");
87                 $scope.editPSGroupPolicyScope1.type.push(splitType[1]);
88                 var splitService = splitValue[2].split("=");
89                 $scope.editPSGroupPolicyScope1.service.push(splitService[1]);
90                 var splitCloop = splitValue[3].split("=");
91                 $scope.editPSGroupPolicyScope1.closedloop.push(splitCloop[1]);
92         }       
93     }
94     
95     $scope.savePSGroupPolicyScope = function(groupPolicyScopeListData, groupPolicyScopeListData1) {
96         var regex = new RegExp("^[a-zA-Z0-9_]*$");
97         if(!regex.test(groupPolicyScopeListData.groupName)) {
98                 Notification.error("Enter Valid Policy Scope Group Name without spaces or special characters");
99         }else{
100                 console.log(groupPolicyScopeListData1);
101                 if(groupPolicyScopeListData1.resource[0] != undefined && groupPolicyScopeListData1.type[0] != undefined && groupPolicyScopeListData1.service[0] != undefined && groupPolicyScopeListData1.closedloop[0] != undefined){
102                         var uuu = "saveDictionary/ps_dictionary/save_psGroupPolicyScope";
103                         var postData={groupPolicyScopeListData: groupPolicyScopeListData,
104                                         groupPolicyScopeListData1: groupPolicyScopeListData1, userid: userid};
105                         $.ajax({
106                                 type : 'POST',
107                                 url : uuu,
108                                 dataType: 'json',
109                                 contentType: 'application/json',
110                                 data: JSON.stringify(postData),
111                                 success : function(data){
112                                         $scope.$apply(function(){
113                                                 $scope.groupPolicyScopeListDatas=data.groupPolicyScopeListDatas;});
114                                         if($scope.groupPolicyScopeListDatas == "Duplicate"){
115                                                 Notification.error("GroupPolicyScope Dictionary exists with Same Group Name.")
116                                         }else if($scope.groupPolicyScopeListDatas == "DuplicateGroup"){
117                                                 Notification.error("GroupPolicyScope Dictionary exists with Same Group List.")
118                                         }else{      
119                                                 console.log($scope.groupPolicyScopeListDatas);
120                                                 $modalInstance.close({groupPolicyScopeListDatas:$scope.groupPolicyScopeListDatas});
121                                         }
122                                 },
123                                 error : function(data){
124                                         Notification.error("Error while saving.");
125                                 }
126                         });
127                 }else{
128                         Notification.error("Please Select all the required fields to Save");
129                 }
130         }
131     };
132
133     $scope.close = function() {
134         $modalInstance.close();
135     };
136 });