Exemple #1
0
 /// <summary>
 /// Generic WhereAndValues
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="objType"></param>
 /// <param name="query"></param>
 /// <returns></returns>
 private IQueryable <T> WhereAndValues <T>(T objType, IQueryable <T> query)
 {
     SitecoreVersion.V75
     .OrNewer(() =>
              query = query.Where(Where, WhereValues.BaseArray()))
     .ElseWriteWarning(this, "Where", true);
     return(query);
 }
Exemple #2
0
        protected override void EndProcessing()
        {
            string index = string.IsNullOrEmpty(Index) ? "sitecore_master_index" : Index;

            using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext())
            {
                // get all items in medialibrary
                var query = context.GetQueryable <SearchResultItem>();

                if (!string.IsNullOrEmpty(Where))
                {
                    SitecoreVersion.V75.OrNewer(() =>
                    {
                        // ReSharper disable once AccessToModifiedClosure
                        query = query.Where(Where, WhereValues.BaseArray());
                    }).ElseWriteWarning(this, nameof(Where), true);
                }

                if (Criteria != null)
                {
                    var predicate = ProcessCriteria(Criteria, SearchOperation.And);

                    query = query.Where(predicate);
                }

                if (Predicate != null)
                {
                    query = query.Where(Predicate);
                }

                if (ScopeQuery != null)
                {
                    query = ProcessScopeQuery(context, ScopeQuery);
                }

                if (!string.IsNullOrEmpty(OrderBy))
                {
                    SitecoreVersion.V75.OrNewer(() =>
                    {
                        query = OrderIfSupported(query);
                    }).ElseWriteWarning(this, nameof(OrderBy), true);
                }

                WriteObject(FilterByPosition(query), true);
            }
        }
Exemple #3
0
        protected override void EndProcessing()
        {
            string index = string.IsNullOrEmpty(Index) ? "sitecore_master_index" : Index;

            using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext())
            {
                // get all items in medialibrary
                IQueryable <SearchResultItem>[] query = { context.GetQueryable <SearchResultItem>() };

                if (!string.IsNullOrEmpty(Where))
                {
                    SitecoreVersion.V75.OrNewer(() =>
                    {
                        query[0] = query[0].Where(Where, WhereValues.BaseArray());
                    }).ElseWriteWarning(this, nameof(Where), true);
                }
                if (Criteria != null)
                {
                    foreach (var filter in Criteria)
                    {
                        var criteria = filter;
                        if (criteria.Value == null)
                        {
                            continue;
                        }

                        var comparer = criteria.CaseSensitive.HasValue && criteria.CaseSensitive.Value
                            ? StringComparison.Ordinal
                            : StringComparison.OrdinalIgnoreCase;
                        switch (criteria.Filter)
                        {
                        case (FilterType.DescendantOf):
                            var ancestorId = string.Empty;
                            if (criteria.Value is Item)
                            {
                                ancestorId = ((Item)criteria.Value).ID.ToShortID().ToString();
                            }
                            if (ID.IsID(criteria.Value.ToString()))
                            {
                                ancestorId = ((ID)criteria.Value).ToShortID().ToString().ToLower();
                            }
                            if (string.IsNullOrEmpty(ancestorId))
                            {
                                WriteError(typeof(ArgumentException), "The root for DescendantOf criteria has to be an Item or an ID.", ErrorIds.InvalidOperation, ErrorCategory.InvalidArgument, criteria.Value);
                                return;
                            }
                            query[0] = criteria.Invert
                                    ? query[0].Where(i => !i["_path"].Contains(ancestorId))
                                    : query[0].Where(i => i["_path"].Contains(ancestorId));
                            break;

                        case (FilterType.StartsWith):
                            query[0] = criteria.Invert
                                    ? query[0].Where(i => !i[criteria.Field].StartsWith(criteria.StringValue, comparer))
                                    : query[0].Where(i => i[criteria.Field].StartsWith(criteria.StringValue, comparer));
                            break;

                        case (FilterType.Contains):
                            if (comparer == StringComparison.OrdinalIgnoreCase && criteria.CaseSensitive.HasValue)
                            {
                                WriteWarning(
                                    "Case insensitiveness is not supported on Contains criteria due to platform limitations.");
                            }
                            query[0] = criteria.Invert
                                    ? query[0].Where(i => !i[criteria.Field].Contains(criteria.StringValue))
                                    : query[0].Where(i => i[criteria.Field].Contains(criteria.StringValue));
                            break;

                        case (FilterType.EndsWith):
                            query[0] = criteria.Invert
                                    ? query[0].Where(i => i[criteria.Field].EndsWith(criteria.StringValue, comparer))
                                    : query[0].Where(i => i[criteria.Field].EndsWith(criteria.StringValue, comparer));
                            break;

                        case (FilterType.Equals):
                            query[0] = criteria.Invert
                                    ? query[0].Where(i => !i[criteria.Field].Equals(criteria.StringValue, comparer))
                                    : query[0].Where(i => i[criteria.Field].Equals(criteria.StringValue, comparer));
                            break;

                        case (FilterType.Fuzzy):
                            query[0] = criteria.Invert
                                    ? query[0].Where(i => !i[criteria.Field].Like(criteria.StringValue))
                                    : query[0].Where(i => i[criteria.Field].Like(criteria.StringValue));
                            break;

                        case (FilterType.InclusiveRange):
                        case (FilterType.ExclusiveRange):
                            if (!(criteria.Value is string[]))
                            {
                                break;
                            }

                            var inclusion = (criteria.Filter == FilterType.InclusiveRange) ? Inclusion.Both : Inclusion.None;

                            var pair  = (object[])criteria.Value;
                            var left  = (pair[0] is DateTime) ? ((DateTime)pair[0]).ToString("yyyyMMdd") : pair[0].ToString();
                            var right = (pair[1] is DateTime) ? ((DateTime)pair[1]).ToString("yyyyMMdd") : pair[1].ToString();
                            query[0] = criteria.Invert
                                    ? query[0].Where(i => !i[criteria.Field].Between(left, right, inclusion))
                                    : query[0].Where(i => i[criteria.Field].Between(left, right, inclusion));
                            break;
                        }
                    }
                }

                if (!string.IsNullOrEmpty(OrderBy))
                {
                    SitecoreVersion.V75.OrNewer(() =>
                    {
                        query[0] = OrderIfSupported(query[0]);
                    }).ElseWriteWarning(this, nameof(OrderBy), true);
                }

                WriteObject(FilterByPosition(query[0]), true);
            }
        }