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