1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.ActiveRecordSupport
.Pagination
18 using System
.Collections
;
19 using System
.Text
.RegularExpressions
;
21 using Castle
.ActiveRecord
.Queries
;
22 using Castle
.MonoRail
.Framework
;
23 using Castle
.MonoRail
.Framework
.Helpers
;
25 using NHibernate
.Criterion
;
30 public class ARPaginationHelper
: AbstractHelper
33 /// Paginates using an <see cref="ARPaginableSimpleQuery"/>
35 public static IPaginatedPage
CreatePagination(int pageSize
, Type targetType
, string hql
, params object[] parameters
)
37 IARPaginableDataSource criteria
= new ARPaginableSimpleQuery(targetType
, hql
, parameters
);
38 return CreatePagination(pageSize
, criteria
);
42 /// Paginates using an <see cref="ARPaginableCriteria"/>
44 public static IPaginatedPage
CreatePagination(int pageSize
, Type targetType
, params Order
[] orders
)
46 return CreatePagination(pageSize
, targetType
, orders
, null);
50 /// Paginates using an <see cref="ARPaginableCriteria"/>
52 public static IPaginatedPage
CreatePagination(int pageSize
, Type targetType
, Order
[] orders
, params ICriterion
[] criterions
)
54 IARPaginableDataSource criteria
= new ARPaginableCriteria(targetType
, orders
, criterions
);
55 return CreatePagination(pageSize
, criteria
);
59 /// Paginates using an <see cref="ARPaginableCriteria"/>
61 public static IPaginatedPage
CreatePagination(int pageSize
, Type targetType
, DetachedCriteria detachedCriteria
)
63 IARPaginableDataSource criteria
= new ARPaginableCriteria(targetType
, detachedCriteria
);
64 return CreatePagination(pageSize
, criteria
);
68 /// Paginates using the specified <see cref="IARPaginableDataSource"/>.
70 public static IPaginatedPage
CreatePagination(int pageSize
, IARPaginableDataSource criteria
)
72 return new ARPager(pageSize
, criteria
, ObtainCurrentPage());
75 private static int ObtainCurrentPage()
77 // Tight coupling that makes difficult to test. Review this!
78 String page
= MonoRailHttpHandlerFactory
.CurrentEngineContext
.Request
.Params
["page"];
79 return page
== null || Regex
.IsMatch(page
, "\\D")
80 ? 1 : Convert
.ToInt32(page
);
87 public class ARPager
: AbstractPage
89 private IEnumerable enumerable
;
91 public ARPager(int pageSize
, IARPaginableDataSource source
, int currentPage
)
93 int count
= source
.ObtainCount();
94 int startIndex
= (pageSize
* currentPage
) - pageSize
;
95 int endIndex
= Math
.Min(startIndex
+ pageSize
, count
);
97 enumerable
= source
.Paginate(pageSize
, currentPage
);
99 CalculatePaginationInfo(startIndex
, endIndex
, count
, pageSize
, currentPage
);
102 public override IEnumerator
GetEnumerator()
104 return enumerable
.GetEnumerator();