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

Source: operation/js/api/PaginationCursor.js

define([], function() {

    'use strict';

    /**
     * Pagination response information produced by the service endpoint.
     *
     * <p>
     * Describes what data were returned, that is count of records, whether they have some offset and whether a total of records is known.
     * </p>
     *
     * <p>
     * This object is useful for both roles, Application Builder Business User and Application Builder Developer.
     * <ul>
     *  <li>
     *      Developer can produce <code>PaginationCursor</code> instance after fetching data from corresponding service endpoint. If the server returns an information
     *      about the pagination state, (s)he can create an instance of <code>PaginationCursor</code> based on the result and pass it into {@link operation/js/api/OperationResult.success OperationResult.success(..)}
     *      so that client can consume it, if interested.
     *  </li>
     *  <li>
     *      Business User doesn't directly create instances of <code>PaginationCursor</code>. Usually calls {@link operation/js/api/OperationResult.Success OperationResult.Success.getPaginationCursor()}
     *      to get one and pass it into {@link operation/js/api/PaginationRequest.createStandard PaginationRequest.createStandard(..)} so that the subsequent calls are aware of the previous pagination state.
     *  </li>
     * </p>
     *
     * @AbcsExtension stable
     * @version 17.1.1
     * @exports operation/js/api/PaginationCursor
     * @constructor
     *
     * @param {Object} params - Object literal with all possible parameters.
     * @param {Integer} params.offset - Index of the first record from the resulted data set.
     * @param {Integer} params.count - Real count of records returned by the performed {@link operation/js/api/Operation Operation}.
     * @param {Integer} params.total - Count of all records available on the server.
     *
     * @see {@link operation/js/api/PaginationRequest PaginationRequest} which can use <code>PaginationCursor</code> for subsequent service calls.
     * @see {@link operation/js/api/PaginationRequest.createStandard PaginationRequest.createStandard(..)} to pass an instance of <code>PaginationCursor</code> to create subsequent {@link operation/js/api/PaginationRequest PaginationRequest}.
     *
     * @example
     * <caption>
     *  Creates an {@link operation/js/api/PaginationRequest PaginationRequest} instance which fetch first 15 records, checks whether there are another 15 records
     *  available and if so, makes subsequent call to fetch another 15 records.
     * </caption>
     *
     * require([
     *     'operation/js/api/PaginationRequest'
     * ], function(PaginationRequest) {
     *
     *     var employee = Abcs.Entities().findById('my.custom.bop.Employee');
     *     var firstPageRequest = PaginationRequest.createStandard({
     *         pageSize: 15,
     *         totalResults: true
     *     });
     *     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
     *
     *             // Checks whether there are at least 15 further records available and
     *             // if so, makes a subsequent call to fetch the next set of 15 records
     *             var paginationCursor = operationResult.getPaginationCursor();
     *             if (paginationCursor && paginationCursor.getTotal() > 30) {
     *                 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
     *                 });
     *             }
     *         }
     *     });
     * });
     */
    var PaginationCursor = function(params) {
        AbcsLib.checkParameterCount(arguments, 0, 1);
        AbcsLib.checkObjectLiteral(params, ['offset', 'count', 'total', 'hasMore']);

        if (params) {
            if (params.offset) {
                AbcsLib.checkDataType(params.offset, AbcsLib.Type.NUMBER);
            }
            if (params.count) {
                AbcsLib.checkDataType(params.count, AbcsLib.Type.NUMBER);
            }
            if (params.total) {
                AbcsLib.checkDataType(params.total, AbcsLib.Type.NUMBER);
            }
            if (params.hasMore) {
                AbcsLib.checkDataType(params.hasMore, AbcsLib.Type.BOOLEAN);
            }

            this._offset = params.offset;
            this._count = params.count;
            this._total = params.total;
            this._hasMore = params.hasMore;
        }
    };

    /**
     * Gets the offset (= index of the first record from the resulted data set).
     *
     * @AbcsAPI stable
     * @version 17.1.1
     *
     * @returns {Integer}
     */
    PaginationCursor.prototype.getOffset = function() {
        AbcsLib.checkParameterCount(arguments, 0);
        return this._offset;
    };

    /**
     * Gets the real count of records returned by the performed {@link operation/js/api/Operation Operation}.
     *
     * <p>
     * In most cases this is equal to what client defined in {@link operation/js/api/PaginationRequest PaginationRequest}'s "pageSize" attribute. It can happen tho, that
     * the server has only 5 records available, altough the client asked for next 10 records. In which case the value here will be the real value of records returned (5 in
     * this case).
     * </p>
     *
     * @AbcsAPI stable
     * @version 17.1.1
     *
     * @returns {Integer}
     */
    PaginationCursor.prototype.getCount = function() {
        AbcsLib.checkParameterCount(arguments, 0);
        return this._count;
    };

    /**
     * Gets the count of all records available on the server.
     *
     * <p>
     * If client created {@link operation/js/api/PaginationRequest PaginationRequest} with "totalResults" attribute set to <code>true</code> to find out the total number
     * of all records, the result will be available here. If the service, don't provide the capability to get the total count of the available records, <code>undefined</code>
     * will be returned here.
     * </p>
     *
     * @AbcsAPI stable
     * @version 17.1.1
     *
     * @returns {Integer}
     */
    PaginationCursor.prototype.getTotal = function() {
        AbcsLib.checkParameterCount(arguments, 0);
        return this._total;
    };

    PaginationCursor.prototype.hasMore = function() {
        return this._hasMore;
    };

    return PaginationCursor;

});