82d353d131d2b2543ce7f03dd3cc189a8212a81f
[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 import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
39 import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
40 import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
41 import { RoleFunction } from '../role-function';
42 import { HttpClient } from '@angular/common/http';
43 import { AdminService } from '../../admin.service';
44 import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
45
46 @Component({
47   selector: 'app-new-role-function',
48   templateUrl: './new-role-function.component.html',
49   styleUrls: ['./new-role-function.component.scss']
50 })
51 export class NewRoleFunctionComponent implements OnInit {
52
53   @Input() title: string;
54   @Input() appId: any;
55   @Input() dialogState: number;
56   @Input() currentRoleFunctions: any;
57   @Input() editRoleFunction: RoleFunction;
58   @Output() passBackRoleFunctionPopup: EventEmitter<any> = new EventEmitter();
59   roleFunction: RoleFunction;
60   otherTypeValue: string;
61   typeOptions: string[] = ['menu', 'url', 'other'];
62   api = '';
63   isEditing: any;
64   editDisable: boolean;
65   showSpinner: boolean;
66   selectedType: string;
67   createOrUpdate: string;
68   constructor(public adminService: AdminService, public activeModal: NgbActiveModal, public ngbModal: NgbModal, public http: HttpClient) { }
69
70   ngOnInit() {
71     this.createOrUpdate = 'create';
72     this.selectedType = 'menu';
73     this.roleFunction = new RoleFunction(this.selectedType, '', '*', '');
74     this.otherTypeValue = '';
75     if (this.editRoleFunction) {
76       this.createOrUpdate = 'update';
77       this.editDisable = true;
78       if (this.editRoleFunction.type !== 'menu' && this.editRoleFunction.type !== 'url') {
79         this.selectedType = 'other';
80         this.otherTypeValue = this.editRoleFunction.type;
81       }else{
82         this.selectedType = this.editRoleFunction.type;
83       }
84       this.roleFunction = new RoleFunction(this.editRoleFunction.type, this.editRoleFunction.code, this.editRoleFunction.action, this.editRoleFunction.name);
85     }
86   }
87
88   saveRoleFunction() {
89     if (/[^a-zA-Z0-9\-\.\_]/.test(this.roleFunction.type)) {
90       this.openConfirmationModal('Confirmation', 'Type can only contain alphanumeric characters, dots(.) and underscores(_)');
91       return;
92     }
93     if (this.roleFunction.action !== '*' && /[^a-zA-Z0-9\-\.\_]/.test(this.roleFunction.action)) {
94       this.openConfirmationModal('Confirmation', 'Action can only contain alphanumeric characters, hyphens(-), dots(.) and underscores(_) and single asterisk character(*)');
95       return;
96     }
97     if (/[^a-zA-Z0-9\-\:\_\./*]/.test(this.roleFunction.code)) {
98       this.openConfirmationModal('Confirmation', 'Instance can only contain alphanumeric characters, hyphens(-), dots(.), colons(:), forwardSlash(/) , asterisk(*) and underscores(_)');
99       return;
100     }
101     const modalInfoRef = this.ngbModal.open(InformationModalComponent);
102     modalInfoRef.componentInstance.title = 'Confirmation';
103     modalInfoRef.componentInstance.message = 'You are about to ' + this.createOrUpdate + ' the role function ' + this.roleFunction.name + '. Do you want to continue?';
104     modalInfoRef.result.then((_res) => {
105       if (_res === 'Ok') {
106         this.showSpinner = true;        
107         var exists = false, x;
108         for (x in this.currentRoleFunctions) {
109           if (this.currentRoleFunctions[x].type == this.roleFunction.type
110             && this.currentRoleFunctions[x].code == this.roleFunction.code
111             && this.currentRoleFunctions[x].action == this.roleFunction.action
112             && this.currentRoleFunctions[x].name == this.roleFunction.name) {
113             this.openConfirmationModal('Confirmation', "Role Function already exist.");
114             exists = true;
115             this.showSpinner = false;
116             break;
117           }
118           if (!this.editDisable) {
119             if (this.currentRoleFunctions[x].type == this.roleFunction.type
120               && this.currentRoleFunctions[x].code == this.roleFunction.code
121               && this.currentRoleFunctions[x].action == this.roleFunction.action
122             ) {
123               this.openConfirmationModal('Confirmation', "Please make sure code, type and action is unique. Please create a role function with a different code or type or action to proceed.");
124               exists = true;
125               this.showSpinner = false;
126               break;
127             }
128           }
129         }
130
131         if (this.selectedType === 'other'){
132           this.roleFunction.type = this.otherTypeValue;
133         }else{
134           this.roleFunction.type = this.selectedType;
135         }
136         
137         if (!exists && this.roleFunction.name.trim() != '' && this.roleFunction.code.trim() != '') {
138           var postData = this.roleFunction;
139           console.log("saveRoleFunction post data :: ",postData);
140           this.adminService.saveRoleFunction(JSON.stringify(postData))
141           .subscribe(_data => {
142             this.showSpinner = false;
143             console.log("saveRoleFunction response",_data);
144             if (this.editRoleFunction) {
145               this.editRoleFunction.name = this.roleFunction.name;
146               this.passBackRoleFunctionPopup.emit(this.editRoleFunction);
147             } else{
148               this.passBackRoleFunctionPopup.emit(this.roleFunction);
149             }
150             if (this.editRoleFunction) {
151                this.openConfirmationModal('Success', "Role function updated successfully.");
152             }else{
153               this.openConfirmationModal('Success', "Role function created successfully.");
154             }  
155           }, error =>{
156             console.log(error);
157             this.showSpinner = false;
158             this.openConfirmationModal('Error', error.message);
159           });
160         }
161       }
162     }, (_dismiss) => {
163
164     })
165   }
166
167
168   openConfirmationModal(_title: string, _message: string) {
169     const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
170     modalInfoRef.componentInstance.title = _title;
171     modalInfoRef.componentInstance.message = _message;
172   }
173
174 }