-
Notifications
You must be signed in to change notification settings - Fork 208
/
IModelTransformer.ts
1037 lines (969 loc) · 63.7 KB
/
IModelTransformer.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module iModels
*/
import * as path from "path";
import * as Semver from "semver";
import { AccessToken, DbResult, Guid, Id64, Id64Set, Id64String, IModelStatus, Logger, LogLevel, MarkRequired } from "@itwin/core-bentley";
import * as ECSchemaMetaData from "@itwin/ecschema-metadata";
import { Point3d, Transform } from "@itwin/core-geometry";
import {
ChannelRootAspect, DefinitionElement, DefinitionModel, DefinitionPartition, ECSqlStatement, Element, ElementAspect, ElementMultiAspect,
ElementOwnsExternalSourceAspects, ElementRefersToElements, ElementUniqueAspect, ExternalSource, ExternalSourceAspect, ExternalSourceAttachment,
FolderLink, GeometricElement2d, GeometricElement3d, IModelCloneContext, IModelDb, IModelJsFs, InformationPartitionElement, KnownLocations, Model,
RecipeDefinitionElement, Relationship, RelationshipProps, Schema, Subject, SynchronizationConfigLink,
} from "@itwin/core-backend";
import {
Code, CodeSpec, ElementAspectProps, ElementProps, ExternalSourceAspectProps, FontProps, GeometricElement2dProps, GeometricElement3dProps, IModel,
IModelError, ModelProps, Placement2d, Placement3d, PrimitiveTypeCode, PropertyMetaData,
} from "@itwin/core-common";
import { IModelExporter, IModelExportHandler } from "./IModelExporter";
import { IModelImporter } from "./IModelImporter";
import { TransformerLoggerCategory } from "./TransformerLoggerCategory";
const loggerCategory: string = TransformerLoggerCategory.IModelTransformer;
/** Options provided to the [[IModelTransformer]] constructor.
* @beta
*/
export interface IModelTransformOptions {
/** The Id of the Element in the **target** iModel that represents the **source** repository as a whole and scopes its [ExternalSourceAspect]($backend) instances.
* When the goal is to consolidate multiple source iModels into a single target iModel, this option must be specified.
*/
targetScopeElementId?: Id64String;
/** Set to `true` if IModelTransformer should not record its provenance.
* Provenance tracks a target element back to its corresponding source element and is essential for [[IModelTransformer.processChanges]] to work properly.
* Turning off IModelTransformer provenance is really only relevant for producing snapshots or another one time transformations.
* @note See the [[includeSourceProvenance]] option for determining whether existing source provenance is cloned into the target.
* @note The default is `false` which means that new IModelTransformer provenance will be recorded.
*/
noProvenance?: boolean;
/** Set to `true` to clone existing source provenance into the target.
* @note See the [[noProvenance]] option for determining whether new IModelTransformer provenance is recorded.
* @note The default is `false` which means that existing provenance in the source will not be carried into the target.
*/
includeSourceProvenance?: boolean;
/** Flag that indicates that the target iModel was created by copying the source iModel.
* This is common when the target iModel is intended to be a *branch* of the source iModel.
* This *hint* is essential to properly initialize the source to target element mapping and to cause provenance to be recorded for future synchronizations.
* @note This *hint* is typically only set for the first synchronization after the iModel was copied since every other synchronization can utilize the provenance.
*/
wasSourceIModelCopiedToTarget?: boolean;
/** Flag that indicates that the current source and target iModels are now synchronizing in the reverse direction from a prior synchronization.
* The most common example is to first synchronize master to branch, make changes to the branch, and then reverse directions to synchronize from branch to master.
* This means that the provenance on the (current) source is used instead.
* @note This also means that only [[IModelTransformer.processChanges]] can detect deletes.
*/
isReverseSynchronization?: boolean;
/** Flag that indicates whether or not the transformation process needs to consider the source geometry before cloning/transforming.
* For standard cases, it is not required to load the source GeometryStream in JavaScript since the cloning happens in native code.
* Also, the target GeometryStream will be available in JavaScript prior to insert.
* @note If the source geometry affects the class mapping or transformation logic, then this flag should be set to `true`. The default is `false`.
* @see [IModelExporter.wantGeometry]($transformer)
*/
loadSourceGeometry?: boolean;
/** Flag that indicates whether or not the transformation process should clone using binary geometry.
* Only transformations that need to manipulate geometry should consider setting this flag as it impacts performance.
* @default true
*/
cloneUsingBinaryGeometry?: boolean;
/** Flag that indicates that ids should be preserved while copying elements to the target
* Intended only for pure-filter transforms, so you can keep parts of the source, while deleting others,
* and element ids are guaranteed to be the same, (other entity ids are not, however)
* @note the target must be empty
* @note it is invalid to insert elements during the transformation, do not use this with transformers that try to
* @note this does not preserve the ids of non-element entities such as link table relationships, or aspects, etc
* @default false
* @beta
*/
preserveElementIdsForFiltering?: boolean;
/** The behavior to use when an element reference (id) is found stored as a predecessor on an element, but the referenced element
* does not actually exist
* It is possible to craft an iModel with dangling predecessors/invalidated relationships by, e.g., deleting certain
* elements without fixing up references.
*
* @note turning this on passes the issue down to consuming applications, iModels that have invalid element references
* like this can cause errors, and you should consider adding custom logic in your transformer to remove the
* reference depending on your use case.
* @default "reject"
* @beta
*/
danglingPredecessorsBehavior?: "reject" | "ignore";
}
/** Base class used to transform a source iModel into a different target iModel.
* @see [iModel Transformation and Data Exchange]($docs/learning/transformer/index.md), [IModelExporter]($transformer), [IModelImporter]($transformer)
* @beta
*/
export class IModelTransformer extends IModelExportHandler {
/** The IModelExporter that will export from the source iModel. */
public readonly exporter: IModelExporter;
/** The IModelImporter that will import into the target iModel. */
public readonly importer: IModelImporter;
/** The normally read-only source iModel.
* @note The source iModel will need to be read/write when provenance is being stored during a reverse synchronization.
*/
public readonly sourceDb: IModelDb;
/** The read/write target iModel. */
public readonly targetDb: IModelDb;
/** The IModelTransformContext for this IModelTransformer. */
public readonly context: IModelCloneContext;
/** The Id of the Element in the **target** iModel that represents the **source** repository as a whole and scopes its [ExternalSourceAspect]($backend) instances. */
public get targetScopeElementId(): Id64String {
return this._options.targetScopeElementId;
}
/** The set of Elements that were deferred during a prior transformation pass. */
protected _deferredElementIds = new Set<Id64String>();
/** the options that were used to initialize this transformer */
private readonly _options: MarkRequired<IModelTransformOptions, "targetScopeElementId" | "danglingPredecessorsBehavior">;
/** Set if it can be determined whether this is the first source --> target synchronization. */
private _isFirstSynchronization?: boolean;
/** Construct a new IModelTransformer
* @param source Specifies the source IModelExporter or the source IModelDb that will be used to construct the source IModelExporter.
* @param target Specifies the target IModelImporter or the target IModelDb that will be used to construct the target IModelImporter.
* @param options The options that specify how the transformation should be done.
*/
public constructor(source: IModelDb | IModelExporter, target: IModelDb | IModelImporter, options?: IModelTransformOptions) {
super();
// initialize IModelTransformOptions
this._options = {
...options,
// non-falsy defaults
cloneUsingBinaryGeometry: options?.cloneUsingBinaryGeometry ?? true,
targetScopeElementId: options?.targetScopeElementId ?? IModel.rootSubjectId,
danglingPredecessorsBehavior: options?.danglingPredecessorsBehavior ?? "reject",
};
this._isFirstSynchronization = this._options.wasSourceIModelCopiedToTarget ? true : undefined;
// initialize exporter and sourceDb
if (source instanceof IModelDb) {
this.exporter = new IModelExporter(source);
} else {
this.exporter = source;
}
this.sourceDb = this.exporter.sourceDb;
this.exporter.registerHandler(this);
this.exporter.wantGeometry = options?.loadSourceGeometry ?? false; // optimization to not load source GeometryStreams by default
if (!this._options.includeSourceProvenance) { // clone provenance from the source iModel into the target iModel?
this.exporter.excludeElementClass(FolderLink.classFullName);
this.exporter.excludeElementClass(SynchronizationConfigLink.classFullName);
this.exporter.excludeElementClass(ExternalSource.classFullName);
this.exporter.excludeElementClass(ExternalSourceAttachment.classFullName);
this.exporter.excludeElementAspectClass(ExternalSourceAspect.classFullName);
}
this.exporter.excludeElementAspectClass(ChannelRootAspect.classFullName); // Channel boundaries within the source iModel are not relevant to the target iModel
this.exporter.excludeElementAspectClass("BisCore:TextAnnotationData"); // This ElementAspect is auto-created by the BisCore:TextAnnotation2d/3d element handlers
// initialize importer and targetDb
if (target instanceof IModelDb) {
this.importer = new IModelImporter(target, { preserveElementIdsForFiltering: this._options.preserveElementIdsForFiltering });
} else {
this.importer = target;
/* eslint-disable deprecation/deprecation */
if (this._options.preserveElementIdsForFiltering !== this.importer.preserveElementIdsForFiltering) {
Logger.logWarning(
loggerCategory,
"A custom importer was passed as a target but its 'preserveElementIdsForFiltering' option is out of sync with the transformer's option."
+ "\nThe custom importer target's option will be force updated to use the transformer's value."
+ "\nThis behavior is deprecated and will be removed in a future version, throwing an error if they are out of sync."
);
this.importer.preserveElementIdsForFiltering = Boolean(this._options.preserveElementIdsForFiltering);
}
/* eslint-enable deprecation/deprecation */
}
this.targetDb = this.importer.targetDb;
// initialize the IModelCloneContext
this.context = new IModelCloneContext(this.sourceDb, this.targetDb);
}
/** Dispose any native resources associated with this IModelTransformer. */
public dispose(): void {
Logger.logTrace(loggerCategory, "dispose()");
this.context.dispose();
}
/** Log current settings that affect IModelTransformer's behavior. */
private logSettings(): void {
Logger.logInfo(TransformerLoggerCategory.IModelExporter, `this.exporter.visitElements=${this.exporter.visitElements}`);
Logger.logInfo(TransformerLoggerCategory.IModelExporter, `this.exporter.visitRelationships=${this.exporter.visitRelationships}`);
Logger.logInfo(TransformerLoggerCategory.IModelExporter, `this.exporter.wantGeometry=${this.exporter.wantGeometry}`);
Logger.logInfo(TransformerLoggerCategory.IModelExporter, `this.exporter.wantSystemSchemas=${this.exporter.wantSystemSchemas}`);
Logger.logInfo(TransformerLoggerCategory.IModelExporter, `this.exporter.wantTemplateModels=${this.exporter.wantTemplateModels}`);
Logger.logInfo(loggerCategory, `this.targetScopeElementId=${this.targetScopeElementId}`);
Logger.logInfo(loggerCategory, `this._noProvenance=${this._options.noProvenance}`);
Logger.logInfo(loggerCategory, `this._includeSourceProvenance=${this._options.includeSourceProvenance}`);
Logger.logInfo(loggerCategory, `this._cloneUsingBinaryGeometry=${this._options.cloneUsingBinaryGeometry}`);
Logger.logInfo(loggerCategory, `this._wasSourceIModelCopiedToTarget=${this._options.wasSourceIModelCopiedToTarget}`);
Logger.logInfo(loggerCategory, `this._isReverseSynchronization=${this._options.isReverseSynchronization}`);
Logger.logInfo(TransformerLoggerCategory.IModelImporter, `this.importer.autoExtendProjectExtents=${this.importer.options.autoExtendProjectExtents}`);
Logger.logInfo(TransformerLoggerCategory.IModelImporter, `this.importer.simplifyElementGeometry=${this.importer.options.simplifyElementGeometry}`);
}
/** Return the IModelDb where IModelTransformer will store its provenance.
* @note This will be [[targetDb]] except when it is a reverse synchronization. In that case it be [[sourceDb]].
*/
public get provenanceDb(): IModelDb {
return this._options.isReverseSynchronization ? this.sourceDb : this.targetDb;
}
/** Create an ExternalSourceAspectProps in a standard way for an Element in an iModel --> iModel transformation. */
private initElementProvenance(sourceElementId: Id64String, targetElementId: Id64String): ExternalSourceAspectProps {
const elementId = this._options.isReverseSynchronization ? sourceElementId : targetElementId;
const aspectIdentifier = this._options.isReverseSynchronization ? targetElementId : sourceElementId;
const aspectProps: ExternalSourceAspectProps = {
classFullName: ExternalSourceAspect.classFullName,
element: { id: elementId, relClassName: ElementOwnsExternalSourceAspects.classFullName },
scope: { id: this.targetScopeElementId },
identifier: aspectIdentifier,
kind: ExternalSourceAspect.Kind.Element,
version: this.sourceDb.elements.queryLastModifiedTime(sourceElementId),
};
aspectProps.id = this.queryExternalSourceAspectId(aspectProps);
return aspectProps;
}
/** Create an ExternalSourceAspectProps in a standard way for a Relationship in an iModel --> iModel transformations.
* The ExternalSourceAspect is meant to be owned by the Element in the target iModel that is the `sourceId` of transformed relationship.
* The `identifier` property of the ExternalSourceAspect will be the ECInstanceId of the relationship in the source iModel.
* The ECInstanceId of the relationship in the target iModel will be stored in the JsonProperties of the ExternalSourceAspect.
*/
private initRelationshipProvenance(sourceRelationship: Relationship, targetRelInstanceId: Id64String): ExternalSourceAspectProps {
const targetRelationship: Relationship = this.targetDb.relationships.getInstance(ElementRefersToElements.classFullName, targetRelInstanceId);
const elementId = this._options.isReverseSynchronization ? sourceRelationship.sourceId : targetRelationship.sourceId;
const aspectIdentifier = this._options.isReverseSynchronization ? targetRelInstanceId : sourceRelationship.id;
const aspectProps: ExternalSourceAspectProps = {
classFullName: ExternalSourceAspect.classFullName,
element: { id: elementId, relClassName: ElementOwnsExternalSourceAspects.classFullName },
scope: { id: this.targetScopeElementId },
identifier: aspectIdentifier,
kind: ExternalSourceAspect.Kind.Relationship,
jsonProperties: JSON.stringify({ targetRelInstanceId }),
};
aspectProps.id = this.queryExternalSourceAspectId(aspectProps);
return aspectProps;
}
private validateScopeProvenance(): void {
const aspectProps: ExternalSourceAspectProps = {
classFullName: ExternalSourceAspect.classFullName,
element: { id: this.targetScopeElementId, relClassName: ElementOwnsExternalSourceAspects.classFullName },
scope: { id: IModel.rootSubjectId }, // the root Subject scopes scope elements
identifier: this._options.isReverseSynchronization ? this.targetDb.iModelId : this.sourceDb.iModelId, // the opposite side of where provenance is stored
kind: ExternalSourceAspect.Kind.Scope,
};
aspectProps.id = this.queryExternalSourceAspectId(aspectProps); // this query includes "identifier"
if (undefined === aspectProps.id) {
// this query does not include "identifier" to find possible conflicts
const sql = `SELECT ECInstanceId FROM ${ExternalSourceAspect.classFullName} WHERE Element.Id=:elementId AND Scope.Id=:scopeId AND Kind=:kind LIMIT 1`;
const hasConflictingScope = this.provenanceDb.withPreparedStatement(sql, (statement: ECSqlStatement): boolean => {
statement.bindId("elementId", aspectProps.element.id);
statement.bindId("scopeId", aspectProps.scope.id);
statement.bindString("kind", aspectProps.kind);
return DbResult.BE_SQLITE_ROW === statement.step();
});
if (hasConflictingScope) {
throw new IModelError(IModelStatus.InvalidId, "Provenance scope conflict");
}
if (!this._options.noProvenance) {
this.provenanceDb.elements.insertAspect(aspectProps);
this._isFirstSynchronization = true; // couldn't tell this is the first time without provenance
}
}
}
private queryExternalSourceAspectId(aspectProps: ExternalSourceAspectProps): Id64String | undefined {
const sql = `SELECT ECInstanceId FROM ${ExternalSourceAspect.classFullName} WHERE Element.Id=:elementId AND Scope.Id=:scopeId AND Kind=:kind AND Identifier=:identifier LIMIT 1`;
return this.provenanceDb.withPreparedStatement(sql, (statement: ECSqlStatement): Id64String | undefined => {
statement.bindId("elementId", aspectProps.element.id);
statement.bindId("scopeId", aspectProps.scope.id);
statement.bindString("kind", aspectProps.kind);
statement.bindString("identifier", aspectProps.identifier);
return (DbResult.BE_SQLITE_ROW === statement.step()) ? statement.getValue(0).getId() : undefined;
});
}
/** Iterate all matching ExternalSourceAspects in the target iModel and call a function for each one. */
private forEachTrackedElement(fn: (sourceElementId: Id64String, targetElementId: Id64String) => void): void {
if (!this.provenanceDb.containsClass(ExternalSourceAspect.classFullName)) {
throw new IModelError(IModelStatus.BadSchema, "The BisCore schema version of the target database is too old");
}
const sql = `SELECT Identifier,Element.Id FROM ${ExternalSourceAspect.classFullName} WHERE Scope.Id=:scopeId AND Kind=:kind`;
this.provenanceDb.withPreparedStatement(sql, (statement: ECSqlStatement): void => {
statement.bindId("scopeId", this.targetScopeElementId);
statement.bindString("kind", ExternalSourceAspect.Kind.Element);
while (DbResult.BE_SQLITE_ROW === statement.step()) {
const aspectIdentifier: Id64String = statement.getValue(0).getString(); // ExternalSourceAspect.Identifier is of type string
const elementId: Id64String = statement.getValue(1).getId();
if (this._options.isReverseSynchronization) {
fn(elementId, aspectIdentifier); // provenance coming from the sourceDb
} else {
fn(aspectIdentifier, elementId); // provenance coming from the targetDb
}
}
});
}
/** Initialize the source to target Element mapping from ExternalSourceAspects in the target iModel.
* @note This method is called from [[processChanges]] and [[processAll]], so it only needs to be called directly when processing a subset of an iModel.
*/
public initFromExternalSourceAspects(): void {
this.forEachTrackedElement((sourceElementId: Id64String, targetElementId: Id64String) => {
this.context.remapElement(sourceElementId, targetElementId);
});
}
/** Returns `true` if *brute force* delete detections should be run.
* @note Not relevant for processChanges when change history is known.
*/
private shouldDetectDeletes(): boolean {
if (this._isFirstSynchronization) return false; // not necessary the first time since there are no deletes to detect
if (this._options.isReverseSynchronization) return false; // not possible for a reverse synchronization since provenance will be deleted when element is deleted
return true;
}
/** Detect Element deletes using ExternalSourceAspects in the target iModel and a *brute force* comparison against Elements in the source iModel.
* @see processChanges
* @note This method is called from [[processAll]] and is not needed by [[processChanges]], so it only needs to be called directly when processing a subset of an iModel.
* @throws [[IModelError]] If the required provenance information is not available to detect deletes.
*/
public async detectElementDeletes(): Promise<void> {
if (this._options.isReverseSynchronization) {
throw new IModelError(IModelStatus.BadRequest, "Cannot detect deletes when isReverseSynchronization=true");
}
const targetElementsToDelete: Id64String[] = [];
this.forEachTrackedElement((sourceElementId: Id64String, targetElementId: Id64String) => {
if (undefined === this.sourceDb.elements.tryGetElementProps(sourceElementId)) {
// if the sourceElement is not found, then it must have been deleted, so propagate the delete to the target iModel
targetElementsToDelete.push(targetElementId);
}
});
targetElementsToDelete.forEach((targetElementId: Id64String) => {
this.importer.deleteElement(targetElementId);
});
}
/** Format an Element for the Logger. */
private formatElementForLogger(elementProps: ElementProps | Element): string {
const namePiece: string = elementProps.code.value ? `${elementProps.code.value} ` : elementProps.userLabel ? `${elementProps.userLabel} ` : "";
return `${elementProps.classFullName} ${namePiece}[${elementProps.id!}]`;
}
/** Mark the specified Element so its processing can be deferred. */
protected skipElement(sourceElement: Element): void {
this._deferredElementIds.add(sourceElement.id);
Logger.logInfo(loggerCategory, `Deferred ${this.formatElementForLogger(sourceElement)}`);
}
/** Transform the specified sourceElement into ElementProps for the target iModel.
* @param sourceElement The Element from the source iModel to transform.
* @returns ElementProps for the target iModel.
* @note A subclass can override this method to provide custom transform behavior.
*/
public onTransformElement(sourceElement: Element): ElementProps {
Logger.logTrace(loggerCategory, `onTransformElement(${sourceElement.id}) "${sourceElement.getDisplayLabel()}"`);
const targetElementProps: ElementProps = this.context.cloneElement(sourceElement, { binaryGeometry: this._options.cloneUsingBinaryGeometry });
if (sourceElement instanceof Subject) {
if (targetElementProps.jsonProperties?.Subject?.Job) {
// don't propagate source channels into target (legacy bridge case)
targetElementProps.jsonProperties.Subject.Job = undefined;
}
}
return targetElementProps;
}
/** Returns true if a change within sourceElement is detected.
* @param sourceElement The Element from the source iModel
* @param targetElementId The Element from the target iModel to compare against.
* @note A subclass can override this method to provide custom change detection behavior.
*/
protected hasElementChanged(sourceElement: Element, targetElementId: Id64String): boolean {
const aspects: ElementAspect[] = this.targetDb.elements.getAspects(targetElementId, ExternalSourceAspect.classFullName);
for (const aspect of aspects) {
const sourceAspect = aspect as ExternalSourceAspect;
if ((sourceAspect.identifier === sourceElement.id) && (sourceAspect.scope.id === this.targetScopeElementId) && (sourceAspect.kind === ExternalSourceAspect.Kind.Element)) {
const lastModifiedTime: string = sourceElement.iModel.elements.queryLastModifiedTime(sourceElement.id);
return (lastModifiedTime !== sourceAspect.version);
}
}
return true;
}
/** Determine if any predecessors have not been imported yet.
* @param sourceElement The Element from the source iModel
*/
private findMissingPredecessors(sourceElement: Element): Id64Set {
const sourcePredecessorIds: Id64Set = sourceElement.getPredecessorIds();
sourcePredecessorIds.forEach((sourcePredecessorId: Id64String) => {
if (Id64.invalid === sourcePredecessorId) {
sourcePredecessorIds.delete(sourcePredecessorId);
} else {
const targetPredecessorId: Id64String = this.context.findTargetElementId(sourcePredecessorId);
if (Id64.isValidId64(targetPredecessorId)) {
// a valid Id indicates that the predecessor has already been remapped
sourcePredecessorIds.delete(sourcePredecessorId);
} else {
const sourcePredecessor = this.sourceDb.elements.tryGetElement(sourcePredecessorId);
if (sourcePredecessor === undefined) {
switch (this._options.danglingPredecessorsBehavior) {
case "ignore":
Logger.logWarning(loggerCategory, `Source element (${sourceElement.id}) "${sourceElement.getDisplayLabel()}" has a missing predecessor (${sourcePredecessorId})`);
sourcePredecessorIds.delete(sourcePredecessorId);
return;
case "reject":
throw new IModelError(
IModelStatus.NotFound,
`Found a reference to an element "${sourcePredecessorId}" that doesn't exist while looking for predecessors of "${sourceElement.id}"`
+ "\nThis must have been caused by an upstream application that changed the iModel."
+ "\nYou can set the IModelTransformerOptions.danglingPredecessorsBehavior option to 'ignore' to ignore this, but this will leave the iModel"
+ "\nin a state where downstream consuming applications will need to handle the invalidity themselves. In some cases, writing a custom"
+ "\ntransformer to remove the reference and fix affected elements may be suitable."
);
}
}
if (!this.exporter.shouldExportElement(sourcePredecessor)) {
// any predecessor that has been explicitly excluded is not considered missing
sourcePredecessorIds.delete(sourcePredecessorId);
Logger.logWarning(loggerCategory, `Source element (${sourceElement.id}) "${sourceElement.getDisplayLabel()}" has an excluded predecessor (${sourcePredecessorId}) "${sourcePredecessor.getDisplayLabel()}"`);
}
}
}
});
return sourcePredecessorIds;
}
/** Cause the specified Element and its child Elements (if applicable) to be exported from the source iModel and imported into the target iModel.
* @param sourceElementId Identifies the Element from the source iModel to import.
* @note This method is called from [[processChanges]] and [[processAll]], so it only needs to be called directly when processing a subset of an iModel.
*/
public async processElement(sourceElementId: Id64String): Promise<void> {
if (sourceElementId === IModel.rootSubjectId) {
throw new IModelError(IModelStatus.BadRequest, "The root Subject should not be directly imported");
}
return this.exporter.exportElement(sourceElementId);
}
/** Import child elements into the target IModelDb
* @param sourceElementId Import the child elements of this element in the source IModelDb.
* @note This method is called from [[processChanges]] and [[processAll]], so it only needs to be called directly when processing a subset of an iModel.
*/
public async processChildElements(sourceElementId: Id64String): Promise<void> {
return this.exporter.exportChildElements(sourceElementId);
}
/** Override of [IModelExportHandler.shouldExportElement]($transformer) that is called to determine if an element should be exported from the source iModel.
* @note Reaching this point means that the element has passed the standard exclusion checks in IModelExporter.
*/
public override shouldExportElement(_sourceElement: Element): boolean { return true; }
/** Override of [IModelExportHandler.onExportElement]($transformer) that imports an element into the target iModel when it is exported from the source iModel.
* This override calls [[onTransformElement]] and then [IModelImporter.importElement]($transformer) to update the target iModel.
*/
public override onExportElement(sourceElement: Element): void {
let targetElementId: Id64String | undefined;
let targetElementProps: ElementProps;
if (this._options.preserveElementIdsForFiltering) {
targetElementId = sourceElement.id;
targetElementProps = this.onTransformElement(sourceElement);
} else if (this._options.wasSourceIModelCopiedToTarget) {
targetElementId = sourceElement.id;
targetElementProps = this.targetDb.elements.getElementProps(targetElementId);
} else {
targetElementId = this.context.findTargetElementId(sourceElement.id);
targetElementProps = this.onTransformElement(sourceElement);
}
// if an existing remapping was not yet found, check by Code as long as the CodeScope is valid (invalid means a missing predecessor so not worth checking)
if (!Id64.isValidId64(targetElementId) && Id64.isValidId64(targetElementProps.code.scope)) {
targetElementId = this.targetDb.elements.queryElementIdByCode(new Code(targetElementProps.code));
if (undefined !== targetElementId) {
const targetElement: Element = this.targetDb.elements.getElement(targetElementId);
if (targetElement.classFullName === targetElementProps.classFullName) { // ensure code remapping doesn't change the target class
this.context.remapElement(sourceElement.id, targetElementId); // record that the targeElement was found by Code
} else {
targetElementId = undefined;
targetElementProps.code = Code.createEmpty(); // clear out invalid code
}
}
}
if (undefined !== targetElementId && Id64.isValidId64(targetElementId)) {
// compare LastMod of sourceElement to ExternalSourceAspect of targetElement to see there are changes to import
if (!this.hasElementChanged(sourceElement, targetElementId)) {
return;
}
} else {
const missingPredecessorIds: Id64Set = this.findMissingPredecessors(sourceElement);
if (missingPredecessorIds.size > 0) {
this.skipElement(sourceElement);
if (Logger.isEnabled(loggerCategory, LogLevel.Trace)) {
for (const missingPredecessorId of missingPredecessorIds) {
const missingPredecessorElement: Element | undefined = this.sourceDb.elements.tryGetElement(missingPredecessorId);
if (missingPredecessorElement) {
Logger.logTrace(loggerCategory, `Remapping not found for predecessor ${this.formatElementForLogger(missingPredecessorElement)}`);
}
}
}
return;
}
}
targetElementProps.id = targetElementId; // targetElementId will be valid (indicating update) or undefined (indicating insert)
if (!this._options.wasSourceIModelCopiedToTarget) {
this.importer.importElement(targetElementProps); // don't need to import if iModel was copied
}
this.context.remapElement(sourceElement.id, targetElementProps.id!); // targetElementProps.id assigned by importElement
if (!this._options.noProvenance) {
const aspectProps: ExternalSourceAspectProps = this.initElementProvenance(sourceElement.id, targetElementProps.id!);
if (aspectProps.id === undefined) {
this.provenanceDb.elements.insertAspect(aspectProps);
} else {
this.provenanceDb.elements.updateAspect(aspectProps);
}
}
}
/** Override of [IModelExportHandler.onDeleteElement]($transformer) that is called when [IModelExporter]($transformer) detects that an Element has been deleted from the source iModel.
* This override propagates the delete to the target iModel via [IModelImporter.deleteElement]($transformer).
*/
public override onDeleteElement(sourceElementId: Id64String): void {
const targetElementId: Id64String = this.context.findTargetElementId(sourceElementId);
if (Id64.isValidId64(targetElementId)) {
this.importer.deleteElement(targetElementId);
}
}
/** Override of [IModelExportHandler.onExportModel]($transformer) that is called when a Model should be exported from the source iModel.
* This override calls [[onTransformModel]] and then [IModelImporter.importModel]($transformer) to update the target iModel.
*/
public override onExportModel(sourceModel: Model): void {
if (IModel.repositoryModelId === sourceModel.id) {
return; // The RepositoryModel should not be directly imported
}
const targetModeledElementId: Id64String = this.context.findTargetElementId(sourceModel.id);
const targetModelProps: ModelProps = this.onTransformModel(sourceModel, targetModeledElementId);
this.importer.importModel(targetModelProps);
}
/** Override of [IModelExportHandler.onDeleteModel]($transformer) that is called when [IModelExporter]($transformer) detects that a [Model]($backend) has been deleted from the source iModel. */
public override onDeleteModel(_sourceModelId: Id64String): void {
// WIP: currently ignored
}
/** Cause the model container, contents, and sub-models to be exported from the source iModel and imported into the target iModel.
* @param sourceModeledElementId Import this [Model]($backend) from the source IModelDb.
* @note This method is called from [[processChanges]] and [[processAll]], so it only needs to be called directly when processing a subset of an iModel.
*/
public async processModel(sourceModeledElementId: Id64String): Promise<void> {
return this.exporter.exportModel(sourceModeledElementId);
}
/** Cause the model contents to be exported from the source iModel and imported into the target iModel.
* @param sourceModelId Import the contents of this model from the source IModelDb.
* @param targetModelId Import into this model in the target IModelDb. The target model must exist prior to this call.
* @param elementClassFullName Optional classFullName of an element subclass to limit import query against the source model.
* @note This method is called from [[processChanges]] and [[processAll]], so it only needs to be called directly when processing a subset of an iModel.
*/
public async processModelContents(sourceModelId: Id64String, targetModelId: Id64String, elementClassFullName: string = Element.classFullName): Promise<void> {
this.targetDb.models.getModel(targetModelId); // throws if Model does not exist
this.context.remapElement(sourceModelId, targetModelId); // set remapping in case importModelContents is called directly
return this.exporter.exportModelContents(sourceModelId, elementClassFullName);
}
/** Cause all sub-models that recursively descend from the specified Subject to be exported from the source iModel and imported into the target iModel. */
private async processSubjectSubModels(sourceSubjectId: Id64String): Promise<void> {
// import DefinitionModels first
const childDefinitionPartitionSql = `SELECT ECInstanceId FROM ${DefinitionPartition.classFullName} WHERE Parent.Id=:subjectId`;
await this.sourceDb.withPreparedStatement(childDefinitionPartitionSql, async (statement: ECSqlStatement) => {
statement.bindId("subjectId", sourceSubjectId);
while (DbResult.BE_SQLITE_ROW === statement.step()) {
await this.processModel(statement.getValue(0).getId());
}
});
// import other partitions next
const childPartitionSql = `SELECT ECInstanceId FROM ${InformationPartitionElement.classFullName} WHERE Parent.Id=:subjectId`;
await this.sourceDb.withPreparedStatement(childPartitionSql, async (statement: ECSqlStatement) => {
statement.bindId("subjectId", sourceSubjectId);
while (DbResult.BE_SQLITE_ROW === statement.step()) {
const modelId: Id64String = statement.getValue(0).getId();
const model: Model = this.sourceDb.models.getModel(modelId);
if (!(model instanceof DefinitionModel)) {
await this.processModel(modelId);
}
}
});
// recurse into child Subjects
const childSubjectSql = `SELECT ECInstanceId FROM ${Subject.classFullName} WHERE Parent.Id=:subjectId`;
await this.sourceDb.withPreparedStatement(childSubjectSql, async (statement: ECSqlStatement) => {
statement.bindId("subjectId", sourceSubjectId);
while (DbResult.BE_SQLITE_ROW === statement.step()) {
await this.processSubjectSubModels(statement.getValue(0).getId());
}
});
}
/** Transform the specified sourceModel into ModelProps for the target iModel.
* @param sourceModel The Model from the source iModel to be transformed.
* @param targetModeledElementId The transformed Model will *break down* or *detail* this Element in the target iModel.
* @returns ModelProps for the target iModel.
* @note A subclass can override this method to provide custom transform behavior.
*/
public onTransformModel(sourceModel: Model, targetModeledElementId: Id64String): ModelProps {
const targetModelProps: ModelProps = sourceModel.toJSON();
targetModelProps.modeledElement.id = targetModeledElementId;
targetModelProps.id = targetModeledElementId;
targetModelProps.parentModel = this.context.findTargetElementId(targetModelProps.parentModel!);
return targetModelProps;
}
/** Import elements that were deferred in a prior pass.
* @note This method is called from [[processChanges]] and [[processAll]], so it only needs to be called directly when processing a subset of an iModel.
*/
public async processDeferredElements(numRetries: number = 3): Promise<void> {
Logger.logTrace(loggerCategory, `processDeferredElements(), numDeferred=${this._deferredElementIds.size}`);
const copyOfDeferredElementIds: Id64Set = this._deferredElementIds;
this._deferredElementIds = new Set<Id64String>();
for (const elementId of copyOfDeferredElementIds) {
await this.processElement(elementId);
}
if (this._deferredElementIds.size > 0) {
if (--numRetries > 0) {
Logger.logTrace(loggerCategory, "Retrying processDeferredElements()");
await this.processDeferredElements(numRetries);
} else {
throw new IModelError(IModelStatus.BadRequest, "Not all deferred elements could be processed");
}
} else {
// process deferred relationships since if their sources+targets were deferred, they may not have been processed
await this.processRelationships(ElementRefersToElements.classFullName);
}
}
/** Imports all relationships that subclass from the specified base class.
* @param baseRelClassFullName The specified base relationship class.
* @note This method is called from [[processChanges]] and [[processAll]], so it only needs to be called directly when processing a subset of an iModel.
*/
public async processRelationships(baseRelClassFullName: string): Promise<void> {
return this.exporter.exportRelationships(baseRelClassFullName);
}
/** Override of [IModelExportHandler.shouldExportRelationship]($transformer) that is called to determine if a [Relationship]($backend) should be exported.
* @note Reaching this point means that the relationship has passed the standard exclusion checks in [IModelExporter]($transformer).
*/
public override shouldExportRelationship(_sourceRelationship: Relationship): boolean { return true; }
/** Override of [IModelExportHandler.onExportRelationship]($transformer) that imports a relationship into the target iModel when it is exported from the source iModel.
* This override calls [[onTransformRelationship]] and then [IModelImporter.importRelationship]($transformer) to update the target iModel.
*/
public override onExportRelationship(sourceRelationship: Relationship): void {
const targetRelationshipProps: RelationshipProps = this.onTransformRelationship(sourceRelationship);
const targetRelationshipInstanceId: Id64String = this.importer.importRelationship(targetRelationshipProps);
if (!this._options.noProvenance && Id64.isValidId64(targetRelationshipInstanceId)) {
const aspectProps: ExternalSourceAspectProps = this.initRelationshipProvenance(sourceRelationship, targetRelationshipInstanceId);
if (undefined === aspectProps.id) {
this.provenanceDb.elements.insertAspect(aspectProps);
}
}
}
/** Override of [IModelExportHandler.onDeleteRelationship]($transformer) that is called when [IModelExporter]($transformer) detects that a [Relationship]($backend) has been deleted from the source iModel.
* This override propagates the delete to the target iModel via [IModelImporter.deleteRelationship]($transformer).
*/
public override onDeleteRelationship(sourceRelInstanceId: Id64String): void {
const sql = `SELECT ECInstanceId,JsonProperties FROM ${ExternalSourceAspect.classFullName} aspect` +
` WHERE aspect.Scope.Id=:scopeId AND aspect.Kind=:kind AND aspect.Identifier=:identifier LIMIT 1`;
this.targetDb.withPreparedStatement(sql, (statement: ECSqlStatement): void => {
statement.bindId("scopeId", this.targetScopeElementId);
statement.bindString("kind", ExternalSourceAspect.Kind.Relationship);
statement.bindString("identifier", sourceRelInstanceId);
if (DbResult.BE_SQLITE_ROW === statement.step()) {
const json: any = JSON.parse(statement.getValue(1).getString());
if (undefined !== json.targetRelInstanceId) {
const targetRelationship = this.targetDb.relationships.tryGetInstance(ElementRefersToElements.classFullName, json.targetRelInstanceId);
if (targetRelationship) {
this.importer.deleteRelationship(targetRelationship.toJSON());
}
this.targetDb.elements.deleteAspect(statement.getValue(0).getId());
}
}
});
}
/** Detect Relationship deletes using ExternalSourceAspects in the target iModel and a *brute force* comparison against relationships in the source iModel.
* @see processChanges
* @note This method is called from [[processAll]] and is not needed by [[processChanges]], so it only needs to be called directly when processing a subset of an iModel.
* @throws [[IModelError]] If the required provenance information is not available to detect deletes.
*/
public async detectRelationshipDeletes(): Promise<void> {
if (this._options.isReverseSynchronization) {
throw new IModelError(IModelStatus.BadRequest, "Cannot detect deletes when isReverseSynchronization=true");
}
const aspectDeleteIds: Id64String[] = [];
const sql = `SELECT ECInstanceId,Identifier,JsonProperties FROM ${ExternalSourceAspect.classFullName} aspect WHERE aspect.Scope.Id=:scopeId AND aspect.Kind=:kind`;
await this.targetDb.withPreparedStatement(sql, async (statement: ECSqlStatement) => {
statement.bindId("scopeId", this.targetScopeElementId);
statement.bindString("kind", ExternalSourceAspect.Kind.Relationship);
while (DbResult.BE_SQLITE_ROW === statement.step()) {
const sourceRelInstanceId: Id64String = Id64.fromJSON(statement.getValue(1).getString());
if (undefined === this.sourceDb.relationships.tryGetInstanceProps(ElementRefersToElements.classFullName, sourceRelInstanceId)) {
const json: any = JSON.parse(statement.getValue(2).getString());
if (undefined !== json.targetRelInstanceId) {
const targetRelationship: Relationship = this.targetDb.relationships.getInstance(ElementRefersToElements.classFullName, json.targetRelInstanceId);
this.importer.deleteRelationship(targetRelationship.toJSON());
}
aspectDeleteIds.push(statement.getValue(0).getId());
}
await new Promise(setImmediate); // workaround for: https://github.com/nodejs/node-addon-api/issues/1140
}
});
this.targetDb.elements.deleteAspect(aspectDeleteIds);
}
/** Transform the specified sourceRelationship into RelationshipProps for the target iModel.
* @param sourceRelationship The Relationship from the source iModel to be transformed.
* @returns RelationshipProps for the target iModel.
* @note A subclass can override this method to provide custom transform behavior.
*/
protected onTransformRelationship(sourceRelationship: Relationship): RelationshipProps {
const targetRelationshipProps: RelationshipProps = sourceRelationship.toJSON();
targetRelationshipProps.sourceId = this.context.findTargetElementId(sourceRelationship.sourceId);
targetRelationshipProps.targetId = this.context.findTargetElementId(sourceRelationship.targetId);
sourceRelationship.forEachProperty((propertyName: string, propertyMetaData: PropertyMetaData) => {
if ((PrimitiveTypeCode.Long === propertyMetaData.primitiveType) && ("Id" === propertyMetaData.extendedType)) {
(targetRelationshipProps as any)[propertyName] = this.context.findTargetElementId(sourceRelationship.asAny[propertyName]);
}
});
return targetRelationshipProps;
}
/** Override of [IModelExportHandler.shouldExportElementAspect]($transformer) that is called to determine if an ElementAspect should be exported from the source iModel.
* @note Reaching this point means that the ElementAspect has passed the standard exclusion checks in [IModelExporter]($transformer).
*/
public override shouldExportElementAspect(sourceAspect: ElementAspect): boolean {
// When exporting elements, the transformer may choose to *defer* an element, which is something the exporter doesn't understand,
// so it tries to export aspects anyway. As such we need to *not* export aspects for deferred elements.
// When we retry processing deferred elements and successfully export them is when we export the aspects again
const targetElementId = this.context.findTargetElementId(sourceAspect.element.id);
if (targetElementId === Id64.invalid && this._deferredElementIds.has(sourceAspect.element.id)) {
Logger.logInfo(
loggerCategory,
`tried to export an aspect for a deferred element, '${sourceAspect.element.id}', ` +
"it will be retried when the deferred element is processed again."
);
return false;
}
return true;
}
/** Override of [IModelExportHandler.onExportElementUniqueAspect]($transformer) that imports an ElementUniqueAspect into the target iModel when it is exported from the source iModel.
* This override calls [[onTransformElementAspect]] and then [IModelImporter.importElementUniqueAspect]($transformer) to update the target iModel.
*/
public override onExportElementUniqueAspect(sourceAspect: ElementUniqueAspect): void {
const targetElementId: Id64String = this.context.findTargetElementId(sourceAspect.element.id);
const targetAspectProps: ElementAspectProps = this.onTransformElementAspect(sourceAspect, targetElementId);
this.importer.importElementUniqueAspect(targetAspectProps);
}
/** Override of [IModelExportHandler.onExportElementMultiAspects]($transformer) that imports ElementMultiAspects into the target iModel when they are exported from the source iModel.
* This override calls [[onTransformElementAspect]] for each ElementMultiAspect and then [IModelImporter.importElementMultiAspects]($transformer) to update the target iModel.
* @note ElementMultiAspects are handled as a group to make it easier to differentiate between insert, update, and delete.
*/
public override onExportElementMultiAspects(sourceAspects: ElementMultiAspect[]): void {
const targetElementId: Id64String = this.context.findTargetElementId(sourceAspects[0].element.id);
// Transform source ElementMultiAspects into target ElementAspectProps
const targetAspectPropsArray: ElementAspectProps[] = sourceAspects.map((sourceAspect: ElementMultiAspect) => {
return this.onTransformElementAspect(sourceAspect, targetElementId);
});
if (this._options.includeSourceProvenance) {
this.importer.importElementMultiAspects(targetAspectPropsArray, (a: ElementMultiAspect) => {
return (a instanceof ExternalSourceAspect) ? a.scope.id !== this.targetScopeElementId : true; // filter out ExternalSourceAspects added by IModelTransformer
});
} else {
this.importer.importElementMultiAspects(targetAspectPropsArray);
}
}
/** Transform the specified sourceElementAspect into ElementAspectProps for the target iModel.
* @param sourceElementAspect The ElementAspect from the source iModel to be transformed.
* @param targetElementId The ElementId of the target Element that will own the ElementAspects after transformation.
* @returns ElementAspectProps for the target iModel.
* @note A subclass can override this method to provide custom transform behavior.
*/
protected onTransformElementAspect(sourceElementAspect: ElementAspect, _targetElementId: Id64String): ElementAspectProps {
const targetElementAspectProps: ElementAspectProps = sourceElementAspect.toJSON();
targetElementAspectProps.id = undefined;
sourceElementAspect.forEachProperty((propertyName: string, propertyMetaData: PropertyMetaData) => {
if (propertyMetaData.isNavigation) {
if (sourceElementAspect.asAny[propertyName]?.id) {
(targetElementAspectProps as any)[propertyName].id = this.context.findTargetElementId(sourceElementAspect.asAny[propertyName].id);
}
} else if ((PrimitiveTypeCode.Long === propertyMetaData.primitiveType) && ("Id" === propertyMetaData.extendedType)) {
(targetElementAspectProps as any)[propertyName] = this.context.findTargetElementId(sourceElementAspect.asAny[propertyName]);
}
});
return targetElementAspectProps;
}
/** The directory where schemas will be exported, a random temporary directory */
protected _schemaExportDir: string = path.join(KnownLocations.tmpdir, Guid.createValue());
/** Override of [IModelExportHandler.shouldExportSchema]($transformer) that is called to determine if a schema should be exported
* @note the default behavior doesn't import schemas older than those already in the target
*/
public override shouldExportSchema(schemaKey: ECSchemaMetaData.SchemaKey): boolean {
const versionInTarget = this.targetDb.querySchemaVersion(schemaKey.name);
if (versionInTarget === undefined)
return true;
return Semver.gt(`${schemaKey.version.read}.${schemaKey.version.write}.${schemaKey.version.minor}`, Schema.toSemverString(versionInTarget));
}
/** Override of [IModelExportHandler.onExportSchema]($transformer) that serializes a schema to disk for [[processSchemas]] to import into
* the target iModel when it is exported from the source iModel. */
public override async onExportSchema(schema: ECSchemaMetaData.Schema): Promise<void> {
this.sourceDb.nativeDb.exportSchema(schema.name, this._schemaExportDir);
}
/** Cause all schemas to be exported from the source iModel and imported into the target iModel.
* @note For performance reasons, it is recommended that [IModelDb.saveChanges]($backend) be called after `processSchemas` is complete.
* It is more efficient to process *data* changes after the schema changes have been saved.
*/
public async processSchemas(): Promise<void> {
try {
IModelJsFs.mkdirSync(this._schemaExportDir);
await this.exporter.exportSchemas();
const exportedSchemaFiles = IModelJsFs.readdirSync(this._schemaExportDir);
if (exportedSchemaFiles.length === 0)
return;
const schemaFullPaths = exportedSchemaFiles.map((s) => path.join(this._schemaExportDir, s));
return await this.targetDb.importSchemas(schemaFullPaths);
} finally {
IModelJsFs.removeSync(this._schemaExportDir);
}
}
/** Cause all fonts to be exported from the source iModel and imported into the target iModel.
* @note This method is called from [[processChanges]] and [[processAll]], so it only needs to be called directly when processing a subset of an iModel.
*/
public async processFonts(): Promise<void> {
return this.exporter.exportFonts();
}
/** Override of [IModelExportHandler.onExportFont]($transformer) that imports a font into the target iModel when it is exported from the source iModel. */
public override onExportFont(font: FontProps, _isUpdate: boolean | undefined): void {
this.context.importFont(font.id);
}
/** Cause all CodeSpecs to be exported from the source iModel and imported into the target iModel.
* @note This method is called from [[processChanges]] and [[processAll]], so it only needs to be called directly when processing a subset of an iModel.
*/
public async processCodeSpecs(): Promise<void> {
return this.exporter.exportCodeSpecs();
}
/** Cause a single CodeSpec to be exported from the source iModel and imported into the target iModel.
* @note This method is called from [[processChanges]] and [[processAll]], so it only needs to be called directly when processing a subset of an iModel.
*/
public async processCodeSpec(codeSpecName: string): Promise<void> {
return this.exporter.exportCodeSpecByName(codeSpecName);
}
/** Override of [IModelExportHandler.shouldExportCodeSpec]($transformer) that is called to determine if a CodeSpec should be exported from the source iModel.
* @note Reaching this point means that the CodeSpec has passed the standard exclusion checks in [IModelExporter]($transformer).
*/
public override shouldExportCodeSpec(_sourceCodeSpec: CodeSpec): boolean { return true; }
/** Override of [IModelExportHandler.onExportCodeSpec]($transformer) that imports a CodeSpec into the target iModel when it is exported from the source iModel. */
public override onExportCodeSpec(sourceCodeSpec: CodeSpec): void {
this.context.importCodeSpec(sourceCodeSpec.id);
}
/** Recursively import all Elements and sub-Models that descend from the specified Subject */
public async processSubject(sourceSubjectId: Id64String, targetSubjectId: Id64String): Promise<void> {
this.sourceDb.elements.getElement(sourceSubjectId, Subject); // throws if sourceSubjectId is not a Subject
this.targetDb.elements.getElement(targetSubjectId, Subject); // throws if targetSubjectId is not a Subject
this.context.remapElement(sourceSubjectId, targetSubjectId);
await this.processChildElements(sourceSubjectId);
await this.processSubjectSubModels(sourceSubjectId);
return this.processDeferredElements();
}
/** Export everything from the source iModel and import the transformed entities into the target iModel.
* @note [[processSchemas]] is not called automatically since the target iModel may want a different collection of schemas.
*/
public async processAll(): Promise<void> {
Logger.logTrace(loggerCategory, "processAll()");
this.logSettings();
this.validateScopeProvenance();
this.initFromExternalSourceAspects();
await this.exporter.exportCodeSpecs();
await this.exporter.exportFonts();
// The RepositoryModel and root Subject of the target iModel should not be transformed.
await this.exporter.exportChildElements(IModel.rootSubjectId); // start below the root Subject
await this.exporter.exportModelContents(IModel.repositoryModelId, Element.classFullName, true); // after the Subject hierarchy, process the other elements of the RepositoryModel
await this.exporter.exportSubModels(IModel.repositoryModelId); // start below the RepositoryModel
await this.exporter.exportRelationships(ElementRefersToElements.classFullName);
await this.processDeferredElements();
if (this.shouldDetectDeletes()) {
await this.detectElementDeletes();
await this.detectRelationshipDeletes();
}
this.importer.computeProjectExtents();
}
/** Export changes from the source iModel and import the transformed entities into the target iModel.
* Inserts, updates, and deletes are determined by inspecting the changeset(s).
* @param accessToken A valid access token string
* @param startChangesetId Include changes from this changeset up through and including the current changeset.
* If this parameter is not provided, then just the current changeset will be exported.
* @note To form a range of versions to process, set `startChangesetId` for the start (inclusive) of the desired range and open the source iModel as of the end (inclusive) of the desired range.
*/
public async processChanges(accessToken: AccessToken, startChangesetId?: string): Promise<void> {
Logger.logTrace(loggerCategory, "processChanges()");
this.logSettings();
this.validateScopeProvenance();
this.initFromExternalSourceAspects();
await this.exporter.exportChanges(accessToken, startChangesetId);
await this.processDeferredElements();
this.importer.computeProjectExtents();
}
}
/** IModelTransformer that clones the contents of a template model.
* @beta
*/
export class TemplateModelCloner extends IModelTransformer {
/** The Placement to apply to the template. */
private _transform3d?: Transform;
/** Accumulates the mapping of sourceElementIds to targetElementIds from the elements in the template model that were cloned. */
private _sourceIdToTargetIdMap?: Map<Id64String, Id64String>;
/** Construct a new TemplateModelCloner
* @param sourceDb The source IModelDb that contains the templates to clone
* @param targetDb Optionally specify the target IModelDb where the cloned template will be inserted.
* Typically this is left unspecified, and the default is to use the sourceDb as the target
* @note The expectation is that the template definitions are within the same iModel where instances will be placed.
*/
public constructor(sourceDb: IModelDb, targetDb: IModelDb = sourceDb) {
const target = new IModelImporter(targetDb, {
autoExtendProjectExtents: false, // autoExtendProjectExtents is intended for transformation service use cases, not template --> instance cloning
});
super(sourceDb, target, { noProvenance: true }); // WIP: need to decide the proper way to handle provenance
}
/** Place a template from the sourceDb at the specified placement in the target model within the targetDb.
* @param sourceTemplateModelId The Id of the template model in the sourceDb
* @param targetModelId The Id of the target model (must be a subclass of GeometricModel3d) where the cloned component will be inserted.
* @param placement The placement for the cloned component.
* @note *Predecessors* like the SpatialCategory must be remapped before calling this method.
* @returns The mapping of sourceElementIds from the template model to the instantiated targetElementIds in the targetDb in case further processing is required.
*/
public async placeTemplate3d(sourceTemplateModelId: Id64String, targetModelId: Id64String, placement: Placement3d): Promise<Map<Id64String, Id64String>> {
this.context.remapElement(sourceTemplateModelId, targetModelId);
this._transform3d = Transform.createOriginAndMatrix(placement.origin, placement.angles.toMatrix3d());
this._sourceIdToTargetIdMap = new Map<Id64String, Id64String>();
await this.exporter.exportModelContents(sourceTemplateModelId);
// Note: the source --> target mapping was needed during the template model cloning phase (remapping parent/child, for example), but needs to be reset afterwards
for (const sourceElementId of this._sourceIdToTargetIdMap.keys()) {
const targetElementId = this.context.findTargetElementId(sourceElementId);
this._sourceIdToTargetIdMap.set(sourceElementId, targetElementId);
this.context.removeElement(sourceElementId); // clear the underlying native remapping context for the next clone operation
}
return this._sourceIdToTargetIdMap; // return the sourceElementId -> targetElementId Map in case further post-processing is required.
}
/** Place a template from the sourceDb at the specified placement in the target model within the targetDb.
* @param sourceTemplateModelId The Id of the template model in the sourceDb
* @param targetModelId The Id of the target model (must be a subclass of GeometricModel2d) where the cloned component will be inserted.
* @param placement The placement for the cloned component.
* @note *Predecessors* like the DrawingCategory must be remapped before calling this method.
* @returns The mapping of sourceElementIds from the template model to the instantiated targetElementIds in the targetDb in case further processing is required.
*/
public async placeTemplate2d(sourceTemplateModelId: Id64String, targetModelId: Id64String, placement: Placement2d): Promise<Map<Id64String, Id64String>> {
this.context.remapElement(sourceTemplateModelId, targetModelId);
this._transform3d = Transform.createOriginAndMatrix(Point3d.createFrom(placement.origin), placement.rotation);
this._sourceIdToTargetIdMap = new Map<Id64String, Id64String>();
await this.exporter.exportModelContents(sourceTemplateModelId);
// Note: the source --> target mapping was needed during the template model cloning phase (remapping parent/child, for example), but needs to be reset afterwards
for (const sourceElementId of this._sourceIdToTargetIdMap.keys()) {
const targetElementId = this.context.findTargetElementId(sourceElementId);
this._sourceIdToTargetIdMap.set(sourceElementId, targetElementId);
this.context.removeElement(sourceElementId); // clear the underlying native remapping context for the next clone operation
}
return this._sourceIdToTargetIdMap; // return the sourceElementId -> targetElementId Map in case further post-processing is required.
}