-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtable-selection-dialog.component.ts
More file actions
188 lines (147 loc) · 5.75 KB
/
table-selection-dialog.component.ts
File metadata and controls
188 lines (147 loc) · 5.75 KB
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
import {Component, inject} from '@angular/core';
import {DatabaseInfo, SchemaInfo, TableInfo} from '../../models/databaseInfo.model';
import {FormsModule} from '@angular/forms';
import {NgForOf, NgIf} from '@angular/common';
import {CrudService} from '../../services/crud.service';
import {AdapterModel, AdapterType, PolyMap} from '../../views/adapters/adapter.model';
import {DeployMode} from '../../models/catalog.model';
@Component({
selector: 'app-table-selection-dialog',
imports: [
FormsModule,
NgForOf,
NgIf
],
templateUrl: './table-selection-dialog.component.html',
standalone: true,
styleUrl: './table-selection-dialog.component.scss'
})
export class TableSelectionDialogComponent {
private readonly _crud = inject(CrudService);
data: DatabaseInfo[] = [];
selectedMetadata: string[] = [];
adapter: AdapterModel;
tablePreviewName = '';
tablePreview: any[] = [];
previewKeys: string[] = [];
tablePreviewAll: { [tableName: string]: any[] } = {};
close(): void {
window.close();
}
ngOnInit(): void {
const rawSettings = localStorage.getItem('adapterSettings');
const metaRaw = localStorage.getItem('metaRoot');
const previewRaw = localStorage.getItem('preview');
console.log(metaRaw);
console.log(previewRaw);
const settingsObj = rawSettings ? JSON.parse(rawSettings) as Record<string, string> : {};
const adapterSettings = new PolyMap<string, string>();
for (const [k, v] of Object.entries(settingsObj)) {
adapterSettings.set(k, v as string);
}
if (!metaRaw || !previewRaw) {
console.error('No meta or preview data found');
return;
}
let rootNode: any = JSON.parse(metaRaw);
if (typeof rootNode === 'string') {
rootNode = JSON.parse(rootNode);
}
let preview: any = JSON.parse(previewRaw);
if (typeof preview === 'string') {
preview = JSON.parse(preview);
}
this.tablePreviewAll = preview;
const [name, rawRows] = Object.entries(preview)[0];
const rows = rawRows as any[];
this.tablePreviewName = name;
this.tablePreview = rows;
this.previewKeys = rows.length > 0 ? Object.keys(rows[0]) : [];
this.data = this.buildDatabaseInfo(rootNode, preview);
const infoRaw = localStorage.getItem('adapterInfo');
const info = infoRaw ? JSON.parse(infoRaw) as Partial<{
uniqueName: string;
adapterName: string;
type: AdapterType;
mode: DeployMode;
persistent: boolean;
}> : {};
this.adapter = new AdapterModel(
info.uniqueName ?? adapterSettings.get('uniqueName') ?? 'adapter_' + Date.now(),
info.adapterName ?? 'UNKNOWN',
adapterSettings,
info.persistent ?? true,
info.type ?? AdapterType.SOURCE,
info.mode ?? DeployMode.REMOTE
);
}
private buildDatabaseInfo(root: any, preview: any): DatabaseInfo[] {
const db: DatabaseInfo = {name: root.name, schemas: []};
for (const schemaNode of root.children ?? []) {
const schema: SchemaInfo = {name: schemaNode.name, tables: []};
for (const tableNode of schemaNode.children ?? []) {
const tableKey = `${schemaNode.name}.${tableNode.name}`;
const sampleRows = preview[tableKey] ?? [];
const table: TableInfo = {name: tableNode.name, attributes: []};
for (const colNode of tableNode.children ?? []) {
const colName = colNode.name;
table.attributes.push({
name: colName,
type: colNode.properties?.type ?? '',
selected: false,
sampleValues: sampleRows.slice(0, 5).map((r: any) => r[colName])
});
}
schema.tables.push(table);
}
db.schemas.push(schema);
}
this.data = [db];
return [db];
}
getSelectedAttributeMetadata(): string[] {
const selected: string[] = [];
for (const db of this.data) {
for (const schema of db.schemas) {
for (const table of schema.tables) {
for (const attr of table.attributes) {
if (attr.selected) {
selected.push(
`${db.name}.${schema.name}.${table.name}.${attr.name} : ${attr.type}`
);
}
}
}
}
}
return selected;
}
showSelectedMetadata(): void {
this.selectedMetadata = this.getSelectedAttributeMetadata();
console.log(this.selectedMetadata);
}
getKeys(rows: any[]): string[] {
return rows.length > 0 ? Object.keys(rows[0]) : [];
}
getTableNames(): string[] {
return Object.keys(this.tablePreviewAll);
}
sendMetadataInfos(): void {
(this.adapter as any).metadata = this.selectedMetadata;
const formdata = new FormData();
formdata.set('body', JSON.stringify(this.adapter));
formdata.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
this._crud.createAdapter(this.adapter, formdata).subscribe({
next: (res) => {
console.log('Adapter + Metadaten erfolgreich gesendet', res);
alert('Daten erfolgreich gesendet.');
},
error: (err) => {
console.error(err);
alert('Fehler beim Senden!');
}
});
}
}