summaryrefslogtreecommitdiffstats
path: root/portal-FE-os/src/app/pages/application-onboarding/application-details-dialog/application-details-dialog.component.ts
blob: 852e97065fd34d37069c9a366504a528938797a3 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * 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, ViewChild, ElementRef, Inject, PLATFORM_ID, Injector } from '@angular/core';
import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { IApplications } from 'src/app/shared/model/applications-onboarding/applications';
import { ApplicationsService } from 'src/app/shared/services';
import { ConfirmationModalComponent } from 'src/app/modals/confirmation-modal/confirmation-modal.component';
import { InformationModalComponent } from 'src/app/modals/information-modal/information-modal.component';
import { isPlatformBrowser } from '@angular/common';

@Component({
  selector: 'app-application-details-dialog',
  templateUrl: './application-details-dialog.component.html',
  styleUrls: ['./application-details-dialog.component.scss'],
})
export class ApplicationDetailsDialogComponent implements OnInit {

  addEditAction: any;
  emptyImg = null;
  emptyImgForPreview:string;
  conflictMessages = {};
  result: any;
  isEditMode: boolean = false;
  appImageTypeError: boolean = false;
  isSaving: boolean = false;
  originalImage: any;
  ECOMP_URL_REGEX = "/^((?:https?\:\/\/|ftp?\:\/\/)?(w{3}.)?(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)[^-_.]$/i";
  private ngbModal:NgbModal;

  constructor(public activeModal: NgbActiveModal,
    public applicationsService : ApplicationsService,@Inject(PLATFORM_ID)private platformId:Object,private injector:Injector) {
      if(isPlatformBrowser(this.platformId))
      {
        this.ngbModal = this.injector.get(NgbModal);
      }
     }

  @Input() applicationObj: IApplications;
  @Input() action: any;
  @Output() passEntry: EventEmitter<any> = new EventEmitter();
  @ViewChild('applicationName') applicationNameElement: ElementRef;
  @ViewChild('inputAppImageUpload') inputAppImageUpload: ElementRef;

  newAppModel = {
    'id': null,
    'appName': null,
    'imageUrl': null,
    'appDescription': null,
    'appNotes': null,
    'landingPage': null,
    'alternateUrl': null,
    'restUrl': null,
    'isOpen': false,
    'appBasicAuthUsername': null,
    'appBasicAuthPassword': null,
    'thumbnail': this.emptyImg,
    'isEnabled': false,
    'applicationType': null,
    'rolesInAAF': false,
    'nameSpace': null,
    'uebTopicName': null,
    'uebKey': null,
    'uebSecret': null,
    'imageLink': null,
    'usesCadi': true,
    'modeOfIntegration': null,
    'appAck': false,
    'restrictedApp': false
  };

  applicationTypeArray: any[] = [
    { name: 'GUI Application', value: "1" },
    { name: 'HyperLink Application', value: "2" },
    { name: 'Non-GUI Application', value: "3" }

  ];

  rolesManagementType: any[] = [
    { name: 'Roles in Application (Non-Centralized)', value: false },
    { name: 'Roles in AAF (Centralized)', value: true }
  ];

  modeOfIntegration: any[] = [
    { name: 'Portal SDK Based', value: 'sdk' },
    { name: 'Framework Based', value: 'fw' }
  ]

  ngOnInit() {

    this.addEditAction = this.action;


    if (this.action === 'add') {
      this.applicationObj.applicationType = "1";
      if (this.applicationObj.applicationType == '1') {
        this.applicationObj.modeOfIntegration = this.modeOfIntegration[0].value;
      }
      this.applicationObj.rolesInAAF = true;
      this.applicationObj.usesCadi = true;
      this.applicationObj.appAck = false;
      console.log("Action : ", this.action);
      console.log("Focus : ", this.applicationNameElement.nativeElement);
      setTimeout(() => { // this will make the execution after the above boolean has changed
        this.applicationNameElement.nativeElement.focus();
      }, 0);
    }

    if (this.applicationObj.id) {
      this.isEditMode = true;
    }else{
      this.isEditMode = false;
    }
    //console.log("isEditMode :: ",this.isEditMode);
    this.originalImage = null
    this.emptyImgForPreview = '../../../assets/images/default_app_image.gif';
  }

