@itwin/presentation-backend 3.7.7 → 3.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CHANGELOG.md +6 -1
  2. package/lib/cjs/assets/primary-presentation-rules/ElementProperties.PresentationRuleSet.json +14 -14
  3. package/lib/cjs/assets/supplemental-presentation-rules/BisCore.PresentationRuleSet.json +764 -764
  4. package/lib/cjs/assets/supplemental-presentation-rules/Functional.PresentationRuleSet.json +139 -139
  5. package/lib/cjs/presentation-backend/BackendLoggerCategory.d.ts +41 -41
  6. package/lib/cjs/presentation-backend/BackendLoggerCategory.js +50 -50
  7. package/lib/cjs/presentation-backend/BackendLoggerCategory.js.map +1 -1
  8. package/lib/cjs/presentation-backend/Constants.d.ts +8 -8
  9. package/lib/cjs/presentation-backend/Constants.js +36 -36
  10. package/lib/cjs/presentation-backend/Constants.js.map +1 -1
  11. package/lib/cjs/presentation-backend/ElementPropertiesHelper.d.ts +11 -11
  12. package/lib/cjs/presentation-backend/ElementPropertiesHelper.js +274 -274
  13. package/lib/cjs/presentation-backend/ElementPropertiesHelper.js.map +1 -1
  14. package/lib/cjs/presentation-backend/NativePlatform.d.ts +87 -87
  15. package/lib/cjs/presentation-backend/NativePlatform.js +140 -140
  16. package/lib/cjs/presentation-backend/NativePlatform.js.map +1 -1
  17. package/lib/cjs/presentation-backend/Presentation.d.ts +106 -106
  18. package/lib/cjs/presentation-backend/Presentation.js +150 -150
  19. package/lib/cjs/presentation-backend/Presentation.js.map +1 -1
  20. package/lib/cjs/presentation-backend/PresentationIpcHandler.d.ts +12 -12
  21. package/lib/cjs/presentation-backend/PresentationIpcHandler.js +41 -41
  22. package/lib/cjs/presentation-backend/PresentationIpcHandler.js.map +1 -1
  23. package/lib/cjs/presentation-backend/PresentationManager.d.ts +445 -445
  24. package/lib/cjs/presentation-backend/PresentationManager.js +311 -311
  25. package/lib/cjs/presentation-backend/PresentationManager.js.map +1 -1
  26. package/lib/cjs/presentation-backend/PresentationManagerDetail.d.ts +61 -61
  27. package/lib/cjs/presentation-backend/PresentationManagerDetail.js +430 -430
  28. package/lib/cjs/presentation-backend/PresentationManagerDetail.js.map +1 -1
  29. package/lib/cjs/presentation-backend/PresentationRpcImpl.d.ts +62 -62
  30. package/lib/cjs/presentation-backend/PresentationRpcImpl.js +388 -388
  31. package/lib/cjs/presentation-backend/PresentationRpcImpl.js.map +1 -1
  32. package/lib/cjs/presentation-backend/RulesetEmbedder.d.ts +102 -102
  33. package/lib/cjs/presentation-backend/RulesetEmbedder.js +283 -283
  34. package/lib/cjs/presentation-backend/RulesetEmbedder.js.map +1 -1
  35. package/lib/cjs/presentation-backend/RulesetManager.d.ts +53 -53
  36. package/lib/cjs/presentation-backend/RulesetManager.js +73 -73
  37. package/lib/cjs/presentation-backend/RulesetManager.js.map +1 -1
  38. package/lib/cjs/presentation-backend/RulesetVariablesManager.d.ts +140 -140
  39. package/lib/cjs/presentation-backend/RulesetVariablesManager.js +129 -129
  40. package/lib/cjs/presentation-backend/RulesetVariablesManager.js.map +1 -1
  41. package/lib/cjs/presentation-backend/SelectionScopesHelper.d.ts +28 -28
  42. package/lib/cjs/presentation-backend/SelectionScopesHelper.js +229 -229
  43. package/lib/cjs/presentation-backend/SelectionScopesHelper.js.map +1 -1
  44. package/lib/cjs/presentation-backend/TemporaryStorage.d.ts +123 -123
  45. package/lib/cjs/presentation-backend/TemporaryStorage.js +151 -151
  46. package/lib/cjs/presentation-backend/TemporaryStorage.js.map +1 -1
  47. package/lib/cjs/presentation-backend/UpdatesTracker.d.ts +27 -27
  48. package/lib/cjs/presentation-backend/UpdatesTracker.js +54 -54
  49. package/lib/cjs/presentation-backend/UpdatesTracker.js.map +1 -1
  50. package/lib/cjs/presentation-backend/Utils.d.ts +49 -49
  51. package/lib/cjs/presentation-backend/Utils.js +106 -106
  52. package/lib/cjs/presentation-backend/Utils.js.map +1 -1
  53. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.d.ts +16 -16
  54. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.js +51 -51
  55. package/lib/cjs/presentation-backend/domain/PresentationRulesDomain.js.map +1 -1
  56. package/lib/cjs/presentation-backend/domain/RulesetElements.d.ts +21 -21
  57. package/lib/cjs/presentation-backend/domain/RulesetElements.js +38 -38
  58. package/lib/cjs/presentation-backend/domain/RulesetElements.js.map +1 -1
  59. package/lib/cjs/presentation-backend.d.ts +19 -19
  60. package/lib/cjs/presentation-backend.js +35 -35
  61. package/lib/cjs/presentation-backend.js.map +1 -1
  62. package/package.json +13 -13
