igniteui-webcomponents-datasources
Version:
Reference custom data providers for the Ignite UI Web Components data source.
160 lines (159 loc) • 7.33 kB
JavaScript
import { __values } from "tslib";
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";
var ODataSchemaProvider = /** @class */ /*@__PURE__*/ (function () {
function ODataSchemaProvider(metadataDocument) {
this._entityTypeSchemaNamespace = null;
this._entitySetSchemaNamespace = null;
this._schema = null;
if (null == metadataDocument) {
return;
}
var xmlDoc = XDocument.parse(metadataDocument);
var schemaElements = toArray(first(fromEnum(first(fromEnum(xmlDoc.elements())).elements())).elements1(XName.get("Schema", ODataSchemaProvider.nS.namespaceName)));
if (null == schemaElements) {
return;
}
var entitySetElements = null;
var entityTypeElements = null;
var elementCount = schemaElements.length;
var entityContainer = XName.get("EntityContainer", ODataSchemaProvider.nS.namespaceName);
var entitySet = XName.get("EntitySet", ODataSchemaProvider.nS.namespaceName);
var namespaceAttribute = XName.get("Namespace", "");
var entityType = XName.get("EntityType", ODataSchemaProvider.nS.namespaceName);
for (var i = 0; i < elementCount; i++) {
var node = schemaElements[i];
if (node.nodeType != XmlNodeType.Element) {
continue;
}
var schemaElement = schemaElements[i];
if (null == entitySetElements) {
var 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);
}
Object.defineProperty(ODataSchemaProvider.prototype, "schema", {
get: function () {
return this._schema;
},
set: function (value) {
this._schema = value;
},
enumerable: false,
configurable: true
});
ODataSchemaProvider.prototype.getODataDataSourceSchema = function (entitySet) {
var e_1, _a, e_2, _b;
if (this.schema == null) {
return null;
}
var valueNames = [];
var valueTypes = [];
var primaryKey = [];
var es = this.schema.entitySets.get(entitySet);
if (null != es) {
var entity = this.schema.entities.get(es.entityName);
if (null != entity) {
try {
for (var _c = __values(entity.properties.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
var property = _d.value;
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);
}
}
}
catch (e_1_1) {
e_1 = { error: e_1_1 };
}
finally {
try {
if (_d && !_d.done && (_a = _c.return))
_a.call(_c);
}
finally {
if (e_1)
throw e_1.error;
}
}
try {
for (var _e = __values(entity.primaryKey), _f = _e.next(); !_f.done; _f = _e.next()) {
var k = _f.value;
primaryKey.push(k);
}
}
catch (e_2_1) {
e_2 = { error: e_2_1 };
}
finally {
try {
if (_f && !_f.done && (_b = _e.return))
_b.call(_e);
}
finally {
if (e_2)
throw e_2.error;
}
}
}
}
return new ODataDataSourceSchema(valueNames, valueTypes, primaryKey);
};
ODataSchemaProvider.nS = XNamespace.get("http://docs.oasis-open.org/odata/ns/edm");
return ODataSchemaProvider;
}());
export { ODataSchemaProvider };