Added new componetnts inside page modules
[portal.git] / portal-FE-common / src / app / pages / role / add-role / add-role.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, Input, Output, EventEmitter } from '@angular/core';
39 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
40 import { RoleService } from 'src/app/shared/services';
41 import { environment } from 'src/environments/environment';
42 import { HttpClient, HttpErrorResponse } from '@angular/common/http';
43 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
44 import { Role } from 'src/app/shared/model';
45 import { MatTableDataSource } from '@angular/material';
46 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
47
48 @Component({
49   selector: 'app-add-role',
50   templateUrl: './add-role.component.html',
51   styleUrls: ['./add-role.component.scss']
52 })
53 export class AddRoleComponent implements OnInit {
54
55   @Input() title: string;
56   @Input() appId: string;
57   @Input() dialogState: number;
58   @Input() availableRole: any;
59   @Input() appRoleFunctions: any;
60   @Output() passBackAddRolePopup: EventEmitter<any> = new EventEmitter();
61   availableRoleFunctions: any;
62   isGlobalRoleChecked = {
63     isChecked: false
64   }
65   role: Role;
66   roleFunctions: any;
67   showGlobalRole: boolean;
68   api = environment.api;
69   showSpinner: boolean;
70   displayedColumns: string[] = ['active', 'name'];
71   roleFunctionsDataSource = new MatTableDataSource(this.roleFunctions);
72   finalSelectedRoleFunctions: any;
73   constructor(public activeModal: NgbActiveModal, public ngbModal: NgbModal, private roleService: RoleService, public http: HttpClient) { }
74
75   ngOnInit() {
76     this.role = new Role;
77     this.finalSelectedRoleFunctions = [];
78     if (this.appId == '1')
79       this.showGlobalRole = true;
80     if (this.dialogState === 2) {
81       this.isGlobalRoleChecked.isChecked = (this.availableRole.name.includes('global_')) ? true : false;
82       this.availableRoleFunctions = [];
83       this.role = this.availableRole;
84       this.roleFunctionsDataSource = new MatTableDataSource(this.setSelectedRoleFucntions());
85     }
86   }
87
88   setSelectedRoleFucntions() {
89     for (var i = 0; i < this.appRoleFunctions.length; i++) {
90       var availableRoleFunction = this.appRoleFunctions[i];
91       availableRoleFunction['selected'] = false;
92       for (var j = 0; j < this.availableRole.roleFunctions.length; j++) {
93         if (availableRoleFunction.code === this.availableRole.roleFunctions[j].code
94           && availableRoleFunction.type === this.availableRole.roleFunctions[j].type
95           && availableRoleFunction.action === this.availableRole.roleFunctions[j].action) {
96           availableRoleFunction.selected = true;
97           console.log(availableRoleFunction.selected);
98         }
99       }
100       this.availableRoleFunctions.push(availableRoleFunction);
101     }
102     return this.availableRoleFunctions;
103   }
104
105   toggleRoleFunction(_element) {
106     if (this.appRoleFunctions) {
107       for (var i = 0; i < this.appRoleFunctions.length; i++) {
108         var availableRoleFunction = this.appRoleFunctions[i];
109         if (availableRoleFunction.selected && !this.finalSelectedRoleFunctions.includes(availableRoleFunction)) {
110           this.finalSelectedRoleFunctions.push(availableRoleFunction);
111         }
112       }
113     }
114     if (!_element.selected) {
115       for (var i = 0; i < this.finalSelectedRoleFunctions.length; i++) {
116         var availableRoleFunction = this.finalSelectedRoleFunctions[i];
117         if (availableRoleFunction.code == _element.code
118           && availableRoleFunction.type == _element.type
119           && availableRoleFunction.action == _element.action) {
120           this.finalSelectedRoleFunctions.splice(i, 1);
121         }
122       }
123     }
124   }
125
126
127
128   saveRole() {
129     var uuu = this.api.saveRole.replace(':appId', this.appId);
130     if (this.isGlobalRoleChecked.isChecked) {
131       this.role.name = (this.role.name.indexOf('global_') == -1) ? ('global_' + this.role.name) : (this.role.name);
132       this.saveOrUpdateRole(uuu);
133     } else {
134       var roleName = this.role.name.toLowerCase();
135       if (roleName.includes('global_')) {
136         const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
137         modalInfoRef.componentInstance.title = 'Confirmation';
138         modalInfoRef.componentInstance.message = 'Global prefix:"global_" can only be used when the global flag is checked for the role name:' + this.role.name + '. Please try again!';
139       } else {
140         this.role.childRoles = [];
141         this.role.roleFunctions = [];
142         this.saveOrUpdateRole(uuu);
143       }
144     }
145   }
146
147   saveOrUpdateRole(uuu) {
148     var confirmMessage = (this.dialogState === 2) ? 'You are about to update the role/role functions. Do you want to continue?' : 'You are about to create the role `' + this.role.name + '` . Do you want to continue?';
149     const modalInfoRef = this.ngbModal.open(InformationModalComponent);
150     modalInfoRef.componentInstance.title = 'Confirmation';
151     modalInfoRef.componentInstance.message = confirmMessage;
152     modalInfoRef.result.then((_res) => {
153       if (_res === 'Ok') {
154         //overriding the final list of rolefunctions to role
155         if (this.finalSelectedRoleFunctions.length > 0)
156           this.role.roleFunctions = this.finalSelectedRoleFunctions;
157         var postData = {
158           role: this.role,
159           childRoles: this.role.childRoles,
160           roleFunctions: this.role.roleFunctions
161         };
162         this.showSpinner = true
163         this.http.post(uuu, postData).toPromise().then((res: any) => {
164           this.showSpinner = false;
165           if (res && res.role) {
166             const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
167             modalInfoRef.componentInstance.title = 'Success';
168             modalInfoRef.componentInstance.message = 'Update Successful.';
169             this.passBackAddRolePopup.emit(this.appId);
170
171           }
172           else {
173             const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
174             modalInfoRef.componentInstance.title = 'Error';
175             modalInfoRef.componentInstance.message = res.error;
176           }
177         }, (res: HttpErrorResponse) => {
178           this.showSpinner = false;
179           const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
180           modalInfoRef.componentInstance.title = 'Error';
181           modalInfoRef.componentInstance.message = 'Error while saving.' + res.status;
182         }
183         );
184       }
185     }, (_dismiss) => {
186
187     })
188   }
189
190 }