1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.sqlmap.engine.mapping.statement;
17
18 import com.ibatis.common.util.PaginatedList;
19 import com.ibatis.sqlmap.client.SqlMapExecutor;
20
21 import java.sql.SQLException;
22 import java.util.*;
23
24
25
26
27
28
29 public class PaginatedDataList implements PaginatedList {
30
31
32 private SqlMapExecutor sqlMapExecutor;
33
34
35 private String statementName;
36
37
38 private Object parameterObject;
39
40
41 private int pageSize;
42
43
44 private int index;
45
46
47 private List prevPageList;
48
49
50 private List currentPageList;
51
52
53 private List nextPageList;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public PaginatedDataList(SqlMapExecutor sqlMapExecutor, String statementName, Object parameterObject, int pageSize)
71 throws SQLException {
72 this.sqlMapExecutor = sqlMapExecutor;
73 this.statementName = statementName;
74 this.parameterObject = parameterObject;
75 this.pageSize = pageSize;
76 this.index = 0;
77 pageTo(0);
78 }
79
80
81
82
83 private void pageForward() {
84 try {
85 prevPageList = currentPageList;
86 currentPageList = nextPageList;
87 nextPageList = getList(index + 1, pageSize);
88 } catch (SQLException e) {
89 throw new RuntimeException("Unexpected error while repaginating paged list. Cause: " + e, e);
90 }
91 }
92
93
94
95
96 private void pageBack() {
97 try {
98 nextPageList = currentPageList;
99 currentPageList = prevPageList;
100 if (index > 0) {
101 prevPageList = getList(index - 1, pageSize);
102 } else {
103 prevPageList = new ArrayList();
104 }
105 } catch (SQLException e) {
106 throw new RuntimeException("Unexpected error while repaginating paged list. Cause: " + e, e);
107 }
108 }
109
110
111
112
113
114
115
116 private void safePageTo(int idx) {
117 try {
118 pageTo(idx);
119 } catch (SQLException e) {
120 throw new RuntimeException("Unexpected error while repaginating paged list. Cause: " + e, e);
121 }
122 }
123
124
125
126
127
128
129
130
131
132
133 public void pageTo(int idx) throws SQLException {
134 index = idx;
135
136 List list;
137
138 if (idx < 1) {
139 list = getList(idx, pageSize * 2);
140 } else {
141 list = getList(idx - 1, pageSize * 3);
142 }
143
144 if (list.size() < 1) {
145 prevPageList = new ArrayList(0);
146 currentPageList = new ArrayList(0);
147 nextPageList = new ArrayList(0);
148 } else {
149 if (idx < 1) {
150 prevPageList = new ArrayList(0);
151 if (list.size() <= pageSize) {
152 currentPageList = list.subList(0, list.size());
153 nextPageList = new ArrayList(0);
154 } else {
155 currentPageList = list.subList(0, pageSize);
156 nextPageList = list.subList(pageSize, list.size());
157 }
158 } else {
159 if (list.size() <= pageSize) {
160 prevPageList = list.subList(0, list.size());
161 currentPageList = new ArrayList(0);
162 nextPageList = new ArrayList(0);
163 } else if (list.size() <= pageSize * 2) {
164 prevPageList = list.subList(0, pageSize);
165 currentPageList = list.subList(pageSize, list.size());
166 nextPageList = new ArrayList(0);
167 } else {
168 prevPageList = list.subList(0, pageSize);
169 currentPageList = list.subList(pageSize, pageSize * 2);
170 nextPageList = list.subList(pageSize * 2, list.size());
171 }
172 }
173 }
174
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 private List getList(int idx, int localPageSize) throws SQLException {
191 return sqlMapExecutor.queryForList(statementName, parameterObject, (idx) * pageSize, localPageSize);
192 }
193
194 public boolean nextPage() {
195 if (isNextPageAvailable()) {
196 index++;
197 pageForward();
198 return true;
199 } else {
200 return false;
201 }
202 }
203
204 public boolean previousPage() {
205 if (isPreviousPageAvailable()) {
206 index--;
207 pageBack();
208 return true;
209 } else {
210 return false;
211 }
212 }
213
214 public void gotoPage(int pageNumber) {
215 safePageTo(pageNumber);
216 }
217
218 public int getPageSize() {
219 return pageSize;
220 }
221
222 public boolean isFirstPage() {
223 return index == 0;
224 }
225
226 public boolean isMiddlePage() {
227 return !(isFirstPage() || isLastPage());
228 }
229
230 public boolean isLastPage() {
231 return nextPageList.size() < 1;
232 }
233
234 public boolean isNextPageAvailable() {
235 return nextPageList.size() > 0;
236 }
237
238 public boolean isPreviousPageAvailable() {
239 return prevPageList.size() > 0;
240 }
241
242 public int size() {
243 return currentPageList.size();
244 }
245
246 public boolean isEmpty() {
247 return currentPageList.isEmpty();
248 }
249
250 public boolean contains(Object o) {
251 return currentPageList.contains(o);
252 }
253
254 public Iterator iterator() {
255 return currentPageList.iterator();
256 }
257
258 public Object[] toArray() {
259 return currentPageList.toArray();
260 }
261
262 public Object[] toArray(Object a[]) {
263 return currentPageList.toArray(a);
264 }
265
266 public boolean containsAll(Collection c) {
267 return currentPageList.containsAll(c);
268 }
269
270 public Object get(int index) {
271 return currentPageList.get(index);
272 }
273
274 public int indexOf(Object o) {
275 return currentPageList.indexOf(o);
276 }
277
278 public int lastIndexOf(Object o) {
279 return currentPageList.lastIndexOf(o);
280 }
281
282 public ListIterator listIterator() {
283 return currentPageList.listIterator();
284 }
285
286 public ListIterator listIterator(int index) {
287 return currentPageList.listIterator(index);
288 }
289
290 public List subList(int fromIndex, int toIndex) {
291 return currentPageList.subList(fromIndex, toIndex);
292 }
293
294 public boolean add(Object o) {
295 return currentPageList.add(o);
296 }
297
298 public boolean remove(Object o) {
299 return currentPageList.remove(o);
300 }
301
302 public boolean addAll(Collection c) {
303 return currentPageList.addAll(c);
304 }
305
306 public boolean addAll(int index, Collection c) {
307 return currentPageList.addAll(index, c);
308 }
309
310 public boolean removeAll(Collection c) {
311 return currentPageList.removeAll(c);
312 }
313
314 public boolean retainAll(Collection c) {
315 return currentPageList.retainAll(c);
316 }
317
318 public void clear() {
319 currentPageList.clear();
320 }
321
322 public Object set(int index, Object element) {
323 return currentPageList.set(index, element);
324 }
325
326 public void add(int index, Object element) {
327 currentPageList.add(index, element);
328 }
329
330 public Object remove(int index) {
331 return currentPageList.remove(index);
332 }
333
334 public int getPageIndex() {
335 return index;
336 }
337
338 }