ScrollPagination.java

/*
 * Copyright 2019 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.genesys.server.api;

import java.util.Arrays;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

/**
 * Data pagination request.
 *
 * @author Matija Obreza
 */
public class ScrollPagination {

	/** The default sort properties. */
	private final String[] DEFAULT_SORT_PROPERTIES = { "id" };

	/** Offset (0-based). */
	private Long o;

	/** Page size (length). */
	private Integer l;

	/** Sort direction. */
	private Sort.Direction d;

	/** Sort properties. */
	private String[] s;

	/**
	 * Gets the o.
	 *
	 * @return the o
	 */
	public Long getO() {
		return o;
	}

	/**
	 * Sets the o.
	 *
	 * @param o the new o
	 */
	public void setO(Long o) {
		this.o = o;
	}

	/**
	 * Gets the l.
	 *
	 * @return the l
	 */
	public int getL() {
		return l;
	}

	/**
	 * Sets the l.
	 *
	 * @param l the new l
	 */
	public void setL(int l) {
		this.l = l;
	}

	/**
	 * Gets the d.
	 *
	 * @return the d
	 */
	public Sort.Direction getD() {
		return d;
	}

	/**
	 * Sets the d.
	 *
	 * @param d the new d
	 */
	public void setD(Sort.Direction d) {
		this.d = d;
	}

	/**
	 * Gets the s.
	 *
	 * @return the s
	 */
	public String[] getS() {
		return s;
	}

	/**
	 * Sets the s.
	 *
	 * @param s the new s
	 */
	public void setS(String[] s) {
		this.s = s;
	}

	/**
	 * To string.
	 *
	 * @return the string
	 */
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Pagination offset=" + o + ", pageSize=" + l + ", dir=" + d + ", sort=" + Arrays.toString(s);
	}

	/**
	 * Get sort direction or {@link Sort.Direction#ASC} if null.
	 *
	 * @param defaultDir the default dir
	 * @return sort direction or {@link Sort.Direction#ASC}
	 */
	private Direction getDirection(Direction defaultDir) {
		return d == null ? defaultDir : d;
	}

	/**
	 * Gets list of sort properties or provided defaults.
	 *
	 * @param defaultSortProps the default sort props
	 * @return provided properties or defaultSortProps
	 */
	private String[] getSortProperties(String[] defaultSortProps) {
		return s == null || s.length == 0 ? defaultSortProps : s;
	}

	/**
	 * To page request using the {@link #DEFAULT_SORT_PROPERTIES} and ASC sort.
	 *
	 * @param maxPageSize the max page size
	 * @return the pageable
	 */
	public Pageable toPageRequest(int maxPageSize) {
		return OffsetRequest.of(o == null ? 0 : o, Integer.min(l == null ? maxPageSize : l, maxPageSize), getDirection(Sort.Direction.ASC), getSortProperties(
			DEFAULT_SORT_PROPERTIES));
	}

	/**
	 * To page request.
	 *
	 * @param maxPageSize the max page size
	 * @param defaultDir the default dir
	 * @param defaultSort the default sort
	 * @return the pageable
	 */
	public Pageable toPageRequest(int maxPageSize, Direction defaultDir, String... defaultSort) {
		return OffsetRequest.of(o == null ? 0 : o, Integer.min(l == null ? maxPageSize : l, maxPageSize), getDirection(defaultDir), getSortProperties(defaultSort));
	}

	/**
	 * To page request.
	 *
	 * @param maxPageSize the max page size
	 * @param sort the sort
	 * @return the pageable
	 */
	public static Pageable toPageRequest(int maxPageSize, Sort sort) {
		return OffsetRequest.of(0, maxPageSize, sort);
	}

}