/// <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;
            }
        }
Пример #2
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;
            }
        }