[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / FWServiceGroupDictController.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('editFWServiceGroupController' ,  function ($scope, $modalInstance, message, UserInfoServiceDS2, PolicyAppService, Notification){
21     if(message.serviceGroupDictionaryData==null){
22         $scope.slchoices = [];
23         $scope.label='Add Service Group'
24     }else{
25         $scope.label='Edit Service Group'
26         $scope.disableCd=true;
27         $scope.slchoices = [];
28         var headers = message.serviceGroupDictionaryData.serviceList;
29         var splitEqual = ',';
30         if(headers != null){
31                 if (headers.indexOf(splitEqual) >= 0) {
32                         var splitValue = headers.split(splitEqual);
33                         for(i = 0; i < splitValue.length; i++){
34                                 var key  = splitValue[i];
35                                 $scope.slchoices.push({'id':'choice'+i+1, 'option': key});
36                         }
37                 }else{
38                         var key  = headers;
39                         $scope.slchoices.push({'id':'choice'+1, 'option': key});
40                 }
41         }
42     }
43     
44                 
45     PolicyAppService.getData('getDictionary/get_ServiceListDictionaryDataByName').then(function (data) {
46         var j = data;
47         $scope.data = JSON.parse(j.data);
48         console.log($scope.data);
49         $scope.serviceListDictionaryDatas = JSON.parse($scope.data.serviceListDictionaryDatas);
50         console.log($scope.serviceListDictionaryDatas);
51     }, function (error) {
52         console.log("failed");
53     });
54
55         
56         /*getting user info from session*/
57         var userid = null;
58         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
59                 .then(function (response) {                     
60                         userid = response.userid;               
61          });
62         
63     $scope.editServiceGroup = message.serviceGroupDictionaryData;
64
65     $scope.saveFWServiceGroup = function(serviceGroupDictionaryData) {
66         var regex = new RegExp("^[a-zA-Z0-9_]*$");
67         if(!regex.test(serviceGroupDictionaryData.groupName)) {
68                 Notification.error("Enter Valid Service Group Name without spaces or special characters");
69         }else{
70                 var finalData = extend(serviceGroupDictionaryData, $scope.attributeDatas[0]);
71                 var uuu = "saveDictionary/fw_dictionary/save_serviceGroup";
72                 var postData={serviceGroupDictionaryData: finalData, userid: userid};
73                 $.ajax({
74                         type : 'POST',
75                         url : uuu,
76                         dataType: 'json',
77                         contentType: 'application/json',
78                         data: JSON.stringify(postData),
79                         success : function(data){
80                                 $scope.$apply(function(){
81                                         $scope.serviceGroupDictionaryDatas=data.serviceGroupDictionaryDatas;});
82                                 if($scope.serviceGroupDictionaryDatas == "Duplicate"){
83                                         Notification.error("FW Service Group Dictionary exists with Same Group Name.")
84                                 }else{      
85                                         console.log($scope.serviceGroupDictionaryDatas);
86                                         $modalInstance.close({serviceGroupDictionaryDatas:$scope.serviceGroupDictionaryDatas});
87                                 }
88                         },
89                         error : function(data){
90                                 alert("Error while saving.");
91                         }
92                 });
93         }
94     };
95
96     $scope.close = function() {
97         $modalInstance.close();
98     };
99     
100     function extend(obj, src) {
101         for (var key in src) {
102             if (src.hasOwnProperty(key)) obj[key] = src[key];
103         }
104         return obj;
105     }
106     
107    
108     
109     $scope.attributeDatas = [{"attributes" : $scope.slchoices}];
110     $scope.addServiceGroupNewChoice = function() {
111         var newItemNo = $scope.slchoices.length+1;
112         $scope.slchoices.push({'id':'choice'+newItemNo});
113     };
114     $scope.removeServiceGroupChoice = function() {
115         var lastItem = $scope.slchoices.length-1;
116         $scope.slchoices.splice(lastItem);
117     };
118 });