001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.sqlmap.engine.mapping.statement;
017:
018: import com.ibatis.common.util.PaginatedList;
019: import com.ibatis.sqlmap.client.SqlMapExecutor;
020:
021: import java.sql.SQLException;
022: import java.util.*;
023:
024: /**
025: * @deprecated All paginated list features have been deprecated
026: */
027: public class PaginatedDataList implements PaginatedList {
028:
029: private SqlMapExecutor sqlMapExecutor;
030: private String statementName;
031: private Object parameterObject;
032:
033: private int pageSize;
034: private int index;
035:
036: private List prevPageList;
037: private List currentPageList;
038: private List nextPageList;
039:
040: public PaginatedDataList(SqlMapExecutor sqlMapExecutor,
041: String statementName, Object parameterObject, int pageSize)
042: throws SQLException {
043: this .sqlMapExecutor = sqlMapExecutor;
044: this .statementName = statementName;
045: this .parameterObject = parameterObject;
046: this .pageSize = pageSize;
047: this .index = 0;
048: pageTo(0);
049: }
050:
051: private void pageForward() {
052: try {
053: prevPageList = currentPageList;
054: currentPageList = nextPageList;
055: nextPageList = getList(index + 1, pageSize);
056: } catch (SQLException e) {
057: throw new RuntimeException(
058: "Unexpected error while repaginating paged list. Cause: "
059: + e, e);
060: }
061: }
062:
063: private void pageBack() {
064: try {
065: nextPageList = currentPageList;
066: currentPageList = prevPageList;
067: if (index > 0) {
068: prevPageList = getList(index - 1, pageSize);
069: } else {
070: prevPageList = new ArrayList();
071: }
072: } catch (SQLException e) {
073: throw new RuntimeException(
074: "Unexpected error while repaginating paged list. Cause: "
075: + e, e);
076: }
077: }
078:
079: private void safePageTo(int idx) {
080: try {
081: pageTo(idx);
082: } catch (SQLException e) {
083: throw new RuntimeException(
084: "Unexpected error while repaginating paged list. Cause: "
085: + e, e);
086: }
087: }
088:
089: public void pageTo(int idx) throws SQLException {
090: index = idx;
091:
092: List list;
093:
094: if (idx < 1) {
095: list = getList(idx, pageSize * 2);
096: } else {
097: list = getList(idx - 1, pageSize * 3);
098: }
099:
100: if (list.size() < 1) {
101: prevPageList = new ArrayList(0);
102: currentPageList = new ArrayList(0);
103: nextPageList = new ArrayList(0);
104: } else {
105: if (idx < 1) {
106: prevPageList = new ArrayList(0);
107: if (list.size() <= pageSize) {
108: currentPageList = list.subList(0, list.size());
109: nextPageList = new ArrayList(0);
110: } else {
111: currentPageList = list.subList(0, pageSize);
112: nextPageList = list.subList(pageSize, list.size());
113: }
114: } else {
115: if (list.size() <= pageSize) {
116: prevPageList = list.subList(0, list.size());
117: currentPageList = new ArrayList(0);
118: nextPageList = new ArrayList(0);
119: } else if (list.size() <= pageSize * 2) {
120: prevPageList = list.subList(0, pageSize);
121: currentPageList = list.subList(pageSize, list
122: .size());
123: nextPageList = new ArrayList(0);
124: } else {
125: prevPageList = list.subList(0, pageSize);
126: currentPageList = list.subList(pageSize,
127: pageSize * 2);
128: nextPageList = list.subList(pageSize * 2, list
129: .size());
130: }
131: }
132: }
133:
134: }
135:
136: private List getList(int idx, int localPageSize)
137: throws SQLException {
138: return sqlMapExecutor.queryForList(statementName,
139: parameterObject, (idx) * pageSize, localPageSize);
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 o) {
199: return currentPageList.contains(o);
200: }
201:
202: public Iterator iterator() {
203: return currentPageList.iterator();
204: }
205:
206: public Object[] toArray() {
207: return currentPageList.toArray();
208: }
209:
210: public Object[] toArray(Object a[]) {
211: return currentPageList.toArray(a);
212: }
213:
214: public boolean containsAll(Collection c) {
215: return currentPageList.containsAll(c);
216: }
217:
218: public Object get(int index) {
219: return currentPageList.get(index);
220: }
221:
222: public int indexOf(Object o) {
223: return currentPageList.indexOf(o);
224: }
225:
226: public int lastIndexOf(Object 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 o) {
243: return currentPageList.add(o);
244: }
245:
246: public boolean remove(Object 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 set(int index, Object element) {
271: return currentPageList.set(index, element);
272: }
273:
274: public void add(int index, Object element) {
275: currentPageList.add(index, element);
276: }
277:
278: public Object remove(int index) {
279: return currentPageList.remove(index);
280: }
281:
282: public int getPageIndex() {
283: return index;
284: }
285:
286: }
|