Pagination.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.gringlobal.api.v1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
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;

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

/**
 * 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). */
	private Integer p;

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

	/** Sort directions. */
	private Sort.Direction[] d;

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

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

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

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

	/**
	 * Sets the l.
	 *
	 * @param l the new l
	 */
	public void setL(final Integer 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(final 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(final String[] s) {
		this.s = s;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Pagination page=" + p + ", pageSize=" + l + ", dir=" + Arrays.toString(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(final 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) {
			// Default uses sort by id DESC
			return Lists.newArrayList(new Sort.Order(Direction.DESC, "id"));
		}

		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];
			var order = new Sort.Order(direction, sortProps[index]).nullsLast();
			sortOrders.add(order);
		}
		return sortOrders;
	}

	/**
	 * To page request using the {@link #DEFAULT_SORT_PROPERTIES} and ASC sort
	 *
	 * @param maxPageSize the max page size
	 * @return the pageable
	 */
	public Pageable toPageRequest(final int maxPageSize, final int defaultPageSize) {
		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 defaultDir the default dir
	 * @param defaultSort the default sort
	 * @return the pageable
	 */
	public Pageable toPageRequest(final int maxPageSize, final int defaultPageSize, final Direction defaultDir, final String... defaultSort) {
		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 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) {
		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 maxPageSize the max page size
	 * @param sort the sort
	 * @return the pageable
	 */
	public static Pageable toPageRequest(final int maxPageSize, final Sort sort) {
		if (sort == null) {
			return PageRequest.of(0, maxPageSize);
		}
		return PageRequest.of(0, maxPageSize, sort);
	}

	/**
	 * Add sorting by parameterName acs to the given pageable.
	 *
	 * @param page the page for adding sort
	 * @param paramNames the names of parameters for adding
	 * @return the updated pageable
	 */
	public static Pageable addSortByParams(Pageable page, Set<String> paramNames) {
		if (CollectionUtils.isNotEmpty(paramNames) && page.isPaged() && page.getSort().isSorted()) {
			Sort sort = page.getSort();
			for(String paramName : paramNames) {
				if (StringUtils.isNotBlank(paramName) && sort.getOrderFor(paramName) == null) {
					sort = sort.and(Sort.by(Direction.ASC, paramName));
				}
			}
			return PageRequest.of(page.getPageNumber(), page.getPageSize(), sort);
		}
		return page;
	}

	/**
	 * To page request.
	 *
	 * @param maxPageSize the max page size
	 * @param orders Using QDSL order specifiers
	 * @return
	 */
	public Pageable toPageRequest(int maxPageSize, int defaultPageSize, OrderSpecifier<?>... orders) {
		if (orders == null || orders.length == 0) {
			return toPageRequest(maxPageSize, defaultPageSize);
		}

		var sorts = Arrays.stream(orders).map(o -> {
			var property = o.getTarget().toString().replace(((Path<?>) o.getTarget()).getRoot().toString() + ".", "");
			Sort.Order order;
			if (o.isAscending()) {
				order = Sort.Order.asc(property);
			} else {
				order = Sort.Order.desc(property);
			}
			if (o.getNullHandling() == NullHandling.NullsLast) {
				order = order.nullsLast();
			} else if (o.getNullHandling() == NullHandling.NullsFirst) {
				order = order.nullsFirst();
			}
			return order;
		}).collect(Collectors.toList());

		return PageRequest.of(p == null ? 0 : p, Integer.min(l == null ? defaultPageSize : l, maxPageSize), Sort.by(sorts));
	}

}