/// <summary>
        /// Populates a Dynamic List by executing the query it specifies.
        /// </summary>
        /// <param name="dynamicList">The Dynamic List which specifies the query and is to be populated.</param>
        /// <param name="localization">The context Localization.</param>
        public void PopulateDynamicList(DynamicList dynamicList, Localization localization)
        {
            using (new Tracer(dynamicList, localization))
            {
                SimpleBrokerQuery simpleBrokerQuery = dynamicList.GetQuery(localization) as SimpleBrokerQuery;
                if (simpleBrokerQuery == null)
                {
                    throw new DxaException($"Unexpected result from {dynamicList.GetType().Name}.GetQuery: {dynamicList.GetQuery(localization)}");
                }

                BrokerQuery brokerQuery   = new BrokerQuery(simpleBrokerQuery);
                string[]    componentUris = brokerQuery.ExecuteQuery().ToArray();
                Log.Debug($"Broker Query returned {componentUris.Length} results. HasMore={brokerQuery.HasMore}");

                if (componentUris.Length > 0)
                {
                    Type resultType = dynamicList.ResultType;
                    ComponentMetaFactory componentMetaFactory = new ComponentMetaFactory(localization.GetCmUri());
                    dynamicList.QueryResults = componentUris
                                               .Select(c => ModelBuilderPipeline.CreateEntityModel(CreateEntityModelData(componentMetaFactory.GetMeta(c)), resultType, localization))
                                               .ToList();
                }

                dynamicList.HasMore = brokerQuery.HasMore;
            }
        }
        protected InputSortParam BuildSort(SimpleBrokerQuery queryParams)
        {
            if (!string.IsNullOrEmpty(queryParams.Sort) && queryParams.Sort.ToLower() != "none")
            {
                InputSortParam sort = new InputSortParam();
                sort.Order = queryParams.Sort.ToLower().EndsWith("asc") ?
                             SortOrderType.Ascending : SortOrderType.Descending;

                int    idx        = queryParams.Sort.Trim().IndexOf(" ", StringComparison.Ordinal);
                string sortColumn = idx > 0 ? queryParams.Sort.Trim().Substring(0, idx) : queryParams.Sort.Trim();
                switch (sortColumn.ToLower())
                {
                case "title":
                    sort.SortBy = SortFieldType.TITLE;
                    break;

                case "pubdate":
                    sort.SortBy = SortFieldType.LAST_PUBLISH_DATE;
                    break;

                default:
                    sort.SortBy = SortFieldType.CREATION_DATE;
                    break;
                }

                return(sort);
            }
            return(null);
        }
        public IEnumerable <IItem> ExecuteQueryItems(SimpleBrokerQuery queryParams)
        {
            InputItemFilter filter   = BuildFilter(queryParams);
            InputSortParam  sort     = BuildSort(queryParams);
            var             client   = ApiClientFactory.Instance.CreateClient();
            int             pageSize = queryParams.PageSize > 0 ? queryParams.PageSize + 1 : queryParams.PageSize;
            var             results  = client.ExecuteItemQuery(filter, sort, new Pagination
            {
                First = pageSize,
                After = queryParams.Cursor
            }, null, ContentIncludeMode.Exclude, false, null);
            var resultList = results.Edges.Select(edge => edge.Node).ToList();

            if (pageSize == -1)
            {
                // returning all items with pageSize = -1
                Cursor = null;
                return(resultList);
            }
            HasMore = results.Edges.Count > queryParams.PageSize;
            int n = HasMore ? queryParams.PageSize : results.Edges.Count;

            Cursor = n > 0 ? results.Edges[n - 1].Cursor : null;
            return(HasMore ? resultList.GetRange(0, queryParams.PageSize) : resultList);
        }
Пример #4
0
        /// <summary>
        /// Populates a Dynamic List by executing the query it specifies.
        /// </summary>
        /// <param name="dynamicList">The Dynamic List which specifies the query and is to be populated.</param>
        /// <param name="localization">The context Localization.</param>
        public override void PopulateDynamicList(DynamicList dynamicList, Localization localization)
        {
            using (new Tracer(dynamicList, localization))
            {
                SimpleBrokerQuery simpleBrokerQuery = dynamicList.GetQuery(localization) as SimpleBrokerQuery;
                if (simpleBrokerQuery == null)
                {
                    throw new DxaException($"Unexpected result from {dynamicList.GetType().Name}.GetQuery: {dynamicList.GetQuery(localization)}");
                }

                // get our cursor indexer for this list
                var cursors = CursorIndexer.GetCursorIndexer(dynamicList.Id);

                // given our start index into the paged list we need to translate that to a cursor
                int start = simpleBrokerQuery.Start;
                simpleBrokerQuery.Cursor = cursors[start];

                // the cursor retrieved may of came from a different start index so we update start
                int startIndex = cursors.StartIndex;
                simpleBrokerQuery.Start = startIndex;
                dynamicList.Start       = startIndex;

                var cachedDynamicList = SiteConfiguration.CacheProvider.GetOrAdd(
                    $"PopulateDynamicList-{dynamicList.Id}-{simpleBrokerQuery.GetHashCode()}", // key
                    CacheRegions.BrokerQuery,
                    () =>
                {
                    var brokerQuery = new GraphQLQueryProvider();

                    var components = brokerQuery.ExecuteQueryItems(simpleBrokerQuery).ToList();
                    Log.Debug($"Broker Query returned {components.Count} results. HasMore={brokerQuery.HasMore}");

                    if (components.Count > 0)
                    {
                        Type resultType          = dynamicList.ResultType;
                        dynamicList.QueryResults = components
                                                   .Select(
                            c =>
                            ModelBuilderPipeline.CreateEntityModel(
                                CreateEntityModelData((Component)c), resultType,
                                localization))
                                                   .ToList();
                    }

                    dynamicList.HasMore = brokerQuery.HasMore;

                    if (brokerQuery.HasMore)
                    {
                        // update cursor
                        cursors[simpleBrokerQuery.Start + simpleBrokerQuery.PageSize] = brokerQuery.Cursor;
                    }

                    return(dynamicList);
                });

                dynamicList.QueryResults = cachedDynamicList.QueryResults;
                dynamicList.HasMore      = cachedDynamicList.HasMore;
            }
        }
