KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > statement > PaginatedDataList


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.engine.mapping.statement;
17
18 import com.ibatis.common.exception.NestedRuntimeException;
19 import com.ibatis.common.util.PaginatedList;
20 import com.ibatis.sqlmap.client.SqlMapExecutor;
21
22 import java.sql.SQLException JavaDoc;
23 import java.util.*;
24
25
26 public class PaginatedDataList implements PaginatedList {
27
28   private SqlMapExecutor sqlMapExecutor;
29   private String JavaDoc statementName;
30   private Object JavaDoc parameterObject;
31
32   private int pageSize;
33   private int index;
34
35   private List prevPageList;
36   private List currentPageList;
37   private List nextPageList;
38
39   public PaginatedDataList(SqlMapExecutor sqlMapExecutor, String JavaDoc statementName, Object JavaDoc parameterObject, int pageSize)
40       throws SQLException JavaDoc {
41     this.sqlMapExecutor = sqlMapExecutor;
42     this.statementName = statementName;
43     this.parameterObject = parameterObject;
44     this.pageSize = pageSize;
45     this.index = 0;
46     pageTo(0);
47   }
48
49   private void pageForward() {
50     try {
51       prevPageList = currentPageList;
52       currentPageList = nextPageList;
53       nextPageList = getList(index + 1, pageSize);
54     } catch (SQLException JavaDoc e) {
55       throw new NestedRuntimeException("Unexpected error while repaginating paged list. Cause: " + e, e);
56     }
57   }
58
59   private void pageBack() {
60     try {
61       nextPageList = currentPageList;
62       currentPageList = prevPageList;
63       if (index > 0) {
64         prevPageList = getList(index - 1, pageSize);
65       } else {
66         prevPageList = new ArrayList();
67       }
68     } catch (SQLException JavaDoc e) {
69       throw new NestedRuntimeException("Unexpected error while repaginating paged list. Cause: " + e, e);
70     }
71   }
72
73   private void safePageTo(int idx) {
74     try {
75       pageTo(idx);
76     } catch (SQLException JavaDoc e) {
77       throw new NestedRuntimeException("Unexpected error while repaginating paged list. Cause: " + e, e);
78     }
79   }
80
81 // private void slowPageTo(int idx) throws SQLException {
82
// index = idx;
83
// if (idx == 0) {
84
// prevPageList = new ArrayList();
85
// } else {
86
// prevPageList = getList(index - 1, pageSize);
87
// }
88
// currentPageList = getList(index, pageSize);
89
// nextPageList = getList(index + 1, pageSize);
90
// }
91

92   public void pageTo(int idx) throws SQLException JavaDoc {
93     index = idx;
94
95     List list;
96
97     if (idx < 1) {
98       list = getList(idx, pageSize * 2);
99     } else {
100       list = getList(idx - 1, pageSize * 3);
101     }
102
103     if (list.size() < 1) {
104       prevPageList = new ArrayList(0);
105       currentPageList = new ArrayList(0);
106       nextPageList = new ArrayList(0);
107     } else {
108       if (idx < 1) {
109         prevPageList = new ArrayList(0);
110         if (list.size() <= pageSize) {
111           currentPageList = list.subList(0, list.size());
112           nextPageList = new ArrayList(0);
113         } else {
114           currentPageList = list.subList(0, pageSize);
115           nextPageList = list.subList(pageSize, list.size());
116         }
117       } else {
118         if (list.size() <= pageSize) {
119           prevPageList = list.subList(0, list.size());
120           currentPageList = new ArrayList(0);
121           nextPageList = new ArrayList(0);
122         } else if (list.size() <= pageSize * 2) {
123           prevPageList = list.subList(0, pageSize);
124           currentPageList = list.subList(pageSize, list.size());
125           nextPageList = new ArrayList(0);
126         } else {
127           prevPageList = list.subList(0, pageSize);
128           currentPageList = list.subList(pageSize, pageSize * 2);
129           nextPageList = list.subList(pageSize * 2, list.size());
130         }
131       }
132     }
133
134   }
135
136
137   private List getList(int idx, int localPageSize) throws SQLException JavaDoc {
138     return sqlMapExecutor.queryForList(statementName, parameterObject, (idx) * pageSize, localPageSize);
139   }
140
141
142   public boolean nextPage() {
143     if (isNextPageAvailable()) {
144       index++;
145       pageForward();
146       return true;
147     } else {
148       return false;
149     }
150   }
151
152   public boolean previousPage() {
153     if (isPreviousPageAvailable()) {
154       index--;
155       pageBack();
156       return true;
157     } else {
158       return false;
159     }
160   }
161
162   public void gotoPage(int pageNumber) {
163     safePageTo(pageNumber);
164   }
165
166   public int getPageSize() {
167     return pageSize;
168   }
169
170   public boolean isFirstPage() {
171     return index == 0;
172   }
173
174   public boolean isMiddlePage() {
175     return !(isFirstPage() || isLastPage());
176   }
177
178   public boolean isLastPage() {
179     return nextPageList.size() < 1;
180   }
181
182   public boolean isNextPageAvailable() {
183     return nextPageList.size() > 0;
184   }
185
186   public boolean isPreviousPageAvailable() {
187     return prevPageList.size() > 0;
188   }
189
190   public int size() {
191     return currentPageList.size();
192   }
193
194   public boolean isEmpty() {
195     return currentPageList.isEmpty();
196   }
197
198   public boolean contains(Object JavaDoc o) {
199     return currentPageList.contains(o);
200   }
201
202   public Iterator iterator() {
203     return currentPageList.iterator();
204   }
205
206   public Object JavaDoc[] toArray() {
207     return currentPageList.toArray();
208   }
209
210   public Object JavaDoc[] toArray(Object JavaDoc a[]) {
211     return currentPageList.toArray(a);
212   }
213
214   public boolean containsAll(Collection c) {
215     return currentPageList.containsAll(c);
216   }
217
218   public Object JavaDoc get(int index) {
219     return currentPageList.get(index);
220   }
221
222   public int indexOf(Object JavaDoc o) {
223     return currentPageList.indexOf(o);
224   }
225
226   public int lastIndexOf(Object JavaDoc o) {
227     return currentPageList.lastIndexOf(o);
228   }
229
230   public ListIterator listIterator() {
231     return currentPageList.listIterator();
232   }
233
234   public ListIterator listIterator(int index) {
235     return currentPageList.listIterator(index);
236   }
237
238   public List subList(int fromIndex, int toIndex) {
239     return currentPageList.subList(fromIndex, toIndex);
240   }
241
242   public boolean add(Object JavaDoc o) {
243     return currentPageList.add(o);
244   }
245
246   public boolean remove(Object JavaDoc o) {
247     return currentPageList.remove(o);
248   }
249
250   public boolean addAll(Collection c) {
251     return currentPageList.addAll(c);
252   }
253
254   public boolean addAll(int index, Collection c) {
255     return currentPageList.addAll(index, c);
256   }
257
258   public boolean removeAll(Collection c) {
259     return currentPageList.removeAll(c);
260   }
261
262   public boolean retainAll(Collection c) {
263     return currentPageList.retainAll(c);
264   }
265
266   public void clear() {
267     currentPageList.clear();
268   }
269
270   public Object JavaDoc set(int index, Object JavaDoc element) {
271     return currentPageList.set(index, element);
272   }
273
274   public void add(int index, Object JavaDoc element) {
275     currentPageList.add(index, element);
276   }
277
278   public Object JavaDoc remove(int index) {
279     return currentPageList.remove(index);
280   }
281
282
283   public int getPageIndex() {
284     return index;
285   }
286
287 }
288
Popular Tags