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