2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
39 import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
40 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
41 import { AdminService } from '../../admin.service';
42 import { UserService } from 'src/app/shared/services/user/user.service';
43 import { MatSort } from '@angular/material/sort';
44 import { MatPaginator } from '@angular/material/paginator';
45 import { MatTableDataSource } from '@angular/material';
46 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
47 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
51 selector: 'app-new-role',
52 templateUrl: './new-role.component.html',
53 styleUrls: ['./new-role.component.scss']
55 export class NewRoleComponent implements OnInit {
60 @Input() isEditMode: boolean;
61 @Input() availableRoles : any;
62 @Input() ociavailableRoleFunctions: any;
63 roleFunctionTableHeaders: Array<string> = [];
64 roleFunctions: Array<Object> = [];
65 roleFunctionsLenght: any;
66 finalSelectedRoleFunctions: any;
67 availableRoleFunctions: any;
68 @Output() passEntry: EventEmitter<any> = new EventEmitter();
69 @ViewChild(MatPaginator, {}) paginator: MatPaginator;
70 @ViewChild(MatSort, {}) sort: MatSort;
71 roleFunctionDataSource = new MatTableDataSource(this.roleFunctions);
73 constructor(public adminService:AdminService, public userService: UserService, public activeModal: NgbActiveModal, public ngbModal: NgbModal) { }
76 this.roleFunctionTableHeaders = ["roleFunctionName","active"];
77 this.finalSelectedRoleFunctions = [];
78 this.availableRoleFunctions = [];
79 if(this.isEditMode && this.ociavailableRoleFunctions && this.ociavailableRoleFunctions.length > 0){
80 this.availableRoleFunctions = this.setSelectedRoleFucntions(this.ociavailableRoleFunctions);
81 this.populateTableData(this.availableRoleFunctions)
85 setSelectedRoleFucntions(ociavailableRoleFunctions: any) {
86 for (var i = 0; i < this.ociavailableRoleFunctions.length; i++) {
87 var availableRoleFunction = this.ociavailableRoleFunctions[i];
88 availableRoleFunction['selected'] = false;
89 for (var j = 0; j < this.role.roleFunctions.length; j++) {
90 if (availableRoleFunction.code === this.role.roleFunctions[j].code
91 && availableRoleFunction.type === this.role.roleFunctions[j].type
92 && availableRoleFunction.action === this.role.roleFunctions[j].action) {
93 availableRoleFunction.selected = true;
96 this.availableRoleFunctions.push(availableRoleFunction);
98 return this.availableRoleFunctions;
102 toggleRoleFunction(_element) {
103 if (this.ociavailableRoleFunctions) {
104 for (var i = 0; i < this.ociavailableRoleFunctions.length; i++) {
105 var availableRoleFunction = this.ociavailableRoleFunctions[i];
106 if (availableRoleFunction.selected && !this.finalSelectedRoleFunctions.includes(availableRoleFunction)) {
107 this.finalSelectedRoleFunctions.push(availableRoleFunction);
111 if (!_element.selected) {
112 for (var i = 0; i < this.finalSelectedRoleFunctions.length; i++) {
113 var availableRoleFunction = this.finalSelectedRoleFunctions[i];
114 if (availableRoleFunction.code == _element.code
115 && availableRoleFunction.type == _element.type
116 && availableRoleFunction.action == _element.action) {
117 this.finalSelectedRoleFunctions.splice(i, 1);
123 delRoleFunctionConfirmPopUp(roleFunction: any, roleId: any){
124 const modalRef = this.ngbModal.open(InformationModalComponent);
125 modalRef.componentInstance.title = "Confirmation";
126 modalRef.componentInstance.message = 'You are about to delete this Role Function : ' + roleFunction.name+ '. Click OK to continue.';
127 modalRef.result.then((result) => {
128 if (result === 'Ok') {
129 let temproleFunctions = this.role.roleFunctions;
131 for(let i=0; i<temproleFunctions.length; i++){
132 if(temproleFunctions[i].code == roleFunction.code){
137 temproleFunctions.splice(index,1);
138 this.populateTableData(temproleFunctions);
139 this.adminService.removeRoleFunction(roleFunction , roleId)
140 .subscribe(_data => {
142 this.passEntry.emit(this.result);
144 this.openConfirmationModal('Error', error.message);
148 this.openConfirmationModal('Error', resut);
153 populateTableData(roleFunctionsList: any){
154 this.roleFunctionDataSource = new MatTableDataSource(roleFunctionsList);
155 this.roleFunctionDataSource.sort = this.sort;
156 this.roleFunctionDataSource.paginator = this.paginator;
159 //Add Or Update Account.
162 if (this.role.priority && this.role.priority != '' && isNaN(parseInt(this.role.priority))) {
163 let errorMsg = 'Priority must be an integer.';
164 this.openConfirmationModal('Error', errorMsg);
166 //update the role object
167 this.showSpinner = true
168 this.role.roleFunctions = this.finalSelectedRoleFunctions;
171 childRoles: this.role.childRoles,
172 roleFunctions : this.role.roleFunctions
174 this.adminService.saveRole(postData, this.role.id)
175 .subscribe(_data => {
176 this.showSpinner = false;
178 this.passEntry.emit(this.result);
179 this.ngbModal.dismissAll();
181 this.openConfirmationModal('Error', error.message);
184 //create new Role Object
185 if (this.role.priority && this.role.priority != '' && isNaN(parseInt(this.role.priority))) {
186 let errorMsg = 'Priority must be an integer.';
187 this.openConfirmationModal('Error', errorMsg);
198 'name':this.role.name,
200 'priority':this.role.priority,
201 'roleFunctions':null,
203 'toggleActiveAltText':"Click to Activate Role ",
204 'toggleActiveImage':" / static fusion images inactive.png ",
205 'editUrl':" role.htm ? role_id = null",
207 if(this.isRoleAlreadyExist(this.role.name)){
208 //msg Role already exit
209 let errorMsg = "Role Name " + this.role.name +" is already present."
210 this.openConfirmationModal('Error', errorMsg);
212 this.showSpinner = true
213 newRoleObj.childRoles = [];
214 newRoleObj.roleFunctions = [];
217 childRoles: newRoleObj.childRoles,
218 roleFunctions : newRoleObj.roleFunctions
220 this.adminService.saveRole(postData, newRoleObj.id)
221 .subscribe(_data => {
222 this.showSpinner = false;
224 this.passEntry.emit(this.result);
225 this.ngbModal.dismissAll();
227 this.openConfirmationModal('Error', error.message);
233 isRoleAlreadyExist(currentRoleName: any){
234 let roles = this.availableRoles;
235 if(roles && roles.length > 0){
236 for(let i=0; i<roles.length; i++){
237 if(roles[i].name === currentRoleName){
245 openConfirmationModal(_title: string, _message: string) {
246 const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
247 modalInfoRef.componentInstance.title = _title;
248 modalInfoRef.componentInstance.message = _message;