UNPKG

@sap/odata-v4

Version:

OData V4.0 server library

138 lines (113 loc) 4.11 kB
'use strict'; const AbstractEdmStructuredType = require('./AbstractEdmStructuredType'); const EdmTypeKind = require('./EdmType').TypeKind; const EdmKeyPropertyRef = require('./EdmKeyPropertyRef'); const FeatureSupport = require('../FeatureSupport'); /** * <a href="./../ODataSpecification/odata-v4.0-errata03-os/complete/part3-csdl/odata-v4.0-errata03-os-part3-csdl-complete.html#_Toc453752548"> * OData CSDL # 8. Element edm:EntityType * </a> * * @extends AbstractEdmStructuredType * @hideconstructor */ class EdmEntityType extends AbstractEdmStructuredType { /** * Creates an instance of EdmEntityType. * * @param {Edm} edm The edm itself * @param {FullQualifiedName} fqn The full qualified name of this entity type * @param {CsdlEntityType} csdlEntityType The csdl entity type structure * @param {Object} configuration Configuration object with additional configuration properties * @param {string[]} configuration.customAggregates the custom aggregates defined for this type */ constructor(edm, fqn, csdlEntityType, configuration) { super(edm, fqn, EdmTypeKind.ENTITY, csdlEntityType, configuration); if (csdlEntityType.hasStream) { FeatureSupport.failUnsupported(FeatureSupport.features.AttributeHasStream); } /** * @type {EdmEntityType} * @private */ this._edmBaseType = null; /** * @type {Map.<string, EdmKeyPropertyRef>} * @private */ this._ownKeyPropertyRefs = null; } /** * Returns the base type. Null if no base type exists. * @override * @returns {?EdmEntityType} */ getBaseType() { if (this._edmBaseType) { return this._edmBaseType; } if (!this.csdlStructuredType.baseType) { return null; } this._edmBaseType = this._edm.getEntityType(this.csdlStructuredType.baseType); return this._edmBaseType; } /** * Returns a Map of key properties. These properties define the key in this edm type excluding * those from base type. * * @returns {Map.<string, EdmProperty>} The Map of keys */ getOwnKeyPropertyRefs() { if (this._ownKeyPropertyRefs) { return this._ownKeyPropertyRefs; } this._ownKeyPropertyRefs = new Map(); let keyPropertyRefs = this.csdlStructuredType.key; keyPropertyRefs.forEach((property) => { // property --> CsdlPropertyRef let edmProp = new EdmKeyPropertyRef(this, property); const name = property.alias || property.name; this._ownKeyPropertyRefs.set(name, edmProp); }); return this._ownKeyPropertyRefs; } /** * Returns a Map of key properties. An entity is uniquely identified within an entity set by * its key consisting of its key property ref objects. * * @returns {Map.<string, EdmKeyPropertyRef>} The Map of keys */ getKeyPropertyRefs() { if (this.getOwnKeyPropertyRefs().size > 0) { return this.getOwnKeyPropertyRefs(); } return this.getBaseType() ? this.getBaseType().getKeyPropertyRefs() : new Map(); } /** * Returns the key property ref found by its name. * * @param {string} name Name of the requested key property * @returns {EdmKeyPropertyRef} The key property ref */ getKeyPropertyRef(name) { return this.getKeyPropertyRefs().get(name); } /** * Returns true if this entity type has a stream. A value of true specifies that the entity * type is a media entity. Media entities are entities that represent a media stream, * such as a photo. * * @returns {boolean} */ hasStream() { if (this.csdlStructuredType.hasStream) { return this.csdlStructuredType.hasStream; } if (this.getBaseType()) { return this.getBaseType().hasStream(); } return false; } } module.exports = EdmEntityType;