Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / controller / dictionaryController / FWAddressGroupDictController.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
21 /**
22  */
23 var editFWAddressGroupController =  function ($scope, $modalInstance, message, FWDictionaryService, PapUrlService, UserInfoService, Notification){
24     if(message.addressGroupDictionaryData==null)
25         $scope.label='Add Address Group',
26         $scope.apchoices = [];
27     else{
28         $scope.label='Edit Address Group'
29         $scope.disableCd=true;
30         $scope.apchoices = [];
31         var headers = message.addressGroupDictionaryData.prefixList;
32         var splitEqual = ',';
33         if(headers != null){
34                 if (headers.indexOf(splitEqual) >= 0) {
35                         var splitValue = headers.split(splitEqual);
36                         for(i = 0; i < splitValue.length; i++){
37                                 var key  = splitValue[i];
38                                 $scope.apchoices.push({'id':'choice'+i+1, 'option': key});
39                         }
40                 }else{
41                         var key  = headers;
42                         $scope.apchoices.push({'id':'choice'+1, 'option': key});
43                 }
44         }
45     }
46     
47         var papUrl;
48         PapUrlService.getPapUrl().then(function(data) {
49                 var config = data;
50                 papUrl = config.PAP_URL;
51                 console.log(papUrl);
52                 
53             FWDictionaryService.getPrefixListDictionaryDataByName(papUrl).then(function (data) {
54                 var j = data;
55                 $scope.data = JSON.parse(j.data);
56                 console.log($scope.data);
57                 $scope.prefixListDictionaryDatas = JSON.parse($scope.data.prefixListDictionaryDatas);
58                 console.log($scope.prefixListDictionaryDatas);
59             }, function (error) {
60                 console.log("failed");
61             });
62             
63         });
64         
65         /*getting user info from session*/
66         var loginId = null;
67         UserInfoService.getFunctionalMenuStaticDetailSession()
68                 .then(function (response) {                     
69                         loginId = response.userid;              
70          });
71     
72     $scope.editAddressGroup = message.addressGroupDictionaryData;
73
74     $scope.saveFWAddressGroup = function(addressGroupDictionaryData) {
75         var finalData = extend(addressGroupDictionaryData, $scope.attributeDatas[0]);
76         var uuu = papUrl + "/ecomp/fw_dictionary/save_addressGroup.htm";
77         var postData={addressGroupDictionaryData: finalData, loginId: loginId};
78         $.ajax({
79             type : 'POST',
80             url : uuu,
81             dataType: 'json',
82             contentType: 'application/json',
83             data: JSON.stringify(postData),
84             success : function(data){
85                 $scope.$apply(function(){
86                     $scope.addressGroupDictionaryDatas=data.addressGroupDictionaryDatas;});
87                 if($scope.addressGroupDictionaryDatas == "Duplicate"){
88                         Notification.error("FW AddressGroup Dictionary exists with Same Address group Name.")
89                 }else{      
90                         console.log($scope.addressGroupDictionaryDatas);
91                     $modalInstance.close({addressGroupDictionaryDatas:$scope.addressGroupDictionaryDatas});
92                 }    
93             },
94             error : function(data){
95                 alert("Error while saving.");
96             }
97         });
98     };
99
100     $scope.close = function() {
101         $modalInstance.close();
102     };
103     
104     function extend(obj, src) {
105         for (var key in src) {
106             if (src.hasOwnProperty(key)) obj[key] = src[key];
107         }
108         return obj;
109     }
110     
111     $scope.attributeDatas = [{"attributes" : $scope.apchoices}];
112     $scope.addAPNewChoice = function() {
113         var newItemNo = $scope.apchoices.length+1;
114         $scope.apchoices.push({'id':'choice'+newItemNo});
115     };
116     $scope.removeAPChoice = function() {
117         var lastItem = $scope.apchoices.length-1;
118         $scope.apchoices.splice(lastItem);
119     };
120 }