@@ -1,284 +1,284 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- /** @packageDocumentation
7
- * @module Core
8
- */
9
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12
- }) : (function(o, m, k, k2) {
13
- if (k2 === undefined) k2 = k;
14
- o[k2] = m[k];
15
- }));
16
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17
- Object.defineProperty(o, "default", { enumerable: true, value: v });
18
- }) : function(o, v) {
19
- o["default"] = v;
20
- });
21
- var __importStar = (this && this.__importStar) || function (mod) {
22
- if (mod && mod.__esModule) return mod;
23
- var result = {};
24
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
- __setModuleDefault(result, mod);
26
- return result;
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.RulesetEmbedder = void 0;
30
- const path = __importStar(require("path"));
31
- const semver_1 = require("semver");
32
- const core_backend_1 = require("@itwin/core-backend");
33
- const core_bentley_1 = require("@itwin/core-bentley");
34
- const core_common_1 = require("@itwin/core-common");
35
- const PresentationRulesDomain_1 = require("./domain/PresentationRulesDomain");
36
- const RulesetElements = __importStar(require("./domain/RulesetElements"));
37
- const Utils_1 = require("./Utils");
38
- /**
39
- * An API for embedding presentation rulesets into iModels.
40
- * @public
41
- */
42
- class RulesetEmbedder {
43
- /**
44
- * Constructs RulesetEmbedder
45
- */
46
- constructor(props) {
47
- this._schemaPath = path.join(core_backend_1.KnownLocations.nativeAssetsDir, "ECSchemas/Domain/PresentationRules.ecschema.xml");
48
- this._rulesetModelName = "PresentationRules";
49
- this._rulesetSubjectName = "PresentationRules";
50
- PresentationRulesDomain_1.PresentationRules.registerSchema();
51
- this._imodel = props.imodel;
52
- }
53
- /**
54
- * Inserts a ruleset into iModel.
55
- * @param ruleset Ruleset to insert.
56
- * @param options Options for inserting a ruleset.
57
- * @returns ID of inserted ruleset element or, if insertion was skipped, ID of existing ruleset with the same ID and highest version.
58
- */
59
- async insertRuleset(ruleset, options) {
60
- const normalizedOptions = normalizeRulesetInsertOptions(options);
61
- const rulesetVersion = (0, Utils_1.normalizeVersion)(ruleset.version);
62
- // ensure imodel has PresentationRules schema and required CodeSpecs
63
- await this.handleElementOperationPrerequisites();
64
- // find all rulesets with the same ID
65
- const rulesetsWithSameId = [];
66
- const query = `
67
- SELECT ECInstanceId, JsonProperties
68
- FROM ${RulesetElements.Ruleset.schema.name}.${RulesetElements.Ruleset.className}
69
- WHERE json_extract(JsonProperties, '$.jsonProperties.id') = :rulesetId`;
70
- // eslint-disable-next-line deprecation/deprecation
71
- for await (const row of this._imodel.query(query, core_common_1.QueryBinder.from({ rulesetId: ruleset.id }), { rowFormat: core_common_1.QueryRowFormat.UseJsPropertyNames })) {
72
- const existingRulesetElementId = row.id;
73
- const existingRuleset = JSON.parse(row.jsonProperties).jsonProperties;
74
- rulesetsWithSameId.push({
75
- id: existingRulesetElementId,
76
- ruleset: existingRuleset,
77
- normalizedVersion: (0, Utils_1.normalizeVersion)(existingRuleset.version),
78
- });
79
- }
80
- // check if we need to do anything at all
81
- const shouldSkip = normalizedOptions.skip === "same-id" && rulesetsWithSameId.length > 0
82
- || normalizedOptions.skip === "same-id-and-version-eq" && rulesetsWithSameId.some((entry) => entry.normalizedVersion === rulesetVersion)
83
- || normalizedOptions.skip === "same-id-and-version-gte" && rulesetsWithSameId.some((entry) => (0, semver_1.gte)(entry.normalizedVersion, rulesetVersion));
84
- if (shouldSkip) {
85
- // we're not inserting anything - return ID of the ruleset element with the highest version
86
- const rulesetEntryWithHighestVersion = rulesetsWithSameId.reduce((highest, curr) => {
87
- if (!highest.ruleset.version || curr.ruleset.version && (0, semver_1.gt)(curr.ruleset.version, highest.ruleset.version))
88
- return curr;
89
- return highest;
90
- }, rulesetsWithSameId[0]);
91
- return rulesetEntryWithHighestVersion.id;
92
- }
93
- // if requested, delete existing rulesets
94
- const rulesetsToRemove = [];
95
- const shouldRemove = (_, normalizedVersion) => {
96
- switch (normalizedOptions.replaceVersions) {
97
- case "all":
98
- return normalizedVersion !== rulesetVersion;
99
- case "all-lower":
100
- return normalizedVersion !== rulesetVersion && (0, semver_1.lt)(normalizedVersion, rulesetVersion);
101
- }
102
- return false;
103
- };
104
- rulesetsWithSameId.forEach((entry) => {
105
- if (shouldRemove(entry.ruleset, entry.normalizedVersion))
106
- rulesetsToRemove.push(entry.id);
107
- });
108
- this._imodel.elements.deleteElement(rulesetsToRemove);
109
- // attempt to update ruleset with same ID and version
110
- const exactMatch = rulesetsWithSameId.find((curr) => curr.normalizedVersion === rulesetVersion);
111
- if (exactMatch !== undefined) {
112
- return this.updateRuleset(exactMatch.id, ruleset, normalizedOptions.onEntityUpdate);
113
- }
114
- // no exact match found - insert a new ruleset element
115
- const model = await this.getOrCreateRulesetModel(normalizedOptions.onEntityInsert);
116
- const rulesetCode = RulesetElements.Ruleset.createRulesetCode(this._imodel, model.id, ruleset);
117
- return this.insertNewRuleset(ruleset, model, rulesetCode, normalizedOptions.onEntityInsert);
118
- }
119
- async updateRuleset(elementId, ruleset, callbacks) {
120
- const existingRulesetElement = this._imodel.elements.tryGetElement(elementId);
121
- (0, core_bentley_1.assert)(existingRulesetElement !== undefined);
122
- existingRulesetElement.jsonProperties.jsonProperties = ruleset;
123
- await this.updateElement(existingRulesetElement, callbacks);
124
- this._imodel.saveChanges();
125
- return existingRulesetElement.id;
126
- }
127
- async insertNewRuleset(ruleset, model, rulesetCode, callbacks) {
128
- const props = {
129
- model: model.id,
130
- code: rulesetCode,
131
- classFullName: RulesetElements.Ruleset.classFullName,
132
- jsonProperties: { jsonProperties: ruleset },
133
- };
134
- const element = await this.insertElement(props, callbacks);
135
- this._imodel.saveChanges();
136
- return element.id;
137
- }
138
- /**
139
- * Get all rulesets embedded in the iModel.
140
- */
141
- async getRulesets() {
142
- if (!this._imodel.containsClass(RulesetElements.Ruleset.classFullName))
143
- return [];
144
- const rulesetList = [];
145
- this._imodel.withPreparedStatement(`SELECT ECInstanceId AS id FROM ${RulesetElements.Ruleset.classFullName}`, (statement) => {
146
- while (core_bentley_1.DbResult.BE_SQLITE_ROW === statement.step()) {
147
- const row = statement.getRow();
148
- const rulesetElement = this._imodel.elements.getElement({ id: row.id });
149
- const ruleset = rulesetElement.jsonProperties.jsonProperties;
150
- rulesetList.push(ruleset);
151
- }
152
- });
153
- return rulesetList;
154
- }
155
- async getOrCreateRulesetModel(callbacks) {
156
- const rulesetModel = this.queryRulesetModel();
157
- if (undefined !== rulesetModel)
158
- return rulesetModel;
159
- const rulesetSubject = await this.insertSubject(callbacks);
160
- const definitionPartition = await this.insertDefinitionPartition(rulesetSubject, callbacks);
161
- return this.insertDefinitionModel(definitionPartition, callbacks);
162
- }
163
- queryRulesetModel() {
164
- const definitionPartition = this.queryDefinitionPartition();
165
- if (undefined === definitionPartition)
166
- return undefined;
167
- return this._imodel.models.getSubModel(definitionPartition.id);
168
- }
169
- queryDefinitionPartition() {
170
- const subject = this.querySubject();
171
- if (undefined === subject)
172
- return undefined;
173
- return this._imodel.elements.tryGetElement(core_backend_1.DefinitionPartition.createCode(this._imodel, subject.id, this._rulesetModelName));
174
- }
175
- querySubject() {
176
- const root = this._imodel.elements.getRootSubject();
177
- const codeSpec = this._imodel.codeSpecs.getByName(core_common_1.BisCodeSpec.subject);
178
- const code = new core_common_1.Code({
179
- spec: codeSpec.id,
180
- scope: root.id,
181
- value: this._rulesetSubjectName,
182
- });
183
- return this._imodel.elements.tryGetElement(code);
184
- }
185
- async insertDefinitionModel(definitionPartition, callbacks) {
186
- const modelProps = {
187
- modeledElement: definitionPartition,
188
- name: this._rulesetModelName,
189
- classFullName: core_backend_1.DefinitionModel.classFullName,
190
- isPrivate: true,
191
- };
192
- return this.insertModel(modelProps, callbacks);
193
- }
194
- async insertDefinitionPartition(rulesetSubject, callbacks) {
195
- const partitionCode = core_backend_1.DefinitionPartition.createCode(this._imodel, rulesetSubject.id, this._rulesetModelName);
196
- const definitionPartitionProps = {
197
- parent: {
198
- id: rulesetSubject.id,
199
- relClassName: "BisCore:SubjectOwnsPartitionElements",
200
- },
201
- model: rulesetSubject.model,
202
- code: partitionCode,
203
- classFullName: core_backend_1.DefinitionPartition.classFullName,
204
- };
205
- return this.insertElement(definitionPartitionProps, callbacks);
206
- }
207
- async insertSubject(callbacks) {
208
- const root = this._imodel.elements.getRootSubject();
209
- const codeSpec = this._imodel.codeSpecs.getByName(core_common_1.BisCodeSpec.subject);
210
- const subjectCode = new core_common_1.Code({
211
- spec: codeSpec.id,
212
- scope: root.id,
213
- value: this._rulesetSubjectName,
214
- });
215
- const subjectProps = {
216
- classFullName: core_backend_1.Subject.classFullName,
217
- model: root.model,
218
- parent: {
219
- id: root.id,
220
- relClassName: "BisCore:SubjectOwnsSubjects",
221
- },
222
- code: subjectCode,
223
- };
224
- return this.insertElement(subjectProps, callbacks);
225
- }
226
- async handleElementOperationPrerequisites() {
227
- if (this._imodel.containsClass(RulesetElements.Ruleset.classFullName))
228
- return;
229
- // import PresentationRules ECSchema
230
- await this._imodel.importSchemas([this._schemaPath]);
231
- // insert CodeSpec for ruleset elements
232
- this._imodel.codeSpecs.insert(core_common_1.CodeSpec.create(this._imodel, PresentationRulesDomain_1.PresentationRules.CodeSpec.Ruleset, core_common_1.CodeScopeSpec.Type.Model));
233
- this._imodel.saveChanges();
234
- }
235
- async insertElement(props, callbacks) {
236
- const element = this._imodel.elements.createElement(props);
237
- // istanbul ignore next
238
- await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onBeforeInsert(element));
239
- try {
240
- return this._imodel.elements.getElement(element.insert());
241
- }
242
- finally {
243
- // istanbul ignore next
244
- await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onAfterInsert(element));
245
- }
246
- }
247
- async insertModel(props, callbacks) {
248
- const model = this._imodel.models.createModel(props);
249
- // istanbul ignore next
250
- await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onBeforeInsert(model));
251
- try {
252
- model.id = model.insert();
253
- return model;
254
- }
255
- finally {
256
- // istanbul ignore next
257
- await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onAfterInsert(model));
258
- }
259
- }
260
- async updateElement(element, callbacks) {
261
- // istanbul ignore next
262
- await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onBeforeUpdate(element));
263
- try {
264
- element.update();
265
- }
266
- finally {
267
- // istanbul ignore next
268
- await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onAfterUpdate(element));
269
- }
270
- }
271
- }
272
- exports.RulesetEmbedder = RulesetEmbedder;
273
- function normalizeRulesetInsertOptions(options) {
274
- var _a, _b;
275
- if (options === undefined)
276
- return { skip: "same-id-and-version-eq", replaceVersions: "exact" };
277
- return {
278
- skip: (_a = options.skip) !== null && _a !== void 0 ? _a : "same-id-and-version-eq",
279
- replaceVersions: (_b = options.replaceVersions) !== null && _b !== void 0 ? _b : "exact",
280
- onEntityUpdate: options.onEntityUpdate,
281
- onEntityInsert: options.onEntityInsert,
282
- };
283
- }
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ /** @packageDocumentation
7
+ * @module Core
8
+ */
9
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
18
+ }) : function(o, v) {
19
+ o["default"] = v;
20
+ });
21
+ var __importStar = (this && this.__importStar) || function (mod) {
22
+ if (mod && mod.__esModule) return mod;
23
+ var result = {};
24
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
+ __setModuleDefault(result, mod);
26
+ return result;
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.RulesetEmbedder = void 0;
30
+ const path = __importStar(require("path"));
31
+ const semver_1 = require("semver");
32
+ const core_backend_1 = require("@itwin/core-backend");
33
+ const core_bentley_1 = require("@itwin/core-bentley");
34
+ const core_common_1 = require("@itwin/core-common");
35
+ const PresentationRulesDomain_1 = require("./domain/PresentationRulesDomain");
36
+ const RulesetElements = __importStar(require("./domain/RulesetElements"));
37
+ const Utils_1 = require("./Utils");
38
+ /**
39
+ * An API for embedding presentation rulesets into iModels.
40
+ * @public
41
+ */
42
+ class RulesetEmbedder {
43
+ /**
44
+ * Constructs RulesetEmbedder
45
+ */
46
+ constructor(props) {
47
+ this._schemaPath = path.join(core_backend_1.KnownLocations.nativeAssetsDir, "ECSchemas/Domain/PresentationRules.ecschema.xml");
48
+ this._rulesetModelName = "PresentationRules";
49
+ this._rulesetSubjectName = "PresentationRules";
50
+ PresentationRulesDomain_1.PresentationRules.registerSchema();
51
+ this._imodel = props.imodel;
52
+ }
53
+ /**
54
+ * Inserts a ruleset into iModel.
55
+ * @param ruleset Ruleset to insert.
56
+ * @param options Options for inserting a ruleset.
57
+ * @returns ID of inserted ruleset element or, if insertion was skipped, ID of existing ruleset with the same ID and highest version.
58
+ */
59
+ async insertRuleset(ruleset, options) {
60
+ const normalizedOptions = normalizeRulesetInsertOptions(options);
61
+ const rulesetVersion = (0, Utils_1.normalizeVersion)(ruleset.version);
62
+ // ensure imodel has PresentationRules schema and required CodeSpecs
63
+ await this.handleElementOperationPrerequisites();
64
+ // find all rulesets with the same ID
65
+ const rulesetsWithSameId = [];
66
+ const query = `
67
+ SELECT ECInstanceId, JsonProperties
68
+ FROM ${RulesetElements.Ruleset.schema.name}.${RulesetElements.Ruleset.className}
69
+ WHERE json_extract(JsonProperties, '$.jsonProperties.id') = :rulesetId`;
70
+ // eslint-disable-next-line deprecation/deprecation
71
+ for await (const row of this._imodel.query(query, core_common_1.QueryBinder.from({ rulesetId: ruleset.id }), { rowFormat: core_common_1.QueryRowFormat.UseJsPropertyNames })) {
72
+ const existingRulesetElementId = row.id;
73
+ const existingRuleset = JSON.parse(row.jsonProperties).jsonProperties;
74
+ rulesetsWithSameId.push({
75
+ id: existingRulesetElementId,
76
+ ruleset: existingRuleset,
77
+ normalizedVersion: (0, Utils_1.normalizeVersion)(existingRuleset.version),
78
+ });
79
+ }
80
+ // check if we need to do anything at all
81
+ const shouldSkip = normalizedOptions.skip === "same-id" && rulesetsWithSameId.length > 0
82
+ || normalizedOptions.skip === "same-id-and-version-eq" && rulesetsWithSameId.some((entry) => entry.normalizedVersion === rulesetVersion)
83
+ || normalizedOptions.skip === "same-id-and-version-gte" && rulesetsWithSameId.some((entry) => (0, semver_1.gte)(entry.normalizedVersion, rulesetVersion));
84
+ if (shouldSkip) {
85
+ // we're not inserting anything - return ID of the ruleset element with the highest version
86
+ const rulesetEntryWithHighestVersion = rulesetsWithSameId.reduce((highest, curr) => {
87
+ if (!highest.ruleset.version || curr.ruleset.version && (0, semver_1.gt)(curr.ruleset.version, highest.ruleset.version))
88
+ return curr;
89
+ return highest;
90
+ }, rulesetsWithSameId[0]);
91
+ return rulesetEntryWithHighestVersion.id;
92
+ }
93
+ // if requested, delete existing rulesets
94
+ const rulesetsToRemove = [];
95
+ const shouldRemove = (_, normalizedVersion) => {
96
+ switch (normalizedOptions.replaceVersions) {
97
+ case "all":
98
+ return normalizedVersion !== rulesetVersion;
99
+ case "all-lower":
100
+ return normalizedVersion !== rulesetVersion && (0, semver_1.lt)(normalizedVersion, rulesetVersion);
101
+ }
102
+ return false;
103
+ };
104
+ rulesetsWithSameId.forEach((entry) => {
105
+ if (shouldRemove(entry.ruleset, entry.normalizedVersion))
106
+ rulesetsToRemove.push(entry.id);
107
+ });
108
+ this._imodel.elements.deleteElement(rulesetsToRemove);
109
+ // attempt to update ruleset with same ID and version
110
+ const exactMatch = rulesetsWithSameId.find((curr) => curr.normalizedVersion === rulesetVersion);
111
+ if (exactMatch !== undefined) {
112
+ return this.updateRuleset(exactMatch.id, ruleset, normalizedOptions.onEntityUpdate);
113
+ }
114
+ // no exact match found - insert a new ruleset element
115
+ const model = await this.getOrCreateRulesetModel(normalizedOptions.onEntityInsert);
116
+ const rulesetCode = RulesetElements.Ruleset.createRulesetCode(this._imodel, model.id, ruleset);
117
+ return this.insertNewRuleset(ruleset, model, rulesetCode, normalizedOptions.onEntityInsert);
118
+ }
119
+ async updateRuleset(elementId, ruleset, callbacks) {
120
+ const existingRulesetElement = this._imodel.elements.tryGetElement(elementId);
121
+ (0, core_bentley_1.assert)(existingRulesetElement !== undefined);
122
+ existingRulesetElement.jsonProperties.jsonProperties = ruleset;
123
+ await this.updateElement(existingRulesetElement, callbacks);
124
+ this._imodel.saveChanges();
125
+ return existingRulesetElement.id;
126
+ }
127
+ async insertNewRuleset(ruleset, model, rulesetCode, callbacks) {
128
+ const props = {
129
+ model: model.id,
130
+ code: rulesetCode,
131
+ classFullName: RulesetElements.Ruleset.classFullName,
132
+ jsonProperties: { jsonProperties: ruleset },
133
+ };
134
+ const element = await this.insertElement(props, callbacks);
135
+ this._imodel.saveChanges();
136
+ return element.id;
137
+ }
138
+ /**
139
+ * Get all rulesets embedded in the iModel.
140
+ */
141
+ async getRulesets() {
142
+ if (!this._imodel.containsClass(RulesetElements.Ruleset.classFullName))
143
+ return [];
144
+ const rulesetList = [];
145
+ this._imodel.withPreparedStatement(`SELECT ECInstanceId AS id FROM ${RulesetElements.Ruleset.classFullName}`, (statement) => {
146
+ while (core_bentley_1.DbResult.BE_SQLITE_ROW === statement.step()) {
147
+ const row = statement.getRow();
148
+ const rulesetElement = this._imodel.elements.getElement({ id: row.id });
149
+ const ruleset = rulesetElement.jsonProperties.jsonProperties;
150
+ rulesetList.push(ruleset);
151
+ }
152
+ });
153
+ return rulesetList;
154
+ }
155
+ async getOrCreateRulesetModel(callbacks) {
156
+ const rulesetModel = this.queryRulesetModel();
157
+ if (undefined !== rulesetModel)
158
+ return rulesetModel;
159
+ const rulesetSubject = await this.insertSubject(callbacks);
160
+ const definitionPartition = await this.insertDefinitionPartition(rulesetSubject, callbacks);
161
+ return this.insertDefinitionModel(definitionPartition, callbacks);
162
+ }
163
+ queryRulesetModel() {
164
+ const definitionPartition = this.queryDefinitionPartition();
165
+ if (undefined === definitionPartition)
166
+ return undefined;
167
+ return this._imodel.models.getSubModel(definitionPartition.id);
168
+ }
169
+ queryDefinitionPartition() {
170
+ const subject = this.querySubject();
171
+ if (undefined === subject)
172
+ return undefined;
173
+ return this._imodel.elements.tryGetElement(core_backend_1.DefinitionPartition.createCode(this._imodel, subject.id, this._rulesetModelName));
174
+ }
175
+ querySubject() {
176
+ const root = this._imodel.elements.getRootSubject();
177
+ const codeSpec = this._imodel.codeSpecs.getByName(core_common_1.BisCodeSpec.subject);
178
+ const code = new core_common_1.Code({
179
+ spec: codeSpec.id,
180
+ scope: root.id,
181
+ value: this._rulesetSubjectName,
182
+ });
183
+ return this._imodel.elements.tryGetElement(code);
184
+ }
185
+ async insertDefinitionModel(definitionPartition, callbacks) {
186
+ const modelProps = {
187
+ modeledElement: definitionPartition,
188
+ name: this._rulesetModelName,
189
+ classFullName: core_backend_1.DefinitionModel.classFullName,
190
+ isPrivate: true,
191
+ };
192
+ return this.insertModel(modelProps, callbacks);
193
+ }
194
+ async insertDefinitionPartition(rulesetSubject, callbacks) {
195
+ const partitionCode = core_backend_1.DefinitionPartition.createCode(this._imodel, rulesetSubject.id, this._rulesetModelName);
196
+ const definitionPartitionProps = {
197
+ parent: {
198
+ id: rulesetSubject.id,
199
+ relClassName: "BisCore:SubjectOwnsPartitionElements",
200
+ },
201
+ model: rulesetSubject.model,
202
+ code: partitionCode,
203
+ classFullName: core_backend_1.DefinitionPartition.classFullName,
204
+ };
205
+ return this.insertElement(definitionPartitionProps, callbacks);
206
+ }
207
+ async insertSubject(callbacks) {
208
+ const root = this._imodel.elements.getRootSubject();
209
+ const codeSpec = this._imodel.codeSpecs.getByName(core_common_1.BisCodeSpec.subject);
210
+ const subjectCode = new core_common_1.Code({
211
+ spec: codeSpec.id,
212
+ scope: root.id,
213
+ value: this._rulesetSubjectName,
214
+ });
215
+ const subjectProps = {
216
+ classFullName: core_backend_1.Subject.classFullName,
217
+ model: root.model,
218
+ parent: {
219
+ id: root.id,
220
+ relClassName: "BisCore:SubjectOwnsSubjects",
221
+ },
222
+ code: subjectCode,
223
+ };
224
+ return this.insertElement(subjectProps, callbacks);
225
+ }
226
+ async handleElementOperationPrerequisites() {
227
+ if (this._imodel.containsClass(RulesetElements.Ruleset.classFullName))
228
+ return;
229
+ // import PresentationRules ECSchema
230
+ await this._imodel.importSchemas([this._schemaPath]);
231
+ // insert CodeSpec for ruleset elements
232
+ this._imodel.codeSpecs.insert(core_common_1.CodeSpec.create(this._imodel, PresentationRulesDomain_1.PresentationRules.CodeSpec.Ruleset, core_common_1.CodeScopeSpec.Type.Model));
233
+ this._imodel.saveChanges();
234
+ }
235
+ async insertElement(props, callbacks) {
236
+ const element = this._imodel.elements.createElement(props);
237
+ // istanbul ignore next
238
+ await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onBeforeInsert(element));
239
+ try {
240
+ return this._imodel.elements.getElement(element.insert());
241
+ }
242
+ finally {
243
+ // istanbul ignore next
244
+ await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onAfterInsert(element));
245
+ }
246
+ }
247
+ async insertModel(props, callbacks) {
248
+ const model = this._imodel.models.createModel(props);
249
+ // istanbul ignore next
250
+ await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onBeforeInsert(model));
251
+ try {
252
+ model.id = model.insert();
253
+ return model;
254
+ }
255
+ finally {
256
+ // istanbul ignore next
257
+ await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onAfterInsert(model));
258
+ }
259
+ }
260
+ async updateElement(element, callbacks) {
261
+ // istanbul ignore next
262
+ await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onBeforeUpdate(element));
263
+ try {
264
+ element.update();
265
+ }
266
+ finally {
267
+ // istanbul ignore next
268
+ await (callbacks === null || callbacks === void 0 ? void 0 : callbacks.onAfterUpdate(element));
269
+ }
270
+ }
271
+ }
272
+ exports.RulesetEmbedder = RulesetEmbedder;
273
+ function normalizeRulesetInsertOptions(options) {
274
+ var _a, _b;
275
+ if (options === undefined)
276
+ return { skip: "same-id-and-version-eq", replaceVersions: "exact" };
277
+ return {
278
+ skip: (_a = options.skip) !== null && _a !== void 0 ? _a : "same-id-and-version-eq",
279
+ replaceVersions: (_b = options.replaceVersions) !== null && _b !== void 0 ? _b : "exact",
280
+ onEntityUpdate: options.onEntityUpdate,
281
+ onEntityInsert: options.onEntityInsert,
282
+ };
283
+ }
284
284
  //# sourceMappingURL=RulesetEmbedder.js.map