  appImageHandler(event: any){
    var reader = new FileReader();
    if(event.target.files && event.target.files[0]){
      reader.readAsDataURL(event.target.files[0]); // read file as data url
      var fileName = event.target.files[0].name;
      var validFormats = ['jpg', 'jpeg', 'bmp', 'gif', 'png'];
      //Get file extension
      var ext = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
      //console.log("fileName::>>",fileName ,ext)
      //console.log("fileExtetion::>>",ext)
      //Check for valid format
      if(validFormats.indexOf(ext) == -1){
          this.newAppModel.thumbnail = this.emptyImg;
          this.originalImage = null;
          this.applicationObj.imageUrl = null;
          this.applicationObj.imageLink = null;
          this.applicationObj.thumbnail = null;
          if(!this.isEditMode){
            this.newAppModel.imageUrl = null;
            this.newAppModel.imageLink = null;
            this.newAppModel.thumbnail = null;
          }
          this.appImageTypeError=true;
      }else{
        reader.onload = (event: any) => { // called once readAsDataURL is completed
          this.applicationObj.imageLink = event.target.result;
          this.applicationObj.imageUrl = event.target.result;
          this.applicationObj.thumbnail = event.target.result;
          this.originalImage =  event.target.result;
          if(!this.isEditMode){
            this.newAppModel.imageLink = event.target.result;
            this.newAppModel.imageUrl = event.target.result;
            this.newAppModel.thumbnail = event.target.result;
            this.originalImage =  event.target.result;
          }
        }
      }
    }
  }

  removeImage(){
    let confirmationMsg = 'Are you sure you want to remove the image?';
    this.openInformationModal("Confirmation",confirmationMsg).result.then((result) => {
      if (result === 'Ok') {
        //this.imageApi.clearFile();
        this.applicationObj.thumbnail = this.emptyImg;
        this.originalImage = null;
        this.applicationObj.imageUrl = null;
        this.applicationObj.imageLink = null;
        this.emptyImgForPreview = '../../../assets/images/default_app_image.gif';
        this.inputAppImageUpload.nativeElement.value = "";
      }
    }, (resut) => {
      return;
    })
  }

