Skip to content

Commit

Permalink
#1120 : Temporary fix for Predicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellwest committed Jul 7, 2019
1 parent a3edd99 commit c7b8700
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
32 changes: 23 additions & 9 deletions Spe/Commands/Data/Search/BaseSearchCommand.cs
Expand Up @@ -20,11 +20,11 @@ namespace Spe.Commands.Data.Search
{
public class BaseSearchCommand : BaseCommand
{
public Expression<Func<SearchResultItem, bool>> ProcessCriteria(SearchCriteria[] criterias, SearchOperation operation)
public Expression<Func<T, bool>> ProcessCriteria<T>(T queryableType, SearchCriteria[] criterias, SearchOperation operation) where T : ISearchResult
{
var predicate = operation == SearchOperation.Or
? PredicateBuilder.False<SearchResultItem>()
: PredicateBuilder.True<SearchResultItem>();
? PredicateBuilder.False<T>()
: PredicateBuilder.True<T>();

if (criterias == null) return predicate;

Expand Down Expand Up @@ -79,8 +79,8 @@ public class BaseSearchCommand : BaseCommand

var valuesAny = ObjectToStringArray(criteria.Value);
predicate = criteria.Invert
? predicate.AddPredicate(valuesAny.Aggregate(PredicateBuilder.True<SearchResultItem>(), (current, keyword) => current.Or(c => !((string)c[(ObjectIndexerKey)criteria.Field]).Contains(keyword))))
: predicate.AddPredicate(valuesAny.Aggregate(PredicateBuilder.True<SearchResultItem>(), (current, keyword) => current.Or(c => ((string)c[(ObjectIndexerKey)criteria.Field]).Contains(keyword))));
? predicate.AddPredicate(valuesAny.Aggregate(PredicateBuilder.True<T>(), (current, keyword) => current.Or(c => !((string)c[(ObjectIndexerKey)criteria.Field]).Contains(keyword))))
: predicate.AddPredicate(valuesAny.Aggregate(PredicateBuilder.True<T>(), (current, keyword) => current.Or(c => ((string)c[(ObjectIndexerKey)criteria.Field]).Contains(keyword))));
break;
case FilterType.ContainsAll:
if (comparer == StringComparison.OrdinalIgnoreCase && criteria.CaseSensitive.HasValue)
Expand All @@ -90,8 +90,8 @@ public class BaseSearchCommand : BaseCommand

var valuesAll = ObjectToStringArray(criteria.Value);
predicate = criteria.Invert
? predicate.AddPredicate(valuesAll.Aggregate(PredicateBuilder.True<SearchResultItem>(), (current, keyword) => current.And(c => !((string)c[(ObjectIndexerKey)criteria.Field]).Contains(keyword))))
: predicate.AddPredicate(valuesAll.Aggregate(PredicateBuilder.True<SearchResultItem>(), (current, keyword) => current.And(c => ((string)c[(ObjectIndexerKey)criteria.Field]).Contains(keyword))));
? predicate.AddPredicate(valuesAll.Aggregate(PredicateBuilder.True<T>(), (current, keyword) => current.And(c => !((string)c[(ObjectIndexerKey)criteria.Field]).Contains(keyword))))
: predicate.AddPredicate(valuesAll.Aggregate(PredicateBuilder.True<T>(), (current, keyword) => current.And(c => ((string)c[(ObjectIndexerKey)criteria.Field]).Contains(keyword))));
break;
case FilterType.EndsWith:
var endsWith = ObjectToString(criteria.Value);
Expand Down Expand Up @@ -138,7 +138,7 @@ public class BaseSearchCommand : BaseCommand
return predicate;
}

private static Expression<Func<SearchResultItem, bool>> GetRangeExpression(Expression<Func<SearchResultItem, bool>> predicate, SearchCriteria criteria, SearchOperation operation)
private static Expression<Func<T, bool>> GetRangeExpression<T>(Expression<Func<T, bool>> predicate, SearchCriteria criteria, SearchOperation operation) where T : ISearchResult
{
var inclusion = (criteria.Filter == FilterType.InclusiveRange)
? Inclusion.Both
Expand Down Expand Up @@ -318,7 +318,7 @@ private static IEnumerable<string> ObjectToStringArray(object value)
return query;
}

internal static IQueryable<T> GetQueryable<T>(T queryableType, IProviderSearchContext searchContext) where T : ISearchResult
internal static IQueryable<T> GetQueryable<T>(T queryableObject, IProviderSearchContext searchContext) where T : ISearchResult
{
return searchContext.GetQueryable<T>();
}
Expand All @@ -333,6 +333,20 @@ private static IEnumerable<string> ObjectToStringArray(object value)
return query.Where(predicate);
}

internal static Expression<Func<T, bool>> GetPredicateBuilder<T>(T queryableObject, bool shouldOr) where T : ISearchResult
{
return shouldOr
? PredicateBuilder.False<T>()
: PredicateBuilder.True<T>();
}

internal static Expression<Func<T, bool>> GetPredicateAndOr<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, bool>> joinedPredicate, bool shouldOr) where T : ISearchResult
{
return shouldOr
? predicate.Or(joinedPredicate)
: predicate.And(joinedPredicate);
}

