-
Notifications
You must be signed in to change notification settings - Fork 1
/
SystemConnectorBase.ts
498 lines (459 loc) · 19.6 KB
/
SystemConnectorBase.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
import { valid as semverValid } from "semver";
import { inspect, Logger } from "../logger";
import { Manifest } from "../manifest";
import { DEVCLASS, PGMID, SOBJ_NAME, TRKORR, TROBJTYPE } from "../client/components";
import { TADIR, TDEVC, TMSCSYS } from "../client/struct";
import { COMMENT_OBJ, Transport } from "../transport";
import { TrmPackage } from "../trmPackage";
import { InstallPackage } from "./InstallPackage";
import * as components from "../client/components";
import * as struct from "../client/struct";
import { ISystemConnectorBase } from "./ISystemConnectorBase";
import { PUBLIC_RESERVED_KEYWORD, Registry, RegistryType } from "../registry";
import { R3trans } from "node-r3trans";
export const TRM_SERVER_PACKAGE_NAME: string = 'trm-server';
export const TRM_REST_PACKAGE_NAME: string = 'trm-rest';
export const SRC_TRKORR_TABL = 'ZTRM_SRC_TRKORR';
export const SKIP_TRKORR_TABL = 'ZTRM_SKIP_TRKORR';
export abstract class SystemConnectorBase implements ISystemConnectorBase {
private _installedPackages: TrmPackage[];
private _installedPackagesI: TrmPackage[];
private _r3transInfoLog: string;
protected abstract readTable(tableName: components.TABNAME, fields: struct.RFC_DB_FLD[], options?: string): Promise<any[]>
protected abstract getSysname(): string
protected abstract getLangu(c: boolean): string
protected abstract getTrmServerVersion(): Promise<string>
protected abstract getTrmRestVersion(): Promise<string>
protected abstract listDevclassObjects(devclass: components.DEVCLASS): Promise<struct.TADIR[]>
protected abstract tdevcInterface(devclass: components.DEVCLASS, parentcl?: components.DEVCLASS, rmParentCl?: boolean, devlayer?: components.DEVLAYER): Promise<void>
protected abstract getR3transInfo(): Promise<string>
constructor() {
}
public async getTransportStatus(trkorr: TRKORR): Promise<string> {
const aTrkorrStatusCheck: any[] = (await this.readTable('E070',
[{ fieldName: 'TRKORR' }, { fieldName: 'TRSTATUS' }],
`TRKORR EQ '${trkorr}'`
));
if (aTrkorrStatusCheck.length !== 1) {
throw new Error(`Transport not found.`);
} else {
return aTrkorrStatusCheck[0].trstatus;
}
}
public async getPackageWorkbenchTransport(oPackage: TrmPackage): Promise<Transport> {
var aTrkorr: TRKORR[] = (await this.readTable('E071',
[{ fieldName: 'TRKORR' }],
`PGMID EQ '*' AND OBJECT EQ '${COMMENT_OBJ}'`
)).map(o => o.trkorr);
//because we're extracting from e071, there will be multiple records with the same trkorr
//unique array
aTrkorr = Array.from(new Set(aTrkorr))
//for each transport, check its status is D (can be released)
var aSkipTrkorr: string[] = [];
for (const sTrkorr of aTrkorr) {
var canBeReleased = false;
try {
canBeReleased = (await this.getTransportStatus(sTrkorr)) === 'D';
} catch (e) {
canBeReleased = false;
}
if (!canBeReleased) {
aSkipTrkorr.push(sTrkorr);
}
}
//filter transports
aTrkorr = aTrkorr.filter(trkorr => !aSkipTrkorr.includes(trkorr));
const transports: Transport[] = aTrkorr.map(trkorr => new Transport(trkorr));
var packageTransports: Transport[] = [];
for (const transport of transports) {
const transportPackage = await transport.getLinkedPackage();
if (transportPackage) {
if (TrmPackage.compare(transportPackage, oPackage)) {
packageTransports.push(transport);
}
}
}
if (packageTransports.length > 0) {
return await Transport.getLatest(packageTransports);
}
return null;
}
public async getSourceTrkorr(): Promise<TRKORR[]> {
Logger.log(`Ready to read installed packages`, true);
Logger.log(`Checking if ${SRC_TRKORR_TABL} exists`, true);
const tablExists: any[] = await this.readTable('TADIR',
[{ fieldName: 'OBJ_NAME' }],
`PGMID EQ 'R3TR' AND OBJECT EQ 'TABL' AND OBJ_NAME EQ '${SRC_TRKORR_TABL}'`);
if (tablExists.length === 1) {
Logger.log(`TABL ${SRC_TRKORR_TABL} exists`, true);
const srcTrkorr: {
trkorr: TRKORR
}[] = await this.readTable(SRC_TRKORR_TABL,
[{ fieldName: 'TRKORR' }]
);
return srcTrkorr.map(o => o.trkorr);
} else {
return [];
}
}
public async getObject(pgmid: PGMID, object: TROBJTYPE, objName: SOBJ_NAME): Promise<TADIR> {
const tadir: TADIR[] = await this.readTable('TADIR',
[{ fieldName: 'PGMID' }, { fieldName: 'OBJECT' }, { fieldName: 'OBJ_NAME' }, { fieldName: 'DEVCLASS' }, { fieldName: 'SRCSYSTEM' }, { fieldName: 'AUTHOR' }],
`PGMID EQ '${pgmid.trim().toUpperCase()}' AND OBJECT EQ '${object.trim().toUpperCase()}' AND OBJ_NAME EQ '${objName.trim().toUpperCase()}'`
);
if (tadir.length === 1) {
return tadir[0];
}
}
public async getIgnoredTrkorr(): Promise<TRKORR[]> {
Logger.log(`Reading ignored transports`, true);
Logger.log(`Checking if ${SKIP_TRKORR_TABL} exists`, true);
const tablExists: any[] = await this.readTable('TADIR',
[{ fieldName: 'OBJ_NAME' }],
`PGMID EQ 'R3TR' AND OBJECT EQ 'TABL' AND OBJ_NAME EQ '${SKIP_TRKORR_TABL}'`);
if (tablExists.length === 1) {
Logger.log(`TABLE ${SKIP_TRKORR_TABL} exists`, true);
const skipTrkorr: {
trkorr: TRKORR
}[] = await this.readTable(SKIP_TRKORR_TABL,
[{ fieldName: 'TRKORR' }]
);
return skipTrkorr.map(o => o.trkorr);
} else {
return [];
}
}
public async getTrmServerPackage(): Promise<TrmPackage> {
var oPackage: TrmPackage;
const oPublicRegistry = new Registry(PUBLIC_RESERVED_KEYWORD);
const intf = await this.getObject('R3TR', 'INTF', 'ZIF_TRM');
if (intf) {
try {
const trmServerVersion = await this.getTrmServerVersion();
const oManifest = new Manifest({
name: TRM_SERVER_PACKAGE_NAME,
version: trmServerVersion
});
if (semverValid(trmServerVersion)) {
oPackage = new TrmPackage(TRM_SERVER_PACKAGE_NAME, oPublicRegistry, oManifest).setDevclass(intf.devclass);
}
} catch (e) { }
}
if (!oPackage) {
throw new Error(`Package ${TRM_SERVER_PACKAGE_NAME} was not found.`);
}
return oPackage;
}
public async getTrmRestPackage(): Promise<TrmPackage> {
var oPackage: TrmPackage;
const oPublicRegistry = new Registry(PUBLIC_RESERVED_KEYWORD);
const intf = await this.getObject('R3TR', 'INTF', 'ZIF_TRM_REST');
if (intf) {
try {
const trmRestVersion = await this.getTrmRestVersion();
const oManifest = new Manifest({
name: TRM_REST_PACKAGE_NAME,
version: trmRestVersion
});
if (semverValid(trmRestVersion)) {
oPackage = new TrmPackage(TRM_REST_PACKAGE_NAME, oPublicRegistry, oManifest).setDevclass(intf.devclass);
}
} catch (e) { }
}
if (!oPackage) {
throw new Error(`Package ${TRM_REST_PACKAGE_NAME} was not found.`);
}
return oPackage;
}
public async getInstalledPackages(includeSoruces: boolean = true, refresh?: boolean): Promise<TrmPackage[]> {
if (!refresh) {
if (includeSoruces && this._installedPackagesI) {
Logger.log(`Cached version of installed packages with sources`, true);
return this._installedPackagesI;
} else if (!includeSoruces && this._installedPackages) {
Logger.log(`Cached version of installed packages without sources`, true);
return this._installedPackages;
}
}
var trmPackages: TrmPackage[] = [];
var packageTransports: {
package: TrmPackage,
transports: Transport[]
}[] = [];
Logger.log(`Ready to read installed packages`, true);
Logger.log(`Include sources: ${includeSoruces}`, true);
const aSourceTrkorr = includeSoruces ? (await this.getSourceTrkorr()) : [];
Logger.log(`Source trkorr ${JSON.stringify(aSourceTrkorr)}`, true);
var aSkipTrkorr = await this.getIgnoredTrkorr();
Logger.log(`Ignored trkorr ${JSON.stringify(aSkipTrkorr)}`, true);
var aTrkorr: TRKORR[] = (await this.readTable('E071',
[{ fieldName: 'TRKORR' }],
`PGMID EQ '*' AND OBJECT EQ '${COMMENT_OBJ}'`
)).map(o => o.trkorr);
//because we're extracting from e071, there will be multiple records with the same trkorr
//unique array
aTrkorr = Array.from(new Set(aTrkorr))
//for each transport, check it was installed and not created on the system
//read tms of current system and with maxrc > 0 and impsing != X
//if there's no match, ignore
for (const sTrkorr of aTrkorr) {
//check tms
//don't check transports from source
if (!aSourceTrkorr.includes(sTrkorr)) {
Logger.log(`${sTrkorr} not from source`, true);
var aTrkorrStatusCheck: any[];
try {
Logger.log(`Checking ${sTrkorr} TMS import result`, true);
aTrkorrStatusCheck = (await this.readTable('TMSBUFFER',
[{ fieldName: 'TRKORR' }, { fieldName: 'MAXRC' }],
//is the condition (IMPFLG EQ 't' OR IMPFLG EQ 'k') necessary?
`SYSNAM EQ '${this.getSysname()}' AND TRKORR EQ '${sTrkorr}' AND IMPSING NE 'X'`
)).filter(o => parseInt(o.maxrc) >= 0);
} catch (e) {
aTrkorrStatusCheck = [];
}
//might be imported multiple times, so do not check if lenght is 1
if (aTrkorrStatusCheck.length === 0) {
Logger.log(`Adding ${sTrkorr} to skipped filter`, true);
aSkipTrkorr.push(sTrkorr);
}
}
}
//filter transports (manually ignored transports and not imported transports)
aTrkorr = aTrkorr.filter(trkorr => !aSkipTrkorr.includes(trkorr));
Logger.log(`Final transports ${JSON.stringify(aTrkorr)}`, true);
const transports: Transport[] = aTrkorr.map(trkorr => new Transport(trkorr));
for (const transport of transports) {
const trmPackage = await transport.getLinkedPackage();
if (trmPackage) {
Logger.log(`Transport ${transport.trkorr}, found linked package`, true);
//only compares package name and registry
var arrayIndex = packageTransports.findIndex(o => TrmPackage.compare(o.package, trmPackage));
if (arrayIndex < 0) {
arrayIndex = packageTransports.push({
package: trmPackage,
transports: []
});
arrayIndex--;
}
packageTransports[arrayIndex].transports.push(transport);
}
}
Logger.log(`Package Transports map: ${inspect(packageTransports, { breakLength: Infinity, compact: true })}`, true);
for (const packageTransport of packageTransports) {
const latestTransport = await Transport.getLatest(packageTransport.transports);
if (latestTransport) {
trmPackages.push(await latestTransport.getLinkedPackage());
}
}
Logger.log(`Packages found: ${inspect(trmPackages, { breakLength: Infinity, compact: true })}`, true);
//exclude trm-server and trm-rest (if installed) and add manually
//this is to ensure the version is correct
//say it was installed via trm, then pulled from abapgit, the version would refer to the old trm version
Logger.log(`Excluding trm-server (adding it manually)`, true);
try {
const trmServerPackage = trmPackages.find(o => o.packageName === TRM_SERVER_PACKAGE_NAME && o.compareRegistry(new Registry(PUBLIC_RESERVED_KEYWORD)));
var generatedTrmServerPackage = await this.getTrmServerPackage();
if (trmServerPackage && trmServerPackage.manifest) {
Logger.log(`trm-server was found (it was imported via transport)`, true);
if (trmServerPackage.manifest.get().version === generatedTrmServerPackage.manifest.get().version) {
Logger.log(`trm-server imported is the one currenlty in use`, true);
generatedTrmServerPackage.manifest = trmServerPackage.manifest;
}
}
trmPackages = trmPackages.filter(o => !(o.packageName === TRM_SERVER_PACKAGE_NAME && o.compareRegistry(new Registry(PUBLIC_RESERVED_KEYWORD))));
trmPackages.push(generatedTrmServerPackage);
} catch (e) {
//trm-server is not installed
Logger.warning(`${TRM_SERVER_PACKAGE_NAME} is not installed`, true);
}
Logger.log(`Excluding trm-rest (adding it manually)`, true);
try {
const trmRestPackage = trmPackages.find(o => o.packageName === TRM_REST_PACKAGE_NAME && o.compareRegistry(new Registry(PUBLIC_RESERVED_KEYWORD)));
var generatedTrmRestPackage = await this.getTrmRestPackage();
if (trmRestPackage && trmRestPackage.manifest) {
Logger.log(`trm-rest was found (it was imported via transport)`, true);
if (trmRestPackage.manifest.get().version === generatedTrmRestPackage.manifest.get().version) {
Logger.log(`trm-rest imported is the one currenlty in use`, true);
generatedTrmRestPackage.manifest = trmRestPackage.manifest;
}
}
trmPackages = trmPackages.filter(o => !(o.packageName === TRM_REST_PACKAGE_NAME && o.compareRegistry(new Registry(PUBLIC_RESERVED_KEYWORD))));
trmPackages.push(generatedTrmRestPackage);
} catch (e) {
//trm-server is not installed
Logger.warning(`${TRM_SERVER_PACKAGE_NAME} is not installed`, true);
}
if (includeSoruces) {
this._installedPackagesI = trmPackages;
} else {
this._installedPackages = trmPackages;
}
return trmPackages;
}
public async getDevclass(devclass: DEVCLASS): Promise<TDEVC> {
const tdevc: TDEVC[] = await this.readTable('TDEVC',
[{ fieldName: 'DEVCLASS' }, { fieldName: 'PARENTCL' }],
`DEVCLASS EQ '${devclass.trim().toUpperCase()}'`
);
if (tdevc.length === 1) {
return tdevc[0];
}
}
public async getTransportTargets(): Promise<TMSCSYS[]> {
//systyp might not be available in some releases?
try {
return await this.readTable('TMSCSYS',
[{ fieldName: 'SYSNAM' }, { fieldName: 'SYSTXT' }, { fieldName: 'SYSTYP' }]
);
} catch (e) {
return await this.readTable('TMSCSYS',
[{ fieldName: 'SYSNAM' }, { fieldName: 'SYSTXT' }]
);
}
}
public async getSubpackages(devclass: DEVCLASS): Promise<TDEVC[]> {
const queryFields = [{ fieldName: 'DEVCLASS' }, { fieldName: 'PARENTCL' }];
var subpackages: {
tdevc: TDEVC,
queryDone: boolean
}[] = [];
const initial: TDEVC[] = await this.readTable('TDEVC',
queryFields,
`DEVCLASS EQ '${devclass.trim().toUpperCase()}'`
);
if (initial.length === 1) {
subpackages.push({
tdevc: initial[0],
queryDone: false
});
}
while (subpackages.find(o => !o.queryDone)) {
const searchParentIndex = subpackages.findIndex(o => !o.queryDone);
const tdevc: TDEVC[] = await this.readTable('TDEVC',
queryFields,
`PARENTCL EQ '${subpackages[searchParentIndex].tdevc.devclass.trim().toUpperCase()}'`
);
subpackages[searchParentIndex].queryDone = true;
tdevc.forEach(o => {
subpackages.push({
tdevc: o,
queryDone: false
});
});
}
return subpackages.map(o => o.tdevc).filter(o => o.devclass !== devclass.trim().toUpperCase());
}
public async getDevclassObjects(devclass: DEVCLASS, includeSubpackages: boolean = true): Promise<TADIR[]> {
var aTadir: TADIR[] = [];
var aDevclass: DEVCLASS[] = [devclass];
if (includeSubpackages) {
aDevclass = aDevclass.concat(((await this.getSubpackages(devclass)).map(o => o.devclass)));
}
for (const d of aDevclass) {
aTadir = aTadir.concat(((await this.listDevclassObjects(d.trim().toUpperCase()))));
}
return aTadir;
}
public async getInstallPackages(packageName: string, registry: Registry): Promise<InstallPackage[]> {
const registryEndpoint = registry.getRegistryType() === RegistryType.PUBLIC ? PUBLIC_RESERVED_KEYWORD : registry.endpoint;
return await this.readTable('ZTRMVINSTALLDEVC',
[{ fieldName: 'ORIGINAL_DEVCLASS' }, { fieldName: 'INSTALL_DEVCLASS' }],
`PACKAGE_NAME EQ '${packageName}' AND PACKAGE_REGISTRY EQ '${registryEndpoint}'`
);
}
public async setPackageSuperpackage(devclass: DEVCLASS, superpackage: DEVCLASS): Promise<void> {
return await this.tdevcInterface(devclass, superpackage);
}
public async clearPackageSuperpackage(devclass: DEVCLASS): Promise<void> {
return await this.tdevcInterface(devclass, null, true);
}
public async setPackageTransportLayer(devclass: DEVCLASS, devlayer: components.DEVLAYER): Promise<void> {
return await this.tdevcInterface(devclass, null, null, devlayer);
}
public async checkSapEntryExists(table: string, sapEntry: any): Promise<boolean> {
try {
var aQuery = [];
Object.keys(sapEntry).forEach(k => {
aQuery.push(`${k.trim().toUpperCase()} EQ '${sapEntry[k]}'`);
});
const entry: any[] = await this.readTable(table.trim().toUpperCase(),
[{ fieldName: Object.keys(sapEntry)[0].trim().toUpperCase() }],
aQuery.join(' AND '));
return entry.length > 0;
} catch (e) {
return false;
}
}
public async getPackageIntegrity(oPackage: TrmPackage): Promise<string> {
const packageName = oPackage.packageName;
const registryEndpoint = oPackage.registry.getRegistryType() === RegistryType.PUBLIC ? PUBLIC_RESERVED_KEYWORD : oPackage.registry.endpoint;
const aIntegrity: { integrity: string }[] = await this.readTable('ZTRM_INTEGRITY',
[{ fieldName: 'INTEGRITY' }],
`PACKAGE_NAME EQ '${packageName}' AND PACKAGE_REGISTRY EQ '${registryEndpoint}'`
);
if (aIntegrity.length === 1) {
return aIntegrity[0].integrity;
} else {
return ''; //avoid returning undefined
}
}
public async getFunctionModule(func: string): Promise<struct.TFDIR> {
const aTfdir: struct.TFDIR[] = await this.readTable('TFDIR',
[{ fieldName: 'FUNCNAME' }, { fieldName: 'PNAME' }],
`FUNCNAME EQ '${func.trim().toUpperCase()}'`
);
if (aTfdir.length === 1) {
return aTfdir[0];
}
}
public async getExistingObjects(objects: TADIR[]): Promise<TADIR[]> {
var ret: TADIR[] = [];
for (const object of objects) {
const oTadir = await this.getObject(object.pgmid, object.object, object.objName);
if (oTadir) {
ret.push(oTadir);
}
}
return ret;
}
public async getNamespace(namespace: components.NAMESPACE): Promise<{
trnspacet: struct.TRNSPACET,
trnspacett: struct.TRNSPACETT[]
}> {
const aNamespace: struct.TRNSPACET[] = await this.readTable('TRNSPACET',
[{ fieldName: 'NAMESPACE' }, { fieldName: 'REPLICENSE' }],
`NAMESPACE EQ '${namespace.toUpperCase()}'`
);
if (aNamespace.length === 1) {
const aNamespacet: struct.TRNSPACETT[] = await this.readTable('TRNSPACETT',
[{ fieldName: 'NAMESPACE' }, { fieldName: 'SPRAS' }, { fieldName: 'DESCRIPTN' }, { fieldName: 'OWNER' }],
`NAMESPACE EQ '${namespace.toUpperCase()}'`
);
return {
trnspacet: aNamespace[0],
trnspacett: aNamespacet
}
}
}
public async getR3transVersion(): Promise<string> {
if(!this._r3transInfoLog){
this._r3transInfoLog = await this.getR3transInfo();
}
return R3trans.getVersion(this._r3transInfoLog);
}
public async getR3transUnicode(): Promise<boolean> {
if(!this._r3transInfoLog){
this._r3transInfoLog = await this.getR3transInfo();
}
return R3trans.isUnicode(this._r3transInfoLog);
}
public async isTransportLayerExist(devlayer: components.DEVLAYER): Promise<boolean> {
const aTransportLayer: any[] = (await this.readTable('TCETRAL',
[{ fieldName: 'VERSION' }, { fieldName: 'TRANSLAYER' }],
`TRANSLAYER EQ '${devlayer}'`
));
//it's sufficient one version exists with tranSLAYER = devlayer (RAINING BLOOOOODDD!!!)
return aTransportLayer.length > 0;
}
}