All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.leanix.dropkit.persistence.PageRequest Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
package net.leanix.dropkit.persistence;

import org.hibernate.NullPrecedence;

import net.leanix.dropkit.BusinessLogicException;

/**
 * Object that represents data of a paginated request.
 *
 *
 */
public class PageRequest extends AbstractPageRequest {

    private int page;

    public PageRequest(int page, int size, String sort, int maxSize) throws BusinessLogicException {
        super(size, sort, maxSize);
        setPage(page);
    }

    /**
     * Creates a new PageRequest for a single page.
     * 
     * @param page
     *            Number of the requested page.
     * @param size
     *            Size of the requested page.
     * @param sort
     *            Sorting of the data set.
     * @param maxSize
     *            Max size allowed for the requested page size.
     * @param nullPrecedence
     *            Specifies how nulls are sorted.
     * @throws net.leanix.dropkit.BusinessLogicException
     */
    public PageRequest(int page, int size, String sort, int maxSize, NullPrecedence nullPrecedence)
            throws BusinessLogicException {
        this(page, size, sort, maxSize);
        this.nullPrecedence = nullPrecedence;
    }

    public PageRequest(int page, int size, String sort) throws BusinessLogicException {
        super(size, sort);
        setPage(page);
    }

    public PageRequest(int page, int size, String sort, NullPrecedence nullPrecedence) throws BusinessLogicException {
        this(page, size, sort);
        this.nullPrecedence = nullPrecedence;
    }

    public final void setPage(int page) throws BusinessLogicException {
        if (page < 0) {
            throw new BusinessLogicException("Negative page values not allowed.", 400);
        }
        this.page = page;
    }

    public int getPage() {
        return page;
    }

    /**
     * Disables paging by setting page and size to zero and maxSize to -1.
     */
    public void removePagingParams() {
        page = 0;
        size = 0;
        this.setMaxSize(-1);
    }

    @Override
    public Integer computeOffset() {
        if (page > 0) {
            return (page - 1) * size;
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy