Added new componetnts inside page modules
[portal.git] / portal-FE-common / src / app / pages / role / role-functions / role-functions.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 import { Component, OnInit, ViewChild } from '@angular/core';
39 import { RoleService, ApplicationsService } from 'src/app/shared/services';
40 import { MatTableDataSource, MatPaginator, MatSort } from '@angular/material';
41 import { RoleFunctionModalComponent } from './role-function-modal/role-function-modal.component';
42 import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
43 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
44 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
45 import { HttpClient } from '@angular/common/http';
46 import { environment } from 'src/environments/environment';
47
48 @Component({
49   selector: 'app-role-functions',
50   templateUrl: './role-functions.component.html',
51   styleUrls: ['./role-functions.component.scss']
52 })
53 export class RoleFunctionsComponent implements OnInit {
54   api = environment.api
55   centralizedApps: any;
56   selectedCentralizedApp: any;
57   availableRoleFunctions: any;
58   displayedColumns: string[] = ['type', 'instance', 'action', 'name', 'edit', 'delete'];
59   roleFunctionsDataSource = new MatTableDataSource(this.availableRoleFunctions);
60   @ViewChild(MatSort) sort: MatSort;
61   @ViewChild(MatPaginator) paginator: MatPaginator;
62   showSpinner: boolean;
63
64   constructor(public ngbModal: NgbModal,private roleService: RoleService, private applicationsService: ApplicationsService, public http: HttpClient) {}
65
66   ngOnInit() {
67     this.availableRoleFunctions = [];
68     this.centralizedApps = [];
69     this.getCentralizedApps(sessionStorage.userId);
70   }
71
72   syncRolesFromExternalAuthSystem() {
73     this.applicationsService.syncRolesEcompFromExtAuthSystem(this.selectedCentralizedApp).toPromise().then((res: any) => {
74       if (res.status == 'OK') {
75         const modalInfoRef = this.ngbModal.open(InformationModalComponent);
76         modalInfoRef.componentInstance.title = 'Success';
77         modalInfoRef.componentInstance.message = 'Sync role functions completed successfully!';
78         modalInfoRef.result.then((_res) => {
79           if (_res === 'Ok')
80             this.getRoleFunctions(this.selectedCentralizedApp);
81         }, (result) => {
82
83         })
84       } else {
85         this.openConfirmationModal('Error', 'Sync failed ' + res.message);
86       }
87     }).catch(err => {
88       this.openConfirmationModal('Error', 'Sync failed' + err);
89     });
90   };
91
92
93   // getCentalizedApps
94   getCentralizedApps(userId) {
95     this.roleService.getCentralizedApps(userId).toPromise().then((res: any) => {
96       if (res.length > 0) {
97         this.centralizedApps = res;
98         this.selectedCentralizedApp = this.centralizedApps[0].appId;
99         this.getRoleFunctions(this.centralizedApps[0].appId);
100       }
101     }).catch(err => {
102       // $log.error('RoleListCtrl::centralizedApps retrieval error: ', err);
103     }).finally(() => {
104       // this.isLoadingTable = false;
105     });
106   }
107
108   getRoleFunctions(id) {
109     this.showSpinner = true;
110     this.roleService.getRoleFunctionList(id).subscribe((data: any) => {
111       this.showSpinner = false;
112       var j = data;
113       var roleFunctions = JSON.parse(j.data);
114       this.availableRoleFunctions = roleFunctions.availableRoleFunctions;
115       this.roleFunctionsDataSource = new MatTableDataSource(this.availableRoleFunctions);
116       this.roleFunctionsDataSource.sort = this.sort;
117       this.roleFunctionsDataSource.paginator = this.paginator;
118     }, (error) => {
119       this.showSpinner = false;
120       this.openConfirmationModal('Error', 'Failed to get role functions. Please try again!' + error.message);
121     })
122   };
123
124   addRoleFunctionModalPopup(){
125     const modalInfoRef = this.ngbModal.open(RoleFunctionModalComponent);
126     modalInfoRef.componentInstance.title = 'Add Role Function';
127     modalInfoRef.componentInstance.appId = this.selectedCentralizedApp;
128     modalInfoRef.componentInstance.currentRoleFunctions = this.availableRoleFunctions;
129     modalInfoRef.componentInstance.passBackRoleFunctionPopup.subscribe((_result: any) => {
130       if(_result){
131         modalInfoRef.close();
132         this.availableRoleFunctions.push(_result);
133         this.roleFunctionsDataSource = new MatTableDataSource(this.availableRoleFunctions);
134         this.roleFunctionsDataSource.sort = this.sort;
135         this.roleFunctionsDataSource.paginator = this.paginator;
136       }
137     }, (_reason: any) => {
138       return;
139     });
140
141   }
142
143   editRoleFunctionModalPopup(_element){
144     const modalInfoRef = this.ngbModal.open(RoleFunctionModalComponent);
145     modalInfoRef.componentInstance.title = 'Edit Role Function';
146     modalInfoRef.componentInstance.appId = this.selectedCentralizedApp;
147     modalInfoRef.componentInstance.editRoleFunction = _element;
148     modalInfoRef.componentInstance.currentRoleFunctions = this.availableRoleFunctions;
149     modalInfoRef.componentInstance.passBackRoleFunctionPopup.subscribe((_result: any) => {
150       if(_result){
151         modalInfoRef.close();
152         this.availableRoleFunctions.splice(this.availableRoleFunctions.indexOf(_element), 1);
153         this.availableRoleFunctions.push(_result);
154         this.roleFunctionsDataSource = new MatTableDataSource(this.availableRoleFunctions);
155         this.roleFunctionsDataSource.sort = this.sort;
156         this.roleFunctionsDataSource.paginator = this.paginator;
157       }
158     }, (_reason: any) => {
159       return;
160     });
161   }
162
163   removeRoleFunction(_element: any){
164     const ngbInfoModal = this.ngbModal.open(InformationModalComponent);
165     ngbInfoModal.componentInstance.title = 'Confirmation';
166     ngbInfoModal.componentInstance.message = 'You are about to delete the role function ' + _element.name + '. Do you want to continue?';
167     ngbInfoModal.result.then(_res =>{
168       if(_res === 'Ok'){
169         this.showSpinner = true;
170                                 var uuu = this.api.removeRoleFunction.replace(':appId', this.selectedCentralizedApp);
171                                 var postData = _element;
172                                 this.http.post(uuu, postData).subscribe((response: any) => {
173           this.showSpinner = false;
174                                         if(response.status == 'OK'){
175             this.openConfirmationModal('Success', response.message);
176             this.availableRoleFunctions.splice(this.availableRoleFunctions.indexOf(_element), 1);
177             this.roleFunctionsDataSource = new MatTableDataSource(this.availableRoleFunctions);
178             this.roleFunctionsDataSource.sort = this.sort;
179             this.roleFunctionsDataSource.paginator = this.paginator;
180                                         } else{
181             this.showSpinner = false;
182                                                 this.openConfirmationModal('Error', "Error while deleting: " + response.message);
183                                         }
184                                 }, (err) => {
185           this.showSpinner = false;
186           this.openConfirmationModal('Error', err.message);
187                                 });
188       }
189     }, (_reason: any) => {
190       return;
191     });
192   }
193
194   openConfirmationModal(_title: string, _message: string) {
195     const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
196     modalInfoRef.componentInstance.title = _title;
197     modalInfoRef.componentInstance.message = _message;
198   }
199
200 }