From 582f921882237c06d229c1ed4d5f1534ddaf78fd Mon Sep 17 00:00:00 2001 From: Nathan Heskew Date: Tue, 9 Nov 2010 11:17:24 -0800 Subject: [PATCH] Adding a Count() to the DefaultContentQuery Also pulling the version option restrictions being applied in Slice(int skip, int count) to an ICriteria extension method in the same file to be used by Count() --HG-- branch : dev --- .../ContentManagement/DefaultContentQuery.cs | 68 +++++++++++-------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/src/Orchard/ContentManagement/DefaultContentQuery.cs b/src/Orchard/ContentManagement/DefaultContentQuery.cs index e514c86d2..94411e2bb 100644 --- a/src/Orchard/ContentManagement/DefaultContentQuery.cs +++ b/src/Orchard/ContentManagement/DefaultContentQuery.cs @@ -124,32 +124,12 @@ namespace Orchard.ContentManagement { } } - private IEnumerable Slice(int skip, int count) { var criteria = BindItemVersionCriteria(); - if (_versionOptions == null) { - criteria.Add(Restrictions.Eq("Published", true)); - } - else if (_versionOptions.IsPublished) { - criteria.Add(Restrictions.Eq("Published", true)); - } - else if (_versionOptions.IsLatest) { - criteria.Add(Restrictions.Eq("Latest", true)); - } - else if (_versionOptions.IsDraft) { - criteria.Add(Restrictions.And( - Restrictions.Eq("Latest", true), - Restrictions.Eq("Published", false))); - } - else if (_versionOptions.IsAllVersions) { - // no-op... all versions will be returned by default - } - else { - throw new ApplicationException("Invalid VersionOptions for content query"); - } + + criteria.ApplyVersionOptionsRestrictions(_versionOptions); // TODO: put 'removed false' filter in place - if (skip != 0) { criteria = criteria.SetFirstResult(skip); } @@ -162,6 +142,15 @@ namespace Orchard.ContentManagement { .ToReadOnlyCollection(); } + int Count() { + var criteria = (ICriteria)BindItemVersionCriteria().Clone(); + criteria.ClearOrders(); + + criteria.ApplyVersionOptionsRestrictions(_versionOptions); + + return criteria.SetProjection(Projections.RowCount()).UniqueResult(); + } + IContentQuery IContentQuery.ForPart() { return new ContentQuery(this); } @@ -195,16 +184,14 @@ namespace Orchard.ContentManagement { return _query.Slice(0, 0).AsPart(); } - int IContentQuery.Count() { - var criteria = (ICriteria)_query.BindItemVersionCriteria().Clone(); - criteria.ClearOrders(); - return criteria.SetProjection( Projections.RowCount() ).UniqueResult(); - } - IEnumerable IContentQuery.Slice(int skip, int count) { return _query.Slice(skip, count).AsPart(); } + int IContentQuery.Count() { + return _query.Count(); + } + IContentQuery IContentQuery.Join() { _query.Where(); return new ContentQuery(_query); @@ -256,4 +243,29 @@ namespace Orchard.ContentManagement { } } + + internal static class CriteriaExtensions { + internal static void ApplyVersionOptionsRestrictions(this ICriteria criteria, VersionOptions versionOptions) { + if (versionOptions == null) { + criteria.Add(Restrictions.Eq("Published", true)); + } + else if (versionOptions.IsPublished) { + criteria.Add(Restrictions.Eq("Published", true)); + } + else if (versionOptions.IsLatest) { + criteria.Add(Restrictions.Eq("Latest", true)); + } + else if (versionOptions.IsDraft) { + criteria.Add(Restrictions.And( + Restrictions.Eq("Latest", true), + Restrictions.Eq("Published", false))); + } + else if (versionOptions.IsAllVersions) { + // no-op... all versions will be returned by default + } + else { + throw new ApplicationException("Invalid VersionOptions for content query"); + } + } + } } \ No newline at end of file