@itwin/ecschema-metadata
Version:
ECObjects core concepts in typescript
232 lines • 14 kB
JavaScript
"use strict";
/*---------------------------------------------------------------------------------------------
* 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.MutableKindOfQuantity = exports.KindOfQuantity = void 0;
const DelayedPromise_1 = require("../DelayedPromise");
const XmlSerializationUtils_1 = require("../Deserialization/XmlSerializationUtils");
const ECObjects_1 = require("../ECObjects");
const Exception_1 = require("../Exception");
const Format_1 = require("./Format");
const InvertedUnit_1 = require("./InvertedUnit");
const OverrideFormat_1 = require("./OverrideFormat");
const SchemaItem_1 = require("./SchemaItem");
const Unit_1 = require("./Unit");
/** A Typescript class representation of a KindOfQuantity.
* @public @preview
*/
class KindOfQuantity extends SchemaItem_1.SchemaItem {
schemaItemType = KindOfQuantity.schemaItemType;
/** @internal */
static get schemaItemType() { return ECObjects_1.SchemaItemType.KindOfQuantity; }
_relativeError = 1.0;
_presentationFormats = [];
_persistenceUnit;
/** The first presentation format in the list of Formats. */
get defaultPresentationFormat() { return this.presentationFormats[0]; }
/** A list of presentation formats. */
get presentationFormats() { return this._presentationFormats; }
/** Persistence unit */
get persistenceUnit() { return this._persistenceUnit; }
get relativeError() { return this._relativeError; }
/**
*
* @param format The Format to add to this KindOfQuantity
* @param isDefault
* @internal
*/
addPresentationFormat(format, isDefault = false) {
// TODO: Add some sort of validation?
// eslint-disable-next-line @typescript-eslint/no-floating-promises
(isDefault) ? this._presentationFormats.splice(0, 0, format) : this._presentationFormats.push(format);
}
/** Creates an OverrideFormat in the context of this KindOfQuantity.
* @param parent The Format to override.
* @param precision The precision override
* @param unitLabelOverrides The list of unit and label overrides.
* @internal
*/
createFormatOverride(parent, precision, unitLabelOverrides) {
if (unitLabelOverrides && parent.units && parent.units.length !== unitLabelOverrides.length)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Cannot add presentation format to KindOfQuantity '${this.name}' because the number of unit overrides is inconsistent with the number in the Format '${parent.name}'.`);
if (parent.units && 0 === parent.units.length && unitLabelOverrides && 0 < unitLabelOverrides.length)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Cannot add a presentation format to KindOfQuantity '${this.name}' without any units and no unit overrides.`);
// TODO: Check compatibility of Unit overrides with the persistence unit
return new OverrideFormat_1.OverrideFormat(parent, precision, unitLabelOverrides);
}
async processPresentationUnits(presentationUnitsJson) {
const presUnitsArr = Array.isArray(presentationUnitsJson) ? presentationUnitsJson : presentationUnitsJson.split(";");
for (const formatString of presUnitsArr) {
const presFormatOverride = OverrideFormat_1.OverrideFormat.parseFormatString(formatString);
const format = await this.schema.lookupItem(presFormatOverride.name, Format_1.Format);
if (undefined === format || format.schemaItemType !== ECObjects_1.SchemaItemType.Format)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate Format '${presFormatOverride.name}' for the presentation unit on KindOfQuantity ${this.fullName}.`);
if (undefined === presFormatOverride.precision && undefined === presFormatOverride.unitAndLabels) {
this.addPresentationFormat(new DelayedPromise_1.DelayedPromiseWithProps(format.key, async () => format));
continue;
}
let unitAndLabels;
if (undefined !== presFormatOverride.unitAndLabels) {
if (4 < presFormatOverride.unitAndLabels.length)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, ``);
unitAndLabels = [];
for (const unitOverride of presFormatOverride.unitAndLabels) {
const unitOrInverted = await this.schema.lookupItem(unitOverride[0]);
if (Unit_1.Unit.isUnit(unitOrInverted))
unitAndLabels.push([new DelayedPromise_1.DelayedPromiseWithProps(unitOrInverted.key, async () => unitOrInverted), unitOverride[1]]);
else if (InvertedUnit_1.InvertedUnit.isInvertedUnit(unitOrInverted))
unitAndLabels.push([new DelayedPromise_1.DelayedPromiseWithProps(unitOrInverted.key, async () => unitOrInverted), unitOverride[1]]);
else
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate SchemaItem ${unitOverride[0]}.`);
}
}
const overrideFormat = this.createFormatOverride(format, presFormatOverride.precision, unitAndLabels);
this.addPresentationFormat(overrideFormat);
}
}
processPresentationUnitsSync(presentationUnitsJson) {
const presUnitsArr = Array.isArray(presentationUnitsJson) ? presentationUnitsJson : presentationUnitsJson.split(";");
for (const formatString of presUnitsArr) {
const presFormatOverride = OverrideFormat_1.OverrideFormat.parseFormatString(formatString);
const format = this.schema.lookupItemSync(presFormatOverride.name, Format_1.Format);
if (undefined === format || format.schemaItemType !== ECObjects_1.SchemaItemType.Format)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate Format '${presFormatOverride.name}' for the presentation unit on KindOfQuantity ${this.fullName}.`);
if (undefined === presFormatOverride.precision && undefined === presFormatOverride.unitAndLabels) {
this.addPresentationFormat(new DelayedPromise_1.DelayedPromiseWithProps(format.key, async () => format));
continue;
}
let unitAndLabels;
if (undefined !== presFormatOverride.unitAndLabels) {
if (4 < presFormatOverride.unitAndLabels.length)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, ``);
unitAndLabels = [];
for (const unitOverride of presFormatOverride.unitAndLabels) {
const unitOrInverted = this.schema.lookupItemSync(unitOverride[0]);
if (Unit_1.Unit.isUnit(unitOrInverted))
unitAndLabels.push([new DelayedPromise_1.DelayedPromiseWithProps(unitOrInverted.key, async () => unitOrInverted), unitOverride[1]]);
else if (InvertedUnit_1.InvertedUnit.isInvertedUnit(unitOrInverted))
unitAndLabels.push([new DelayedPromise_1.DelayedPromiseWithProps(unitOrInverted.key, async () => unitOrInverted), unitOverride[1]]);
else
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `Unable to locate SchemaItem ${unitOverride[0]}.`);
}
}
const overrideFormat = this.createFormatOverride(format, presFormatOverride.precision, unitAndLabels);
this.addPresentationFormat(overrideFormat);
}
}
/**
* Save this KindOfQuantity'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);
schemaJson.relativeError = this.relativeError;
schemaJson.persistenceUnit = this.persistenceUnit.fullName;
if (undefined !== this.presentationFormats && 0 < this.presentationFormats.length)
schemaJson.presentationUnits = this.presentationFormats.map((format) => format.fullName);
return schemaJson;
}
/** @internal */
async toXml(schemaXml) {
const itemElement = await super.toXml(schemaXml);
const persistenceUnit = await this.persistenceUnit;
if (undefined !== persistenceUnit) {
const unitName = XmlSerializationUtils_1.XmlSerializationUtils.createXmlTypedName(this.schema, persistenceUnit.schema, persistenceUnit.name);
itemElement.setAttribute("persistenceUnit", unitName);
}
if (undefined !== this.presentationFormats) {
const presUnitStrings = [];
for (const format of this.presentationFormats) {
if (!OverrideFormat_1.OverrideFormat.isOverrideFormat(format)) {
const resolvedFormat = await format;
presUnitStrings.push(XmlSerializationUtils_1.XmlSerializationUtils.createXmlTypedName(this.schema, resolvedFormat.schema, format.name));
continue;
}
presUnitStrings.push(format.fullNameXml(this.schema));
}
;
itemElement.setAttribute("presentationUnits", presUnitStrings.join(";"));
}
itemElement.setAttribute("relativeError", this.relativeError.toString());
return itemElement;
}
fromJSONSync(kindOfQuantityProps) {
super.fromJSONSync(kindOfQuantityProps);
this._relativeError = kindOfQuantityProps.relativeError;
const persistenceUnit = this.schema.lookupItemSync(kindOfQuantityProps.persistenceUnit);
if (undefined === persistenceUnit)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `The Unit ${kindOfQuantityProps.persistenceUnit} does not exist.`);
if (!Unit_1.Unit.isUnit(persistenceUnit) && !InvertedUnit_1.InvertedUnit.isInvertedUnit(persistenceUnit))
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `The item ${kindOfQuantityProps.persistenceUnit} is not a Unit or InvertedUnit.`);
if (Unit_1.Unit.isUnit(persistenceUnit))
this._persistenceUnit = new DelayedPromise_1.DelayedPromiseWithProps(persistenceUnit.key, async () => persistenceUnit);
else
this._persistenceUnit = new DelayedPromise_1.DelayedPromiseWithProps(persistenceUnit.key, async () => persistenceUnit);
if (undefined !== kindOfQuantityProps.presentationUnits)
this.processPresentationUnitsSync(kindOfQuantityProps.presentationUnits);
}
async fromJSON(kindOfQuantityProps) {
await super.fromJSON(kindOfQuantityProps);
this._relativeError = kindOfQuantityProps.relativeError;
const persistenceUnit = await this.schema.lookupItem(kindOfQuantityProps.persistenceUnit);
if (undefined === persistenceUnit)
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `The Unit ${kindOfQuantityProps.persistenceUnit} does not exist.`);
if (!Unit_1.Unit.isUnit(persistenceUnit) && !InvertedUnit_1.InvertedUnit.isInvertedUnit(persistenceUnit))
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidECJson, `The item ${kindOfQuantityProps.persistenceUnit} is not a Unit or InvertedUnit.`);
if (Unit_1.Unit.isUnit(persistenceUnit))
this._persistenceUnit = new DelayedPromise_1.DelayedPromiseWithProps(persistenceUnit.key, async () => persistenceUnit);
else
this._persistenceUnit = new DelayedPromise_1.DelayedPromiseWithProps(persistenceUnit.key, async () => persistenceUnit);
if (undefined !== kindOfQuantityProps.presentationUnits)
await this.processPresentationUnits(kindOfQuantityProps.presentationUnits);
}
/**
* Used for schema editing.
* @internal
*/
setRelativeError(relativeError) {
this._relativeError = relativeError;
}
/**
* Used for schema editing.
* @internal
*/
setPersistenceUnit(value) {
this._persistenceUnit = value;
}
/**
* Type guard to check if the SchemaItem is of type KindOfQuantity.
* @param item The SchemaItem to check.
* @returns True if the item is a KindOfQuantity, false otherwise.
*/
static isKindOfQuantity(item) {
if (item && item.schemaItemType === ECObjects_1.SchemaItemType.KindOfQuantity)
return true;
return false;
}
/**
* Type assertion to check if the SchemaItem is of type KindOfQuantity.
* @param item The SchemaItem to check.
* @returns The item cast to KindOfQuantity if it is a KindOfQuantity, undefined otherwise.
* @internal
*/
static assertIsKindOfQuantity(item) {
if (!this.isKindOfQuantity(item))
throw new Exception_1.ECSchemaError(Exception_1.ECSchemaStatus.InvalidSchemaItemType, `Expected '${ECObjects_1.SchemaItemType.KindOfQuantity}' (KindOfQuantity)`);
}
}
exports.KindOfQuantity = KindOfQuantity;
/**
* @internal
* An abstract class used for schema editing.
*/
class MutableKindOfQuantity extends KindOfQuantity {
}
exports.MutableKindOfQuantity = MutableKindOfQuantity;
//# sourceMappingURL=KindOfQuantity.js.map