@itwin/ecschema-metadata
Version:
ECObjects core concepts in typescript
148 lines • 6.3 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Metadata
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MutableMixin = exports.Mixin = void 0;
const DelayedPromise_1 = require("../DelayedPromise");
const XmlSerializationUtils_1 = require("../Deserialization/XmlSerializationUtils");
const ECObjects_1 = require("../ECObjects");
const Exception_1 = require("../Exception");
const Class_1 = require("./Class");
const EntityClass_1 = require("./EntityClass");
/**
* A Typescript class representation of a Mixin.
* @public @preview
*/
class Mixin extends Class_1.ECClass {
schemaItemType = Mixin.schemaItemType;
/** @internal */
static get schemaItemType() { return ECObjects_1.SchemaItemType.Mixin; }
_appliesTo;
get appliesTo() {
return this._appliesTo;
}
/** @internal */
constructor(schema, name) {
super(schema, name, ECObjects_1.ECClassModifier.Abstract);
}
/**
*
* @param name
* @param relationship
* @param direction
* @internal
*/
async createNavigationProperty(name, relationship, direction) {
return this.addProperty(await (0, EntityClass_1.createNavigationProperty)(this, name, relationship, direction));
}
/**
*
* @param name
* @param relationship
* @param direction
* @returns
*
* @internal
*/
createNavigationPropertySync(name, relationship, direction) {
return this.addProperty((0, EntityClass_1.createNavigationPropertySync)(this, name, relationship, direction));
}
/**
* @internal
*/
setAppliesTo(appliesTo) {
this._appliesTo = appliesTo;
}
/**
* Save this Mixin's properties to an object for serializing to JSON.
* @param standalone Serialization includes only this object (as opposed to the full schema).
* @param includeSchemaVersion Include the Schema's version information in the serialized object.
*/
toJSON(standalone = false, includeSchemaVersion = false) {
const schemaJson = super.toJSON(standalone, includeSchemaVersion);
if (undefined !== this.appliesTo) {
schemaJson.appliesTo = this.appliesTo.fullName;
}
return schemaJson;
}
/** @internal */
async toXml(schemaXml) {
const itemElement = await super.toXml(schemaXml);
// When CustomAttributes are added, there must be a check to see if the ECCustomAttributes
// already exist for this item before creating a new one to apply IsMixin
const customAttributes = schemaXml.createElement("ECCustomAttributes");
const isMixinElement = schemaXml.createElement("IsMixin");
const coreCustomSchema = this.schema.getReferenceSync("CoreCustomAttributes");
if (undefined !== coreCustomSchema) {
const xmlns = `CoreCustomAttributes.${coreCustomSchema.schemaKey.version.toString()}`;
isMixinElement.setAttribute("xmlns", xmlns);
}
const appliesToElement = schemaXml.createElement("AppliesToEntityClass");
const appliesTo = await this.appliesTo;
if (undefined !== appliesTo) {
const appliesToName = XmlSerializationUtils_1.XmlSerializationUtils.createXmlTypedName(this.schema, appliesTo.schema, appliesTo.name);
appliesToElement.textContent = appliesToName;
isMixinElement.appendChild(appliesToElement);
}
customAttributes.appendChild(isMixinElement);
itemElement.appendChild(customAttributes);
return itemElement;
}
fromJSONSync(mixinProps) {
super.fromJSONSync(mixinProps);
const entityClassSchemaItemKey = this.schema.getSchemaItemKey(mixinProps.appliesTo);
if (!entityClassSchemaItemKey)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the appliesTo ${mixinProps.appliesTo}.`);
this._appliesTo = new DelayedPromise_1.DelayedPromiseWithProps(entityClassSchemaItemKey, async () => {
const appliesTo = await this.schema.lookupItem(entityClassSchemaItemKey, EntityClass_1.EntityClass);
if (undefined === appliesTo)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate the appliesTo ${mixinProps.appliesTo}.`);
return appliesTo;
});
}
async fromJSON(mixinProps) {
this.fromJSONSync(mixinProps);
}
async applicableTo(entityClass) {
if (!this.appliesTo)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidType, `appliesTo is undefined in the class ${this.fullName}.`);
const appliesTo = await this.appliesTo;
if (appliesTo === undefined)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidType, `Unable to locate the appliesTo ${this.appliesTo.fullName}.`);
return appliesTo.is(entityClass);
}
/**
* Type guard to check if the SchemaItem is of type Mixin.
* @param item The SchemaItem to check.
* @returns True if the item is a Mixin, false otherwise.
*/
static isMixin(item) {
if (item && item.schemaItemType === ECObjects_1.SchemaItemType.Mixin)
return true;
return false;
}
/**
* Type assertion to check if the SchemaItem is of type Mixin.
* @param item The SchemaItem to check.
* @returns The item cast to Mixin if it is a Mixin, undefined otherwise.
* @internal
*/
static assertIsMixin(item) {
if (!this.isMixin(item))
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidSchemaItemType, `Expected '${ECObjects_1.SchemaItemType.Mixin}' (Mixin)`);
}
}
exports.Mixin = Mixin;
/**
* @internal
* An abstract class used for schema editing.
*/
class MutableMixin extends Mixin {
}
exports.MutableMixin = MutableMixin;
//# sourceMappingURL=Mixin.js.map