This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
add-dataservice-wizard.component.ts
483 lines (430 loc) · 15.1 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
/**
* @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 { Validators } from "@angular/forms";
import { Router } from "@angular/router";
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 { VdbStatus } from "@dataservices/shared/vdb-status.model";
import { VdbService } from "@dataservices/shared/vdb.service";
import { VdbsConstants } from "@dataservices/shared/vdbs-constants";
import { LoadingState } from "@shared/loading-state.enum";
import { WizardComponent } from "patternfly-ng";
import { WizardEvent } from "patternfly-ng";
import { WizardStepConfig } from "patternfly-ng";
import { WizardConfig } from "patternfly-ng";
import { Subscription } from "rxjs/Subscription";
@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 readonly dataserviceSummaryLink: string = DataservicesConstants.dataservicesRootPath;
public loadingState = LoadingState; // need local ref of enum for html to use
// Wizard Config
public wizardConfig: WizardConfig;
public basicPropertyForm: FormGroup;
public createComplete = true;
public createSuccessful = false;
public tableSelectorLoadingState = LoadingState.LOADING;
// Wizard Step 1
public step1Config: WizardStepConfig;
// Wizard Step 2
public step2Config: WizardStepConfig;
// Wizard Step 3
public step3Config: WizardStepConfig;
public step3aConfig: WizardStepConfig;
public step3bConfig: WizardStepConfig;
@ViewChild("wizard") public wizard: WizardComponent;
@ViewChild(ConnectionTableSelectorComponent) public tableSelector: ConnectionTableSelectorComponent;
private dataserviceService: DataserviceService;
private notifierService: NotifierService;
private vdbService: VdbService;
private logger: LoggerService;
private router: Router;
private deploymentChangeSubscription: Subscription;
private sourceVdbUnderDeployment: string;
private errorDetailMessage: string;
constructor(router: Router, dataserviceService: DataserviceService,
notifierService: NotifierService, logger: LoggerService, vdbService: VdbService ) {
this.dataserviceService = dataserviceService;
this.notifierService = notifierService;
this.vdbService = vdbService;
this.router = router;
this.logger = logger;
this.createBasicPropertyForm();
}
/*
* Initialization
*/
public ngOnInit(): void {
// Subscribe to Vdb deployment change messages
this.deploymentChangeSubscription = this.notifierService.getVdbDeploymentStatus().subscribe((status) => {
this.onSourceVdbDeploymentStateChanged(status);
});
// Step 1 - Name and Description
this.step1Config = {
id: "step1",
priority: 0,
title: "Name and Description",
allowClickNav: false
} as WizardStepConfig;
// Step 2 - Tables
this.step2Config = {
id: "step2",
priority: 0,
title: "Table Selection",
allowClickNav: false
} as WizardStepConfig;
// Step 3 - Review and Create
this.step3Config = {
id: "step3",
priority: 0,
title: "Review and Create",
allowClickNav: false
} as WizardStepConfig;
this.step3aConfig = {
id: "step3a",
priority: 0,
title: "Review",
allowClickNav: false
} as WizardStepConfig;
this.step3bConfig = {
id: "step3b",
priority: 1,
title: "Create",
allowClickNav: false
} as WizardStepConfig;
// Wizard Configuration
this.wizardConfig = {
embedInPage: true,
loadingTitle: "Add Dataservice Wizard loading",
loadingSecondaryInfo: "Please wait for the wizard to finish loading...",
title: "Add Dataservice",
contentHeight: "500px",
done: false
} as WizardConfig;
this.tableSelectorLoadingState = LoadingState.LOADING;
this.setNavAway(false);
}
public ngOnDestroy(): void {
this.deploymentChangeSubscription.unsubscribe();
}
// ----------------
// Public Methods
// ----------------
/**
* Determine if table selector is loading
*/
public get tableSelectorLoading( ): boolean {
return this.tableSelectorLoadingState === LoadingState.LOADING;
}
/**
* Determine if table selector is loaded and valid
*/
public get tableSelectorLoadedValid( ): boolean {
return this.tableSelectorLoadingState === LoadingState.LOADED_VALID;
}
/**
* Determine if table selector is loaded and invalid
*/
public get tableSelectorLoadedInvalid( ): boolean {
return this.tableSelectorLoadingState === LoadingState.LOADED_INVALID;
}
/*
* Return the name valid state
*/
public get nameValid(): boolean {
return this.basicPropertyForm.controls["name"].valid;
}
/*
* Step 1 instruction message
*/
public get step1InstructionMessage(): string {
if (!this.nameValid) {
return "Please enter a name for the Dataservice";
} else {
return "Click Next to continue";
}
}
/*
* Step 2 instruction message
*/
public get step2InstructionMessage(): string {
if (!this.tableSelector.valid) {
return "Please select tables for the Dataservice";
} else {
return "Select tables, then click Next to continue";
}
}
/*
* Step 3 instruction message
*/
public get step3InstructionMessage(): string {
return "Review your entries. When finished, click Create to create the Dataservice";
}
/*
* 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 {
this.createComplete = false;
this.createSuccessful = false;
const sourceVdbName = this.tableSelector.getSelectedTables()[0].getConnection().getId() + VdbsConstants.SOURCE_VDB_SUFFIX;
this.step3bConfig.nextEnabled = false;
this.step3bConfig.previousEnabled = false;
this.sourceVdbUnderDeployment = sourceVdbName;
const self = this;
this.vdbService
.deployVdbForTable(this.tableSelector.getSelectedTables()[0])
.subscribe(
(wasSuccess) => {
// Deployment succeeded - wait for source vdb to become active
if (wasSuccess) {
self.vdbService.pollForActiveVdb(sourceVdbName, 30, 5);
} else {
self.createComplete = true;
self.createSuccessful = false;
self.step3bConfig.nextEnabled = false;
self.step3bConfig.previousEnabled = true;
self.sourceVdbUnderDeployment = null;
}
},
(error) => {
self.logger.error("[AddDataserviceWizardComponent] Error: %o", error);
self.setErrorDetails(error);
self.createComplete = true;
self.createSuccessful = false;
self.step3bConfig.nextEnabled = false;
self.step3bConfig.previousEnabled = true;
self.sourceVdbUnderDeployment = null;
}
);
}
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
});
}
/*
* Listens for the source VDB deployment completion. If the source VDB is active, proceed with
* creation of the Dataservice
* @param status the VDB deployment status
*/
public onSourceVdbDeploymentStateChanged(status: VdbStatus): void {
// if null received, ignore
if (!status || (status.getName() !== this.sourceVdbUnderDeployment) ) {
return;
// non-null received, unsubscribe to stop any further notifications
} else {
// source VDB state change - no longer watching it's deployment
this.sourceVdbUnderDeployment = null;
}
if (this.tableSelector && this.tableSelector.hasSelectedConnection()) {
const selectedConnectionName = this.tableSelector.selectedConnection.getId();
const selectedVdbName = selectedConnectionName + VdbsConstants.SOURCE_VDB_SUFFIX;
if (selectedVdbName === status.getName()) {
if (status.isActive()) {
this.createDataserviceForSingleTable();
} else if (status.isFailed()) {
this.createComplete = true;
this.createSuccessful = false;
this.step3bConfig.nextEnabled = false;
this.step3bConfig.previousEnabled = true;
}
}
}
}
public stepChanged($event: WizardEvent): void {
if ($event.step.config.id === "step1") {
this.updatePage1ValidStatus();
this.wizardConfig.nextTitle = "Next >";
} else if ($event.step.config.id === "step2") {
this.updatePage2ValidStatus();
this.wizardConfig.nextTitle = "Next >";
} else if ($event.step.config.id === "step3a") {
this.wizardConfig.nextTitle = "Create";
} else if ($event.step.config.id === "step3b") {
// Note: The next button is not disabled by default when wizard is done
this.step3Config.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 {string} the selected source table names in string form
*/
public get dataserviceSourceTables(): string {
let tableStr = "None";
const tables = this.tableSelector.getSelectedTables();
if (tables.length > 0) {
tableStr = tables[0].getConnection().getId() + " [" + tables[0].getName() + "]";
}
if (tables.length === 2) {
tableStr = tableStr + ", " + tables[1].getConnection().getId() + " [" + tables[1].getName() + "]";
}
return tableStr;
}
public updatePage2ValidStatus( ): void {
this.step2Config.nextEnabled = this.tableSelector.valid();
this.setNavAway(this.step2Config.nextEnabled);
}
/**
* @returns {string} the error details message
*/
public get errorDetails(): string {
return this.errorDetailMessage;
}
// ----------------
// Private Methods
// ----------------
/*
* Create the BasicProperty form (page 1)
*/
private createBasicPropertyForm(): void {
this.basicPropertyForm = new FormGroup({
name: new FormControl("", Validators.required),
description: new FormControl("")
});
// Responds to basic property changes - updates the page status
this.basicPropertyForm.valueChanges.subscribe((val) => {
this.updatePage1ValidStatus( );
});
}
private setNavAway(allow: boolean): void {
this.step1Config.allowNavAway = allow;
}
private updatePage1ValidStatus( ): void {
this.step1Config.nextEnabled = this.basicPropertyForm.valid;
this.setNavAway(this.step1Config.nextEnabled);
}
/*
* Create the Dataservice for the selected source table. This is invoked
* only after the source VDB has successfully deployed.
*/
private createDataserviceForSingleTable(): void {
const dataservice: NewDataservice = new NewDataservice();
// Dataservice basic properties from step 1
dataservice.setId(this.dataserviceName);
dataservice.setDescription(this.dataserviceDescription);
const self = this;
this.dataserviceService
.createDataserviceForSingleTable(dataservice, this.tableSelector.getSelectedTables()[0])
.subscribe(
(wasSuccess) => {
// Deployment succeeded - wait for source vdb to become active
if (wasSuccess) {
self.createComplete = true;
self.createSuccessful = true;
self.step3bConfig.nextEnabled = false;
this.step3bConfig.previousEnabled = true;
} else {
self.createComplete = true;
self.createSuccessful = false;
self.step3bConfig.nextEnabled = false;
this.step3bConfig.previousEnabled = true;
}
},
(error) => {
self.logger.error("[AddDataserviceWizardComponent] Error: %o", error);
self.setErrorDetails(error);
self.createComplete = true;
self.createSuccessful = false;
self.step3bConfig.nextEnabled = false;
this.step3bConfig.previousEnabled = true;
}
);
}
/**
* 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) {
this.errorDetailMessage = resp.json().error;
}
// Error visible if message has content
if (this.errorDetailMessage.length === 0) {
this.errorDetailMessage = "Please check dataservice entries and retry";
}
}
}