vnpt.media.efinder.model.PaginationResult.java Source code

Java tutorial

Introduction

Here is the source code for vnpt.media.efinder.model.PaginationResult.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vnpt.media.efinder.model;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;

/**
 *
 * @author vnpt2
 */
public class PaginationResult<E> {

    private int totalRecords;
    private int currentPage;
    private List<E> list;
    private int maxResult;
    private int totalPages;

    private int maxNavigationPage;

    private List<Integer> navigationPages;

    // @page: 1, 2, ...
    public PaginationResult(Query query, int page, int maxResult, int maxNavigationPage) {
        final int pageIndex = page - 1 < 0 ? 0 : page - 1;
        int fromRecordIndex = pageIndex * maxResult;
        int maxRecordIndex = fromRecordIndex + maxResult;

        ScrollableResults resultScroll = query.scroll(ScrollMode.SCROLL_INSENSITIVE);

        //resultScroll.g
        List results = new ArrayList<>();
        boolean hasResult = resultScroll.first();
        if (hasResult) {
            // Cuon toi vi tri
            hasResult = resultScroll.scroll(fromRecordIndex);
            if (hasResult) {
                do {
                    E record = (E) resultScroll.get(0);
                    results.add(record);
                } while (resultScroll.next()//
                        && resultScroll.getRowNumber() >= fromRecordIndex
                        && resultScroll.getRowNumber() < maxRecordIndex);
            }

            // chuyen toi ban ghi cuoi
            resultScroll.last();
        }

        // Tong so ban ghi
        this.totalRecords = resultScroll.getRowNumber() + 1;
        this.currentPage = pageIndex + 1;
        this.list = results;
        this.maxResult = maxResult;
        this.totalPages = (this.totalRecords / this.maxResult);
        if (totalRecords % this.maxResult != 0) {
            this.totalPages = this.totalPages + 1;
        }
        this.maxNavigationPage = totalPages;

        if (maxNavigationPage < totalPages) {
            this.maxNavigationPage = maxNavigationPage;
        }

        this.calcNavigationPages();

    }

    private void calcNavigationPages() {
        this.navigationPages = new ArrayList<>();

        int current = this.currentPage > this.totalPages ? this.totalPages : this.currentPage;

        int begin = current - this.maxNavigationPage / 2;
        int end = current + this.maxNavigationPage / 2;

        // Trang dau tien
        navigationPages.add(1);
        if (begin > 2) {
            // Dung cho '....'
            navigationPages.add(-1);
        }

        for (int i = begin; i < end; i++) {
            if (i > 1 && i < this.totalPages) {
                navigationPages.add(i);
            }
        }

        if (end < this.totalPages - 2) {
            // Dung cho '...'
            navigationPages.add(-1);
        }

        // Trang cuoi cung
        navigationPages.add(this.totalPages);

    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public List<E> getList() {
        return list;
    }

    public int getMaxResult() {
        return maxResult;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public int getMaxNavigationPage() {
        return maxNavigationPage;
    }

    public List<Integer> getNavigationPages() {
        return navigationPages;
    }

}