[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryGridController / attributeDictGridController.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('attributeDictGridController', function ($scope, PolicyAppService, modalService, $modal){
21         $( "#dialog" ).hide();
22
23         PolicyAppService.getData('getDictionary/get_AttributeData').then(function (data) {
24                 var j = data;
25                 $scope.data = JSON.parse(j.data);
26                 console.log($scope.data);
27                 $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
28                 console.log($scope.attributeDictionaryDatas);
29         }, function (error) {
30                 console.log("failed");
31         });
32
33
34         $scope.exportData = function () {
35                 var blob = new Blob([document.getElementById('attributeTable').innerHTML], {
36                         type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
37                 });
38                 saveAs(blob, "Attribute.xls");
39         };
40
41         PolicyAppService.getData('get_LockDownData').then(function(data){
42                 var j = data;
43                 $scope.data = JSON.parse(j.data);
44                 $scope.lockdowndata = JSON.parse($scope.data.lockdowndata);
45                 if($scope.lockdowndata[0].lockdown == true){
46                         $scope.attributeDictionaryGrid.columnDefs[0].visible = false;
47                         $scope.gridApi.grid.refresh();
48                 }else{
49                         $scope.attributeDictionaryGrid.columnDefs[0].visible = true;
50                         $scope.gridApi.grid.refresh();
51                 }
52         },function(error){
53                 console.log("failed");
54         });
55
56         $scope.attributeDictionaryGrid = {
57                         data : 'attributeDictionaryDatas',
58                         enableFiltering: true,
59                         exporterCsvFilename: 'Attribute.csv',
60                         enableGridMenu: true,
61                         enableSelectAll: true,
62                         columnDefs: [{
63                                 field: 'id' , enableFiltering: false, headerCellTemplate: '' +
64                                 '<button id=\'New\' ng-click="grid.appScope.createNewAttributeWindow()" class="btn btn-success">' + 'Create</button>',
65                                 cellTemplate:
66                                         '<button  type="button"  class="btn btn-primary"  ng-click="grid.appScope.editAttributeWindow(row.entity)"><i class="fa fa-pencil-square-o"></i></button> ' +
67                                         '<button  type="button"  class="btn btn-danger"  ng-click="grid.appScope.deleteAttribute(row.entity)" ><i class="fa fa-trash-o"></i></button> ',  width: '8%'
68                         },{ field: 'xacmlId', displayName : 'Attribute ID', sort: { direction: 'asc', priority: 0 }},
69                         { field: 'datatypeBean.shortName', displayName : 'Data Type' },
70                         { field: 'attributeValue'},
71                         { field: 'description' },
72                         { field: 'priority' , displayName : 'Priority'},
73                         {field: 'userCreatedBy.userName', displayName : 'Created By' },
74                         {field: 'userModifiedBy.userName', displayName : 'Modified By'},
75                         {field: 'createdDate',type: 'date', cellFilter: 'date:\'yyyy-MM-dd\''  },
76                         {field: 'modifiedDate',type: 'date', cellFilter: 'date:\'yyyy-MM-dd\''  }
77                         ],
78                         exporterMenuPdf: false,
79                         exporterPdfDefaultStyle: {fontSize: 9},
80                         exporterPdfTableStyle: {margin: [30, 30, 30, 30]},
81                         exporterPdfTableHeaderStyle: {fontSize: 10, bold: true, italics: true, color: 'red'},
82                         exporterPdfHeader: { text: "My Header", style: 'headerStyle' },
83                         exporterPdfFooter: function ( currentPage, pageCount ) {
84                                 return { text: currentPage.toString() + ' of ' + pageCount.toString(), style: 'footerStyle' };
85                         },
86                         exporterPdfCustomFormatter: function ( docDefinition ) {
87                                 docDefinition.styles.headerStyle = { fontSize: 22, bold: true };
88                                 docDefinition.styles.footerStyle = { fontSize: 10, bold: true };
89                                 return docDefinition;
90                         },
91                         exporterFieldCallback: function(grid, row, col, input) {
92                  if( col.name == 'createdDate' || col.name == 'modifiedDate') {
93                          var date = new Date(input);
94                          return date.toString("yyyy-MM-dd HH:MM:ss a");
95                  } else {
96                          return input;
97                  }
98             },
99                         exporterPdfOrientation: 'portrait',
100                         exporterPdfPageSize: 'LETTER',
101                         exporterPdfMaxGridWidth: 500,
102                         exporterCsvLinkElement: angular.element(document.querySelectorAll(".custom-csv-link-location")),
103                         onRegisterApi: function(gridApi){
104                                 $scope.gridApi = gridApi;
105                         }
106         };
107
108
109         $scope.editAttributeName = null;
110         $scope.createNewAttributeWindow = function(){
111                 $scope.editAttributeName = null;
112                 var modalInstance = $modal.open({
113                         backdrop: 'static', keyboard: false,
114                         templateUrl : 'add_Attribute_popup.html',
115                         controller: 'editAttributeController',
116                         resolve: {
117                                 message: function () {
118                                         var message = {
119                                                         attributeDictionaryDatas: $scope.editAttributeName
120                                         };
121                                         return message;
122                                 }
123                         }
124                 });
125                 modalInstance.result.then(function(response){
126                         console.log('response', response);
127                         $scope.attributeDictionaryDatas=response.attributeDictionaryDatas;
128                 });
129         };
130
131         $scope.editAttributeWindow = function(attributeDictionaryData) {
132                 $scope.editAttributeName = attributeDictionaryData;
133                 var modalInstance = $modal.open({
134                         backdrop: 'static', keyboard: false,
135                         templateUrl : 'add_Attribute_popup.html',
136                         controller: 'editAttributeController',
137                         resolve: {
138                                 message: function () {
139                                         var message = {
140                                                         attributeDictionaryData: $scope.editAttributeName
141                                         };
142                                         return message;
143                                 }
144                         }
145                 });
146                 modalInstance.result.then(function(response){
147                         console.log('response', response);
148                         $scope.attributeDictionaryDatas = response.attributeDictionaryDatas;
149                 });
150         };
151
152         $scope.deleteAttribute = function(data) {
153                 var uuu = "searchDictionary";
154                 var postData={data: data, type: "attribute"};
155                 var searchString = "\n";
156                 $.ajax({
157                         type : 'POST',
158                         url : uuu,
159                         dataType: 'json',
160                         contentType: 'application/json',
161                         data: JSON.stringify(postData),
162                         success : function(resultList){
163                                 $scope.$apply(function(){
164                                         $scope.list =resultList.result;});
165                                 $scope.searchData = JSON.stringify(resultList.result);
166                                 $scope.searchDatas = JSON.parse($scope.searchData);        
167                                 $scope.success = true;
168                                 var i;
169                                 if($scope.searchDatas.length > 0){
170                                         for(i = 0 ; i < $scope.searchDatas.length; i++){
171                                                 searchString += $scope.searchDatas[i] + "\n";
172                                         }       
173                                 }else{
174                                         searchString += "No Policies is Using this Value"
175                                 }
176
177                                 console.log($scope.list);
178                                 if($scope.success){
179                                         modalService.popupConfirmWin("Confirm","You are about to delete the Attribute Dictionary :  "+data.xacmlId+". \n "+searchString+" \n Do you want to continue?",
180                                                         function(){
181                                                 var uuu = "deleteDictionary/attribute_dictionary/remove_attribute";
182                                                 var postData={data: data};
183                                                 $.ajax({
184                                                         type : 'POST',
185                                                         url : uuu,
186                                                         dataType: 'json',
187                                                         contentType: 'application/json',
188                                                         data: JSON.stringify(postData),
189                                                         success : function(data){
190                                                                 $scope.$apply(function(){$scope.attributeDictionaryDatas=data.attributeDictionaryDatas;});
191                                                         },
192                                                         error : function(data){
193                                                                 console.log(data);
194                                                                 modalService.showFailure("Fail","Error while deleting: "+ data.responseText);
195                                                         }
196                                                 });
197
198                                         })}
199                         },
200                         error : function(data){
201                                 alert("Error while Searching.");
202                         }
203                 });
204         };
205
206
207 });