Deliver centralized role management feature
[portal.git] / ecomp-portal-FE-os / client / src / views / applications / applications.controller.js
1 /*-
2  * ================================================================================
3  * eCOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 'use strict';
21 (function () {
22     class ApplicationsCtrl {
23
24         constructor($log, $cookies, conf, ngDialog,
25                                 applicationsService, confirmBoxService, userProfileService, utilsService,$modal) {
26             this.emptyImgForPreview = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
27             let getOnboardingApps = () => {
28                 this.isLoadingTable = true;
29                 applicationsService.getOnboardingApps()
30                     .then(appsList => {
31                         // Use proper back-end URL to load image
32                         for (var i = 0; i < appsList.length; i++) {
33                                 appsList[i].imageLink = '';
34                                 if (appsList[i].imageUrl){
35                                         appsList[i].imageLink = conf.api.appThumbnail.replace(':appId', appsList[i].id);
36                                         appsList[i].imageLink = appsList[i].imageLink+'?' + new Date().getTime();
37                                 }
38                         }
39                         this.appsList = appsList;
40                     }).catch(err => {
41                         confirmBoxService.showInformation('There was a problem retrieving the Applications. ' +
42                             'Please try again later. Error Status: '+ err.status).then(isConfirmed => {});
43                         $log.error('ApplicationsCtrl:openAddNewAppModal: Error: ', err);
44                     }).finally(()=> {
45                         this.isLoadingTable = false;
46                     });
47             };
48
49             let init = () => {
50                 this.isLoadingTable = false;
51                 getOnboardingApps();
52                 this.searchString = '';
53                 this.appsTableHeaders = [
54                     {name: 'Application Name', value: 'name', isSortable: true},
55                     {name: 'Active', value: 'isEnabled', isSortable: true},
56                     {name: 'Integration Type', value: 'restrictedApp', isSortable: true},
57                     {name: 'Guest Access', value: 'isOpen', isSortable: true},
58                     {name: 'URL', value: 'url', isSortable: true},
59                     {name: 'REST URL', value: 'restUrl', isSortable: true},                
60                     {name: 'Communication Topic', value: 'uebTopicName', isSortable: true},
61                     {name: 'Communication Key', value: 'uebKey', isSortable: true},
62                     {name: 'Communication Secret', value: 'uebSecret', isSortable: true},
63                     {name: 'Application Namespace', value: 'nameSpace', isSortable: true},
64                     {name: 'Central Access Type', value: 'isCentralAuth', isSortable: true}
65                 ];
66                 this.appsList = [];
67             };
68
69             init();
70
71             this.openAddNewAppModal = (selectedApp) => {
72                 let data = null;
73                 if (selectedApp) {
74                     if (!selectedApp.id) {
75                         $log.error('App id not found');
76                         return;
77                     }
78                     data = {
79                         app: selectedApp
80                     }
81                 }
82                 var modalInstance = $modal.open({
83                     templateUrl: 'app/views/applications/application-details-dialog/application-details.modal.html',
84                     controller: 'AppDetailsModalCtrl as appDetails',
85                     sizeClass: 'modal-large', 
86                     resolve: {
87                                         items: function () {
88                                   return data;
89                                         }
90                         }
91                 })
92                 
93                 modalInstance.result.finally(function (){
94                                 getOnboardingApps();
95                 });
96
97             };
98
99             this.deleteApp = application => {
100                 $log.debug('ApplicationsCtrl:deleteApp:: ', application.name);
101
102                 confirmBoxService.deleteItem(application.name).then(isConfirmed => {
103                     if(isConfirmed){
104                         if(!application || !application.id){
105                             $log.error('ApplicationsCtrl:deleteApp:: No application or ID... cannot delete');
106                             return;
107                         }
108                         applicationsService.deleteOnboardingApp(application.id).then(() => {
109                             this.appsList.splice(this.appsList.indexOf(application), 1);
110                         }).catch(err => {
111                             confirmBoxService.showInformation('There was a problem deleting the Application. ' +
112                                 'Please try again later. Error Status: '+ err.status).then(isConfirmed => {});
113                             $log.error('ApplicationsCtrl:deleteApp: Error: ', err);
114                         });
115                     }
116                 }).catch(err => {
117                     confirmBoxService.showInformation('There was a problem deleting the Application. ' +
118                         'Please try again later. Error Status: '+ err.status).then(isConfirmed => {});
119                     $log.error('ApplicationsCtrl:deleteApp error:: ', err);
120                 });
121
122             };
123
124         }
125     }
126     ApplicationsCtrl.$inject = ['$log', '$cookies', 'conf', 'ngDialog',
127                                 'applicationsService', 'confirmBoxService', 'userProfileService', 'utilsService','$modal'];
128     angular.module('ecompApp').controller('ApplicationsCtrl', ApplicationsCtrl);
129 })();