diff --git a/process/Entity_lib/process.js b/process/Entity_lib/process.js index 2e00cc8b9f1b726bab18bca4603751b12ec94c4d..93360446bcf5ed3682da3749fd127d9c10002322 100644 --- a/process/Entity_lib/process.js +++ b/process/Entity_lib/process.js @@ -1,3 +1,4 @@ +import("system.process"); import("system.translate"); import("system.entities"); import("system.result"); @@ -371,4 +372,149 @@ EntityConsumerUtils.rmInsertedConsumerRows = function(pConsumer) { neon.deleteRecord(pConsumer, row["#UID"]); }); -}; \ No newline at end of file +}; + +/** + * Object that makes getting the current consumer rows of an entity easier. + * + * @param {Array} [pRows] The rows that should be initially loaded into the object, in most cases that would be the stored records. + */ +function EntityConsumerRowsHelper (pRows) +{ + this._uidField = "#UID"; + this._consumer = null; + this._rows = pRows || []; +} + +/** + * Static function for getting all current rows of a consumer with all changes applied (insertedRows, changedRows, deletedRows). + * + * @param {String} pConsumer the name of the consumer + * @param {String[]} pFields the entity fields that should be loaded, this only affects the stored rows that are loaded from the consumer entity + * @return {Object[]} current consumer rows + * @example + * var currentAttributes = EntityConsumerRowsHelper.getCurrentConsumerRows("Attributes", ["AB_ATTRIBUTE_ID", "VALUE"]); + */ +EntityConsumerRowsHelper.getCurrentConsumerRows = function (pConsumer, pFields) +{ + return new EntityConsumerRowsHelper() + .consumer(pConsumer) + .fetchRowsFromConsumer(pFields) + .applyConsumerRowChanges() + .getRows(); +} + +/** + * Sets the consumer that is used for loading the rows. + * + * @param {String} pConsumer the name of the consumer + * @return {EntityConsumerRowsHelper} current object + */ +EntityConsumerRowsHelper.prototype.consumer = function (pConsumer) +{ + this._consumer = pConsumer; + return this; +} + +/** + * Loads the stored rows from the consumer into the object. + * + * @param {String[]} pFields the entity fields that should be loaded + * @return {EntityConsumerRowsHelper} current object + */ +EntityConsumerRowsHelper.prototype.fetchRowsFromConsumer = function (pFields) +{ + if (!pFields) + pFields = [this._uidField]; + else if (pFields.indexOf(this._uidField) === -1) + pFields.push(this._uidField); + + var loadConfig = entities.createConfigForLoadingConsumerRows() + .consumer(this._consumer) + .fields(pFields); + this._rows = entities.getRows(loadConfig); + return this; +} + +/** + * Applies all current changes to the consumer rows (insertedRows, changedRows, deletedRows). + * + * @return {EntityConsumerRowsHelper} current object + */ +EntityConsumerRowsHelper.prototype.applyConsumerRowChanges = function () +{ + var insertedRows = vars.get("$field." + this._consumer + ".insertedRows"); + var changedRows = vars.get("$field." + this._consumer + ".changedRows"); + var deletedRows = vars.get("$field." + this._consumer + ".deletedRows"); + + if (deletedRows && deletedRows.length > 0) + this.removeRows(deletedRows); + if (changedRows && changedRows.length > 0) + this.changeRows(changedRows); + if (insertedRows && insertedRows.length > 0) + this.appendRows(insertedRows); + + return this; +} + +/** + * Adds rows to the object. + * + * @param {Object[]} pRowsToAppend the rows that should be added (insertedRows) + * @return {EntityConsumerRowsHelper} current object + */ +EntityConsumerRowsHelper.prototype.appendRows = function (pRowsToAppend) +{ + this._rows = this._rows.concat(pRowsToAppend); + return this; +} + +/** + * Updates the given rows in the object. + * + * @param {Object[]} pRowsToChange the rows that should be changed, they replace existing rows based on the uid (changedRows) + * @return {EntityConsumerRowsHelper} current object + */ +EntityConsumerRowsHelper.prototype.changeRows = function (pRowsToChange) +{ + var changeCatalog = {}; + pRowsToChange.forEach(function (changedRow) + { + changeCatalog[changedRow[this._uidField]] = changedRow; + }); + this._rows = this._rows.map(function (row) + { + return row[this._uidField] in changeCatalog ? changeCatalog[row[this._uidField]] : row; + }); + return this; +} + +/** + * Removes the given rows from the object. + * + * @param {Object[]} pRowsToRemove the rows that should be removed (deletedRows) + * @return {EntityConsumerRowsHelper} current object + */ +EntityConsumerRowsHelper.prototype.removeRows = function (pRowsToRemove) +{ + var deleteCatalog = {}; + pRowsToRemove.forEach(function (deletedRow) + { + deleteCatalog[deletedRow[this._uidField]] = true; + }); + this._rows = this._rows.filter(function (row) + { + return !deleteCatalog[row[this._uidField]]; + }); + return this; +} + +/** + * Returns all rows inside the object. + * + * @return {Object[]} all the current rows + */ +EntityConsumerRowsHelper.prototype.getRows = function () +{ + return this._rows; +}