1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
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
|
|
|
37
|
|
|
38
|
|
|
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
|
|
|
57
|
|
|
58
|
|
|
59
|
|
|
60
|
|
|
61
|
|
|
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
|
|
|
73
|
|
|
74
|
|
|
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
|
|
|
90
|
|
|
91
|
|
|
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
|
|
|
114
|
|
|
115
|
|
|
116
|
|
|
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
|
|
|
131
|
|
|
132
|
|
|
133
|
|
|
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
|
|
|
196
|
|
|
197
|
|
|
198
|
|
|
199
|
|
|
200
|
|
|
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
|
|
|
236
|
|
|
237
|
|
|
238
|
0
|
public bool IsEmpty
|
239
|
|
{
|
240
|
|
get
|
241
|
|
{
|
242
|
|
return (_currentPageList.Count == 0);
|
243
|
|
}
|
244
|
|
}
|
245
|
|
|
246
|
|
|
247
|
|
|
248
|
|
|
249
|
|
|
250
|
|
|
251
|
|
|
252
|
|
|
253
|
|
|
254
|
|
|
255
|
|
|
256
|
|
|
257
|
|
|
258
|
|
|
259
|
|
|
260
|
|
|
261
|
|
|
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
|
|
|
270
|
|
|
271
|
|
|
272
|
0
|
public int PageIndex
|
273
|
|
{
|
274
|
|
get
|
275
|
|
{
|
276
|
|
return _index;
|
277
|
|
}
|
278
|
|
}
|
279
|
|
|
280
|
|
|
281
|
|
|
282
|
|
|
283
|
|
public bool IsPreviousPageAvailable
|
284
|
|
{
|
285
|
25
|
get
|
286
|
|
{
|
287
|
25
|
return (_prevPageList.Count > 0);
|
288
|
|
}
|
289
|
|
}
|
290
|
|
|
291
|
|
|
292
|
|
|
293
|
|
|
294
|
0
|
public bool IsFirstPage
|
295
|
|
{
|
296
|
|
get
|
297
|
|
{
|
298
|
|
return (_index == 0);
|
299
|
|
}
|
300
|
|
}
|
301
|
|
|
302
|
|
|
303
|
|
|
304
|
|
|
305
|
|
|
306
|
6
|
public void GotoPage(int pageIndex)
|
307
|
|
{
|
308
|
6
|
SafePageTo(pageIndex);
|
309
|
|
}
|
310
|
|
|
311
|
|
|
312
|
|
|
313
|
|
|
314
|
0
|
public int PageSize
|
315
|
|
{
|
316
|
|
get
|
317
|
|
{
|
318
|
|
return _pageSize;
|
319
|
|
}
|
320
|
|
}
|
321
|
|
|
322
|
|
|
323
|
|
|
324
|
|
|
325
|
|
|
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
|
|
|
341
|
|
|
342
|
|
|
343
|
0
|
public bool IsMiddlePage
|
344
|
|
{
|
345
|
|
get
|
346
|
|
{
|
347
|
|
return !(this.IsFirstPage || this.IsLastPage);
|
348
|
|
|
349
|
|
}
|
350
|
|
}
|
351
|
|
|
352
|
|
|
353
|
|
|
354
|
|
|
355
|
|
|
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
|
|
|
371
|
|
|
372
|
|
|
373
|
|
public bool IsNextPageAvailable
|
374
|
|
{
|
375
|
23
|
get
|
376
|
|
{
|
377
|
23
|
return (_nextPageList.Count > 0);
|
378
|
|
}
|
379
|
|
}
|
380
|
|
|
381
|
|
|
382
|
|
|
383
|
|
|
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
|
|
|
397
|
|
|
398
|
|
|
399
|
0
|
public bool IsReadOnly
|
400
|
|
{
|
401
|
|
get
|
402
|
|
{
|
403
|
|
return _currentPageList.IsReadOnly;
|
404
|
|
}
|
405
|
|
}
|
406
|
|
|
407
|
|
|
408
|
|
|
409
|
|
|
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
|
|
|
423
|
|
|
424
|
|
|
425
|
|
|
426
|
0
|
public void RemoveAt(int index)
|
427
|
|
{
|
428
|
|
_currentPageList.RemoveAt(index);
|
429
|
|
}
|
430
|
|
|
431
|
|
|
432
|
|
|
433
|
|
|
434
|
|
|
435
|
|
|
436
|
0
|
public void Insert(int index, object value)
|
437
|
|
{
|
438
|
|
_currentPageList.Insert(index, value);
|
439
|
|
}
|
440
|
|
|
441
|
|
|
442
|
|
|
443
|
|
|
444
|
|
|
445
|
0
|
public void Remove(object value)
|
446
|
|
{
|
447
|
|
_currentPageList.Remove(value);
|
448
|
|
}
|
449
|
|
|
450
|
|
|
451
|
|
|
452
|
|
|
453
|
|
|
454
|
|
|
455
|
0
|
public bool Contains(object value)
|
456
|
|
{
|
457
|
|
return _currentPageList.Contains(value);
|
458
|
|
}
|
459
|
|
|
460
|
|
|
461
|
|
|
462
|
|
|
463
|
0
|
public void Clear()
|
464
|
|
{
|
465
|
|
_currentPageList.Clear();
|
466
|
|
}
|
467
|
|
|
468
|
|
|
469
|
|
|
470
|
|
|
471
|
|
|
472
|
|
|
473
|
0
|
public int IndexOf(object value)
|
474
|
|
{
|
475
|
|
return _currentPageList.IndexOf(value);
|
476
|
|
}
|
477
|
|
|
478
|
|
|
479
|
|
|
480
|
|
|
481
|
|
|
482
|
|
|
483
|
0
|
public int Add(object value)
|
484
|
|
{
|
485
|
|
return _currentPageList.Add(value);
|
486
|
|
}
|
487
|
|
|
488
|
|
|
489
|
|
|
490
|
|
|
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
|
|
|
504
|
|
|
505
|
|
|
506
|
0
|
public bool IsSynchronized
|
507
|
|
{
|
508
|
|
get
|
509
|
|
{
|
510
|
|
return _currentPageList.IsSynchronized;
|
511
|
|
}
|
512
|
|
}
|
513
|
|
|
514
|
|
|
515
|
|
|
516
|
|
|
517
|
|
public int Count
|
518
|
|
{
|
519
|
21
|
get
|
520
|
|
{
|
521
|
21
|
return _currentPageList.Count;
|
522
|
|
}
|
523
|
|
}
|
524
|
|
|
525
|
|
|
526
|
|
|
527
|
|
|
528
|
|
|
529
|
|
|
530
|
0
|
public void CopyTo(Array array, int index)
|
531
|
|
{
|
532
|
|
_currentPageList.CopyTo(array, index);
|
533
|
|
}
|
534
|
|
|
535
|
|
|
536
|
|
|
537
|
|
|
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
|
|
|
551
|
|
|
552
|
|
|
553
|
|
|
554
|
0
|
public IEnumerator GetEnumerator()
|
555
|
|
{
|
556
|
|
return _currentPageList.GetEnumerator();
|
557
|
|
|
558
|
|
}
|
559
|
|
|
560
|
|
#endregion
|
561
|
|
|
562
|
|
#region IEnumerator Members
|
563
|
|
|
564
|
|
|
565
|
|
|
566
|
|
|
567
|
|
|
568
|
0
|
public void Reset()
|
569
|
|
{
|
570
|
|
_currentPageList.GetEnumerator().Reset();
|
571
|
|
}
|
572
|
|
|
573
|
|
|
574
|
|
|
575
|
|
|
576
|
0
|
public object Current
|
577
|
|
{
|
578
|
|
get
|
579
|
|
{
|
580
|
|
return _currentPageList.GetEnumerator().Current;
|
581
|
|
}
|
582
|
|
}
|
583
|
|
|
584
|
|
|
585
|
|
|
586
|
|
|
587
|
|
|
588
|
|
|
589
|
|
|
590
|
|
|
591
|
0
|
public bool MoveNext()
|
592
|
|
{
|
593
|
|
return _currentPageList.GetEnumerator().MoveNext();
|
594
|
|
}
|
595
|
|
|
596
|
|
#endregion
|
597
|
|
}
|
598
|
|
}
|
599
|
|
|