/// <summary> /// Filters the query. /// </summary> /// <param name="query">The query.</param> /// <param name="request">The request.</param> /// <param name="hasnotSeoDisjunction">The has seo disjunction.</param> /// <returns> /// Query, filtered with specified filter parameters /// </returns> protected override NHibernate.IQueryOver<PagesView, PagesView> FilterQuery(NHibernate.IQueryOver<PagesView, PagesView> query, PagesFilter request, Junction hasnotSeoDisjunction) { query = base.FilterQuery(query, request, hasnotSeoDisjunction); var filter = request as UntranslatedPagesFilter; if (filter != null) { PageProperties alias = null; // Exclude from results if (filter.ExistingItemsArray.Any()) { query = query.Where(Restrictions.Not(Restrictions.In(Projections.Property(() => alias.Id), filter.ExistingItemsArray))); } // Excluded language id if (filter.ExcludedLanguageId.HasValue) { var languageProxy = repository.AsProxy<Language>(filter.ExcludedLanguageId.Value); query = query.Where(() => (alias.Language != languageProxy || alias.Language == null)); } if (filter.ExcplicitlyIncludedPagesArray.Any()) { // Include to results explicitly included or untranslated query = query.Where(Restrictions.Disjunction() .Add(Restrictions.In(Projections.Property(() => alias.Id), filter.ExcplicitlyIncludedPagesArray)) .Add(Restrictions.IsNull(Projections.Property(() => alias.LanguageGroupIdentifier)))); } else { // Only untranslated query = query.Where(Restrictions.IsNull(Projections.Property(() => alias.LanguageGroupIdentifier))); } } return query; }
protected virtual IQueryOver<PagesView, PagesView> FilterQuery(IQueryOver<PagesView, PagesView> query, PagesFilter request, Junction hasnotSeoDisjunction) { 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.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.Categories != null) { var categories = request.Categories.Select(c => new Guid(c.Key)).Distinct().ToList(); foreach (var category in categories) { var childCategories = categoryService.GetChildCategoriesIds(category).ToArray(); query = query.WithSubquery.WhereExists(QueryOver.Of<PageCategory>().Where(cat => !cat.IsDeleted && cat.Page.Id == alias.Id).WhereRestrictionOn(cat => cat.Category.Id).IsIn(childCategories).Select(cat => 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) { if (request.SeoStatus.Value == SeoStatusFilterType.HasNotSeo) { query = query.Where(hasnotSeoDisjunction); } else { query = query.Where(Restrictions.Not(hasnotSeoDisjunction)); } } 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); } } if (request.ContentId.HasValue) { Root.Models.Content contentAlias = null; ChildContent childContentAlias = null; HtmlContent htmlContentAlias = null; PageContent pageContentAlias = null; var htmlChildContentSubQuery = QueryOver.Of(() => htmlContentAlias) .JoinAlias(h => h.ChildContents, () => childContentAlias) .Where(() => htmlContentAlias.Id == contentAlias.Id) .And(() => childContentAlias.Child.Id == request.ContentId.Value) .Select(pageContent => 1); var pageContentSubQuery = QueryOver.Of(() => pageContentAlias) .JoinAlias(() => pageContentAlias.Content, () => contentAlias) .And(() => pageContentAlias.Page.Id == alias.Id) .And(() => !contentAlias.IsDeleted) .And(() => !pageContentAlias.IsDeleted) .And(Restrictions.Or( Restrictions.Where(() => contentAlias.Id == request.ContentId.Value), Subqueries.WhereExists(htmlChildContentSubQuery) )) .Select(pageContent => 1); query = query.WithSubquery.WhereExists(pageContentSubQuery); } return query; }
/// <summary> /// Translate the criteria for NHibernate /// </summary> /// <exception cref="ArgumentException"></exception> public void Execute() { //Add order clauses directly to the ICriteria foreach (var clause in _query.SortOrder) _criteria.AddOrder(new Order(clause.PropertyName, clause.Order == OrderDirections.Ascending)); foreach (var myCriterion in _query.Criteria) { ICriterion criterion = null; switch (myCriterion.Operator) { #region property criteria case CriteriaOperators.Equal: criterion = Restrictions.Eq(myCriterion.PropertyName, myCriterion.Value); break; case CriteriaOperators.NotEqual: criterion = Restrictions.Not(Restrictions.Eq(myCriterion.PropertyName, myCriterion.Value)); break; case CriteriaOperators.GreaterThan: criterion = Restrictions.Gt(myCriterion.PropertyName, myCriterion.Value); break; case CriteriaOperators.GreaterThanOrEqual: criterion = Restrictions.Ge(myCriterion.PropertyName, myCriterion.Value); break; case CriteriaOperators.LesserThan: criterion = Restrictions.Lt(myCriterion.PropertyName, myCriterion.Value); break; case CriteriaOperators.LesserThanOrEqual: criterion = Restrictions.Le(myCriterion.PropertyName, myCriterion.Value); break; case CriteriaOperators.Like: criterion = Restrictions.InsensitiveLike(myCriterion.PropertyName, myCriterion.Value); break; case CriteriaOperators.NotLike: criterion = Restrictions.Not(Restrictions.InsensitiveLike(myCriterion.PropertyName, myCriterion.Value)); break; case CriteriaOperators.IsNull: criterion = Restrictions.IsNull(myCriterion.PropertyName); break; case CriteriaOperators.IsNotNull: criterion = Restrictions.IsNotNull(myCriterion.PropertyName); break; case CriteriaOperators.In: criterion = Restrictions.In(myCriterion.PropertyName, myCriterion.Values); break; case CriteriaOperators.NotIn: criterion = Restrictions.Not(Restrictions.In(myCriterion.PropertyName, myCriterion.Values)); break; #endregion #region logical criteria // we will create a new _junction, nesting the previous _junction group, if necessary case CriteriaOperators.And: if (_junction != null) { var prevJunction = _junction; _junction = Restrictions.Conjunction(); _junction.Add(prevJunction); } else { _junction = Restrictions.Conjunction(); } break; case CriteriaOperators.Or: if (_junction != null) { var prevJunction = _junction; _junction = Restrictions.Disjunction(); _junction.Add(prevJunction); } else { _junction = Restrictions.Disjunction(); } break; #endregion default: throw new ArgumentException("Operator not supported in NHibernate Provider"); } if (myCriterion.Operator == CriteriaOperators.And || myCriterion.Operator == CriteriaOperators.Or) continue; if (_junction == null) _criteria.Add(criterion); else _junction.Add(criterion); } if (_junction != null) _criteria.Add(_junction); }
/// <summary> /// Builds the criteria. /// </summary> /// <param name="criteria">The criteria.</param> /// <param name="dependencyCriterion">The dependency criterion.</param> public override void BuildCriteria(DetachedCriteria criteria, Junction dependencyCriterion) { if (criteria == null) { ThrowHelper.ThrowArgumentNullException("criteria"); } if (dependencyCriterion != null) { dependencyCriterion.Add(mCriterion); } else { criteria.Add(this.mCriterion); } }