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.rssaggregator.util;
35  
36  import info.magnolia.context.MgnlContext;
37  
38  import java.util.Collection;
39  import java.util.List;
40  
41  import javax.jcr.Node;
42  import javax.jcr.RepositoryException;
43  
44  import org.apache.commons.lang3.StringEscapeUtils;
45  import org.apache.commons.lang3.StringUtils;
46  
47  
48  /**
49   * Copy of the STK Pager to avoid cyclic dependencies.
50   * Will be replaced by infinite scroll in a later version.
51   */
52  public class PagerUtil {
53  
54      private int count;
55      private String link;
56      private int maxResultsPerPage;
57      private Collection items;
58      private String position;
59  
60      public PagerUtil(String link, Collection items, Node content) {
61          this.count = items.size();
62          this.items = items;
63          this.link = link;
64  
65          getPagerProperties(content);
66      }
67  
68      protected PagerUtil() {
69      }
70  
71      protected void setResults(int maxResultsPerPage) {
72          this.maxResultsPerPage = maxResultsPerPage;
73      }
74  
75      protected void setCount(int count) {
76          this.count = count;
77      }
78  
79      public Collection getPageItems() {
80  
81          Collection subList = items;
82          int offset = getOffset();
83          if (count > 0) {
84              int limit = maxResultsPerPage + offset;
85              if (items.size() < limit) {
86                  limit = count;
87              }
88              subList = ((List) items).subList(offset, limit);
89  
90          }
91          return subList;
92      }
93  
94      protected int getOffset() {
95          int offset = ((getCurrentPage() - 1) * maxResultsPerPage);
96          if (offset > count) {
97              int pages = count / maxResultsPerPage;
98              offset = pages * maxResultsPerPage;
99          }
100         return offset;
101     }
102 
103     public int getCurrentPage() {
104         int currentPage = 1;
105         if (MgnlContext.getParameter("currentPage") != null && (Integer.parseInt(MgnlContext.getParameter("currentPage")) > 1)) {
106             currentPage = Integer.parseInt(MgnlContext.getParameter("currentPage"));
107         }
108         return currentPage;
109     }
110 
111     public String getPageLink(int i) {
112         String current = "currentPage=";
113         if (this.link.indexOf('?') > 0) {
114             final String query = StringUtils.substringAfter(this.link, "?");
115             StringBuilder newQuery = new StringBuilder("?");
116             String[] params = query.split("&");
117             boolean pageSet = false;
118             int cnt = 0;
119             for (String param : params) {
120                 if (param.startsWith(current)) {
121                     newQuery.append(current).append(i);
122                     pageSet = true;
123                 } else {
124                     newQuery.append(StringEscapeUtils.escapeHtml4(param)); //XSS without this escaping possible
125                 }
126                 cnt++;
127                 if (cnt < params.length) {
128                     newQuery.append("&");
129                 }
130             }
131             if (!pageSet) {
132                 if (newQuery.length() > 1) {
133                     newQuery.append("&");
134                 }
135                 newQuery.append(current).append(i);
136 
137             }
138             return StringUtils.substringBefore(this.link, "?") + newQuery.toString();
139         } else {
140             String link = this.link + "?" + current + i;
141             return link;
142         }
143     }
144 
145     public int getNumPages() {
146         int numPages = count / maxResultsPerPage;
147         if ((count % maxResultsPerPage) > 0) {
148             numPages++;
149         }
150         return numPages;
151     }
152 
153     public int getBeginIndex() {
154         if (getCurrentPage() - 2 <= 1) {
155             return 1;
156         } else {
157             return getCurrentPage() - 2;
158         }
159     }
160 
161     public int getEndIndex() {
162         if (getCurrentPage() + 2 >= getNumPages()) {
163             return getNumPages();
164         } else {
165             return getCurrentPage() + 2;
166         }
167     }
168 
169     public String getPosition() {
170         return position;
171     }
172 
173     protected void getPagerProperties(Node content) {
174 
175         maxResultsPerPage = 10000;
176         try {
177             if (content.hasProperty("maxResultsPerPage")) {
178                 int max = (int) content.getProperty("maxResultsPerPage").getLong();
179                 if (max > 0) {
180                     maxResultsPerPage = max;
181                 }
182             }
183 
184             position = "top";
185             if (content.hasProperty("position")) {
186                 position = content.getProperty("position").getString();
187             }
188 
189         } catch (RepositoryException e) {
190             //use defaults
191         }
192 
193     }
194 }