Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.ActiveRecordSupport / Pagination / ARPaginableCriteria.cs
blobed8f581d8b936cfcd7b7ab55171fa4d008b4799a
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;
20 using Castle.ActiveRecord;
21 using Castle.ActiveRecord.Framework;
23 using NHibernate;
24 using NHibernate.Expressions;
26 /// <summary>
27 /// A paginable criteria.
28 /// Mimics the <see cref="ActiveRecordMediator.FindAll(Type)"/> interface.
29 /// </summary>
30 public class ARPaginableCriteria : IARPaginableDataSource
32 private Type targetType;
33 private ICriterion[] criterions;
34 private Order[] orders;
35 private DetachedCriteria detachedCriteria;
37 private ICriteria cachedCriteria;
39 private int pageSize, currentPage;
41 public ARPaginableCriteria(Type targetType, Order[] orders, params ICriterion[] criterions)
43 this.targetType = targetType;
44 this.orders = orders;
45 this.criterions = criterions;
48 public ARPaginableCriteria(Type targetType, params ICriterion[] criterions) : this(targetType, null, criterions) { }
50 public ARPaginableCriteria(Type targetType, params Order[] orders) : this(targetType, orders, null) { }
52 public ARPaginableCriteria(Type targetType, DetachedCriteria detachedCriteria) : this(targetType, null, (ICriterion[])null)
54 this.detachedCriteria = detachedCriteria;
57 protected virtual ICriteria BuildCriteria(ISession session)
59 if (cachedCriteria == null)
61 if (detachedCriteria != null)
63 cachedCriteria = detachedCriteria.GetExecutableCriteria(session);
65 else
67 cachedCriteria = session.CreateCriteria(targetType);
69 if (criterions != null)
71 foreach (ICriterion queryCriteria in criterions)
73 cachedCriteria.Add(queryCriteria);
78 if (orders != null)
80 foreach (Order order in orders)
82 cachedCriteria.AddOrder(order);
87 return cachedCriteria;
90 /// <summary>
91 /// Implementors should execute a query
92 /// to return the record count
93 /// </summary>
94 /// <remarks>
95 /// This needs a performance boost. Couldn't think of a better
96 /// way of get the count.
97 /// </remarks>
98 public virtual int ObtainCount()
100 ISessionFactoryHolder holder = ActiveRecordMediator.GetSessionFactoryHolder();
101 ISession session = holder.CreateSession(targetType);
105 ICriteria criteria = BuildCriteria(session);
107 return criteria.List().Count;
109 finally
111 holder.ReleaseSession(session);
115 public IEnumerable Paginate(int pageSize, int currentPage)
117 this.pageSize = pageSize;
118 this.currentPage = currentPage;
119 return InternalExecute(false);
122 public IEnumerable ListAll()
124 return InternalExecute(true);
127 private IEnumerable InternalExecute(bool skipPagination)
129 ISessionFactoryHolder holder = ActiveRecordMediator.GetSessionFactoryHolder();
130 ISession session = holder.CreateSession(targetType);
134 ICriteria criteria = BuildCriteria(session);
136 if (!skipPagination)
138 criteria.SetFirstResult(pageSize * (currentPage-1));
139 criteria.SetMaxResults(pageSize);
142 return criteria.List();
144 finally
146 holder.ReleaseSession(session);