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