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

Source: operation/js/api/PaginationRequest.js

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

    'use strict';

    /**
     * Describes a request to fetch only subset of records.
     *
     * <p>
     * First and subsequent requests to single service can be distinguished. First time caller can specify only
     * "pageSize" to indicates how many records should be returned. All other attributes are left blank.
     * </p>
     *
     * <p>
     * Subsequent request can be more specific based on what is REST service capable of and how the service responded
     * via {@link operation/js/api/PaginationCursor PaginationCursor}. For example if service returned "total" amount
     * of records then subsequest requests can specify "offset" and "pageSize" within the range.
     * </p>
     *
     * @AbcsAPI stable
     * @version 17.1.1
     * @exports operation/js/api/PaginationRequest
     *
     * @private
     * @constructor
     *
     * @param {Object} [params] - Object literal with all possible parameters.
     * @param {Integer} [params.pageSize]
     * @param {Integer} [params.offset]
     * @param {Boolean} [params.totalResults]
     * @param {PaginationCursor} [params.cursor] pagination cursor from the previous pagination request.
     *
     * @see {@link operation/js/api/PaginationCursor PaginationCursor} which holds an information coming from previous {@link operation/js/api/Operation#perform Operation.perform()} call.
     * @see {@link module:api/js/Operations.read Abcs.Operations().read(..)} to use <code>PaginationRequest</code> from Application Builder custom code.
     */
    var PaginationRequest = function(params) {
        AbcsLib.checkThis(this);

        if (params) {
            this._move = params.move;
            this._offset = params.offset ? params.offset : 0;
            this._pageSize = params.pageSize ? params.pageSize : 15;
            this._totalResults = (params.totalResults === true);
            this._paginationCursor = params.cursor;
        }
    };

    /**
     * Creates an instance of <code>PaginationRequest</code>.
     *
     * <p>
     * You can pass an instance of <code>PaginationRequest</code> into {@link module:api/js/Operations.read Abcs.Operations().read(..)} method to fetch just
     * particular page of records from Application Builder custom code.
     * </p>
     *
     * @AbcsAPI stable
     * @version 17.1.1
     *
     * @param {Object} params - Object literal with all possible parameters.
     * @param {Integer} [params.pageSize=15] - Number of records requested to be fetched.
     * @param {Integer} [params.offset=0] - Offset of the first record that should be included in the result. Offset is numbered from 0. For example when "offset"
     *                                    is set to 5 and "pageSize" to 10, it means client is requesting 10 records starting from 6th item (including).
     * @param {Integer} [params.totalResults=false] - Flag whether you want to get also total count of the available records. Use wisely even if your service
     *                                                supports this as counting total number of records is in general more costly for a backend to calculate.
     * @param {operation/js/api/PaginationCursor} [params.cursor] - {@link operation/js/api/PaginationCursor PaginationCursor} information from the previous pagination request.
     * @returns {operation/js/api/PaginationRequest}
     *
     * @example
     * <caption>
     *  Creates an <code>PaginationRequest</code> instance which fetch first 30 records.
     * </caption>
     *
     * PaginationRequest.createStandard({
     *     pageSize: 30
     * });
     *
     * @example
     * <caption>
     *  Creates an <code>PaginationRequest</code> instance which fetch second 15 records.
     * </caption>
     *
     * PaginationRequest.createStandard({
     *     offset: 15,
     *     pageSize: 15
     * });
     *
     * @example
     * <caption>
     *  Creates an <code>PaginationRequest</code> instance which fetch first 15 records and also returns total count of available records.
     * </caption>
     *
     * PaginationRequest.createStandard({
     *     pageSize: 15,
     *     totalResult: true
     * });
     *
     * @example
     * <caption>
     *  Creates an <code>PaginationRequest</code> instance which fetch first 15 records and subsequently fetches second page of records using {@link operation/js/api/PaginationCursor PaginationCursor}.
     * </caption>
     *
     * require([
     *     'operation/js/api/PaginationRequest'
     * ], function(PaginationRequest) {
     *
     *     var employee = Abcs.Entities().findById('my.custom.bop.Employee');
     *     var firstPageRequest = PaginationRequest.createStandard({
     *         pageSize: 15
     *     });
     *     var operation = Abcs.Operations().read({
     *         entity: employee,
     *         pagination: firstPageRequest
     *     });
     *
     *     operation.perform().then(function(operationResult) {
     *         if (operationResult.isSuccess()) {
     *             // Process data from the first page any way you need
     *
     *             var paginationCursor = operationResult.getPaginationCursor();
     *             var secondPageRequest = PaginationRequest.createStandard({
     *                 pageSize: 15,
     *                 cursor: paginationCursor
     *             });
     *             Abcs.Operations().read({
     *                 entity: employee,
     *                 pagination: secondPageRequest
     *             }).perform().then(function(secondResult) {
     *                 // Process data from the second page any way you need
     *             });
     *         }
     *     });
     * });
     */
    PaginationRequest.createStandard = function(params) {
        AbcsLib.checkParameterCount(arguments, 0, 1);
        AbcsLib.checkObjectLiteral(params, ['offset', 'pageSize', 'totalResults', 'cursor']);
        if (params) {
            if (params.offset) {
                AbcsLib.checkDataType(params.offset, AbcsLib.Type.NUMBER);
            }
            if (params.pageSize) {
                AbcsLib.checkDataType(params.pageSize, AbcsLib.Type.NUMBER);
            }
            if (params.totalResults) {
                AbcsLib.checkDataType(params.totalResults, AbcsLib.Type.BOOLEAN);
            }
            if (params.cursor) {
                AbcsLib.checkDataType(params.cursor, PaginationCursor);
            }
        }

        return new PaginationRequest(params);
    };

    /**
     * Gets {@link operation/js/api/PaginationCursor PaginationCursor} defined for this <code>PaginationRequest</code>.
     *
     * <p>
     * Use {@link operation/js/api/PaginationCursor PaginationCursor} if your {@link operation/js/api/Operation Operation} implementation requires information returned from
     * previous REST call, to build and perform subsequent REST call.
     * </p>
     *
     * @AbcsExtension stable
     * @version 17.1.1
     *
     * @returns {operation/js/api/PaginationCursor}
     */
    PaginationRequest.prototype.getPaginationCursor = function() {
        AbcsLib.checkParameterCount(arguments, 0);
        return this._paginationCursor;
    };

    /**
     * Gets page size (= number of records requested to be fetched) defined for this <code>PaginationRequest</code>.
     *
     * @AbcsExtension stable
     * @version 17.1.1
     *
     * @returns {Integer}
     */
    PaginationRequest.prototype.getPageSize = function() {
        AbcsLib.checkParameterCount(arguments, 0);
        return this._pageSize;
    };

    /**
     * Go to previous or next page. Positive value means next page, negative value
     * means previous page. Value itself is number of pages.
     * @returns {Integer}
     */
    PaginationRequest.prototype.getMove = function() {
        AbcsLib.checkParameterCount(arguments, 0);
        return this._move;
    };

    /**
     * Gets the offset defined for this <code>PaginationRequest</code>.
     *
     * <p>
     * Offset is numbered from 0. For example when "offset" is set to 5 and "pageSize" to 10, it means client is requesting 10 records starting from 6th item (inclusive).
     * </p>
     *
     * @AbcsExtension stable
     * @version 17.1.1
     *
     * @returns {Integer}
     */
    PaginationRequest.prototype.getOffset = function() {
        AbcsLib.checkParameterCount(arguments, 0);
        return this._offset;
    };

    /**
     * Gets the flag of whether client asks for total results available.
     *
     * <p>
     * Use wisely even if your service supports this as counting total number of records is in general more costly for a backend to calculate.
     * </p>
     *
     * @AbcsExtension stable
     * @version 17.1.1
     *
     * @returns {Boolean}
     */
    PaginationRequest.prototype.isTotalResults = function() {
        AbcsLib.checkParameterCount(arguments, 0);
        return this._totalResults;
    };

    return PaginationRequest;

});