Пример #5
0
        private SortColumn GetSortColumn(SimpleBrokerQuery queryParams)
        {
            //TODO add more options if required
            int    pos  = queryParams.Sort.Trim().IndexOf(" ", StringComparison.Ordinal);
            string sort = pos > 0 ? queryParams.Sort.Trim().Substring(0, pos) : queryParams.Sort.Trim();

            switch (sort.ToLower())
            {
            case "title":
                return(SortParameter.ItemTitle);

            case "pubdate":
                return(SortParameter.ItemLastPublishedDate);

            default:
                //Default is to assume that its a custom metadata date field;
                return(new CustomMetaKeyColumn(queryParams.Sort, MetadataType.DATE));
            }
        }
Пример #6
0
        private Criteria BuildCriteria(SimpleBrokerQuery queryParams)
        {
            List <Criteria> children = new List <Criteria> {
                new ItemTypeCriteria(16)
            };

            if (queryParams.SchemaId > 0)
            {
                children.Add(new ItemSchemaCriteria(queryParams.SchemaId));
            }
            if (queryParams.PublicationId > 0)
            {
                children.Add(new PublicationCriteria(queryParams.PublicationId));
            }
            if (KeywordFilters != null)
            {
                children.AddRange((from taxonomy in KeywordFilters.Keys from keyword in KeywordFilters[taxonomy] select new TaxonomyKeywordCriteria(taxonomy, keyword, true)).Cast <Criteria>());
            }
            return(new AndCriteria(children.ToArray()));
        }
        public IEnumerable <string> ExecuteQuery(SimpleBrokerQuery queryParams)
        {
            InputItemFilter filter  = BuildFilter(queryParams);
            InputSortParam  sort    = BuildSort(queryParams);
            var             client  = ApiClientFactory.Instance.CreateClient();
            var             results = client.ExecuteItemQuery(filter, sort, new Pagination
            {
                First = queryParams.PageSize + 1,
                After = queryParams.Cursor
            }, null, ContentIncludeMode.Exclude, false, null);

            HasMore = results.Edges.Count > queryParams.PageSize;
            int n          = HasMore ? queryParams.PageSize : results.Edges.Count;
            var resultList = new List <string>();

            for (int i = 0; i < n; i++)
            {
                resultList.Add(results.Edges[i].Node.CmUri());
            }
            Cursor = n > 0 ? results.Edges[n - 1].Cursor : null;
            return(resultList);
        }
Пример #8
0
        public IEnumerable <string> ExecuteQuery(SimpleBrokerQuery queryParams)
        {
            Criteria criteria = BuildCriteria(queryParams);

            global::Tridion.ContentDelivery.DynamicContent.Query.Query query = new global::Tridion.ContentDelivery.DynamicContent.Query.Query(criteria);
            if (!string.IsNullOrEmpty(queryParams.Sort) && queryParams.Sort.ToLower() != "none")
            {
                query.AddSorting(GetSortParameter(queryParams));
            }
            if (queryParams.MaxResults > 0)
            {
                query.SetResultFilter(new LimitFilter(queryParams.MaxResults));
            }

            int pageSize = queryParams.PageSize;

            if (pageSize > 0)
            {
                //We set the page size to one more than what we need, to see if there are more pages to come...
                query.SetResultFilter(new PagingFilter(queryParams.Start, pageSize + 1));
            }
            try
            {
                string[] componentIds = query.ExecuteQuery();
                if (componentIds == null)
                {
                    return(new string[0]);
                }

                HasMore = componentIds.Length > pageSize;

                return((pageSize > 0) ? componentIds.Take(pageSize) : componentIds);
            }
            catch (Exception ex)
            {
                throw new DxaException("Error executing Broker Query", ex);
            }
        }
        protected InputItemFilter BuildFilter(SimpleBrokerQuery queryParams)
        {
            InputItemFilter filter = new InputItemFilter
            {
                ItemTypes = new List <FilterItemType> {
                    FilterItemType.COMPONENT
                },
            };

            if (queryParams.SchemaId > 0)
            {
                filter.Schema = new InputSchemaCriteria {
                    Id = queryParams.SchemaId
                };
            }
            if (queryParams.PublicationId > 0)
            {
                filter.PublicationIds = new List <int?> {
                    queryParams.PublicationId
                };
            }
            return(filter);
        }
Пример #10
0
 internal BrokerQuery(SimpleBrokerQuery queryParameters)
 {
     _queryParameters = queryParameters;
 }
Пример #11
0
        private SortParameter GetSortParameter(SimpleBrokerQuery queryParams)
        {
            SortDirection dir = queryParams.Sort.ToLower().EndsWith("asc") ? SortParameter.Ascending : SortParameter.Descending;

            return(new SortParameter(GetSortColumn(queryParams), dir));
        }