Pagination.java

/*
 * Copyright 2018 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.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.common.collect.Lists;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Path;

import io.swagger.v3.oas.annotations.media.Schema;

import org.springframework.data.domain.PageRequest;
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 Pagination {

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

	/** The default sort direction. */
	private final Direction DEFAULT_SORT_DIRECTION = Direction.ASC;

	/** Page (0-based). */
	@Schema(example = "0", description = "0-based page to retrieve")
	private Integer p;

	/** Page size (length). */
	@Schema(example = "50", description = "Number of records per page")
	private Integer l;

	/** Sort direction. */
	@Schema(description = "Sort directions", type = "string", allowableValues = { "ASC", "DESC" })
	private Sort.Direction[] d;

	/** Sort properties. */
	@Schema(description = "Sort properties")
	private String[] s;

	/**
	 * Gets the p.
	 *
	 * @return the p
	 */
	public int getP() {
		return p;
	}

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

	/**
	 * 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;
	}

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

	/**
	 * Get sort direction or {@link Sort.Direction#ASC} if null.
	 *
	 * @param defaultDirs the default sort directions
	 * @return sort direction or {@link Sort.Direction#ASC}
	 */
	private Direction[] getDirections(final Direction... defaultDirs) {
		return d == null || d.length == 0 ? defaultDirs : 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;
	}

	/**
	 * Gets list of sort orders or provided defaults.
	 *
	 * @param sortDirs the default sort directions
	 * @param sortProps the default sort props
	 * @return sort orders
	 */
	private List<Sort.Order> getSortOrders(final Direction[] sortDirs, final String[] sortProps) {
		if (sortProps == null || sortProps.length == 0) {
			return Lists.newArrayList(new Sort.Order(DEFAULT_SORT_DIRECTION, DEFAULT_SORT_PROPERTIES[0]));
		}

		List<Sort.Order> sortOrders = new ArrayList<>(sortProps.length);
		for (int index = 0; index < sortProps.length; index ++) {
			Direction direction = sortDirs == null || sortDirs.length < (index + 1) ? DEFAULT_SORT_DIRECTION : sortDirs[index];
			sortOrders.add(new Sort.Order(direction, sortProps[index]));
		}
		return sortOrders;
	}

	/**
	 * To page request using the {@link #DEFAULT_SORT_PROPERTIES} and ASC sort
	 *
	 * @param maxPageSize the max page size
	 * @param defaultPageSize use default page size when not provided
	 * @return the pageable
	 */
	public Pageable toPageRequest(int maxPageSize, int defaultPageSize) {
		assert(defaultPageSize <= maxPageSize);
		return PageRequest.of(p == null ? 0 : p, Integer.min(l == null ? defaultPageSize : l, maxPageSize), Sort.by(getSortOrders(getDirections(DEFAULT_SORT_DIRECTION),
				getSortProperties(DEFAULT_SORT_PROPERTIES))));
	}

	/**
	 * To page request.
	 *
	 * @param maxPageSize the max page size
	 * @param defaultPageSize use default page size when not provided
	 * @param defaultDir the default dir
	 * @param defaultSort the default sort
	 * @return the pageable
	 */
	public Pageable toPageRequest(int maxPageSize, int defaultPageSize, Direction defaultDir, String... defaultSort) {
		assert(defaultPageSize <= maxPageSize);
		return PageRequest.of(p == null ? 0 : p, Integer.min(l == null ? defaultPageSize : l, maxPageSize), Sort.by(getSortOrders(getDirections(defaultDir),
				getSortProperties(defaultSort))));
	}

	/**
	 * To page request.
	 *
	 * @param maxPageSize the max page size
	 * @param defaultPageSize use default page size when not provided
	 * @param defaultDirs the default directions
	 * @param defaultSort the default sort
	 * @return the pageable
	 */
	public Pageable toPageRequest(final int maxPageSize, final int defaultPageSize, final Direction[] defaultDirs, final String... defaultSort) {
		assert(defaultPageSize <= maxPageSize);
		return PageRequest.of(p == null ? 0 : p, Integer.min(l == null ? defaultPageSize : l, maxPageSize), Sort.by(getSortOrders(getDirections(defaultDirs),
				getSortProperties(defaultSort))));
	}

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

	/**
	 * To page request.
	 *
	 * @param maxPageSize the max page size
	 * @param defaultPageSize use default page size when not provided
	 * @param orders Using QDSL order specifiers
	 * @return
	 */
	public Pageable toPageRequest(int maxPageSize, int defaultPageSize, OrderSpecifier<?>... orders) {
		assert(defaultPageSize <= maxPageSize);
		if (orders == null || orders.length == 0) {
			return toPageRequest(maxPageSize, defaultPageSize);
		}
		Direction[] sortDirs = Arrays.stream(orders).map(o -> o.isAscending() ? Direction.ASC : Direction.DESC).toArray(Direction[]::new);
		String[] sortProps = Arrays.stream(orders).map(OrderSpecifier::getTarget)
				.map(t -> t.toString().replace(((Path<?>)t).getRoot().toString() + ".", "")).toArray(String[]::new);
		return toPageRequest(maxPageSize, defaultPageSize, sortDirs, sortProps);
	}

}