App onboarding fixes
[portal.git] / portal-FE-os / src / app / pages / application-onboarding / application-onboarding.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38
39 import { Component, OnInit, ViewChild, Input} from '@angular/core';
40 import { MatTableDataSource } from '@angular/material';
41 import { MatSort, MatPaginator } from '@angular/material';
42 import { ApplicationsService } from '../../shared/services/index';
43 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
44 import { IApplications } from 'src/app/shared/model/applications-onboarding/applications';
45 import { environment } from '../../../environments/environment';
46 import { ApplicationDetailsDialogComponent } from './application-details-dialog/application-details-dialog.component';
47 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
48 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
49
50 @Component({
51   selector: 'app-application-onboarding',
52   templateUrl: './application-onboarding.component.html',
53   styleUrls: ['./application-onboarding.component.scss']
54 })
55 export class ApplicationOnboardingComponent implements OnInit {
56
57   api = environment.api;
58   appsList: Array<IApplications> = [];
59   result: any;
60   isEditMode: boolean = false;
61   emptyImgForPreview: string;
62   isUserSuperAdmin: boolean = false;
63   displayedColumns: string[] = ['thumbnail', 'applicationName','active', 
64   'integrationType', 'modeOfIntegration', 'guestAccess', 'landingPage','restURL',
65   'communicationKey', 'applicationNamespace', 'centralAuthAccess'];
66   dataSource = new MatTableDataSource(this.appsList);
67   @ViewChild(MatSort) sort: MatSort;
68   @ViewChild(MatPaginator) paginator: MatPaginator;
69   showEcompSpinner:boolean = false;
70
71   constructor(public applicationsService: ApplicationsService, public ngbModal: NgbModal) { }
72
73   ngOnInit() {
74     this.emptyImgForPreview = '../../../assets/images/default_app_image.gif';
75     this.checkIfUserIsSuperAdmin();
76     this.getOnboardingApps();
77   }
78
79   getOnboardingApps(){
80     //console.log("getOnboardingApps called");
81     this.showEcompSpinner = true;
82     this.applicationsService.getOnboardingApps()
83       .subscribe(_data => {
84           this.result = _data;
85           if (this.result == null || this.result == 'undefined') {
86               console.log('WidgetOnboardingService::getOnboardingWidgets Failed: Result or result.data is null');
87               this.showEcompSpinner = false;
88           }else {
89             this.appsList = _data;
90             for (var i = 0; i < this.appsList.length; i++) {
91               this.appsList[i].imageLink = '';
92               if (this.appsList[i].imageUrl){
93                 this.appsList[i].imageLink = this.api.appThumbnail.replace(':appId', this.appsList[i].id);
94                 this.appsList[i].imageLink = this.appsList[i].imageLink+'?' + new Date().getTime();
95               }else{
96                 this.appsList[i].imageLink = this.emptyImgForPreview;
97               }                                         
98             }
99             this.populateTableData(this.appsList);
100             this.showEcompSpinner = false;
101           }
102       }, error =>{
103         console.log(error);
104         this.showEcompSpinner = false;
105         this.openConfirmationModal('Error', error.message);
106     });
107     this.showEcompSpinner = false;
108   }
109
110   applyFilter(filterValue: string) {
111     this.dataSource.filter = filterValue.trim().toLowerCase();
112   };
113
114   
115   populateTableData(appsList: Array<IApplications>){
116     this.dataSource = new MatTableDataSource(appsList);
117     this.dataSource.sort = this.sort;
118     this.dataSource.paginator = this.paginator;
119   };
120
121   openAddApplicationModal(rowData: any, action:any) {
122     console.log("Action : ", action);
123     const modalRef = this.ngbModal.open(ApplicationDetailsDialogComponent, { size: 'lg', backdrop: 'static', keyboard: false });
124     modalRef.componentInstance.title = 'Application Details';
125     modalRef.componentInstance.action = action;
126     //console.log("selectedData in parent",rowData);
127     if(rowData != 'undefined' && rowData){
128       modalRef.componentInstance.applicationObj = rowData;
129       this.isEditMode = true;
130     }else{
131       modalRef.componentInstance.applicationObj  = {};
132       this.isEditMode = false;
133     }
134     modalRef.componentInstance.passEntry.subscribe((receivedEntry: any) => {
135       //console.log("receivedEntry >>> ",receivedEntry);
136       if(receivedEntry){
137         this.appsList.push(receivedEntry);
138         //this.populateTableData(this.appsList);
139         this.getOnboardingApps();
140       }
141     });
142   }
143
144   deleteApplication(application: IApplications){
145     let confirmationMsg = 'You are about to delete this App : ' + application.appName+ '. Click OK to continue.';
146     this.openInformationModal("Confirmation",confirmationMsg).result.then((result) => {
147       if (result === 'Ok') {
148         if(!application || application == null){
149           console.log('ApplicationOnboardingCtrl::deleteApplication: No apllication or ID... cannot delete');
150           return;
151         }
152         this.showEcompSpinner = true;
153         this.appsList.splice(this.appsList.indexOf(application), 1);
154         this.applicationsService.deleteOnboardingApp(application.id)
155           .subscribe( data => {
156             this.result = data;
157             this.getOnboardingApps();
158             this.showEcompSpinner = false;
159           }, error => {
160             console.log(error);
161             this.showEcompSpinner = false;
162             if(error && error.status == 405){
163               this.openConfirmationModal('', 'Application : ' + application.appName+ ' can not be deleted as it is associsted with one of the Microservices.');
164             }else{
165               this.openConfirmationModal('Error', error.message);
166             }
167         });
168       }
169     }, (resut) => {
170       return;
171     })
172   }
173
174   checkIfUserIsSuperAdmin(){
175     this.applicationsService.checkIfUserIsSuperAdmin()
176       .subscribe(res => {
177         if(res) {
178           this.isUserSuperAdmin = true;
179           this.displayedColumns = ['thumbnail', 'applicationName','active', 
180           'integrationType', 'modeOfIntegration', 'guestAccess', 'landingPage','restURL',
181           'communicationKey', 'applicationNamespace', 'centralAuthAccess', 'delete'];
182         }  
183         //console.log("isUserSuperAdmin :: ",this.isUserSuperAdmin);
184       }, error =>{
185         console.log(error);
186         this.openConfirmationModal('Error', 'ApplicationsCtrl.checkIfUserIsSuperAdmin:: Failed '+error.message);
187     });
188   }
189
190   openConfirmationModal(_title: string, _message: string) {
191     const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
192     modalInfoRef.componentInstance.title = _title;
193     modalInfoRef.componentInstance.message = _message;
194   }
195
196   openInformationModal(_title: string, _message: string){
197     const modalInfoRef = this.ngbModal.open(InformationModalComponent);
198     modalInfoRef.componentInstance.title = _title;
199     modalInfoRef.componentInstance.message = _message;
200     return modalInfoRef;
201   }
202
203 }