6b1470fa946ed9e9cf847e89b1d6a13a48a026cb
[portal/sdk.git] /
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
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, 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';
48
49
50 @Component({
51   selector: 'app-new-role',
52   templateUrl: './new-role.component.html',
53   styleUrls: ['./new-role.component.scss']
54 })
55 export class NewRoleComponent implements OnInit {
56
57   showSpinner: boolean;
58   result: any;
59   @Input() role: any;
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);
72
73   constructor(public adminService:AdminService, public userService: UserService, public activeModal: NgbActiveModal, public ngbModal: NgbModal) { }
74
75   ngOnInit() {
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)
82     }
83   }
84
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;
94         }
95       }
96       this.availableRoleFunctions.push(availableRoleFunction);
97     }
98     return this.availableRoleFunctions;
99   }
100
101
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);
108         }
109       }
110     }
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);
118         }
119       }
120     }
121   }
122
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;
130         let index = 0;
131         for(let i=0; i<temproleFunctions.length; i++){
132           if(temproleFunctions[i].code == roleFunction.code){
133             break;
134           }
135           index = index + 1;
136         };
137         temproleFunctions.splice(index,1);
138         this.populateTableData(temproleFunctions);
139         this.adminService.removeRoleFunction(roleFunction , roleId)
140           .subscribe(_data => {
141               this.result = _data
142               this.passEntry.emit(this.result);
143           }, error =>{
144             this.openConfirmationModal('Error', error.message);
145           });
146       }
147     }, (resut) => {
148       this.openConfirmationModal('Error', resut);
149       return;
150     })
151   }
152
153   populateTableData(roleFunctionsList: any){
154     this.roleFunctionDataSource = new MatTableDataSource(roleFunctionsList);
155     this.roleFunctionDataSource.sort = this.sort;
156     this.roleFunctionDataSource.paginator = this.paginator;
157   }
158
159   //Add Or Update Account.
160   saveChanges(){
161     if(this.isEditMode){
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);
165       }
166       //update the role object
167       this.showSpinner = true
168       this.role.roleFunctions = this.finalSelectedRoleFunctions;
169       let postData={
170         role: this.role, 
171         childRoles: this.role.childRoles, 
172         roleFunctions : this.role.roleFunctions
173       };
174       this.adminService.saveRole(postData, this.role.id)
175         .subscribe(_data => {
176           this.showSpinner = false;
177           this.result = _data
178           this.passEntry.emit(this.result);
179           this.ngbModal.dismissAll();
180       }, error =>{
181         this.openConfirmationModal('Error', error.message);
182       });
183     }else{
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);
188       }
189       let newRoleObj = {
190                                 'id':null,
191                                 'created':null,
192                                 'modified':null,
193                                 'createdId':null,
194                                 'modifiedId':null,
195                                 'rowNum':null,
196                                 'auditUserId':null,
197                                 'auditTrail':null,
198                                 'name':this.role.name,
199                                 'active':true,
200                                 'priority':this.role.priority,
201                                 'roleFunctions':null,
202                                 'childRoles':null,
203                                 'toggleActiveAltText':"Click to Activate Role ",
204                                 'toggleActiveImage':" / static fusion images inactive.png ",
205                                 'editUrl':" role.htm ? role_id = null",
206       };
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);
211       }else{
212         this.showSpinner = true
213         newRoleObj.childRoles = [];
214         newRoleObj.roleFunctions = [];
215         let postData={
216                                         role: newRoleObj, 
217                                         childRoles: newRoleObj.childRoles, 
218                                         roleFunctions : newRoleObj.roleFunctions
219         };
220         this.adminService.saveRole(postData, newRoleObj.id)
221           .subscribe(_data => {
222             this.showSpinner = false;
223             this.result = _data
224             this.passEntry.emit(this.result);
225             this.ngbModal.dismissAll();
226         }, error =>{
227           this.openConfirmationModal('Error', error.message);
228         });
229       }
230     }    
231   }
232
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){
238           return true;
239         }
240       }
241     }
242     return false;
243   }
244
245   openConfirmationModal(_title: string, _message: string) {
246     const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
247     modalInfoRef.componentInstance.title = _title;
248     modalInfoRef.componentInstance.message = _message;
249   }
250 }