This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
add-dataservice-wizard.component.ts
562 lines (503 loc) · 17.5 KB
/
add-dataservice-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
/**
* @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, OnDestroy,
OnInit,
ViewChild,
ViewEncapsulation,
} from "@angular/core";
import { FormControl, FormGroup } from "@angular/forms";
import { AbstractControl } from "@angular/forms";
import { Router } from "@angular/router";
import { ConnectionTable } from "@connections/shared/connection-table.model";
import { Connection } from "@connections/shared/connection.model";
import { LoggerService } from "@core/logger.service";
import { ConnectionTableSelectorComponent } from "@dataservices/connection-table-selector/connection-table-selector.component";
import { DataserviceService } from "@dataservices/shared/dataservice.service";
import { DataservicesConstants } from "@dataservices/shared/dataservices-constants";
import { NewDataservice } from "@dataservices/shared/new-dataservice.model";
import { NotifierService } from "@dataservices/shared/notifier.service";
import { VdbService } from "@dataservices/shared/vdb.service";
import { WizardService } from "@dataservices/shared/wizard.service";
import { NotificationType, WizardComponent } from "patternfly-ng";
import { WizardEvent } from "patternfly-ng";
import { WizardStepConfig } from "patternfly-ng";
import { WizardConfig } from "patternfly-ng";
@Component({
encapsulation: ViewEncapsulation.None,
selector: "app-add-dataservice-wizard",
templateUrl: "./add-dataservice-wizard.component.html",
styleUrls: ["./add-dataservice-wizard.component.css"]
})
export class AddDataserviceWizardComponent implements OnInit, OnDestroy {
public emptyConnection = new Connection(); // a bogus connection used in drop down to give instructions
public readonly selectConnectionErrorMsg = "A connection must be selected";
// Wizard Config
public wizardConfig: WizardConfig;
public basicPropertyForm: FormGroup;
public createComplete = true;
public createSuccessful = false;
// Wizard Step 1
public step1Config: WizardStepConfig;
public connectionNotificationDismissable = false;
public connectionNotificationHeader = "Source Selection";
public connectionNotificationMessage = "You can select tables from multiple connections, but currently we only support "
+ " tables from a single connection (multiple connections coming soon). You will "
+ "choose the connection in the next step.";
public connectionNotificationType = NotificationType.WARNING;
// Wizard Step 2
public step2Config: WizardStepConfig;
public step2aConfig: WizardStepConfig;
public step2bConfig: WizardStepConfig;
@ViewChild("wizard") public wizard: WizardComponent;
@ViewChild(ConnectionTableSelectorComponent) public tableSelector: ConnectionTableSelectorComponent;
public selectedConnection: Connection;
public nameValidationError = "";
private dataserviceService: DataserviceService;
private notifierService: NotifierService;
private vdbService: VdbService;
private wizardService: WizardService;
private logger: LoggerService;
private router: Router;
private errorDetailMessage: string;
private theFinalPageTitle = "";
private theFinalPageMessage = "";
constructor(router: Router, dataserviceService: DataserviceService, wizardService: WizardService,
notifierService: NotifierService, logger: LoggerService, vdbService: VdbService ) {
this.dataserviceService = dataserviceService;
this.notifierService = notifierService;
this.vdbService = vdbService;
this.wizardService = wizardService;
this.router = router;
this.logger = logger;
this.emptyConnection.setId( " -- select a connection -- " );
this.selectedConnection = this.emptyConnection;
this.createBasicPropertyForm();
}
/*
* Initialization
*/
public ngOnInit(): void {
// Step 1 - Select Tables
this.step1Config = {
id: "step1",
priority: 0,
title: "Select Tables",
allowClickNav: false
} as WizardStepConfig;
// Step 2 - Review and Create
this.step2Config = {
id: "step2",
priority: 0,
title: this.step2Title,
allowClickNav: false
} as WizardStepConfig;
this.step2aConfig = {
id: "step2a",
priority: 0,
title: "Review",
allowClickNav: false
} as WizardStepConfig;
this.step2bConfig = {
id: "step2b",
priority: 1,
title: this.step2bTitle,
allowClickNav: false
} as WizardStepConfig;
// Wizard Configuration
this.wizardConfig = {
embedInPage: true,
loadingTitle: "Data Virtualization Wizard loading",
loadingSecondaryInfo: "Please wait for the wizard to finish loading...",
contentHeight: "500px",
done: false
} as WizardConfig;
if (this.wizardService.isEdit()) {
const selectedService = this.dataserviceService.getSelectedDataservice();
const dsName = selectedService.getId();
const dsDescr = selectedService.getDescription();
this.basicPropertyForm.controls["name"].setValue(dsName);
this.basicPropertyForm.controls["description"].setValue(dsDescr);
this.basicPropertyForm.get("name").disable();
} else {
this.basicPropertyForm.controls["name"].setValue(null);
this.basicPropertyForm.controls["description"].setValue(null);
}
}
public ngOnDestroy(): void {
// Nothing to do
}
// ----------------
// Public Methods
// ----------------
/**
* Determine if Dataservice is being edited.
*/
public get isEdit( ): boolean {
return this.wizardService.isEdit();
}
public handleNameChanged( input: AbstractControl ): void {
const self = this;
this.dataserviceService.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.updatePage2aValidStatus();
},
( error ) => {
self.logger.error( "[handleNameChanged] Error: %o", error );
} );
}
/*
* Return the name valid state
*/
public get nameValid(): boolean {
return this.nameValidationError == null || this.nameValidationError.length === 0;
}
/**
* 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;
}
/*
* Step 1 instruction message
*/
public get step1InstructionMessage(): string {
if (!this.tableSelector.valid) {
return "Please select tables for the Data virtualization";
} else {
return "Select tables, then click Next to continue";
}
}
/*
* Step 2 instruction message
*/
public get step2InstructionMessage(): string {
if (this.wizardService.isEdit()) {
return "Review selections. Click Update to update the Virtualization";
}
return "Enter a name, select a connection, and review the table selections. Click Create to create the Virtualization";
}
/*
* Return the name error message if invalid
*/
public getBasicPropertyErrorMessage( name: string ): string {
const control: AbstractControl = this.basicPropertyForm.controls[name];
if (control.invalid) {
// The first error found is returned
if (control.errors.required) {
return name + " is a required property";
}
}
return "";
}
public nextClicked($event: WizardEvent): void {
if ($event.step.config.id === "step1") {
// TODO implement nextClicked
}
}
public cancelClicked($event: WizardEvent): void {
const link: string[] = [ DataservicesConstants.dataservicesRootPath ];
this.logger.log("[AddDataserviceWizardComponent] Navigating to: %o", link);
this.router.navigate(link).then(() => {
// nothing to do
});
}
/*
* Create the Dataservice via komodo REST interface,
* using the currently entered properties
*/
public createDataservice(): void {
// Sets page in progress status
this.setFinalPageInProgress();
if ( !this.isEdit ) {
this.createDataserviceForSingleSourceTables();
} else {
this.updateDataserviceForSingleSourceTables();
}
}
public onDeployDataservice(): void {
// Start the deployment and then redirect to the dataservice summary page
this.dataserviceService
.deployDataservice(this.dataserviceName)
.subscribe(
(wasSuccess) => {
// Nothing to do
},
(error) => {
// Nothing to do
}
);
const link: string[] = [ DataservicesConstants.dataservicesRootPath ];
this.logger.log("[AddDataserviceWizardComponent] Navigating to: %o", link);
this.router.navigate(link).then(() => {
// nothing to do
});
}
public stepChanged($event: WizardEvent): void {
if ($event.step.config.id === "step1") {
this.updatePage1ValidStatus();
this.wizardConfig.nextTitle = "Next >";
} else if ($event.step.config.id === "step2a") {
this.updatePage2aValidStatus();
if (this.wizardService.isEdit()) {
this.wizardConfig.nextTitle = "Update";
} else {
this.wizardConfig.nextTitle = "Create";
}
} else if ($event.step.config.id === "step2b") {
// Note: The next button is not disabled by default when wizard is done
this.step2Config.nextEnabled = false;
} else {
this.wizardConfig.nextTitle = "Next >";
}
}
/**
* @returns {string} the name of the dataservice
*/
public get dataserviceName(): string {
return this.basicPropertyForm.controls["name"].value;
}
/**
* @returns {string} the description of the dataservice
*/
public get dataserviceDescription(): string {
return this.basicPropertyForm.controls["description"].value;
}
/**
* @returns {ConnectionTable[]} the selected connection tables
*/
public get dataserviceSourceTables(): ConnectionTable[] {
return this.tableSelector.getSelectedTables();
}
/**
*
* @returns {Connection[]} the selected source table connections
*/
public get sourceTableConnections(): Connection[] {
const tables = this.tableSelector.getSelectedTables();
const connections: Connection[] = [];
for ( const table of tables ) {
const connection = table.getConnection();
if ( connections.indexOf( connection) === -1 ) {
connections.push( connection );
}
}
return connections;
}
public selectedConnectionChanged( $event ): void {
// since the dropdown has a dummy first element subtract 1
const index = $event.target.selectedIndex - 1;
this.selectedConnection = this.sourceTableConnections[ index ];
this.updatePage2aValidStatus();
}
public shouldCheck( table: ConnectionTable ): boolean {
return ( this.selectedConnection && ( table.getConnection() === this.selectedConnection ) );
}
/**
* @returns {boolean} `true` if a connection has been selected
*/
public hasSelectedConnection(): boolean {
return ( this.selectedConnection != null ) && ( this.selectedConnection !== this.emptyConnection );
}
/**
* Updates the page1 status
*/
public updatePage1ValidStatus( ): void {
this.step1Config.nextEnabled = this.tableSelector.valid();
}
/**
* @returns {string} the error details message
*/
public get errorDetails(): string {
return this.errorDetailMessage;
}
// ----------------
// Private Methods
// ----------------
/*
* Create the BasicProperty form (page 1)
*/
private createBasicPropertyForm(): void {
if (!this.wizardService.isEdit()) {
this.basicPropertyForm = new FormGroup({
name: new FormControl( "", this.handleNameChanged.bind( this ) ),
description: new FormControl("")
});
// Responds to basic property changes - updates the page status
this.basicPropertyForm.valueChanges.subscribe((val) => {
this.updatePage2aValidStatus( );
});
} else {
this.basicPropertyForm = new FormGroup({
name: new FormControl( "" ),
description: new FormControl("")
});
}
}
private updatePage2aValidStatus( ): void {
if (!this.step2aConfig) {
return;
}
if (this.wizardService.isEdit()) {
this.step2aConfig.nextEnabled = true;
} else {
this.step2aConfig.nextEnabled = this.nameValid && this.hasSelectedConnection();
}
}
/*
* Create the Dataservice for the selected single source tables. This is invoked
* only after the source VDB has successfully deployed.
*/
private createDataserviceForSingleSourceTables(): void {
// Dataservice basic properties from step 1
const dataservice: NewDataservice = this.dataserviceService.newDataserviceInstance(this.dataserviceName, this.dataserviceDescription);
const self = this;
this.dataserviceService
.createDataserviceForSingleSourceTables(dataservice, this.tableSelector.getSelectedTables())
.subscribe(
(wasSuccess) => {
self.setFinalPageComplete(wasSuccess);
},
(error) => {
self.logger.error("[AddDataserviceWizardComponent] Error: %o", error);
self.setErrorDetails(error);
self.setFinalPageComplete(false);
}
);
}
/**
* Update the selected Dataservice for the selected single source tables. This is invoked
* only after the source VDB has successfully deployed.
*/
private updateDataserviceForSingleSourceTables(): void {
// Dataservice basic properties from step 1
const dataservice: NewDataservice = this.dataserviceService.newDataserviceInstance(this.dataserviceName, this.dataserviceDescription);
const self = this;
this.dataserviceService
.updateDataserviceForSingleSourceTables(dataservice, this.tableSelector.getSelectedTables())
.subscribe(
(wasSuccess) => {
self.setFinalPageComplete(wasSuccess);
},
(error) => {
self.logger.error("[AddDataserviceWizardComponent] Error: %o", error);
self.setErrorDetails(error);
self.setFinalPageComplete(false);
}
);
}
/**
* Step 2 title - changes based on create or edit
* @returns {string} step 2 title
*/
private get step2Title(): string {
if (this.wizardService.isEdit()) {
return "Review and Update";
} else {
return "Review and Create";
}
}
/**
* Step 2b title - changes based on create or edit
* @returns {string} step 2b title
*/
private get step2bTitle(): string {
if (this.wizardService.isEdit()) {
return "Update";
} else {
return "Create";
}
}
/**
* Sets the final page in progress status
*/
private setFinalPageInProgress(): void {
this.createComplete = false;
this.createSuccessful = false;
if (this.wizardService.isEdit()) {
this.theFinalPageTitle = "Update in progress";
this.theFinalPageMessage = "The virtualization is being updated.";
} else {
this.theFinalPageTitle = "Creation in progress";
this.theFinalPageMessage = "The virtualization 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.wizardService.isEdit()) {
this.theFinalPageTitle = "Update was successful";
this.theFinalPageMessage = "The data virtualization was updated successfully. Click on the button to see all virtualizations.";
} else {
this.theFinalPageTitle = "Creation was successful";
this.theFinalPageMessage = "The data virtualization was created successfully. Click on the button to see all virtualizations.";
}
} else {
if (this.wizardService.isEdit()) {
this.theFinalPageTitle = "Update failed";
this.theFinalPageMessage = "The data virtualization update failed!";
} else {
this.theFinalPageTitle = "Creation failed";
this.theFinalPageMessage = "The data virtualization 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";
}
}
}