001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.dataAccess.filtering;
019
020
021import com.fasterxml.jackson.annotation.JsonIgnore;
022
023import java.util.ArrayList;
024import java.util.List;
025
026import static io.stallion.utils.Literals.list;
027
028/**
029 * A class holding the paged results from a filter operation.
030 *
031 * @param <T>
032 */
033public class Pager<T> {
034    private List<T> currentItems = new ArrayList<T>();
035    private int pageCount = 0;
036    private int currentPage = 1;
037    private int itemsPerPage = 10;
038    private boolean hasNextPage = false;
039    private boolean hasPreviousPage = false;
040    private int nextPageNumber = 2;
041    private int previousPageNumber = 0;
042
043
044
045    /**
046     * All the returned items.
047     *
048     * @return
049     */
050    public List<T> getItems() {
051        return getCurrentItems();
052    }
053
054    /**
055     * Alias for getItems()
056     * @return
057     */
058    @JsonIgnore
059    public List<T> getCurrentItems() {
060        return currentItems;
061    }
062
063
064
065    /**
066     * The total available pages of items matching the filterchain
067     * @return
068     */
069    public int getPageCount() {
070        return pageCount;
071    }
072
073
074
075    /**
076     * The current page number of data, starting at 1
077     * @return
078     */
079    public int getCurrentPage() {
080        return currentPage;
081    }
082
083
084
085    /**
086     * The number of items per page
087     *
088     * @return
089     */
090    public int getItemsPerPage() {
091        return itemsPerPage;
092    }
093
094
095
096    /**
097     * True if there is a next page of data.
098     * @return
099     */
100    public boolean isHasNextPage() {
101        return hasNextPage;
102    }
103
104    /**
105     * True if there is a previous page of data (false if the page number is 1)
106     * @return
107     */
108    public boolean isHasPreviousPage() {
109        return hasPreviousPage;
110    }
111
112
113    /**
114     * The page number for the next page
115     * @return
116     */
117    public int getNextPageNumber() {
118        return nextPageNumber;
119    }
120
121
122    /**
123     * The page number for the previous page.
124     * @return
125     */
126    public int getPreviousPageNumber() {
127        return previousPageNumber;
128    }
129
130    public Pager setCurrentItems(List<T> currentItems) {
131        this.currentItems = currentItems;
132        return this;
133    }
134
135    public Pager setPageCount(int pageCount) {
136        this.pageCount = pageCount;
137        return this;
138    }
139
140    public Pager setCurrentPage(int currentPage) {
141        this.currentPage = currentPage;
142        return this;
143    }
144
145    public Pager setItemsPerPage(int itemsPerPage) {
146        this.itemsPerPage = itemsPerPage;
147        return this;
148    }
149
150    public Pager setHasNextPage(boolean hasNextPage) {
151        this.hasNextPage = hasNextPage;
152        return this;
153    }
154
155    public Pager setHasPreviousPage(boolean hasPreviousPage) {
156        this.hasPreviousPage = hasPreviousPage;
157        return this;
158    }
159
160    public Pager setNextPageNumber(int nextPageNumber) {
161        this.nextPageNumber = nextPageNumber;
162        return this;
163    }
164
165    public Pager setPreviousPageNumber(int previousPageNumber) {
166        this.previousPageNumber = previousPageNumber;
167        return this;
168    }
169
170    public List<Integer> getSurroundingPages() {
171        List<Integer> pages = list();
172        int start = currentPage - 4;
173        if (start < 1) {
174            start = 1;
175        }
176        int end = start + 7;
177        if (end > pageCount) {
178            end = pageCount;
179        }
180        for(int x = start; x <= end; x++) {
181            pages.add(x);
182        }
183        return pages;
184    }
185
186
187
188    // surrounding(2)
189    // firstSurroundingAndLast(1)
190    // PageLink(isElippisis, pageNumber, isFirst, isLast, isCurrent, cssClass)
191    // getPageLinks().setIncludeElippis(false).setSurrounding(3).includeFirst(true).includeLast(false).setIsCurrentClass('current-page')
192}