JavaScript Application Development API for Oracle Visual Builder Cloud Service - Classic Applications

Source: operation/js/api/Value.js

/* eslint-disable no-use-before-define */

define([
    'operation/js/FailureResultRenderer',
    'operation/js/api/OperationResult'
], function (
        FailureResultRenderer,
        OperationResult
    ) {

    'use strict';

    /**
     * API object representing single <code>Value</code> coming as part of the data record.
     *
     * @AbcsAPI stable
     * @version 17.1.3
     * @exports operation/js/api/Value
     *
     * @constructor
     * @private
     *
     * @see {@link module:api/js/Operations.read Operations.read(..)} to see where are instances of <code>Value</code> coming from.
     */
    var Value = function () {
       AbcsLib.throwStaticClassError();
    };

    Value.reference = function (params) {
        AbcsLib.checkDefined(params, 'params');
        AbcsLib.checkObjectLiteral(params, ['id', 'displayName', 'record']);
        return new ReferenceValue(params);
    };

    Value.brokenReference = function (params) {
        AbcsLib.checkDefined(params, 'params');
        AbcsLib.checkDefined(params.renderer, 'params.renderer');
        AbcsLib.checkObjectLiteral(params, ['id', 'renderer']);
        return new BrokenReferenceValue(params);
    };

    Value.failureResult = function (params) {
        AbcsLib.checkDefined(params, 'params');
        AbcsLib.checkObjectLiteral(params, ['entityName', 'operationResult'], ['entityName', 'operationResult']);
        AbcsLib.checkDataType(params.operationResult, OperationResult);

        return new FailureResultValue(params);
    };

    Value.incompleteReference = function (params) {
        AbcsLib.checkDefined(params, 'params');
        AbcsLib.checkObjectLiteral(params, ['id', 'fromNewRecord']);
        return new IncompleteReferenceValue(params);
    };

    Value.attachment = function (params) {
        AbcsLib.checkDefined(params, 'params');
        AbcsLib.checkDefined(params.binaryContent, 'params.binaryContent');
        AbcsLib.checkObjectLiteral(params, ['binaryContent',
            'showInCollections', 'fileInfo', 'empty', 'toString']);
        if (params.fileInfo) {
            AbcsLib.checkObjectLiteral(params.fileInfo, ['name', 'size', 'type',
                'lastModifiedDate']);
        }
        return new AttachmentValue(params);
    };

    /**
     * Checks whether the given Object is <code>ReferenceValue</code> or not.
     *
     * @AbcsAPI stable
     * @version 17.1.3
     *
     * @public
     * @static
     *
     * @param {Object} obj - Instance to check.
     * @returns {Boolean} - <code>true</code> if the given Object is a reference, <code>false</code> otherwise.
     */
    Value.isReference = function (obj) {
        AbcsLib.checkParameterCount(arguments, 1);

        return obj instanceof ReferenceValue;
    };

    /**
     * Checks whether the given Object is <code>BrokenReferenceValue</code> or not.
     *
     * @AbcsAPI stable
     * @version 17.1.3
     *
     * @public
     * @static
     *
     * @param {Object} obj - Instance to check.
     * @returns {Boolean} - <code>true</code> if the given Object is a broken reference, <code>false</code> otherwise.
     */
    Value.isBrokenReference = function (obj) {
        AbcsLib.checkParameterCount(arguments, 1);

        return obj instanceof BrokenReferenceValue;
    };

    Value.isIncompleteReference = function (obj) {
        return obj instanceof IncompleteReferenceValue;
    };

    Value.isFailureResult = function (obj) {
        return obj instanceof FailureResultValue;
    };

    Value.isAttachment = function (obj) {
        return obj instanceof AttachmentValue;
    };

    /**
     * Representation of properly loaded reference value.
     *
     * @AbcsAPI stable
     * @version 17.1.3
     * @memberof operation/js/api/Value
     * @augments operation/js/api/Value
     *
     * @constructor
     * @private
     *
     * @param {object} params Object with actual data.
     */
    var ReferenceValue = function (params) {
        this.id = params.id;
        this.displayName = params.displayName;
        this.record = params.record;
    };
    AbcsLib.extend(ReferenceValue, Value);

    /**
     * Gets ID of this <code>ReferenceValue</code>.
     *
     * @AbcsAPI stable
     * @version 17.1.3
     *
     * @returns {String}
     */
    ReferenceValue.prototype.getId = function () {
        AbcsLib.checkParameterCount(arguments, 0);
        return this.id;
    };

    /**
     * Gets display name of this <code>ReferenceValue</code>.
     *
     * @AbcsAPI stable
     * @version 17.1.3
     *
     * @returns {String}
     */
    ReferenceValue.prototype.getDisplayName = function () {
        AbcsLib.checkParameterCount(arguments, 0);
        return this.displayName;
    };

    /**
     * Gets whole record of this <code>ReferenceValue</code>.
     *
     * @AbcsAPI stable
     * @version 17.1.3
     *
     * @returns {module:api/js/Operations~Record}
     */
    ReferenceValue.prototype.getRecord = function () {
        AbcsLib.checkParameterCount(arguments, 0);
        return this.record;
    };

    ReferenceValue.prototype.clone = function () {
        return new ReferenceValue(this._getDefinition());
    };

    ReferenceValue.prototype._getDefinition = function () {
        return {
            id: AbcsLib.clone(this.id),
            displayName: AbcsLib.clone(this.displayName),
            record: AbcsLib.clone(this.record)
        };
    };

    ReferenceValue.prototype.hasSpecialRenderer = function () {
        return false;
    };

    /**
     * Internal representation of a broken reference value.
     *
     * <p>
     * Broken reference usually means that the refering record is not available anymore. This can't happen in case of internal BOs where Application Builder is taking care of the data integrity.
     * But in case you have a reference between two different and unrelated data sources, this can easily happen and in such case an instance of <code>BrokenReferenceValue</code> is representing
     * such state.
     * </p>
     *
     * @AbcsAPI stable
     * @version 17.1.3
     * @memberof operation/js/api/Value
     * @augments operation/js/api/Value
     *
     * @constructor
     * @private
     *
     * @param {Object} params - Object with actual data.
     */
    var BrokenReferenceValue = function (params) {
        var displayName;
        if (params.renderer && AbcsLib.isFunction(params.renderer.getChildName)) {
            var childName = params.renderer.getChildName();
            displayName = AbcsLib.i18n('components.brokenReferenceLabel', {
                targetEntityName: childName,
                key: params.id
            });
        }
        ReferenceValue.call(this, $.extend(params, {
            displayName: displayName || params.id
        }));
        this.renderer = params.renderer;
        this.id = params.id;
    };
    AbcsLib.extend(BrokenReferenceValue, ReferenceValue);

    /**
     * Gets the ID of the original refered record that is broken now.
     *
     * @AbcsAPI stable
     * @version 17.1.3
     *
     * @returns {String}
     */
    BrokenReferenceValue.prototype.getId = function () {
        AbcsLib.checkParameterCount(arguments, 0);
        return this.id;
    };

    BrokenReferenceValue.prototype.clone = function () {
        return new BrokenReferenceValue(this._getDefinition());
    };

    BrokenReferenceValue.prototype._getDefinition = function () {
        return $.extend(ReferenceValue.prototype._getDefinition.call(this), {
            renderer: AbcsLib.clone(this.renderer)
        });
    };

    BrokenReferenceValue.prototype.hasSpecialRenderer = function () {
        return !!this.renderer;
    };

    /**
     * Internal representation of failure result value coming from the server.
     *
     * <p>
     * Failure result can have several different reasons:
     *  <ul>
     *      <li>
     *          Client has not sufficient privileges to get the value. (BUFP-6535)
     *      </li>
     *      <li>
     *          Referenced BO don't exist anymore or data model is corrupted. (BUFP-9786)
     *      </li>
     *  </ul>
     * </p>
     *
     * @param {object} params Object with actual data.
     *
     * @constructor
     * @private
     */
    var FailureResultValue = function (params) {
        this.renderer = new FailureResultRenderer(params);
    };

    FailureResultValue.prototype.clone = function () {
        return new FailureResultValue(this._getDefinition());
    };

    FailureResultValue.prototype._getDefinition = function () {
        return AbcsLib.clone(this.renderer.getDefinition());
    };

    FailureResultValue.prototype.hasSpecialRenderer = function () {
        return !!this.renderer;
    };

    /**
     * Internal representation of incomplete reference values.
     *
     * @param {object} params Object with actual data.
     *
     * @constructor
     * @private
     */
    var IncompleteReferenceValue = function (params) {
        ReferenceValue.call(this, $.extend(params, {
            displayName: params.id
        }));
        this.fromNewRecord = params.fromNewRecord;
    };
    AbcsLib.extend(IncompleteReferenceValue, ReferenceValue);

    IncompleteReferenceValue.prototype.clone = function () {
        return new IncompleteReferenceValue(this._getDefinition());
    };

    IncompleteReferenceValue.prototype._getDefinition = function () {
        return $.extend(ReferenceValue.prototype._getDefinition.call(this), {
            fromNewRecord: this.fromNewRecord
        });
    };

    IncompleteReferenceValue.prototype.isFromNewRecord = function () {
        return this.fromNewRecord;
    };

    /**
     * Internal representation of attachment values.
     *
     * @param {object} params Object with actual data.
     *
     * @constructor
     * @private
     */
    var AttachmentValue = function (params) {
        this.binaryContent = params.binaryContent;
        this.showInCollections = params.showInCollections;
        this.fileInfo = params.fileInfo;
        this.empty = params.empty;
    };

    AttachmentValue.prototype.toString = function () {
        return 'An attachment';
    };

    AttachmentValue.prototype.clone = function () {
        return new AttachmentValue(this);
    };

    return Value;
});