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

net.leanix.dropkit.persistence.AbstractPageRequest 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 abstract class AbstractPageRequest {

    int size;
    private String sort;

    private int maxSize = 100;
    NullPrecedence nullPrecedence = NullPrecedence.LAST;

    public AbstractPageRequest(int size, String sort, int maxSize) throws BusinessLogicException {
        setMaxSize(maxSize);
        setSize(size);
        setSort(sort);
    }

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

    public AbstractPageRequest(int size, String sort) throws BusinessLogicException {
        setSize(size);
        setSort(sort);
    }

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

    public int getSize() {
        return size;
    }

    public final void setSize(int size) throws BusinessLogicException {
        if (maxSize > -1 && size > maxSize) {
            throw new BusinessLogicException("Size exceeds maximum size of " + this.maxSize, 400);
        }

        this.size = size;
    }

    /**
     * Returns the sorting param.
     *
     * #97768642 if sort is not empty, secondary sorting by id is added, since otherwise paginated requests might contain results twice
     * (items with equal matches to the sort might be ordered randomly).
     *
     * @return
     */
    public String getSort() {
        String safe = sort;
        if (hasSort() && !sort.contains("id-")) {
            safe += ",id-asc";
        }

        return safe;
    }

    public final void setSort(String sort) {
        this.sort = sort;
    }

    public boolean hasSort() {
        return this.sort != null && !this.sort.isEmpty();
    }

    public final void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    /**
     * Precedence of null values for the ORDER BY clause.
     * 
     * @return
     */
    public NullPrecedence getNullPrecedence() {
        return this.nullPrecedence;
    }

    /**
     * Provides the zero based offset which is directly used in criteria (sql query)
     * 
     * @return
     */
    public abstract Integer computeOffset();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy