Clover.NET coverage report - Coverage

Coverage timestamp: Friday, May 20, 2005 9:17:00 PM

File Stats: LOC: 599   Methods: 37
NCLOC: 356 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
MappedStatements\PaginatedList.cs 90.9 % 72.7 % 37.8 % 67.1 %
coverage coverage
1  
2   #region Apache Notice
3   /*****************************************************************************
4   * $Header: $
5   * $Revision: $
6   * $Date: $
7   *
8   * iBATIS.NET Data Mapper
9   * Copyright (C) 2004 - Gilles Bayon
10   *
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   *
16   * http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
23   *
24   ********************************************************************************/
25   #endregion
26  
27   using System;
28   using System.Collections;
29  
30   using IBatisNet.Common;
31   using IBatisNet.Common.Pagination;
32   using IBatisNet.DataMapper.Exceptions;
33  
34   namespace IBatisNet.DataMapper.MappedStatements
35   {
36   /// <summary>
37   /// Summary description for PaginatedDataList.
38   /// </summary>
39   public class PaginatedList : IPaginatedList
40   {
41  
42   #region Fields
43  
44   private int _pageSize = 0;
45   private int _index = 0;
46  
47   private IList _prevPageList = null;
48   private IList _currentPageList = null;
49   private IList _nextPageList = null;
50  
51   private IMappedStatement _mappedStatement = null;
52   private object _parameterObject = null;
53  
54   #endregion
55  
56   /// <summary>
57   /// Constructor
58   /// </summary>
59   /// <param name="mappedStatement"></param>
60   /// <param name="parameterObject"></param>
61   /// <param name="pageSize"></param>
62 4 public PaginatedList(IMappedStatement mappedStatement, object parameterObject, int pageSize)
63   {
64 4 _mappedStatement = mappedStatement;
65 4 _parameterObject = parameterObject;
66 4 _pageSize = pageSize;
67 4 _index = 0;
68 4 PageTo(0);
69   }
70  
71  
72   /// <summary>
73   ///
74   /// </summary>
75 3 private void PageForward()
76   {
77 3 try
78   {
79 3 _prevPageList = _currentPageList;
80 3 _currentPageList = _nextPageList;
81 3 _nextPageList = GetList(_index + 1, _pageSize);
82   }
83   catch (DataMapperException e)
84   {
85 0 throw new DataMapperException("Unexpected error while repaginating paged list. Cause: " + e.Message, e);
86   }
87   }
88  
89   /// <summary>
90   ///
91   /// </summary>
92 3 private void PageBack()
93   {
94 3 try
95   {
96 3 _nextPageList = _currentPageList;
97 3 _currentPageList = _prevPageList;
98 3 if (_index > 0)
99   {
100 1 _prevPageList = GetList(_index - 1, _pageSize);
101   }
102   else
103   {
104 2 _prevPageList = new ArrayList();
105   }
106   }
107   catch (DataMapperException e)
108   {
109 0 throw new DataMapperException("Unexpected error while repaginating paged list. Cause: " + e.Message, e);
110   }
111   }
112  
113   /// <summary>
114   ///
115   /// </summary>
116   /// <param name="index"></param>
117 6 private void SafePageTo(int index)
118   {
119 6 try
120   {
121 6 PageTo(index);
122   }
123   catch (DataMapperException e)
124   {
125 0 throw new DataMapperException("Unexpected error while repaginating paged list. Cause: " + e.Message, e);
126   }
127   }
128  
129  
130   /// <summary>
131   ///
132   /// </summary>
133   /// <param name="index"></param>
134 10 public void PageTo(int index)
135   {
136 10 _index = index;
137 10 IList list = null;
138  
139 10 if (index < 1)
140   {
141 7 list = GetList(_index, _pageSize * 2);
142   }
143   else
144   {
145 3 list = GetList(index - 1, _pageSize * 3);
146   }
147  
148 10 if (list.Count < 1)
149   {
150 2 _prevPageList = new ArrayList();
151 2 _currentPageList = new ArrayList();
152 2 _nextPageList = new ArrayList();
153   }
154   else
155   {
156 8 if (index < 1)
157   {
158 5 _prevPageList = new ArrayList();
159 5 if (list.Count <= _pageSize)
160   {
161 2 _currentPageList = SubList(list, 0, list.Count);
162 2 _nextPageList = new ArrayList();
163   }
164   else
165   {
166 3 _currentPageList = SubList(list, 0, _pageSize);
167 3 _nextPageList = SubList(list, _pageSize, list.Count);
168   }
169   }
170   else
171   {
172 3 if (list.Count <= _pageSize)
173   {
174 1 _prevPageList = SubList(list, 0, list.Count);
175 1 _currentPageList = new ArrayList();
176 1 _nextPageList = new ArrayList();
177   }
178 2 else if (list.Count <= _pageSize * 2)
179   {
180 1 _prevPageList = SubList(list, 0, _pageSize);
181 1 _currentPageList = SubList(list, _pageSize, list.Count);
182 1 _nextPageList = new ArrayList();
183   }
184   else
185   {
186 1 _prevPageList = SubList(list,0, _pageSize);
187 1 _currentPageList = SubList(list ,_pageSize, _pageSize * 2);
188 1 _nextPageList = SubList(list ,_pageSize * 2, list.Count);
189   }
190   }
191   }
192   }
193  
194  
195   /// <summary>
196   ///
197   /// </summary>
198   /// <param name="index"></param>
199   /// <param name="localPageSize"></param>
200   /// <returns></returns>
201 14 private IList GetList(int index, int localPageSize)
202   {
203 14 bool isSessionLocal = false;
204  
205 14 IDalSession session = _mappedStatement.SqlMap.LocalSession;
206  
207 14 if (session == null)
208   {
209 14 session = new SqlMapSession(_mappedStatement.SqlMap.DataSource);
210 14 session.OpenConnection();
211 14 isSessionLocal = true;
212   }
213  
214 14 IList list = null;
215 14 try
216   {
217 14 list = _mappedStatement.ExecuteQueryForList(session, _parameterObject, (index) * _pageSize, localPageSize);
218   }
219   catch
220   {
221 0 throw;
222   }
223   finally
224   {
225 14 if ( isSessionLocal )
226   {
227 14 session.CloseConnection();
228   }
229   }
230  
231 14 return list;
232   }
233  
234  
235   /// <summary>
236   ///
237   /// </summary>
238 0 public bool IsEmpty
239   {
240   get
241   {
242   return (_currentPageList.Count == 0);
243   }
244   }
245  
246  
247   /// <summary>
248   /// Provides a view of the IList pramaeter
249   /// from the specified position <paramref name="fromIndex"/>
250   /// to the specified position <paramref name="toIndex"/>.
251   /// </summary>
252   /// <param name="list">The IList elements.</param>
253   /// <param name="fromIndex">Starting position for the view of elements. </param>
254   /// <param name="toIndex">Ending position for the view of elements. </param>
255   /// <returns> A view of list.
256   /// </returns>
257   /// <remarks>
258   /// The list that is returned is just a view, it is still backed
259   /// by the orignal list. Any changes you make to it will be
260   /// reflected in the orignal list.
261   /// </remarks>
262 14 private IList SubList(IList list, int fromIndex, int toIndex)
263   {
264 14 return ((ArrayList)list).GetRange(fromIndex, toIndex-fromIndex);
265   }
266  
267   #region IPaginatedList Members
268  
269   /// <summary>
270   ///
271   /// </summary>
272 0 public int PageIndex
273   {
274   get
275   {
276   return _index;
277   }
278   }
279  
280   /// <summary>
281   ///
282   /// </summary>
283   public bool IsPreviousPageAvailable
284   {
285 25 get
286   {
287 25 return (_prevPageList.Count > 0);
288   }
289   }
290  
291   /// <summary>
292   ///
293   /// </summary>
294 0 public bool IsFirstPage
295   {
296   get
297   {
298   return (_index == 0);
299   }
300   }
301  
302   /// <summary>
303   ///
304   /// </summary>
305   /// <param name="pageIndex"></param>
306 6 public void GotoPage(int pageIndex)
307   {
308 6 SafePageTo(pageIndex);
309   }
310  
311   /// <summary>
312   ///
313   /// </summary>
314 0 public int PageSize
315   {
316   get
317   {
318   return _pageSize;
319   }
320   }
321  
322   /// <summary>
323   ///
324   /// </summary>
325   /// <returns></returns>
326 5 public bool NextPage()
327   {
328 5 if (this.IsNextPageAvailable == true)
329   {
330 3 _index++;
331 3 PageForward();
332 3 return true;
333   }
334   else
335   {
336 2 return false;
337   }
338   }
339  
340   /// <summary>
341   ///
342   /// </summary>
343 0 public bool IsMiddlePage
344   {
345   get
346   {
347   return !(this.IsFirstPage || this.IsLastPage);
348  
349   }
350   }
351  
352   /// <summary>
353   ///
354   /// </summary>
355   /// <returns></returns>
356 6 public bool PreviousPage()
357   {
358 6 if (this.IsPreviousPageAvailable == true)
359   {
360 3 _index--;
361 3 PageBack();
362 3 return true;
363   }
364   else
365   {
366 3 return false;
367   }
368   }
369  
370   /// <summary>
371   ///
372   /// </summary>
373   public bool IsNextPageAvailable
374   {
375 23 get
376   {
377 23 return (_nextPageList.Count > 0);
378   }
379   }
380  
381   /// <summary>
382   ///
383   /// </summary>
384 0 public bool IsLastPage
385   {
386   get
387   {
388   return (_nextPageList.Count < 1);
389   }
390   }
391  
392   #endregion
393  
394   #region IList Members
395  
396   /// <summary>
397   ///
398   /// </summary>
399 0 public bool IsReadOnly
400   {
401   get
402   {
403   return _currentPageList.IsReadOnly;
404   }
405   }
406  
407   /// <summary>
408   ///
409   /// </summary>
410   public object this[int index]
411   {
412 19 get
413   {
414 19 return _currentPageList[index];
415   }
416 0 set
417   {
418   _currentPageList[index] = value;
419   }
420   }
421  
422   /// <summary>
423   ///
424   /// </summary>
425   /// <param name="index"></param>
426 0 public void RemoveAt(int index)
427   {
428   _currentPageList.RemoveAt(index);
429   }
430  
431   /// <summary>
432   ///
433   /// </summary>
434   /// <param name="index"></param>
435   /// <param name="value"></param>
436 0 public void Insert(int index, object value)
437   {
438   _currentPageList.Insert(index, value);
439   }
440  
441   /// <summary>
442   ///
443   /// </summary>
444   /// <param name="value"></param>
445 0 public void Remove(object value)
446   {
447   _currentPageList.Remove(value);
448   }
449  
450   /// <summary>
451   ///
452   /// </summary>
453   /// <param name="value"></param>
454   /// <returns></returns>
455 0 public bool Contains(object value)
456   {
457   return _currentPageList.Contains(value);
458   }
459  
460   /// <summary>
461   ///
462   /// </summary>
463 0 public void Clear()
464   {
465   _currentPageList.Clear();
466   }
467  
468   /// <summary>
469   ///
470   /// </summary>
471   /// <param name="value"></param>
472   /// <returns></returns>
473 0 public int IndexOf(object value)
474   {
475   return _currentPageList.IndexOf(value);
476   }
477  
478   /// <summary>
479   ///
480   /// </summary>
481   /// <param name="value"></param>
482   /// <returns></returns>
483 0 public int Add(object value)
484   {
485   return _currentPageList.Add(value);
486   }
487  
488   /// <summary>
489   ///
490   /// </summary>
491 0 public bool IsFixedSize
492   {
493   get
494   {
495   return _currentPageList.IsFixedSize;
496   }
497   }
498  
499   #endregion
500  
501   #region ICollection Members
502  
503   /// <summary>
504   ///
505   /// </summary>
506 0 public bool IsSynchronized
507   {
508   get
509   {
510   return _currentPageList.IsSynchronized;
511   }
512   }
513  
514   /// <summary>
515   ///
516   /// </summary>
517   public int Count
518   {
519 21 get
520   {
521 21 return _currentPageList.Count;
522   }
523   }
524  
525   /// <summary>
526   ///
527   /// </summary>
528   /// <param name="array"></param>
529   /// <param name="index"></param>
530 0 public void CopyTo(Array array, int index)
531   {
532   _currentPageList.CopyTo(array, index);
533   }
534  
535   /// <summary>
536   ///
537   /// </summary>
538 0 public object SyncRoot
539   {
540   get
541   {
542   return _currentPageList.SyncRoot;
543   }
544   }
545  
546   #endregion
547  
548   #region IEnumerable Members
549  
550   /// <summary>
551   ///
552   /// </summary>
553   /// <returns></returns>
554 0 public IEnumerator GetEnumerator()
555   {
556   return _currentPageList.GetEnumerator();
557  
558   }
559  
560   #endregion
561  
562   #region IEnumerator Members
563  
564   /// <summary>
565   /// Sets the enumerator to its initial position,
566   /// which is before the first element in the collection.
567   /// </summary>
568 0 public void Reset()
569   {
570   _currentPageList.GetEnumerator().Reset();
571   }
572  
573   /// <summary>
574   /// Gets the current element in the page.
575   /// </summary>
576 0 public object Current
577   {
578   get
579   {
580   return _currentPageList.GetEnumerator().Current;
581   }
582   }
583  
584   /// <summary>
585   /// Advances the enumerator to the next element of the collection.
586   /// </summary>
587   /// <returns>
588   /// true if the enumerator was successfully advanced to the next element;
589   /// false if the enumerator has passed the end of the collection.
590   /// </returns>
591 0 public bool MoveNext()
592   {
593   return _currentPageList.GetEnumerator().MoveNext();
594   }
595  
596   #endregion
597   }
598   }
599