#region Apache Notice /***************************************************************************** * $Header: $ * $Revision: $ * $Date: $ * * iBATIS.NET Data Mapper * Copyright (C) 2004 - Gilles Bayon * * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ********************************************************************************/ #endregion using System; using System.Collections; using IBatisNet.Common; using IBatisNet.Common.Pagination; using IBatisNet.DataMapper.Exceptions; namespace IBatisNet.DataMapper.MappedStatements { /// /// Summary description for PaginatedDataList. /// public class PaginatedList : IPaginatedList { #region Fields private int _pageSize = 0; private int _index = 0; private IList _prevPageList = null; private IList _currentPageList = null; private IList _nextPageList = null; private IMappedStatement _mappedStatement = null; private object _parameterObject = null; #endregion /// /// Constructor /// /// /// /// public PaginatedList(IMappedStatement mappedStatement, object parameterObject, int pageSize) { _mappedStatement = mappedStatement; _parameterObject = parameterObject; _pageSize = pageSize; _index = 0; PageTo(0); } /// /// /// private void PageForward() { try { _prevPageList = _currentPageList; _currentPageList = _nextPageList; _nextPageList = GetList(_index + 1, _pageSize); } catch (DataMapperException e) { throw new DataMapperException("Unexpected error while repaginating paged list. Cause: " + e.Message, e); } } /// /// /// private void PageBack() { try { _nextPageList = _currentPageList; _currentPageList = _prevPageList; if (_index > 0) { _prevPageList = GetList(_index - 1, _pageSize); } else { _prevPageList = new ArrayList(); } } catch (DataMapperException e) { throw new DataMapperException("Unexpected error while repaginating paged list. Cause: " + e.Message, e); } } /// /// /// /// private void SafePageTo(int index) { try { PageTo(index); } catch (DataMapperException e) { throw new DataMapperException("Unexpected error while repaginating paged list. Cause: " + e.Message, e); } } /// /// /// /// public void PageTo(int index) { _index = index; IList list = null; if (index < 1) { list = GetList(_index, _pageSize * 2); } else { list = GetList(index - 1, _pageSize * 3); } if (list.Count < 1) { _prevPageList = new ArrayList(); _currentPageList = new ArrayList(); _nextPageList = new ArrayList(); } else { if (index < 1) { _prevPageList = new ArrayList(); if (list.Count <= _pageSize) { _currentPageList = SubList(list, 0, list.Count); _nextPageList = new ArrayList(); } else { _currentPageList = SubList(list, 0, _pageSize); _nextPageList = SubList(list, _pageSize, list.Count); } } else { if (list.Count <= _pageSize) { _prevPageList = SubList(list, 0, list.Count); _currentPageList = new ArrayList(); _nextPageList = new ArrayList(); } else if (list.Count <= _pageSize * 2) { _prevPageList = SubList(list, 0, _pageSize); _currentPageList = SubList(list, _pageSize, list.Count); _nextPageList = new ArrayList(); } else { _prevPageList = SubList(list,0, _pageSize); _currentPageList = SubList(list ,_pageSize, _pageSize * 2); _nextPageList = SubList(list ,_pageSize * 2, list.Count); } } } } /// /// /// /// /// /// private IList GetList(int index, int localPageSize) { bool isSessionLocal = false; IDalSession session = _mappedStatement.SqlMap.LocalSession; if (session == null) { session = new SqlMapSession(_mappedStatement.SqlMap.DataSource); session.OpenConnection(); isSessionLocal = true; } IList list = null; try { list = _mappedStatement.ExecuteQueryForList(session, _parameterObject, (index) * _pageSize, localPageSize); } catch { throw; } finally { if ( isSessionLocal ) { session.CloseConnection(); } } return list; } /// /// /// public bool IsEmpty { get { return (_currentPageList.Count == 0); } } /// /// Provides a view of the IList pramaeter /// from the specified position /// to the specified position . /// /// The IList elements. /// Starting position for the view of elements. /// Ending position for the view of elements. /// A view of list. /// /// /// The list that is returned is just a view, it is still backed /// by the orignal list. Any changes you make to it will be /// reflected in the orignal list. /// private IList SubList(IList list, int fromIndex, int toIndex) { return ((ArrayList)list).GetRange(fromIndex, toIndex-fromIndex); } #region IPaginatedList Members /// /// /// public int PageIndex { get { return _index; } } /// /// /// public bool IsPreviousPageAvailable { get { return (_prevPageList.Count > 0); } } /// /// /// public bool IsFirstPage { get { return (_index == 0); } } /// /// /// /// public void GotoPage(int pageIndex) { SafePageTo(pageIndex); } /// /// /// public int PageSize { get { return _pageSize; } } /// /// /// /// public bool NextPage() { if (this.IsNextPageAvailable == true) { _index++; PageForward(); return true; } else { return false; } } /// /// /// public bool IsMiddlePage { get { return !(this.IsFirstPage || this.IsLastPage); } } /// /// /// /// public bool PreviousPage() { if (this.IsPreviousPageAvailable == true) { _index--; PageBack(); return true; } else { return false; } } /// /// /// public bool IsNextPageAvailable { get { return (_nextPageList.Count > 0); } } /// /// /// public bool IsLastPage { get { return (_nextPageList.Count < 1); } } #endregion #region IList Members /// /// /// public bool IsReadOnly { get { return _currentPageList.IsReadOnly; } } /// /// /// public object this[int index] { get { return _currentPageList[index]; } set { _currentPageList[index] = value; } } /// /// /// /// public void RemoveAt(int index) { _currentPageList.RemoveAt(index); } /// /// /// /// /// public void Insert(int index, object value) { _currentPageList.Insert(index, value); } /// /// /// /// public void Remove(object value) { _currentPageList.Remove(value); } /// /// /// /// /// public bool Contains(object value) { return _currentPageList.Contains(value); } /// /// /// public void Clear() { _currentPageList.Clear(); } /// /// /// /// /// public int IndexOf(object value) { return _currentPageList.IndexOf(value); } /// /// /// /// /// public int Add(object value) { return _currentPageList.Add(value); } /// /// /// public bool IsFixedSize { get { return _currentPageList.IsFixedSize; } } #endregion #region ICollection Members /// /// /// public bool IsSynchronized { get { return _currentPageList.IsSynchronized; } } /// /// /// public int Count { get { return _currentPageList.Count; } } /// /// /// /// /// public void CopyTo(Array array, int index) { _currentPageList.CopyTo(array, index); } /// /// /// public object SyncRoot { get { return _currentPageList.SyncRoot; } } #endregion #region IEnumerable Members /// /// /// /// public IEnumerator GetEnumerator() { return _currentPageList.GetEnumerator(); } #endregion #region IEnumerator Members /// /// Sets the enumerator to its initial position, /// which is before the first element in the collection. /// public void Reset() { _currentPageList.GetEnumerator().Reset(); } /// /// Gets the current element in the page. /// public object Current { get { return _currentPageList.GetEnumerator().Current; } } /// /// Advances the enumerator to the next element of the collection. /// /// /// true if the enumerator was successfully advanced to the next element; /// false if the enumerator has passed the end of the collection. /// public bool MoveNext() { return _currentPageList.GetEnumerator().MoveNext(); } #endregion } }