This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
add-connection-wizard.component.ts
624 lines (563 loc) · 20.7 KB
/
add-connection-wizard.component.ts
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/**
* @license
* Copyright 2017 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file 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.
*/
import {
Component,
OnInit,
ViewChild,
ViewEncapsulation,
} from "@angular/core";
import { FormControl, FormGroup } from "@angular/forms";
import { AbstractControl } from "@angular/forms";
import { Router } from "@angular/router";
import { ConnectionType } from "@connections/shared/connection-type.model";
import { ConnectionService } from "@connections/shared/connection.service";
import { ConnectionsConstants } from "@connections/shared/connections-constants";
import { NewConnection } from "@connections/shared/new-connection.model";
import { ServiceCatalogSource } from "@connections/shared/service-catalog-source.model";
import { LoggerService } from "@core/logger.service";
import { NotificationType, WizardEvent } from "patternfly-ng";
import { WizardStepConfig } from "patternfly-ng";
import { WizardConfig } from "patternfly-ng";
import { WizardComponent } from "patternfly-ng";
import { SelectionService } from "@core/selection.service";
@Component({
encapsulation: ViewEncapsulation.None,
selector: "app-add-connection-wizard",
templateUrl: "./add-connection-wizard.component.html",
styleUrls: ["./add-connection-wizard.component.css"]
})
export class AddConnectionWizardComponent implements OnInit {
public readonly connectionSummaryLink: string = ConnectionsConstants.connectionsRootPath;
public emptyServiceCatalogSource = new ServiceCatalogSource(); // a bogus service catalog source used in drop down to give instructions
public readonly selectServiceCatalogSourceErrorMsg = "A service catalog source must be selected";
// Wizard Config
public wizardConfig: WizardConfig;
public connectionBasicPropertyForm: FormGroup;
public createComplete = true;
public createSuccessful = false;
public connectionTypesLoading = true;
public connectionTypesLoadSuccess = false;
public serviceCatalogSourcesLoading = true;
public serviceCatalogSourcesLoadSuccess = false;
public nameValidationError = "";
public selectedServiceCatalogSource: ServiceCatalogSource;
// Wizard Step 1
public step1Config: WizardStepConfig;
// Wizard Step 2
public step2Config: WizardStepConfig;
public step2aConfig: WizardStepConfig;
public step2bConfig: WizardStepConfig;
public noSourcesNotificationDismissable = false;
public noSourcesNotificationHeader = "No Sources Available";
public noSourcesNotificationMessage = "No Sources of the correct type to select. ";
public noSourcesNotificationType = NotificationType.DANGER;
@ViewChild("wizard") public wizard: WizardComponent;
private connectionService: ConnectionService;
private selectionService: SelectionService;
private selectedConnTypes: ConnectionType[] = [];
private connTypes: ConnectionType[] = [];
private serviceCatSources: ServiceCatalogSource[] = [];
private logger: LoggerService;
private router: Router;
private errorDetailMessage: string;
private theFinalPageTitle = "";
private theFinalPageMessage = "";
constructor( router: Router, connectionService: ConnectionService,
selectionService: SelectionService, logger: LoggerService ) {
this.connectionService = connectionService;
this.selectionService = selectionService;
this.router = router;
this.logger = logger;
this.emptyServiceCatalogSource.setId( " -- select catalog source -- " );
this.selectedServiceCatalogSource = this.emptyServiceCatalogSource;
this.createConnectionBasicPropertyForm();
}
/*
* Initialization
*/
public ngOnInit(): void {
// Step 1 - Basic Properties
this.step1Config = {
id: "step1",
priority: 0,
title: "Connection Type",
allowClickNav: false
} as WizardStepConfig;
// Step 3 - Review and Create
this.step2Config = {
id: "step2",
priority: 0,
title: "Connection Definition",
allowClickNav: false
} as WizardStepConfig;
this.step2aConfig = {
id: "step2a",
priority: 0,
title: "Define",
allowClickNav: false
} as WizardStepConfig;
this.step2bConfig = {
id: "step2b",
priority: 1,
title: "Review",
allowClickNav: false
} as WizardStepConfig;
// Wizard Configuration
this.wizardConfig = {
embedInPage: true,
loadingTitle: "Add Connection Wizard loading",
loadingSecondaryInfo: "Please wait for the wizard to finish loading...",
contentHeight: "500px",
done: false
} as WizardConfig;
// Load the available connection types for the first step
this.connTypes = this.connectionService.getConnectionTypes();
this.connectionTypesLoadSuccess = true;
this.connectionTypesLoading = false;
// Select connection type if editing
if ( this.selectionService.hasSelectedConnection ) {
const selectedConnection = this.selectionService.getSelectedConnection();
const connType = selectedConnection.getDriverName();
for (const cType of this.connectionTypes) {
if (connType === cType.getName()) {
this.onConnectionTypeSelected(cType);
break;
}
}
}
}
// ----------------
// Public Methods
// ----------------
public handleNameChanged( input: AbstractControl ): void {
const self = this;
this.connectionService.isValidName( input.value ).subscribe(
( errorMsg ) => {
if ( errorMsg ) {
// only update if error has changed
if ( errorMsg !== self.nameValidationError ) {
self.nameValidationError = errorMsg;
}
} else { // name is valid
self.nameValidationError = "";
}
self.updatePage2ValidStatus();
},
( error ) => {
self.logger.error( "[handleNameChanged] Error: %o", error );
} );
}
/*
* Return the name valid state
*/
public get nameValid(): boolean {
if ( this.selectionService.hasSelectedConnection ) {
return true;
}
return this.nameValidationError == null || this.nameValidationError.length === 0;
}
/**
* Handles service catalog source change
* @param {string} newValue the new serviceCatalog source
*/
public selectedServiceCatalogSourceChanged( newValue ): void {
this.selectedServiceCatalogSource = this.serviceCatSources.find((src) => src.getId() === newValue);
if (!this.selectedServiceCatalogSource || this.selectedServiceCatalogSource === null) {
this.selectedServiceCatalogSource = this.emptyServiceCatalogSource;
}
this.updatePage2ValidStatus();
}
/*
* Determine if there are one or more service catalog sources
*/
public get hasServiceCatalogSources(): boolean {
return this.serviceCatSources.length > 0;
}
/**
* @returns {boolean} `true` if a service catalog source has been selected
*/
public get hasSelectedServiceCatalogSource(): boolean {
return ( this.selectedServiceCatalogSource != null ) && ( this.selectedServiceCatalogSource !== this.emptyServiceCatalogSource );
}
/**
* @returns {boolean} `true` if the serviceCatalogSource is selected, but type is different than
* the selected connection type.
*/
public get serviceCatalogSelectedWrongType(): boolean {
if (!this.hasSelectedServiceCatalogSource) {
return false;
}
const selectedSvcCatSourceType = this.selectedServiceCatalogSource.getType();
const selectedConnType = this.selectedConnTypes[0];
return selectedConnType.getName() !== selectedSvcCatSourceType;
}
/**
* Gets the Title to be displayed on the final wizard page
* @returns {string}
*/
public get finalPageTitle(): string {
return this.theFinalPageTitle;
}
/**
* Gets the message to be displayed on the final wizard page
* @returns {string}
*/
public get finalPageMessage(): string {
return this.theFinalPageMessage;
}
/**
* @returns {string} the error details message
*/
public get errorDetails(): string {
return this.errorDetailMessage;
}
/*
* Step 1 instruction message
*/
public get step1InstructionMessage(): string {
return "Please select a Connection type";
}
/*
* Step 2 instruction message
*/
public get step2InstructionMessage(): string {
if (this.serviceCatSources.length === 0) {
return "No sources available";
} else if (!this.nameValid) {
return "Please enter a name for the Connection";
} else if (!this.hasSelectedServiceCatalogSource) {
return "Please select a catalog source for the Connection";
} else {
if (this.selectionService.hasSelectedConnection) {
return "Review selections. Click Update to update the Connection";
} else {
return "When finished, click Create to create the Connection";
}
}
}
/*
* Return the name error message if invalid
*/
public getConnectionBasicPropertyErrorMessage( name: string ): string {
const control: AbstractControl = this.connectionBasicPropertyForm.controls[name];
if (control.invalid) {
// The first error found is returned
if (control.errors.required) {
return name + " is a required property";
}
}
return "";
}
/*
* Return the array of ConnectionTypes
*/
public get connectionTypes(): ConnectionType[] {
return this.connTypes;
}
/*
* Return the currently selected ConnectionType
*/
public get selectedConnectionTypes(): ConnectionType[] {
return this.selectedConnTypes;
}
/**
* Handles connection type selection
* @param {ConnectionType} connectionType the connection type
*/
public onConnectionTypeSelected(connectionType: ConnectionType): void {
// Only allow one item to be selected
this.selectedConnTypes.shift();
this.selectedConnTypes.push(connectionType);
// Selecting type clears the serviceCatalog source selection
this.selectedServiceCatalogSource = this.emptyServiceCatalogSource;
this.updatePage1ValidStatus();
}
/**
* Handles connection type de-selection
* @param {ConnectionType} connectionType the connection type
*/
public onConnectionTypeDeselected(connectionType: ConnectionType): void {
// Only one item is selected at a time
this.selectedConnTypes.shift();
}
/*
* Return the array of ServiceCatalogSources
*/
public get serviceCatalogSources(): ServiceCatalogSource[] {
return this.serviceCatSources;
}
public nextClicked($event: WizardEvent): void {
// When leaving page 1, load the available service catalog sources for the selected type
if ($event.step.config.id === "step1") {
// load the available catalog sources
this.loadServiceCatalogSources(this.selectedConnTypes[0]);
}
}
public cancelClicked($event: WizardEvent): void {
const link: string[] = [ ConnectionsConstants.connectionsRootPath ];
this.logger.log("[AddConnectionWizardComponent] Navigating to: %o", link);
this.router.navigate(link).then(() => {
// nothing to do
});
}
/*
* Create the Connection via komodo REST interface,
* using the currently entered properties
*/
public createConnection(): void {
this.createComplete = false;
this.createSuccessful = false;
const connection: NewConnection = new NewConnection();
// Connection basic properties
connection.setName(this.connectionName);
connection.setDescription(this.connectionDescription);
connection.setServiceCatalogSource(this.selectedServiceCatalogSource.getId());
if (this.selectionService.hasSelectedConnection) {
this.updateDeployConnection(connection);
} else {
this.createDeployConnection(connection);
}
}
public stepChanged($event: WizardEvent): void {
if ($event.step.config.id === "step1") {
this.wizardConfig.nextTitle = "Next >";
this.updatePage1ValidStatus();
} else if ($event.step.config.id === "step2a") {
if (this.selectionService.hasSelectedConnection) {
this.wizardConfig.nextTitle = "Update";
} else {
this.wizardConfig.nextTitle = "Create";
}
this.updatePage2ValidStatus();
} else if ($event.step.config.id === "step2b") {
this.step2Config.nextEnabled = false;
} else {
this.wizardConfig.nextTitle = "Next >";
}
}
/**
* @returns {string} the name of the connection
*/
public get connectionName(): string {
return this.connectionBasicPropertyForm.controls["name"].value;
}
/**
* @returns {string} the description of the connection
*/
public get connectionDescription(): string {
return this.connectionBasicPropertyForm.controls["description"].value;
}
// ----------------
// Private Methods
// ----------------
/**
* Create the Connection Basic properties form
*/
private createConnectionBasicPropertyForm(): void {
this.connectionBasicPropertyForm = new FormGroup({
name: new FormControl( "", this.handleNameChanged.bind( this ) ),
description: new FormControl(""),
});
// Initialize form values
if (!this.selectionService.hasSelectedConnection) {
this.connectionBasicPropertyForm.controls["name"].setValue(null);
this.connectionBasicPropertyForm.controls["description"].setValue(null);
} else {
const selectedConnection = this.selectionService.getSelectedConnection();
this.connectionBasicPropertyForm.controls["name"].setValue(selectedConnection.name);
this.connectionBasicPropertyForm.controls["description"].setValue(selectedConnection.getDescription());
this.connectionBasicPropertyForm.get("name").disable();
}
// Responds to basic property changes - updates the page status
this.connectionBasicPropertyForm.valueChanges.subscribe((val) => {
this.updatePage2ValidStatus( );
});
}
/**
* Load the available service catalog sources for the supplied connection type
* @param {ConnectionType} connType the connection type
*/
private loadServiceCatalogSources(connType: ConnectionType): void {
// Load the available service catalog sources for the second step
this.serviceCatalogSourcesLoading = true;
this.serviceCatalogSourcesLoadSuccess = false;
const self = this;
this.connectionService
.getAllServiceCatalogSources()
.subscribe(
(sources) => {
// Only keep the service catalog sources whose type matches the connectionType. empty source is always included.
self.serviceCatSources = [];
for ( const source of sources ) {
if ( source.getType() === connType.getName() ) {
self.serviceCatSources.push(source);
}
}
// Create mode - if only one service catalog source available, pre-select it
if (!self.selectionService.hasSelectedConnection && self.serviceCatSources.length === 1) {
self.selectedServiceCatalogSource = self.serviceCatSources[0];
}
// Edit mode select the service catalog source
if (self.selectionService.hasSelectedConnection && (!self.hasSelectedServiceCatalogSource || self.serviceCatalogSelectedWrongType)) {
const selectedConnection = self.selectionService.getSelectedConnection();
const connSvcSourceName = selectedConnection.getServiceCatalogSourceName();
self.selectedServiceCatalogSource = this.emptyServiceCatalogSource;
for (const svcSource of self.serviceCatSources) {
if (svcSource.getId() === connSvcSourceName) {
self.selectedServiceCatalogSource = svcSource;
break;
}
}
}
self.updatePage2ValidStatus();
self.serviceCatalogSourcesLoading = false;
self.serviceCatalogSourcesLoadSuccess = true;
},
(error) => {
// Creates the connection property form
this.createConnectionBasicPropertyForm();
self.logger.error("[AddConnectionWizardComponent] Error getting service catalog sources: %o", error);
self.updatePage2ValidStatus();
self.serviceCatalogSourcesLoading = false;
self.serviceCatalogSourcesLoadSuccess = false;
}
);
}
/**
* Updates the page 1 status
*/
private updatePage1ValidStatus( ): void {
this.step1Config.nextEnabled = this.selectedConnTypes.length > 0;
}
/**
* Updates the page 2 status
*/
private updatePage2ValidStatus( ): void {
if (!this.step2aConfig) {
return;
}
if (this.selectionService.hasSelectedConnection) {
this.step2aConfig.nextEnabled = this.connectionBasicPropertyForm.valid;
} else {
this.step2aConfig.nextEnabled = this.nameValid && this.hasSelectedServiceCatalogSource;
}
}
/**
* Creates the workspace connection, binds to serviceCatalogSource,
* and deploys a corresponding connection VDB to teiid.
* @param {Connection} connection the new connection
*/
private createDeployConnection(connection: NewConnection): void {
const self = this;
// flag with selection service that this connection will need schema generated
this.selectionService.setConnectionIdForSchemaRegen(connection.getName());
this.connectionService
.createDeployConnection(connection)
.subscribe(
(wasSuccess) => {
self.setFinalPageComplete(wasSuccess);
},
(error) => {
self.logger.error("[AddConnectionWizardComponent] Error creating connection: %o", error);
self.setErrorDetails(error);
self.setFinalPageComplete(false);
}
);
}
/**
* Updates the workspace connection, binds to serviceCatalogSource,
* and re-deploys the corresponding connection VDB to teiid.
* @param {Connection} connection the new connection
*/
private updateDeployConnection(connection: NewConnection): void {
const self = this;
this.connectionService
.updateDeployConnection(connection)
.subscribe(
(wasSuccess) => {
self.setFinalPageComplete(wasSuccess);
},
(error) => {
self.logger.error("[AddConnectionWizardComponent] Error updating connection: %o", error);
self.setErrorDetails(error);
self.setFinalPageComplete(false);
}
);
}
/**
* Sets the final page in progress status
*/
private setFinalPageInProgress(): void {
this.createComplete = false;
this.createSuccessful = false;
if (this.selectionService.hasSelectedConnection) {
this.theFinalPageTitle = "Update in progress";
this.theFinalPageMessage = "The connection is being updated.";
} else {
this.theFinalPageTitle = "Creation in progress";
this.theFinalPageMessage = "The connection is being created.";
}
this.step2bConfig.nextEnabled = false;
this.step2bConfig.previousEnabled = false;
}
/**
* Sets the final page completion status
* @param {boolean} wasSuccessful 'true' if the create or update was successful
*/
private setFinalPageComplete(wasSuccessful: boolean): void {
this.createComplete = true;
this.createSuccessful = wasSuccessful;
this.step2bConfig.nextEnabled = false;
this.step2bConfig.previousEnabled = true;
if (wasSuccessful) {
if (this.selectionService.hasSelectedConnection) {
this.theFinalPageTitle = "Update was successful";
this.theFinalPageMessage = "The connection was updated successfully. Click on the button to see all connections.";
} else {
this.theFinalPageTitle = "Creation was successful";
this.theFinalPageMessage = "The connection was created successfully. Click on the button to see all connections.";
}
} else {
if (this.selectionService.hasSelectedConnection) {
this.theFinalPageTitle = "Update failed";
this.theFinalPageMessage = "The connection update failed!";
} else {
this.theFinalPageTitle = "Creation failed";
this.theFinalPageMessage = "The connection creation failed!";
}
}
}
/**
* Sets the error details for the response
* @param resp the rest call response
*/
private setErrorDetails( resp: any ): void {
// Get the error from the response json
this.errorDetailMessage = "";
if (resp) {
try {
this.errorDetailMessage = resp.json().error;
} catch ( e ) {
this.errorDetailMessage = resp.text();
}
}
// Error visible if message has content
if (this.errorDetailMessage.length === 0) {
this.errorDetailMessage = "Please check data virtualization entries and retry";
}
}
}