51909162afcb5f0c3fb4427fb7409a95da4174d9
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / attributeDictController.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 app.controller('editAttributeController' ,function ($scope, $modalInstance, message, UserInfoServiceDS2, Notification){
21     if(message.attributeDictionaryData==null)
22         $scope.label='Add New Attribute',
23         $scope.attributes = [];
24     else{
25         $scope.attributes = [];
26         $scope.label='Edit Attribute'
27         $scope.disableCd=true;
28         var headers = message.attributeDictionaryData.attributeValue;
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.attributes.push({'id':'choice'+i+1, 'attributeValues': key});
36                         }
37                 }else{
38                  var key  = headers;
39                  $scope.attributes.push({'id':'choice'+1, 'attributeValues': key});
40             }
41         }
42     }
43     
44         /*getting user info from session*/
45         var userid = null;
46         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
47                 .then(function (response) {                     
48                         userid = response.userid;               
49          });
50         
51     $scope.editAttributeName = message.attributeDictionaryData;
52
53     $scope.saveAttributeName = function(attributeDictionaryData) {
54         var regex = new RegExp("^[a-zA-Z0-9_]*$");
55         if(!regex.test(attributeDictionaryData.xacmlId)) {
56                 Notification.error("Enter Valid Attribute Name without spaces or special characters");
57         }else{
58                 var finalData = extend(attributeDictionaryData, $scope.attributeDatas[0]);
59                 var uuu =  "saveDictionary/attribute_dictionary/save_attribute";
60                 var postData={attributeDictionaryData: attributeDictionaryData, userid: userid};
61                 $.ajax({
62                         type : 'POST',
63                         url : uuu,
64                         dataType: 'json',
65                         contentType: 'application/json',
66                         data: JSON.stringify(postData),
67                         success : function(data){
68                                 $scope.$apply(function(){
69                                         $scope.attributeDictionaryDatas=data.attributeDictionaryDatas;});
70                                 if($scope.attributeDictionaryDatas == "Duplicate"){
71                                         Notification.error("Attribute Dictionary exists with Same Attribute Name.")
72                                 }else{      
73                                         console.log($scope.attributeDictionaryDatas);
74                                         $modalInstance.close({attributeDictionaryDatas:$scope.attributeDictionaryDatas});
75                                 }
76                         },
77                         error : function(data){
78                                 alert("Error while saving.");
79                         }
80                 });
81         }       
82     };
83
84     function extend(obj, src) {
85         for (var key in src) {
86             if (src.hasOwnProperty(key)) obj[key] = src[key];
87         }
88         return obj;
89     }
90     
91     $scope.attributeDatas = [{"userDataTypeValues" : $scope.attributes}];
92     $scope.addNewChoice = function() {
93       var newItemNo = $scope.attributes.length+1;
94       $scope.attributes.push({'id':'choice'+newItemNo});
95     };    
96     $scope.removeChoice = function() {
97       var lastItem = $scope.attributes.length-1;
98       $scope.attributes.splice(lastItem);
99     };
100     
101     $scope.close = function() {
102         $modalInstance.close();
103     };
104 });