2 Copyright 2020 TATA ELXSI
4 Licensed under the Apache License, Version 2.0 (the 'License');
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file Roles Create and Edit Component
21 import { HttpClient, HttpHeaders } from '@angular/common/http';
22 import { Component, Injector, Input, OnInit } from '@angular/core';
23 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { ActivatedRoute, Router } from '@angular/router';
25 import { TranslateService } from '@ngx-translate/core';
26 import { NotifierService } from 'angular-notifier';
27 import { APIURLHEADER, ERRORDATA } from 'CommonModel';
28 import { environment } from 'environment';
29 import * as jsonpath from 'jsonpath';
30 import { RestService } from 'RestService';
31 import { Permission, PermissionGroup, RoleConfig, RoleData } from 'RolesModel';
32 import { SharedService } from 'SharedService';
33 import { isNullOrUndefined } from 'util';
37 * @Component takes RolesCreateEditComponent.html as template url
40 selector: 'app-roles-create-edit',
41 templateUrl: './RolesCreateEditComponent.html',
42 styleUrls: ['./RolesCreateEditComponent.scss']
44 /** Exporting a class @exports RolesCreateEditComponent */
45 export class RolesCreateEditComponent implements OnInit {
46 /** To inject services @public */
47 public injector: Injector;
49 /** contains role permissions from config file @public */
50 public rolePermissions: {}[];
52 /** contains role form group information @public */
53 public roleForm: FormGroup;
55 /** Instance of the rest service @public */
56 public restService: RestService;
58 /** Check the loading results for loader status @public */
59 public isLoadingResults: boolean = false;
61 /** Give the message for the loading @public */
62 public message: string = 'PLEASEWAIT';
64 /** Instance to check role form submitted status @public */
65 public submitted: boolean = false;
67 /** Contains role create or edit value @public */
68 public getRoleType: string;
70 /** Contains view selection value either text or preview @public */
71 public viewMode: string = 'text';
73 /** Contains role id value @private */
74 private roleRef: string;
76 /** Notifier service to popup notification @private */
77 private notifierService: NotifierService;
79 /** Contains tranlsate instance @private */
80 private translateService: TranslateService;
82 /** contians form builder module @private */
83 private formBuilder: FormBuilder;
85 /** Contains all methods related to shared @private */
86 private sharedService: SharedService;
88 /** Controls the header form @private */
89 private headers: HttpHeaders;
91 /** Varaibles to hold http client @private */
92 private httpClient: HttpClient;
94 /** Holds the instance of activatedRoute of router service @private */
95 private activatedRoute: ActivatedRoute;
97 /** Holds the instance of roter service @private */
98 private router: Router;
100 constructor(injector: Injector) {
101 this.injector = injector;
102 this.restService = this.injector.get(RestService);
103 this.notifierService = this.injector.get(NotifierService);
104 this.translateService = this.injector.get(TranslateService);
105 this.formBuilder = this.injector.get(FormBuilder);
106 this.sharedService = this.injector.get(SharedService);
107 this.httpClient = this.injector.get(HttpClient);
108 this.activatedRoute = this.injector.get(ActivatedRoute);
109 this.router = this.injector.get(Router);
112 /** Lifecyle Hooks the trigger before component is instantiate @public */
113 public ngOnInit(): void {
114 this.headers = new HttpHeaders({
115 'Content-Type': 'application/json',
116 Accept: 'application/json',
117 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
119 this.roleForm = this.formBuilder.group({
120 roleName: ['', [Validators.required]],
123 this.getRolePermissions();
126 /** Get role permission information based on action @public */
127 public getRolePermissions(): void {
128 this.isLoadingResults = true;
129 this.loadPermissions().then((response: RoleConfig) => {
130 this.rolePermissions = response.rolePermissions;
131 if (this.activatedRoute.snapshot.url[0].path === 'create') {
132 this.getRoleType = 'Add';
133 this.isLoadingResults = false;
135 this.getRoleType = 'Edit';
143 /** Check Role Create or Edit and proceed action @public */
144 public roleCheck(): void {
145 this.submitted = true;
146 if (!this.roleForm.valid) {
147 const errorIp: Element = document.querySelector('.ng-invalid[formControlName]');
149 errorIp.scrollIntoView({ behavior: 'smooth', block: 'center' });
152 if (this.getRoleType === 'Add') {
160 /** Convenience getter for easy access to form fields */
161 get f(): FormGroup['controls'] { return this.roleForm.controls; }
163 /** Create role @private */
164 public createRole(): void {
165 const apiURLHeader: APIURLHEADER = {
166 url: environment.ROLES_URL,
167 httpOptions: { headers: this.headers }
169 let permissionData: Object = {};
170 this.sharedService.cleanForm(this.roleForm);
171 if (!this.roleForm.invalid) {
172 if (this.viewMode === 'preview') {
173 this.isLoadingResults = true;
174 permissionData = this.generatePermissions();
175 this.roleCreateAPI(apiURLHeader, permissionData);
177 if (this.checkPermission()) {
178 this.isLoadingResults = true;
179 permissionData = this.roleForm.value.permissions !== '' ? JSON.parse(this.roleForm.value.permissions) : {};
180 this.roleCreateAPI(apiURLHeader, permissionData);
186 /** Method to handle role create API call @public */
187 public roleCreateAPI(apiURLHeader: APIURLHEADER, permissionData: {}): void {
188 const postData: {} = {
189 name: this.roleForm.value.roleName,
190 permissions: !isNullOrUndefined(permissionData) ? permissionData : {}
192 this.restService.postResource(apiURLHeader, postData).subscribe(() => {
193 this.notifierService.notify('success', this.translateService.instant('PAGE.ROLES.CREATEDSUCCESSFULLY'));
194 this.router.navigate(['/roles/details']).catch();
195 }, (error: ERRORDATA) => {
196 this.isLoadingResults = false;
197 this.restService.handleError(error, 'post');
201 /** Edit role @private */
202 public editRole(): void {
203 const apiURLHeader: APIURLHEADER = {
204 url: environment.ROLES_URL + '/' + this.roleRef,
205 httpOptions: { headers: this.headers }
207 let permissionData: Object = {};
208 this.sharedService.cleanForm(this.roleForm);
209 if (!this.roleForm.invalid) {
210 if (this.viewMode === 'preview') {
211 this.isLoadingResults = true;
212 permissionData = this.generatePermissions();
213 this.roleEditAPI(apiURLHeader, permissionData);
215 if (this.checkPermission()) {
216 this.isLoadingResults = true;
217 permissionData = this.roleForm.value.permissions !== '' ? JSON.parse(this.roleForm.value.permissions) : {};
218 this.roleEditAPI(apiURLHeader, permissionData);
224 /** Method to handle role edit API call */
225 public roleEditAPI(apiURLHeader: APIURLHEADER, permissionData: {}): void {
226 const postData: {} = {
227 name: this.roleForm.value.roleName,
228 permissions: !isNullOrUndefined(permissionData) ? permissionData : {}
230 this.restService.patchResource(apiURLHeader, postData).subscribe(() => {
231 this.notifierService.notify('success', this.translateService.instant('PAGE.ROLES.UPDATEDSUCCESSFULLY'));
232 this.router.navigate(['/roles/details']).catch();
233 }, (error: ERRORDATA) => {
234 this.isLoadingResults = false;
235 this.restService.handleError(error, 'patch');
239 /** Method to handle toggle role view selection @public */
240 public viewSelection(): void {
241 if (this.viewMode === 'text' && this.checkPermission()) {
242 this.loadPermissions().then((response: RoleConfig) => {
243 this.rolePermissions = response.rolePermissions;
244 const permissions: {} = this.roleForm.value.permissions !== '' ? JSON.parse(this.roleForm.value.permissions) : {};
245 this.filterRoleData(permissions);
246 this.viewMode = 'preview';
251 const rolePermission: {} = this.generatePermissions();
252 if (Object.keys(rolePermission).length !== 0) {
253 this.roleForm.patchValue({ permissions: JSON.stringify(rolePermission, null, '\t') });
255 this.viewMode = 'text';
260 /** Generate role permission post data @private */
261 private generatePermissions(): Object {
262 const permissions: Object = {};
263 this.rolePermissions.forEach((permissionGroup: PermissionGroup) => {
264 if (!isNullOrUndefined(permissionGroup)) {
265 permissionGroup.permissions.forEach((permission: Permission) => {
266 if (!isNullOrUndefined(permission.value) && permission.value !== 'NA') {
267 permissions[permission.operation] = permission.value;
275 /** Validation method for check permission json string @private */
276 private checkPermission(): boolean {
277 if (this.roleForm.value.permissions) {
278 if (!this.sharedService.checkJson(this.roleForm.value.permissions)) {
279 this.notifierService.notify('error', this.translateService.instant('PAGE.ROLES.ROLEJSONERROR'));
282 this.roleForm.value.permissions = this.roleForm.value.permissions.replace(/'/g, '"');
283 const rolePermission: {} = JSON.parse(this.roleForm.value.permissions);
284 for (const key of Object.keys(rolePermission)) {
285 if (typeof rolePermission[key] !== 'boolean') {
286 this.notifierService.notify('error', this.translateService.instant('PAGE.ROLES.ROLEKEYERROR', { roleKey: key }));
295 /** Get role data from NBI based on ID and apply filter @private */
296 private getRoleData(): void {
297 // tslint:disable-next-line: no-backbone-get-set-outside-model
298 this.roleRef = this.activatedRoute.snapshot.paramMap.get('id');
299 if (!isNullOrUndefined(this.roleRef)) {
300 this.restService.getResource(environment.ROLES_URL + '/' + this.roleRef).subscribe((data: RoleData) => {
301 this.roleForm.setValue({ roleName: data.name, permissions: JSON.stringify(data.permissions, null, '\t') });
302 this.filterRoleData(data.permissions);
303 this.isLoadingResults = false;
304 }, (error: ERRORDATA) => {
305 this.router.navigate(['/roles/details']).catch();
306 this.restService.handleError(error, 'get');
311 /** Method to filter role data @private */
312 private filterRoleData(permissions: {}): void {
313 Object.keys(permissions).forEach((permission: string) => {
314 jsonpath.apply(this.rolePermissions, '$..permissions[?(@.operation == "' + permission + '")]', (response: Permission) => {
315 if (response.operation === permission) {
316 response.value = permissions[permission];
323 /** Method to load the role permission Json file @private */
324 private async loadPermissions(): Promise<Object> {
325 const jsonFile: string = environment.PERMISSIONS_CONFIG_FILE + '?cb=' + new Date().getTime();
326 return new Promise<Object>((resolve: Function, reject: Function): void => {
327 this.httpClient.get(jsonFile).subscribe((response: Response) => {