internal static IQueryable<T> OrderIfSupported<T>(IQueryable<T> query, string orderBy) where T : ISearchResult
{
return query.OrderBy(orderBy);
Expand Down
3 changes: 2 additions & 1 deletion Spe/Commands/Data/Search/FindItemCommand.cs
Expand Up @@ -32,6 +32,7 @@ public static string[] Indexes

[Parameter(ParameterSetName = "Dynamic")]
[Parameter(ParameterSetName = "ScopeQuery")]
[Parameter(ParameterSetName = "Criteria")]
public Type QueryType { get; set; }

[Parameter(ParameterSetName = "Predicate")]
Expand Down Expand Up @@ -74,7 +75,7 @@ protected override void EndProcessing()

if (Criteria != null)
{
var criteriaPredicate = ProcessCriteria(Criteria, SearchOperation.And);
var criteriaPredicate = ProcessCriteria(objType, Criteria, SearchOperation.And);
query = WherePredicate(query, criteriaPredicate);
}

Expand Down
15 changes: 8 additions & 7 deletions Spe/Commands/Data/Search/NewSearchPredicateCommand.cs
Expand Up @@ -22,31 +22,32 @@ public class NewSearchPredicateCommand : BaseSearchCommand
[Parameter] public SearchOperation Operation { get; set; } = SearchOperation.And;

protected override void EndProcessing()
{
{
var queryableType = typeof(SearchResultItem);
var objType = (dynamic)Activator.CreateInstance(queryableType);

if (First != null && Second != null)
{
var shouldOr = Operation == SearchOperation.Or;
var predicate = shouldOr
? PredicateBuilder.False<SearchResultItem>()
: PredicateBuilder.True<SearchResultItem>();
var predicate = GetPredicateBuilder(objType, shouldOr);

if (shouldOr)
{
var joinedPredicate = First.Or(Second);
predicate = predicate.Or(joinedPredicate);
predicate = GetPredicateAndOr(predicate, joinedPredicate, true);
WriteObject(predicate, true);
}
else
{
var joinedPredicate = First.And(Second);
predicate = predicate.And(joinedPredicate);
predicate = GetPredicateAndOr(predicate, joinedPredicate, false);
WriteObject(predicate, true);
}
}

if (Criteria != null)
{
var predicate = ProcessCriteria(Criteria, Operation);
var predicate = ProcessCriteria(objType, Criteria, Operation);
if (predicate != null)
{
WriteObject(predicate, true);
Expand Down
4 changes: 2 additions & 2 deletions Spe/Core/Extensions/ExpressionExtensions.cs
@@ -1,14 +1,14 @@
using System;
using System.Linq.Expressions;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Linq.Utilities;
using Sitecore.ContentSearch.SearchTypes;
using Spe.Commands.Data.Search;

namespace Spe.Core.Extensions
{
public static class ExpressionExtensions
{
public static Expression<Func<SearchResultItem, bool>> AddPredicate(this Expression<Func<SearchResultItem, bool>> first, Expression<Func<SearchResultItem, bool>> second, SearchOperation operation = SearchOperation.And)
public static Expression<Func<T, bool>> AddPredicate<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second, SearchOperation operation = SearchOperation.And) where T : ISearchResult
{
switch (operation)
{
Expand Down

0 comments on commit c7b8700

Please sign in to comment.