UNPKG

@sap/xsodata

Version:

Expose data from a HANA database as OData V2 service with help of .xsodata files.

124 lines (98 loc) 3.64 kB
'use strict'; var InternalError = require('./../utils/errors/internalError'); function NavExp() { this.showAllProperties = true; //default show property, and navprop as link this.expandAllAvailExpands = true; //default expand all available navigations from $expand query this.isStar = false; //overwrite the .properties this.properties = []; // explicitly shown properties this.expandNavigations = []; // explicitly expanded navigations, means // the navigation is defined in $expand and $select this.availableExpands = {}; // available expands defined by $expand } NavExp.prototype.toPureJsonObject = function () { var ret = { showAllProperties: this.showAllProperties, expandAllAvailExpands: this.expandAllAvailExpands, isStar: this.isStar, properties: this.properties, expandNavigations: this.expandNavigations, availableExpands: {} }; for (var exp in this.availableExpands) { if (this.availableExpands.hasOwnProperty(exp)) { ret.availableExpands[exp] = this.availableExpands[exp].toPureJsonObject(); } } return ret; }; NavExp.prototype.getNav = function (name) { return this.availableExpands[name]; }; NavExp.prototype.addNav = function (name, nav) { this.availableExpands[name] = nav; return this.availableExpands[name]; }; NavExp.prototype.applySelect = function (select) { var part = select.shift(); if (part === '*') { this.isStar = true; this.expandAllAvailExpands = false; } else { this.showAllProperties = false; this.expandAllAvailExpands = false; var nav = this.availableExpands[part]; if (nav) { if (this.properties.indexOf(part) === -1) { this.properties.push(part); //also a property which is not in the availNavigation list can be a navigation property } if (this.expandNavigations.indexOf(part) === -1) { this.expandNavigations.push(part); } if (select.length > 0) { nav.applySelect(select); } } else { if (this.properties.indexOf(part) === -1) { this.properties.push(part); //also a property which is not in the availNavigation list can be a navigation property } if (select.length > 0) { throw new InternalError('part to long'); } } } }; function processSelects(selects, tree) { for (var i = 0; i < selects.length; i++) { var select = selects[i]; tree.applySelect(select); } } function createExpandTree(expandClauses) { var tree = new NavExp(); if (expandClauses) { for (var i = 0; i < expandClauses.length; i++) { var expandClause = expandClauses[i]; var root = tree; for (var ii = 0; ii < expandClause.length; ii++) { var expandItem = expandClause[ii]; var nav = root.getNav(expandItem); if (nav !== undefined) { root = nav; } else { root = root.addNav(expandItem, new NavExp()); } } } } return tree; } function processExpandSelectTree(expands, selects) { var tree = createExpandTree(expands); if (selects) { processSelects(selects, tree); } return tree; } exports.processExpandSelectTree = processExpandSelectTree;