Exemple #1
0
        protected virtual IQueryOver <PageProperties, PageProperties> FilterQuery(IQueryOver <PageProperties, PageProperties> query,
                                                                                  PagesFilter request, Junction hasSeoDisjunction)
        {
            PageProperties alias = null;

            if (!request.IncludeArchived)
            {
                query = query.Where(() => !alias.IsArchived);
            }

            if (request.OnlyMasterPages)
            {
                query = query.Where(() => alias.IsMasterPage);
            }
            else if (!request.IncludeMasterPages)
            {
                query = query.Where(() => !alias.IsMasterPage);
            }

            if (!string.IsNullOrWhiteSpace(request.SearchQuery))
            {
                var searchQuery = string.Format("%{0}%", request.SearchQuery);
                query = query.Where(Restrictions.Disjunction()
                                    .Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.Title), searchQuery))
                                    .Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.PageUrl), searchQuery))
                                    .Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.MetaTitle), searchQuery))
                                    .Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.MetaDescription), searchQuery))
                                    .Add(Restrictions.InsensitiveLike(Projections.Property(() => alias.MetaKeywords), searchQuery)));
            }

            if (request.CategoryId.HasValue)
            {
                query = query.Where(Restrictions.Eq(Projections.Property(() => alias.Category.Id), request.CategoryId.Value));
            }

            if (request.LanguageId.HasValue)
            {
                if (request.LanguageId.Value.HasDefaultValue())
                {
                    query = query.Where(Restrictions.IsNull(Projections.Property(() => alias.Language.Id)));
                }
                else
                {
                    query = query.Where(Restrictions.Eq(Projections.Property(() => alias.Language.Id), request.LanguageId.Value));
                }
            }

            if (request.Tags != null)
            {
                foreach (var tagKeyValue in request.Tags)
                {
                    var id = tagKeyValue.Key.ToGuidOrDefault();
                    query = query.WithSubquery.WhereExists(QueryOver.Of <PageTag>().Where(tag => tag.Tag.Id == id && tag.Page.Id == alias.Id).Select(tag => 1));
                }
            }

            if (request.Status.HasValue)
            {
                if (request.Status.Value == PageStatusFilterType.OnlyPublished)
                {
                    query = query.Where(() => alias.Status == PageStatus.Published);
                }
                else if (request.Status.Value == PageStatusFilterType.OnlyUnpublished)
                {
                    query = query.Where(() => alias.Status != PageStatus.Published);
                }
                else if (request.Status.Value == PageStatusFilterType.ContainingUnpublishedContents)
                {
                    const ContentStatus draft        = ContentStatus.Draft;
                    Root.Models.Content contentAlias = null;
                    var subQuery = QueryOver.Of <PageContent>()
                                   .JoinAlias(p => p.Content, () => contentAlias)
                                   .Where(pageContent => pageContent.Page.Id == alias.Id)
                                   .And(() => contentAlias.Status == draft)
                                   .And(() => !contentAlias.IsDeleted)
                                   .Select(pageContent => 1);

                    query = query.WithSubquery.WhereExists(subQuery);
                }
            }

            if (request.SeoStatus.HasValue)
            {
                var subQuery = QueryOver.Of <SitemapNode>()
                               .Where(x => x.Page.Id == alias.Id || x.UrlHash == alias.PageUrlHash)
                               .And(x => !x.IsDeleted)
                               .JoinQueryOver(s => s.Sitemap)
                               .And(x => !x.IsDeleted)
                               .Select(s => 1);

                if (request.SeoStatus.Value == SeoStatusFilterType.HasSeo)
                {
                    // NOT(seo disjunction) AND EXISTS(subquery)
                    query = query
                            .Where(Restrictions.Not(hasSeoDisjunction))
                            .WithSubquery.WhereExists(subQuery);
                }
                else
                {
                    // seo disjunction OR NOT EXISTS(subquery)
                    var disjunction = hasSeoDisjunction
                                      .DeepClone()
                                      .Add(Subqueries.WhereNotExists(subQuery));
                    query = query.Where(disjunction);
                }
            }

            if (!string.IsNullOrWhiteSpace(request.Layout))
            {
                Guid id;
                var  length = request.Layout.Length - 2;
                if (request.Layout.StartsWith("m-") && Guid.TryParse(request.Layout.Substring(2, length), out id))
                {
                    query = query.Where(() => alias.MasterPage.Id == id);
                }

                if (request.Layout.StartsWith("l-") && Guid.TryParse(request.Layout.Substring(2, length), out id))
                {
                    query = query.Where(() => alias.Layout.Id == id);
                }
            }

            return(query);
        }