This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
jdbc-table-selector.component.ts
548 lines (474 loc) · 16.2 KB
/
jdbc-table-selector.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
/**
* @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 } from "@angular/core";
import { EventEmitter } from "@angular/core";
import { Output } from "@angular/core";
import { Input } from "@angular/core";
import { Connection } from "@connections/shared/connection.model";
import { ConnectionService } from "@connections/shared/connection.service";
import { JdbcTableFilter } from "@connections/shared/jdbc-table-filter.model";
import { SchemaInfo } from "@connections/shared/schema-info.model";
import { LoggerService } from "@core/logger.service";
import { CatalogSchema } from "@dataservices/shared/catalog-schema.model";
import { TableSelector } from "@dataservices/shared/table-selector";
import { Table } from "@dataservices/shared/table.model";
import { WizardService } from "@dataservices/shared/wizard.service";
import { LoadingState } from "@shared/loading-state.enum";
@Component({
selector: "app-jdbc-table-selector",
templateUrl: "./jdbc-table-selector.component.html",
styleUrls: ["./jdbc-table-selector.component.css"]
})
export class JdbcTableSelectorComponent implements OnInit, TableSelector {
@Input() public connection: Connection;
@Output() public tableSelectionAdded: EventEmitter<Table> = new EventEmitter<Table>();
@Output() public tableSelectionRemoved: EventEmitter<Table> = new EventEmitter<Table>();
public selectedAllRows = false;
public readonly customClasses = {
sortAscending: "fa fa-sort-asc",
sortDescending: "fa fa-sort-desc"
};
private connectionService: ConnectionService;
private wizardService: WizardService;
private logger: LoggerService;
private schemas: CatalogSchema[] = [];
private filteredSchemas: CatalogSchema[] = [];
private schemaFilter = "";
private tables: Table[] = [];
private filteredTables: Table[] = [];
private tableFilter = "";
private currentSchema: CatalogSchema = null;
private schemaLoadingState: LoadingState = LoadingState.LOADING;
private tableLoadingState: LoadingState = LoadingState.LOADING;
constructor(connectionService: ConnectionService, wizardService: WizardService, logger: LoggerService ) {
this.connectionService = connectionService;
this.wizardService = wizardService;
this.logger = logger;
}
public ngOnInit(): void {
// Load the schema info for a connection
this.setConnection(this.connection);
}
/*
* Set the connection for this jdbc table selector
* @param {Connection} conn the jdbc connection
*/
public setConnection(conn: Connection): void {
this.clearSchemas();
this.clearTables();
if (conn && conn.isJdbc()) {
this.connection = conn;
// Load the schema info for a connection
this.schemaLoadingState = LoadingState.LOADING;
const self = this;
this.connectionService
.getConnectionSchemaInfos(this.connection.getId())
.subscribe(
(infos) => {
self.generateSchemaNames(infos);
self.schemaLoadingState = LoadingState.LOADED_VALID;
},
(error) => {
self.logger.error("[JdbcTableSelectorComponent] Error getting schemas: %o", error);
self.schemaLoadingState = LoadingState.LOADED_INVALID;
}
);
} else {
this.schemaLoadingState = LoadingState.LOADING;
}
}
public clearSchemas(): void {
this.schemas = [];
this.filteredSchemas = [];
this.currentSchema = null;
}
public clearTables(): void {
this.tables = [];
this.filteredTables = [];
this.tableLoadingState = LoadingState.LOADING;
}
// callback from schema selection in datatable
public onSchemaSelect( { selected }): void {
// schema table is single select so use first element
const schema: CatalogSchema = selected[ 0 ];
// only set if schema selection has changed (see setter)
if ( this.hasSelectedSchema ) {
if ( this.selectedSchema.getDisplayName() !== schema.getDisplayName() ) {
this.selectedSchema = schema;
}
} else {
this.selectedSchema = schema;
}
}
/*
* Determines if the provided schema is selected
* @param {CatalogSchema} schema the CatalogSchema to check
*/
public isSchemaSelected(schema: CatalogSchema): boolean {
return schema === this.currentSchema;
}
/*
* Returns the currently selected schema.
* @returns {CatalogSchema} the selected schema
*/
public get selectedSchema( ): CatalogSchema {
return this.currentSchema;
}
public set selectedSchema( schema: CatalogSchema ) {
this.currentSchema = schema;
const filterInfo = new JdbcTableFilter();
filterInfo.setConnectionName(this.connection.getId());
filterInfo.setCatalogFilter(schema.getCatalogName());
filterInfo.setSchemaFilter(schema.getName());
filterInfo.setTableFilter("%");
this.loadTablesForSchema(filterInfo);
}
/*
* Returns the currently selected schema.
* @returns {CatalogSchema} the selected schema
*/
public get hasSelectedSchema( ): boolean {
return this.currentSchema != null;
}
/**
* Determine if schemas are loading
*/
public get schemasLoading( ): boolean {
return this.schemaLoadingState === LoadingState.LOADING;
}
/**
* Determine if schemas are loaded, valid and not empty
*/
public get schemasLoadedValidNotEmpty( ): boolean {
return (this.schemaLoadingState === LoadingState.LOADED_VALID) && this.schemas.length > 0;
}
/**
* Determine if schemas are loaded, valid but empty
*/
public get schemasLoadedValidEmpty( ): boolean {
return (this.schemaLoadingState === LoadingState.LOADED_VALID) && this.schemas.length === 0;
}
/**
* Determine if schemas are loaded and invalid
*/
public get schemasLoadedInvalid( ): boolean {
return this.schemaLoadingState === LoadingState.LOADED_INVALID;
}
/**
* Callback when key is pressed in schema column filter.
*/
public schemaFilterChanged( event: any ): void {
this.schemaFilter = event.target.value;
if ( this.schemaFilter.length !== 0 ) {
this.schemaFilter = "^" + this.schemaFilter.replace( "*", ".*" );
}
this.filteredSchemas = this.schemas.filter( ( schema ) => schema.getDisplayName().match( this.schemaFilter ) != null );
}
public get schemaTableMessages(): { emptyMessage: string; totalMessage: string | string } {
const numAll = this.schemas.length;
const numFiltered = this.filteredSchemas.length;
let msg: string;
if ( numAll === numFiltered ) {
if ( this.schemaFilter.length === 0 ) {
msg = numAll === 1 ? "schema" : "schemas";
} else {
msg = numAll === 1 ? "matched schema" : "matched schemas";
}
} else {
msg = numFiltered === 1 ? "matched schema" : "matched schemas";
}
return {
// message shows in an empty row in table
emptyMessage: "",
// footer total message
totalMessage: msg
};
}
/**
* Called when the table is sorted.
* @param {string} thisName the name being sorted
* @param {string} thatName the name being compared to
* @returns {number} -1 if less than, 0 if equal, or 1 if greater than
*/
public nameComparator( thisName: string,
thatName: string ): number {
return thisName.localeCompare( thatName );
}
/*
* Get all schemas
* @returns {CatalogSchema[]} the array of schema
*/
public getSchemas(): CatalogSchema[] {
return this.schemas;
}
/**
* Determine if tables are loading
*/
public get tablesLoading( ): boolean {
return this.tableLoadingState === LoadingState.LOADING;
}
/**
* Determine if tables are loaded, valid and not empty
*/
public get tablesLoadedValidNotEmpty( ): boolean {
return (this.tableLoadingState === LoadingState.LOADED_VALID) && this.tables.length > 0;
}
/**
* Determine if tables are loaded, valid but empty
*/
public get tablesLoadedValidEmpty( ): boolean {
return (this.tableLoadingState === LoadingState.LOADED_VALID) && this.tables.length === 0;
}
/**
* Determine if tables are loaded and invalid
*/
public get tablesLoadedInvalid( ): boolean {
return this.tableLoadingState === LoadingState.LOADED_INVALID;
}
public get tableTableMessages(): { emptyMessage: string; totalMessage: string | string } {
const numAll = this.tables.length;
const numFiltered = this.filteredTables.length;
let msg: string;
if ( numAll === numFiltered ) {
if ( this.tableFilter.length === 0 ) {
msg = numAll === 1 ? "table" : "tables";
} else {
msg = numAll === 1 ? "matched table" : "matched tables";
}
} else {
msg = numFiltered === 1 ? "matched table" : "matched tables";
}
return {
// message shows in an empty row in table
emptyMessage: "",
// footer total message
totalMessage: msg
};
}
/**
* Callback when key is pressed in table column filter.
*/
public tableFilterChanged( event: any ): void {
this.tableFilter = event.target.value;
if ( this.tableFilter.length !== 0 ) {
this.tableFilter = "^" + this.tableFilter.replace( "*", ".*" );
}
this.filteredTables = this.tables.filter( ( table ) => table.getName().match( this.tableFilter ) != null );
// need to set column header checkbox state
let enable = true;
for ( const filteredTable of this.filteredTables ) {
if ( !filteredTable.selected ) {
enable = false;
break;
}
}
if ( this.selectedAllRows !== enable ) {
this.selectedAllRows = enable;
}
}
/*
* Get all tables
* @returns {Table[]} the current tables for the selected schema
*/
public getTables(): Table[] {
return this.tables;
}
/*
* Determine if any tables are currently selected
* @returns {boolean} true if one or more tables are selected
*/
public hasSelectedTables(): boolean {
for ( const table of this.tables ) {
if ( table.selected ) {
return true;
}
}
return false;
}
/*
* Get the array of currently selected Tables
* @returns {Table[]} the array of selected Tables
*/
public getSelectedTables(): Table[] {
return this.tables.filter( ( table ) => table.selected );
}
public selectAllTables(): void {
this.selectedAllRows = !this.selectedAllRows;
const self = this;
this.filteredTables.forEach( ( table ) => {
if ( table.selected !== self.selectedAllRows ) {
self.selectedTableChanged( table );
}
} );
}
/*
* Handler for changes in table selection
*/
public selectedTableChanged( table: Table ): void {
table.selected = !table.selected;
if ( table.selected ) {
this.tableSelectionAdded.emit(table);
// check column header checkbox if all are selected
if ( !this.selectedAllRows ) {
let selectAll = true;
for ( const tbl of this.filteredTables ) {
if ( !tbl.selected ) {
selectAll = false;
}
}
if ( selectAll ) {
this.selectedAllRows = true;
}
}
} else {
this.tableSelectionRemoved.emit(table);
// uncheck column header checkbox if needed
if ( this.selectedAllRows ) {
this.selectedAllRows = false;
}
}
}
/**
* Deselects the table if one with a matching name and connection is currently selected
* @param {Table} table
*/
public deselectTable(table: Table): void {
const connName = table.getConnection().getId();
const tableName = table.getName();
for (const theTable of this.tables) {
const theConnName = theTable.getConnection().getId();
const theTableName = theTable.getName();
if (theConnName === connName && theTableName === tableName) {
theTable.selected = false;
// need to set column header checkbox state
let enable = true;
for ( const filteredTable of this.filteredTables ) {
if ( !filteredTable.selected ) {
enable = false;
break;
}
}
if ( this.selectedAllRows !== enable ) {
this.selectedAllRows = enable;
}
break;
}
}
}
private isSelected( selectedTables: Table[],
table: Table ): boolean {
for ( const selected of selectedTables ) {
if ( selected.getName() === table.getName() ) {
return true;
}
}
return false;
}
/*
* Builds the array of CatalogSchema items from the SchemaInfo coming from
* the Komodo rest call
* @param {SchemaInfo[]} infos the array of SchemaInfo from komodo
*/
private generateSchemaNames(infos: SchemaInfo[]): void {
for (const info of infos) {
const infoName = info.getName();
const infoType = info.getType();
const schemaNames = info.getSchemaNames();
if (infoType === "Catalog") {
if (schemaNames && schemaNames.length > 0) {
for (const sName of schemaNames) {
const item: CatalogSchema = new CatalogSchema();
item.setName(sName);
item.setType("Schema");
item.setCatalogName(infoName);
this.schemas.push(item);
this.filteredSchemas.push(item);
}
} else {
const item: CatalogSchema = new CatalogSchema();
item.setName(infoName);
item.setType("Catalog");
this.schemas.push(item);
this.filteredSchemas.push(item);
}
} else if (infoType === "Schema") {
const item: CatalogSchema = new CatalogSchema();
item.setName(infoName);
item.setType("Schema");
this.schemas.push(item);
this.filteredSchemas.push(item);
}
}
}
/*
* Populates tables array, given the supplied JdbcTableFilter
* @para {JdbcTableFilter} tableFilter the filters for getting tables
*/
private loadTablesForSchema(tableFilter: JdbcTableFilter): void {
// Load the table names for the selected Connection and Schema
this.tables = [];
this.filteredTables = [];
this.selectedAllRows = false;
this.tableLoadingState = LoadingState.LOADING;
const self = this;
this.connectionService
.getJdbcConnectionTables(tableFilter)
.subscribe(
(tableNames) => {
for ( const tName of tableNames ) {
const table = new Table();
table.setName(tName);
table.setConnection(self.connection);
table.setCatalogName(self.selectedSchema.getCatalogName());
table.setSchemaName(self.selectedSchema.getName());
self.tables.push(table);
self.filteredTables.push(table);
}
// select any of the tables that are already selected
self.setInitialTableSelections();
self.tableLoadingState = LoadingState.LOADED_VALID;
},
(error) => {
self.logger.error("[JdbcTableSelectorComponent] Error getting tables: %o", error);
self.tableLoadingState = LoadingState.LOADED_INVALID;
}
);
}
private setInitialTableSelections(): void {
let enableSelectAll = true;
for ( const table of this.tables ) {
// const catName = table.getCatalogName();
// const schemaName = table.getSchemaName();
const tableName = table.getName();
const connName = table.getConnection().getId();
for ( const initialTable of this.wizardService.getWizardSelectedTables() ) {
// const iCatName = initialTable.getCatalogName();
// const iSchemaName = initialTable.getSchemaName();
const iTableName = initialTable.getName();
const iConnName = initialTable.getConnection().getId();
if (iConnName === connName && iTableName === tableName ) {
table.selected = true;
break;
}
}
if ( !table.selected ) {
enableSelectAll = false;
}
}
this.selectedAllRows = enableSelectAll;
}
}