View Javadoc

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