Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-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
21 /**
22  */
23 var editAttributeController =  function ($scope, $modalInstance, message, PapUrlService, UserInfoService, Notification){
24     if(message.attributeDictionaryData==null)
25         $scope.label='Add New Attribute',
26         $scope.attributes = [];
27     else{
28         $scope.attributes = [];
29         $scope.label='Edit Attribute'
30         $scope.disableCd=true;
31         var headers = message.attributeDictionaryData.attributeValue;
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.attributes.push({'id':'choice'+i+1, 'attributeValues': key});
39                         }
40                 }else{
41                  var key  = headers;
42                  $scope.attributes.push({'id':'choice'+1, 'attributeValues': 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         
54         /*getting user info from session*/
55         var loginId = null;
56         UserInfoService.getFunctionalMenuStaticDetailSession()
57                 .then(function (response) {                     
58                         loginId = response.userid;              
59          });
60         
61     $scope.editAttributeName = message.attributeDictionaryData;
62
63     $scope.saveAttributeName = function(attributeDictionaryData) {
64         var finalData = extend(attributeDictionaryData, $scope.attributeDatas[0]);
65         var uuu = papUrl + "/ecomp/attribute_dictionary/save_attribute.htm";
66         var postData={attributeDictionaryData: attributeDictionaryData, loginId: loginId};
67         $.ajax({
68             type : 'POST',
69             url : uuu,
70             dataType: 'json',
71             contentType: 'application/json',
72             data: JSON.stringify(postData),
73             success : function(data){
74                 $scope.$apply(function(){
75                     $scope.attributeDictionaryDatas=data.attributeDictionaryDatas;});
76                                         if($scope.attributeDictionaryDatas == "Duplicate"){
77                                                 Notification.error("Attribute Dictionary exists with Same Attribute Name.")
78                                         }else{      
79                                                 console.log($scope.attributeDictionaryDatas);
80                                                 $modalInstance.close({attributeDictionaryDatas:$scope.attributeDictionaryDatas});
81                                         }
82             },
83             error : function(data){
84                 alert("Error while saving.");
85             }
86         });
87     };
88
89     function extend(obj, src) {
90         for (var key in src) {
91             if (src.hasOwnProperty(key)) obj[key] = src[key];
92         }
93         return obj;
94     }
95     
96     $scope.attributeDatas = [{"userDataTypeValues" : $scope.attributes}];
97     $scope.addNewChoice = function() {
98       var newItemNo = $scope.attributes.length+1;
99       $scope.attributes.push({'id':'choice'+newItemNo});
100     };    
101     $scope.removeChoice = function() {
102       var lastItem = $scope.attributes.length-1;
103       $scope.attributes.splice(lastItem);
104     };
105     
106     $scope.close = function() {
107         $modalInstance.close();
108     };
109 }