Updates Castle to the latest NHibernate Trunk libs. This required namespace changes...
[castle.git] / MonoRail / Castle.MonoRail.ActiveRecordSupport / Pagination / ARPagination.cs
bloba66fe8fea1a75f92c7b8e6f4c817f62708bfe891
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
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;
27 /// <summary>
28 /// Pendent
29 /// </summary>
30 public class ARPaginationHelper : AbstractHelper
32 /// <summary>
33 /// Paginates using an <see cref="ARPaginableSimpleQuery"/>
34 /// </summary>
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);
41 /// <summary>
42 /// Paginates using an <see cref="ARPaginableCriteria"/>
43 /// </summary>
44 public static IPaginatedPage CreatePagination(int pageSize, Type targetType, params Order[] orders)
46 return CreatePagination(pageSize, targetType, orders, null);
49 /// <summary>
50 /// Paginates using an <see cref="ARPaginableCriteria"/>
51 /// </summary>
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);
58 /// <summary>
59 /// Paginates using an <see cref="ARPaginableCriteria"/>
60 /// </summary>
61 public static IPaginatedPage CreatePagination(int pageSize, Type targetType, DetachedCriteria detachedCriteria)
63 IARPaginableDataSource criteria = new ARPaginableCriteria(targetType, detachedCriteria);
64 return CreatePagination(pageSize, criteria);
67 /// <summary>
68 /// Paginates using the specified <see cref="IARPaginableDataSource"/>.
69 /// </summary>
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);
84 /// <summary>
85 /// Pendent
86 /// </summary>
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();