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
;
20 using Castle
.ActiveRecord
;
21 using Castle
.ActiveRecord
.Framework
;
24 using NHibernate
.Expressions
;
27 /// A paginable criteria.
28 /// Mimics the <see cref="ActiveRecordMediator.FindAll(Type)"/> interface.
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
;
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
);
67 cachedCriteria
= session
.CreateCriteria(targetType
);
69 if (criterions
!= null)
71 foreach (ICriterion queryCriteria
in criterions
)
73 cachedCriteria
.Add(queryCriteria
);
80 foreach (Order order
in orders
)
82 cachedCriteria
.AddOrder(order
);
87 return cachedCriteria
;
91 /// Implementors should execute a query
92 /// to return the record count
95 /// This needs a performance boost. Couldn't think of a better
96 /// way of get the count.
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
;
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
);
138 criteria
.SetFirstResult(pageSize
* (currentPage
-1));
139 criteria
.SetMaxResults(pageSize
);
142 return criteria
.List();
146 holder
.ReleaseSession(session
);