View Javadoc
1   /**
2    * This file Copyright (c) 2008-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.templatingkit.util;
35  
36  
37  import info.magnolia.context.MgnlContext;
38  
39  import java.util.Collection;
40  import java.util.List;
41  
42  import javax.jcr.Node;
43  import javax.jcr.RepositoryException;
44  
45  import org.apache.commons.lang3.StringEscapeUtils;
46  import org.apache.commons.lang3.StringUtils;
47  
48  /**
49   * Allows to define pagination.
50   */
51  public class STKPager {
52  
53      private int count;
54      private String link;
55      private int maxResultsPerPage;
56      private Collection items;
57      private String position;
58  
59      public STKPager(String link, Collection items, Node content) {
60          this.count = items.size();
61          this.items = items;
62          this.link = link;
63  
64          getPagerProperties(content);
65      }
66  
67      protected STKPager() {
68      }
69  
70      protected void setResults(int maxResultsPerPage) {
71          this.maxResultsPerPage = maxResultsPerPage;
72      }
73  
74      protected void setCount(int count) {
75          this.count = count;
76      }
77  
78      public Collection getPageItems() {
79  
80          Collection subList = items;
81          int offset = getOffset();
82          if (count > 0) {
83              int limit = maxResultsPerPage + offset;
84              if (items.size() < limit) {
85                  limit = count;
86              }
87              subList = ((List) items).subList(offset, limit);
88  
89          }
90          return subList;
91      }
92  
93      protected int getOffset() {
94          int offset = ((getCurrentPage() - 1) * maxResultsPerPage);
95          if (offset > count) {
96              int pages = count / maxResultsPerPage;
97              offset = pages * maxResultsPerPage;
98          }
99          return offset;
100     }
101 
102     public int getCurrentPage() {
103         int currentPage = 1;
104         if (MgnlContext.getParameter("currentPage") != null && (Integer.parseInt(MgnlContext.getParameter("currentPage")) > 1)) {
105             currentPage = Integer.parseInt(MgnlContext.getParameter("currentPage"));
106         }
107         return currentPage;
108     }
109 
110     public String getPageLink(int i) {
111         String current = "currentPage=";
112         if (this.link.indexOf('?') > 0) {
113             final String query = StringUtils.substringAfter(this.link, "?");
114             StringBuilder newQuery = new StringBuilder("?");
115             String[] params = query.split("&");
116             boolean pageSet = false;
117             int cnt = 0;
118             for (String param : params) {
119                 if (param.startsWith(current)) {
120                     newQuery.append(current).append(i);
121                     pageSet = true;
122                 } else {
123                     newQuery.append(StringEscapeUtils.escapeHtml4(param)); //XSS without this escaping possible
124                 }
125                 cnt++;
126                 if (cnt < params.length) {
127                     newQuery.append("&");
128                 }
129             }
130             if (!pageSet) {
131                 if (newQuery.length() > 1) {
132                     newQuery.append("&");
133                 }
134                 newQuery.append(current).append(i);
135 
136             }
137             return StringUtils.substringBefore(this.link, "?") + newQuery.toString();
138         } else {
139             String link = this.link + "?" + current + i;
140             return link;
141         }
142     }
143 
144     public int getNumPages() {
145         int numPages = count / maxResultsPerPage;
146         if ((count % maxResultsPerPage) > 0) {
147             numPages++;
148         }
149         return numPages;
150     }
151 
152     public int getBeginIndex() {
153         if (getCurrentPage() - 2 <= 1) {
154             return 1;
155         } else {
156             return getCurrentPage() - 2;
157         }
158     }
159 
160     public int getEndIndex() {
161         if (getCurrentPage() + 2 >= getNumPages()) {
162             return getNumPages();
163         } else {
164             return getCurrentPage() + 2;
165         }
166     }
167 
168     public String getPosition() {
169         return position;
170     }
171 
172     protected void getPagerProperties(Node content) {
173 
174         maxResultsPerPage = 10000;
175         try {
176             if (content.hasProperty("maxResultsPerPage")) {
177                 int max = (int) content.getProperty("maxResultsPerPage").getLong();
178                 if (max > 0) {
179                     maxResultsPerPage = max;
180                 }
181             }
182 
183             position = "top";
184             if (content.hasProperty("position")) {
185                 position = content.getProperty("position").getString();
186             }
187 
188         } catch (RepositoryException e) {
189             //use defaults
190         }
191 
192     }
193 
194     public int getCount() {
195         return count;
196     }
197 
198 }