UNPKG

igniteui-webcomponents-datasources

Version:

Reference custom data providers for the Ignite UI Web Components data source.

123 lines (122 loc) 6.21 kB
import { Schema } from "./Schema"; import { XDocument } from "igniteui-webcomponents-core"; import { XElement } from "igniteui-webcomponents-core"; import { XName } from "igniteui-webcomponents-core"; import { XNamespace } from "igniteui-webcomponents-core"; import { DataSourceSchemaPropertyType } from "igniteui-webcomponents-core"; import { ODataDataSourceSchema } from "igniteui-webcomponents-core"; import { XmlNodeType } from "igniteui-webcomponents-core"; import { toArray, first } from './util'; import { fromEnum, typeCast } from "igniteui-webcomponents-core"; export let ODataSchemaProvider = /*@__PURE__*/ (() => { class ODataSchemaProvider { constructor(metadataDocument) { this._entityTypeSchemaNamespace = null; this._entitySetSchemaNamespace = null; this._schema = null; if (null == metadataDocument) { return; } let xmlDoc = XDocument.parse(metadataDocument); let schemaElements = toArray(first(fromEnum(first(fromEnum(xmlDoc.elements())).elements())).elements1(XName.get("Schema", ODataSchemaProvider.nS.namespaceName))); if (null == schemaElements) { return; } let entitySetElements = null; let entityTypeElements = null; let elementCount = schemaElements.length; let entityContainer = XName.get("EntityContainer", ODataSchemaProvider.nS.namespaceName); let entitySet = XName.get("EntitySet", ODataSchemaProvider.nS.namespaceName); let namespaceAttribute = XName.get("Namespace", ""); let entityType = XName.get("EntityType", ODataSchemaProvider.nS.namespaceName); for (let i = 0; i < elementCount; i++) { let node = schemaElements[i]; if (node.nodeType != XmlNodeType.Element) { continue; } let schemaElement = schemaElements[i]; if (null == entitySetElements) { let nodes = toArray(schemaElement.elements1(entityContainer)); if (null != nodes && nodes.length > 0) { entitySetElements = toArray((typeCast(XElement.$type, nodes[0])).elements1(entitySet)); if (null != entitySetElements) { this._entitySetSchemaNamespace = schemaElement.attribute(namespaceAttribute).value; } } } if (null == entityTypeElements) { entityTypeElements = toArray(schemaElement.elements1(entityType)); if (null != entityTypeElements) { this._entityTypeSchemaNamespace = schemaElement.attribute(namespaceAttribute).value; } } } if (null == entitySetElements || null == entityTypeElements) { return; } this.schema = new Schema(this._entityTypeSchemaNamespace, entityTypeElements, entitySetElements); } get schema() { return this._schema; } set schema(value) { this._schema = value; } getODataDataSourceSchema(entitySet) { if (this.schema == null) { return null; } let valueNames = []; let valueTypes = []; let primaryKey = []; let es = this.schema.entitySets.get(entitySet); if (null != es) { let entity = this.schema.entities.get(es.entityName); if (null != entity) { for (let property of entity.properties.values()) { valueNames.push(property.name); if (property.type == "Edm.String") { valueTypes.push(DataSourceSchemaPropertyType.StringValue); } else if (property.type == "Edm.Int16" || property.type == "Edm.Int32") { valueTypes.push(DataSourceSchemaPropertyType.IntValue); } else if (property.type == "Edm.Double") { valueTypes.push(DataSourceSchemaPropertyType.DoubleValue); } else if (property.type == "Edm.Single") { valueTypes.push(DataSourceSchemaPropertyType.SingleValue); } else if (property.type == "Edm.Boolean") { valueTypes.push(DataSourceSchemaPropertyType.BooleanValue); } else if (property.type == "Edm.Byte") { valueTypes.push(DataSourceSchemaPropertyType.ShortValue); } else if (property.type == "Edm.DateTime" || property.type == "Edm.DateTimeOffset") { valueTypes.push(DataSourceSchemaPropertyType.DateTimeValue); } else if (property.type == "Edm.Int64") { valueTypes.push(DataSourceSchemaPropertyType.LongValue); } else if (property.type == "Edm.Decimal") { valueTypes.push(DataSourceSchemaPropertyType.DecimalValue); } else if (property.type == "Edm.SByte") { valueTypes.push(DataSourceSchemaPropertyType.ShortValue); } else { valueTypes.push(DataSourceSchemaPropertyType.ObjectValue); } } for (let k of entity.primaryKey) { primaryKey.push(k); } } } return new ODataDataSourceSchema(valueNames, valueTypes, primaryKey); } } ODataSchemaProvider.nS = /*@__PURE__*/ XNamespace.get("http://docs.oasis-open.org/odata/ns/edm"); return ODataSchemaProvider; })();