1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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));
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
190 }
191
192 }
193
194 public int getCount() {
195 return count;
196 }
197
198 }