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

io.mosip.pms.common.util.PageUtils Maven / Gradle / Ivy

There is a newer version: 1.3.0-dp.1
Show newest version
package io.mosip.pms.common.util;

import java.util.Collections;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import io.mosip.pms.common.constant.SearchErrorCode;
import io.mosip.pms.common.dto.PageResponseDto;
import io.mosip.pms.common.dto.Pagination;
import io.mosip.pms.common.dto.SearchSort;
import io.mosip.pms.common.entity.BaseEntity;
import io.mosip.pms.common.exception.RequestException;


/**
 * Utility class to calculate the page details* 
 * 
 *
 */
@Component
public class PageUtils {

	private SortUtils sortUtils;

	public PageUtils() {
		sortUtils = new SortUtils();
	}

	/**
	 * Method to create page metadata
	 * 
	 * @param 
	 * 
	 * @param page request to be
	 * @return {@link PageResponseDto}
	 */
	public static  PageResponseDto pageResponse(Page page) {
		PageResponseDto pageResponse = new PageResponseDto<>();
		if (page != null) {
			long totalItem = page.getTotalElements();
			int pageSize = page.getSize();
			int start = (page.getNumber() * pageSize) + 1;
			pageResponse.setFromRecord(start);
			pageResponse.setToRecord((long) (start - 1) + page.getNumberOfElements());
			pageResponse.setTotalRecord(totalItem);
		}
		return pageResponse;
	}

	public  void validateSortField(Class clazz, List searchSorts) {
		sortUtils.validateSortField(clazz, searchSorts);
	}

	public  void validateSortField(Class class1, Class class2, List list) {
		sortUtils.validateSortField(class1, class2, list);
	}

	public  PageResponseDto sortPage(List content, List sort, Pagination page,long totalNumberOfRecords) {
		PageResponseDto pageResponse = new PageResponseDto<>();
		page.setPageFetch(page.getPageFetch());
		List sortedList = sortUtils.sort(content, sort);
		List pageList = Collections.emptyList();
		if (validate(page)) {
			pageList = getPage(sortedList, page);
		}
		if (!pageList.isEmpty()) {
			pageResponse = pageResponse(
					new PageImpl<>(pageList, PageRequest.of(page.getPageStart(), page.getPageFetch()), content.size()));
			pageResponse.setData(pageList);
		}
		return pageResponse;
	}

	public  PageResponseDto applyPagination(List list, Pagination page) {
		return sortPage(list, Collections.emptyList(), page,list.size());
	}

	private boolean validate(Pagination page) {
		if (page != null) {
			if (page.getPageStart() < 0 || page.getPageFetch() < 1) {
				throw new RequestException(SearchErrorCode.INVALID_PAGINATION_VALUE.getErrorCode(),
						String.format(SearchErrorCode.INVALID_PAGINATION_VALUE.getErrorMessage(),
								page.getPageStart(), page.getPageFetch()));
			} else {
				return true;
			}
		} else {
			throw new RequestException(SearchErrorCode.INVALID_PAGINATION.getErrorCode(),
					SearchErrorCode.INVALID_PAGINATION.getErrorMessage());
		}
	}

	public  List getPage(List list, Pagination page) {
		int fromIndex = (page.getPageStart()) * page.getPageFetch();
		if (list == null || list.size() < fromIndex) {
			return Collections.emptyList();
		}
		int endIndex = Math.min(fromIndex + page.getPageFetch(), list.size());
		return list.subList(fromIndex, endIndex);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy