summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/admin/role-functions/new-role-function/new-role-function.component.ts
blob: ce894fe91d52e4f7a5ffadfe3079046817cd1f9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal SDK
 * ===================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 * 
 */
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
import { RoleFunction } from '../role-function';
import { HttpClient } from '@angular/common/http';
import { AdminService } from '../../admin.service';
import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';

@Component({
  selector: 'app-new-role-function',
  templateUrl: './new-role-function.component.html',
  styleUrls: ['./new-role-function.component.scss']
})
export class NewRoleFunctionComponent implements OnInit {

  @Input() title: string;
  @Input() appId: any;
  @Input() dialogState: number;
  @Input() currentRoleFunctions: any;
  @Input() editRoleFunction: RoleFunction;
  @Output() passBackRoleFunctionPopup: EventEmitter<any> = new EventEmitter();
  roleFunction: RoleFunction;
  otherTypeValue: string;
  typeOptions: string[] = ['menu', 'url', 'other'];
  api = '';
  isEditing: any;
  editDisable: boolean;
  showSpinner: boolean;
  selectedType: string;
  createOrUpdate: string;
  constructor(public adminService: AdminService, public activeModal: NgbActiveModal, public ngbModal: NgbModal, public http: HttpClient) { }

  ngOnInit() {
    this.createOrUpdate = 'create';
    this.selectedType = 'menu';
    this.roleFunction = new RoleFunction(this.selectedType, '', '*', '');
    this.otherTypeValue = '';
    if (this.editRoleFunction) {
      this.createOrUpdate = 'update';
      this.editDisable = true;
      if (this.editRoleFunction.type !== 'menu' && this.editRoleFunction.type !== 'url') {
        this.selectedType = 'other';
        this.otherTypeValue = this.editRoleFunction.type;
      }else{
        this.selectedType = this.editRoleFunction.type;
      }
      this.roleFunction = new RoleFunction(this.editRoleFunction.type, this.editRoleFunction.code, this.editRoleFunction.action, this.editRoleFunction.name);
    }
  }

  saveRoleFunction() {
    if (/[^a-zA-Z0-9\-\.\_]/.test(this.roleFunction.type)) {
      this.openConfirmationModal('Confirmation', 'Type can only contain alphanumeric characters, dots(.) and underscores(_)');
      return;
    }
    if (this.roleFunction.action !== '*' && /[^a-zA-Z0-9\-\.\_]/.test(this.roleFunction.action)) {
      this.openConfirmationModal('Confirmation', 'Action can only contain alphanumeric characters, hyphens(-), dots(.) and underscores(_) and single asterisk character(*)');
      return;
    }
    if (/[^a-zA-Z0-9\-\:\_\./*]/.test(this.roleFunction.code)) {
      this.openConfirmationModal('Confirmation', 'Instance can only contain alphanumeric characters, hyphens(-), dots(.), colons(:), forwardSlash(/) , asterisk(*) and underscores(_)');
      return;
    }
    const modalInfoRef = this.ngbModal.open(InformationModalComponent);
    modalInfoRef.componentInstance.title = 'Confirmation';
    modalInfoRef.componentInstance.message = 'You are about to ' + this.createOrUpdate + ' the role function ' + this.roleFunction.name + '. Do you want to continue?';
    modalInfoRef.result.then((_res) => {
      if (_res === 'Ok') {
        this.showSpinner = true;        
        var exists = false, x;
        for (x in this.currentRoleFunctions) {
          if (this.currentRoleFunctions[x].type == this.roleFunction.type
            && this.currentRoleFunctions[x].code == this.roleFunction.code
            && this.currentRoleFunctions[x].action == this.roleFunction.action
            && this.currentRoleFunctions[x].name == this.roleFunction.name) {
            this.openConfirmationModal('Confirmation', "Role Function already exist.");
            exists = true;
            this.showSpinner = false;
            break;
          }
          if (!this.editDisable) {
            if (this.currentRoleFunctions[x].type == this.roleFunction.type
              && this.currentRoleFunctions[x].code == this.roleFunction.code
              && this.currentRoleFunctions[x].action == this.roleFunction.action
            ) {
              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.");
              exists = true;
              this.showSpinner = false;
              break;
            }
          }
        }

        if (this.selectedType === 'other'){
          this.roleFunction.type = this.otherTypeValue;
        }else{
          this.roleFunction.type = this.selectedType;
        }
        
        if (!exists && this.roleFunction.name.trim() != '' && this.roleFunction.code.trim() != '') {
          var postData = this.roleFunction;
          this.adminService.saveRoleFunction(JSON.stringify(postData))
          .subscribe(_data => {
            this.showSpinner = false;
            if (this.editRoleFunction) {
              this.editRoleFunction.name = this.roleFunction.name;
              this.passBackRoleFunctionPopup.emit(this.editRoleFunction);
            } else{
              this.passBackRoleFunctionPopup.emit(this.roleFunction);
            }
            if (this.editRoleFunction) {
               this.openConfirmationModal('Success', "Role function updated successfully.");
            }else{
              this.openConfirmationModal('Success', "Role function created successfully.");
            }  
          }, error =>{
            this.showSpinner = false;
            this.openConfirmationModal('Error', error.message);
          });
        }
      }
    }, (_dismiss) => {

    })
  }


  openConfirmationModal(_title: string, _message: string) {
    const modalInfoRef = this.ngbModal.open(ConfirmationModalComponent);
    modalInfoRef.componentInstance.title = _title;
    modalInfoRef.componentInstance.message = _message;
  }

}