  /** Add/Edit Application Method*/
  saveChanges() {
    //console.log("addNewApplication getting called..");
    if (this.applicationObj.rolesInAAF) {
      //if valid.
      if (this.applicationObj.applicationType == "1") {
        console.log("Gui Application valodations");
        //if valid.
        if (!this.applicationObj.isEnabled) {
          if (((this.applicationObj.appName == 'undefined' || !this.applicationObj.appName) || (this.applicationObj.nameSpace == 'undefined'
            || !this.applicationObj.nameSpace) || (this.applicationObj.appBasicAuthUsername == 'undefined' || !this.applicationObj.appBasicAuthUsername))) {
            this.openConfirmationModal('', 'Please fill in all required fields(*) for centralized application');
            return;
          }
        }
        if (this.applicationObj.isEnabled) {
          if (((this.applicationObj.appName == 'undefined' || !this.applicationObj.appName)
            || (this.applicationObj.landingPage == 'undefined' || !this.applicationObj.landingPage)
            || (this.applicationObj.appBasicAuthUsername == 'undefined' || !this.applicationObj.appBasicAuthUsername) || (this.applicationObj.nameSpace == 'undefined'
              || !this.applicationObj.nameSpace))) {

            this.openConfirmationModal('', 'Please fill in all required fields(*) for centralized active application');
            return;
          }
        }
      } else if (this.applicationObj.applicationType == "3") {
        console.log("Non-Gui Application valodations");
        if (!this.applicationObj.isEnabled) {
          if (((this.applicationObj.appName == 'undefined' || !this.applicationObj.appName) || (this.applicationObj.nameSpace == 'undefined'
            || !this.applicationObj.nameSpace) || (this.applicationObj.appBasicAuthUsername == 'undefined' || !this.applicationObj.appBasicAuthUsername))) {
            this.openConfirmationModal('', 'Please fill in all required fields(*) for centralized application');
            return;
          }
        }

        if (this.applicationObj.isEnabled) {
          if (((this.applicationObj.appName == 'undefined' || !this.applicationObj.appName)
            || (this.applicationObj.appBasicAuthUsername == 'undefined' || !this.applicationObj.appBasicAuthUsername) || (this.applicationObj.nameSpace == 'undefined'
              || !this.applicationObj.nameSpace))) {

            this.openConfirmationModal('', 'Please fill in all required fields(*) for centralized active application');
            return;
          }
        }
      }
    } else {
      console.log("Non-centralized applications validation");
      this.applicationObj.appAck = null;
      if (!this.applicationObj.isEnabled) {
        if ((this.applicationObj.appName == 'undefined' || !this.applicationObj.appName)) {
          this.openConfirmationModal('', 'Please fill in all required field(*) ApplicationName to Save the applictaion');
          return;
        }
      } else if (this.applicationObj.isEnabled && (this.applicationObj.applicationType == "1")) {
        if ((this.applicationObj.appName == 'undefined' || !this.applicationObj.appName)
          || (this.applicationObj.landingPage == 'undefined' || !this.applicationObj.landingPage)
          || (this.applicationObj.appBasicAuthUsername == 'undefined' || !this.applicationObj.appBasicAuthUsername) ||
          (this.applicationObj.appBasicAuthPassword == 'undefined' || !this.applicationObj.appBasicAuthPassword)) {

          this.openConfirmationModal('', 'Please fill in all required fields(*) along with password as the app is not centralized');
          return;
        }
      } else if (this.applicationObj.isEnabled && (this.applicationObj.applicationType == "3")) {
        console.log("Non gui validation");
        if ((this.applicationObj.appName == 'undefined' || !this.applicationObj.appName)
          || (this.applicationObj.appBasicAuthUsername == 'undefined' || !this.applicationObj.appBasicAuthUsername)) {
          this.openConfirmationModal('', 'Please fill in all required fields(*)');
          return;
        }
      } else if (this.applicationObj.isEnabled && (this.applicationObj.applicationType == "2")) {
        if ((this.applicationObj.appName == 'undefined' || !this.applicationObj.appName) || (this.applicationObj.landingPage == 'undefined'
          || !this.applicationObj.landingPage)) {
          this.openConfirmationModal('', 'Please fill in all required fields(*)');
          return;
        }
      }
    }

    //URL Validation
    if (this.applicationObj.isEnabled && this.applicationObj.applicationType == "1") {
      if (this.applicationObj.landingPage && this.applicationObj.landingPage != 'undefined' && this.applicationObj.landingPage != '') {
        let isValidURL = this.isUrlValid(this.applicationObj.landingPage);
        if (!isValidURL) {
          this.openConfirmationModal('Error', 'Application URL must be a valid URL.');
          return;
        }
      } else {
        this.openConfirmationModal('Error', 'Application URL is required.');
        return;
      }
    }


    this.isSaving = true;
    // For a restricted app, null out all irrelevant fields
    if (this.applicationObj.applicationType == "2") {
      console.log("Hyperlinka pplication validation");
      this.newAppModel.restUrl = null;
      this.newAppModel.isOpen = true;
      this.newAppModel.appBasicAuthUsername = null;
      this.newAppModel.appBasicAuthPassword = null;
      this.newAppModel.uebTopicName = null;
      this.newAppModel.uebKey = null;
      this.newAppModel.uebSecret = null;
      this.newAppModel.restrictedApp = true;

      /**Need to set below fields values based on input provided in the dialog */
      this.newAppModel.applicationType = this.applicationObj.applicationType;
      this.newAppModel.appName = this.applicationObj.appName;
      this.newAppModel.landingPage = this.applicationObj.landingPage;
      this.newAppModel.usesCadi = this.applicationObj.usesCadi;
      if (this.applicationObj.isEnabled) {
        this.newAppModel.isEnabled = this.applicationObj.isEnabled;
      }else{
        this.newAppModel.isEnabled = false;
      }
      console.log("New Model : ", this.newAppModel);
    } else {

      /**Need to set below fields values based on input provided in the dialog */
      this.newAppModel.applicationType = this.applicationObj.applicationType;
      this.newAppModel.appName = this.applicationObj.appName;
      this.newAppModel.landingPage = this.applicationObj.landingPage;
      this.newAppModel.restUrl = this.applicationObj.restUrl;
      this.newAppModel.appBasicAuthUsername = this.applicationObj.appBasicAuthUsername;
      this.newAppModel.appBasicAuthPassword = this.applicationObj.appBasicAuthPassword;
      this.newAppModel.modeOfIntegration = this.applicationObj.modeOfIntegration;
      this.newAppModel.usesCadi = this.applicationObj.usesCadi;
      this.newAppModel.appAck = this.applicationObj.appAck;

       if(this.applicationObj.isEnabled){
        this.newAppModel.isEnabled = this.applicationObj.isEnabled;
       }else{
        this.newAppModel.isEnabled = false;
       }

       if(this.applicationObj.isOpen){
        this.newAppModel.isOpen = this.applicationObj.isOpen;
       }else{
        this.newAppModel.isOpen = false;
       }
       //console.log("this.applicationObj.isOpen",this.applicationObj.isOpen);

      if (this.applicationObj.rolesInAAF) {
        this.newAppModel.rolesInAAF = this.applicationObj.rolesInAAF;
      } else {
        this.newAppModel.rolesInAAF = false;
        this.newAppModel.usesCadi = false;
      }

    }

    if (this.applicationObj.nameSpace=="") {
      this.newAppModel.nameSpace = null;
    }else{
      this.newAppModel.nameSpace = this.applicationObj.nameSpace;
    }

    if (this.applicationObj.applicationType == "2" || this.applicationObj.applicationType == "3") {
      this.applicationObj.modeOfIntegration = null;
    }

    if (this.newAppModel.applicationType == "2" || this.newAppModel.applicationType == "3") {
      this.newAppModel.modeOfIntegration = null;
    }

    if (this.isEditMode) {
      console.log("Edit application Object : ", JSON.stringify(this.applicationObj));
      console.log("Mode Of iNtegration : ", this.applicationObj.modeOfIntegration);
      this.applicationsService.updateOnboardingApp(this.applicationObj)
        .subscribe( _data => {
          this.result = _data;
          //console.log("update application response :: ",this.result);
          if(this.result !=null && this.result.httpStatusCode ==200){
            this.passEntry.emit(this.result);
            this.ngbModal.dismissAll();
          }else{
            this.openConfirmationModal('Error','There was a problem updating the application changes. Please try again. If the problem persists, then try again later. Error: '+this.result);
            return
          }
        }, error => {
          console.log(error);
          if(error.status == 409){
            this.openConfirmationModal('Error', 'There was a problem updating the application changes. ' +
            'The Application Name and Namespace should  be unique.  Error: ' +
            error.status);
            return;
          }else if(error.status == 500){
            this.openConfirmationModal('Error', 'There was a problem updating the application information. ' +
            'Please try again later. Error: ' +error.status);
            return;
          }else if(error.status == 404 || error.status == 403){
            this.openConfirmationModal('Error', 'There was a problem updating the application information. ' +
            'Invalid namespace. Error: ' +error.status);
            return;
          }else if(error.status == 401){
            this.openConfirmationModal('Error', 'There was a problem updating the application information. ' +
            'Portal Mechid is unauthorized to access the given namespace. Error: ' +error.status);
            return;
          }else if(error.status == 400){
            this.openConfirmationModal('Error','Bad Request Error: ' + error.status);
            return;
          } else{
            this.openConfirmationModal('Error', 'There was a problem updating the application changes. ' +
            'Please try again. If the problem persists, then try again later. Error: ' +
            error.status);
            return;
          }
      });

    }else{
      //console.log("Coming inside add application",this.newAppModel);

      this.applicationsService.addOnboardingApp(this.newAppModel)
        .subscribe( _data => {
          this.result = _data;
          //console.log("Add application response :: ",this.result);
          if(this.result !=null && this.result.httpStatusCode ==200){
            this.passEntry.emit(this.result);
            this.ngbModal.dismissAll();
          }else{
            this.openConfirmationModal('Error','There was a problem adding the application changes. Please try again. If the problem persists, then try again later. Error: '+this.result);
            return
          }
        }, error => {
          console.log(error);
          if(error.status == 409){
            this.openConfirmationModal('Error', 'There was a problem adding the application changes. ' +
            'The Application Name and URL should  be unique.  Error: ' +
            error.status);
            return;
          } else if(error.status == 500){
            this.openConfirmationModal('Error', 'There was a problem adding the application information. ' +
            'Please try again later. Error: ' +error.status);
            return;
          }else if(error.status == 400){
            this.openConfirmationModal('Error','Bad Request Error: ' + error.status);
            return;
          } else{
            this.openConfirmationModal('Error', 'There was a problem adding the application changes. ' +
            'Please try again. If the problem persists, then try again later. Error: ' +
            error.status);
            return;
          }
      });
    }
  }

  isUrlValid(userInput){
    //let  regexQuery = "/^((?:https?\:\/\/|ftp?\:\/\/)?(w{3}.)?(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)[^-_.]$/i";
    let  regexQuery = "https?://.+";
    let res = userInput.match(regexQuery);
    if(res == null){
      return false;
    }else{
      return true;
    }
  }

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

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