This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
add-connection-wizard.component.ts
461 lines (411 loc) · 13.8 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
/**
* @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 { Validators } from "@angular/forms";
import { Router } from "@angular/router";
import { ConnectionService } from "@connections/shared/connection.service";
import { ConnectionsConstants } from "@connections/shared/connections-constants";
import { NewConnection } from "@connections/shared/new-connection.model";
import { TemplateDefinition } from "@connections/shared/template-definition.model";
import { LoggerService } from "@core/logger.service";
import { PropertyDefinition } from "@shared/property-form/property-definition.model";
import { PropertyFormComponent } from "@shared/property-form/property-form.component";
import { 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-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;
// Wizard Config
public wizardConfig: WizardConfig;
public basicPropertyForm: FormGroup;
public createComplete = true;
public createSuccessful = false;
public detailPropertiesLoading = true;
public detailPropertiesLoadSuccess = false;
public detailPropertiesLoadedType = "";
public requiredPropValues: Array<[string, string]> = [];
public templatesLoading = true;
public templatesLoadSuccess = false;
// 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(PropertyFormComponent) public detailPropForm: PropertyFormComponent;
private connectionService: ConnectionService;
private allTemplates: TemplateDefinition[] = [];
private detailProperties: Array<PropertyDefinition<any>> = [];
private logger: LoggerService;
private router: Router;
constructor( router: Router, connectionService: ConnectionService, logger: LoggerService ) {
this.connectionService = connectionService;
this.router = router;
this.logger = logger;
this.createBasicPropertyForm();
}
/*
* Initialization
*/
public ngOnInit(): void {
// Step 1 - Basic Properties
this.step1Config = {
id: "step1",
priority: 0,
title: "Basic Properties",
allowClickNav: false
} as WizardStepConfig;
// Step 2 - Advanced Properties
this.step2Config = {
id: "step2",
priority: 0,
title: "Detail Properties",
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 Connection Wizard loading",
loadingSecondaryInfo: "Please wait for the wizard to finish loading...",
title: "Add Connection",
contentHeight: "500px",
done: false
} as WizardConfig;
// Load the templates for the first step
this.templatesLoading = true;
this.templatesLoadSuccess = true;
const self = this;
this.connectionService
.getConnectionTemplates()
.subscribe(
(templates) => {
self.allTemplates = templates;
self.templatesLoading = false;
self.templatesLoadSuccess = true;
},
(error) => {
self.logger.error("[AddConnectionWizardComponent] Error getting templates: %o", error);
self.templatesLoading = false;
self.templatesLoadSuccess = false;
}
);
this.setNavAway(false);
}
// ----------------
// Public Methods
// ----------------
/*
* Return the name valid state
*/
public get nameValid(): boolean {
return this.basicPropertyForm.controls["name"].valid;
}
/*
* Return the driver valid state
*/
public get driverValid(): boolean {
return this.basicPropertyForm.controls["driver"].valid;
}
/*
* Return the jndi valid state
*/
public get jndiValid(): boolean {
return this.basicPropertyForm.controls["jndi"].valid;
}
/*
* Step 1 instruction message
*/
public get step1InstructionMessage(): string {
if (!this.driverValid) {
return "Please select a Connection type";
} else if (!this.nameValid) {
return "Please enter a name for the Connection";
} else if (!this.jndiValid) {
return "Please enter a JNDI identifier for the Connection";
} else {
return "When finished entering properties, click Next to continue";
}
}
/*
* Step 2 instruction message
*/
public get step2InstructionMessage(): string {
return "Enter advanced properties for the Connection, then click Next to continue";
}
/*
* Step 3 instruction message
*/
public get step3InstructionMessage(): string {
return "Review your entries. When finished, click Create to create the Connection";
}
/*
* 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 "";
}
/*
* Return the array of template names
*/
public get templateNames(): string[] {
const templateNames: string[] = [];
for ( const templ of this.allTemplates ) {
templateNames.push(templ.getId());
}
return templateNames.sort();
}
public nextClicked($event: WizardEvent): void {
// When leaving page 1, load the driver-specific property definitions
if ($event.step.config.id === "step1") {
const selectedDriver = this.basicPropertyForm.controls["driver"].value;
if (!this.detailPropertiesLoadSuccess || (this.detailPropertiesLoadedType !== selectedDriver)) {
this.loadPropertyDefinitions(selectedDriver);
}
}
}
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
});
}
/*
* Return the array of [name,value] for the required properties.
* So that the current required property entries can be shown on page 3 (review)
*/
public get requiredPropertyValues(): Array<[string, string]> {
return this.requiredPropValues;
}
/*
* 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 from step 1
connection.setName(this.connectionName);
connection.setJndiName(this.connectionJndiName);
connection.setDriverName(this.connectionDriverName);
connection.setJdbc(this.isJdbc);
// Connection advanced properties from step 2
const propMap: Map<string, string> = this.detailPropForm.propertyValuesNonDefault;
connection.setProperties(propMap);
const self = this;
this.connectionService
.createConnection(connection)
.subscribe(
(wasSuccess) => {
self.createComplete = true;
self.createSuccessful = wasSuccess;
self.step3bConfig.nextEnabled = false;
},
(error) => {
self.logger.error("[AddConnectionWizardComponent] Error: %o", error);
self.createComplete = true;
self.createSuccessful = false;
self.step3bConfig.nextEnabled = false;
}
);
}
public stepChanged($event: WizardEvent): void {
if ($event.step.config.id === "step1") {
this.updatePage1ValidStatus();
} else if ($event.step.config.id === "step3a") {
this.wizardConfig.nextTitle = "Create";
this.updateRequiredPropertyValues();
} 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 >";
}
}
/**
* Handler for property form initialization
* @param {boolean} isValid form valid state
*/
public onDetailPropertyInit(isValid: boolean): void {
this.updatePage2ValidStatus(isValid);
}
/**
* Handler for property form changes
* @param {boolean} isValid form valid state
*/
public onDetailPropertyChanged(isValid: boolean): void {
this.updatePage2ValidStatus(isValid);
}
/**
* @returns {string} the name of the connection
*/
public get connectionName(): string {
return this.basicPropertyForm.controls["name"].value;
}
/**
* @returns {string} the driver name of the connection
*/
public get connectionDriverName(): string {
return this.basicPropertyForm.controls["driver"].value;
}
/**
* @returns {string} the JNDI name of the connection
*/
public get connectionJndiName(): string {
return this.basicPropertyForm.controls["jndi"].value;
}
/**
* @returns {boolean} 'true' if connection is JDBC
*/
public get isJdbc(): boolean {
const driver = this.connectionDriverName;
let jdbc = true;
// TODO: this needs to be a hooked up to komodo call instead
if (driver === null || driver === "cassandra" || driver === "file" || driver === "google"
|| driver === "ldap" || driver === "mongodb" || driver === "salesforce"
|| driver === "salesforce-34" || driver === "solr" || driver === "webservice") {
jdbc = false;
}
return jdbc;
}
// ----------------
// Private Methods
// ----------------
/*
* Create the BasicProperty form (page 1)
*/
private createBasicPropertyForm(): void {
this.basicPropertyForm = new FormGroup({
name: new FormControl("", Validators.required),
jndi: new FormControl("", Validators.required),
driver: new FormControl("", Validators.required)
});
// Responds to basic property changes - updates the page status
const self = this;
this.basicPropertyForm.valueChanges.subscribe((val) => {
self.updatePage1ValidStatus( );
});
}
private setNavAway(allow: boolean): void {
this.step1Config.allowNavAway = allow;
}
private updatePage1ValidStatus( ): void {
this.step1Config.nextEnabled = this.basicPropertyForm.valid;
this.setNavAway(this.step1Config.nextEnabled);
}
private updatePage2ValidStatus(formValid: boolean): void {
this.step2Config.nextEnabled = formValid;
this.setNavAway(this.step2Config.nextEnabled);
}
/**
* Load the driver-specific property definitions
*/
private loadPropertyDefinitions( driverName ): void {
this.detailPropertiesLoading = false;
this.detailPropertiesLoadSuccess = false;
const self = this;
this.connectionService
.getConnectionTemplateProperties(driverName)
.subscribe(
(props) => {
// Sort the properties. (Required properties first)
const firstProps: any[] = [];
const nextProps: any[] = [];
for (const prop of props) {
if (prop.isRequired()) {
firstProps.push(prop);
} else {
nextProps.push(prop);
}
}
self.detailProperties = firstProps.concat(nextProps);
self.detailPropertiesLoading = false;
self.detailPropertiesLoadSuccess = true;
self.detailPropertiesLoadedType = driverName;
},
(error) => {
self.logger.error("[AddConnectionWizardComponent] Error: %o", error);
self.detailPropertiesLoading = false;
self.detailPropertiesLoadSuccess = false;
}
);
}
/**
* @returns {PropertyDefinition<any>[]} the property definitions (can be null)
*/
private getPropertyDefinitions(): Array<PropertyDefinition<any>> {
return this.detailProperties;
}
/**
* Generates array of required property values when page 3 (Review) is shown
*/
private updateRequiredPropertyValues(): void {
const propMap: Map<string, string> = new Map<string, string>();
for (const property of this.detailProperties) {
if (property.isRequired()) {
const name = property.getId();
const theValue = this.detailPropForm.getPropertyValue(name);
propMap.set(name, theValue);
}
}
this.requiredPropValues = Array.from(propMap);
}
}