Ejemplo n.º 1
0
        /// <summary>
        /// Populates a Content List (of Teasers) by executing the query it specifies.
        /// </summary>
        /// <param name="contentList">The Content List which specifies the query and is to be populated.</param>
        /// <param name="localization">The context Localization.</param>
        public virtual void PopulateDynamicList <T>(ContentList <T> contentList, Localization localization) where T : EntityModel
        {
            using (new Tracer(contentList, localization))
            {
                BrokerQuery query = new BrokerQuery
                {
                    Start         = contentList.Start,
                    PublicationId = Int32.Parse(localization.LocalizationId),
                    PageSize      = contentList.PageSize,
                    SchemaId      = MapSchema(contentList.ContentType.Key, localization),
                    Sort          = contentList.Sort.Key
                };

                // TODO: For now BrokerQuery always returns Teasers
                IEnumerable <Teaser> queryResults = query.ExecuteQuery();

                ILinkResolver linkResolver = SiteConfiguration.LinkResolver;
                foreach (Teaser item in queryResults)
                {
                    item.Link.Url = linkResolver.ResolveLink(item.Link.Url);
                }

                contentList.ItemListElements = queryResults.Cast <T>().ToList();
                contentList.HasMore          = query.HasMore;
            }
        }
        /// <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;
            }
        }
        public async Task CanRequestBrokerAllFields()
        {
            // arrange
            this._dbContext.DbBrokers.Add(
                new Broker
            {
                Id         = 1,
                ExternalId = string.Empty,
                Name       = "TOURMALINE",
                CreatedOn  = new DateTime(2019, 07, 15, 8, 56, 56)
            });

            this._dbContext.DbBrokers.Add(
                new Broker
            {
                Id         = 4751,
                ExternalId = string.Empty,
                Name       = string.Empty,
                CreatedOn  = new DateTime(2019, 07, 15, 15, 0, 0)
            });

            await this._dbContext.SaveChangesAsync();

            var query = new BrokerQuery();

            query.Filter.Node.FieldId().FieldName().FieldCreatedOn().FieldExternalId();

            // act
            var brokers = await this._apiClient.QueryAsync(query, CancellationToken.None);

            // assert
            var databaseBrokers = this._dbContext.DbBrokers.ToList();

            Assert.That(brokers, Has.Count.EqualTo(databaseBrokers.Count));

            for (var i = 0; i < this._dbContext.DbMarkets.Count(); i++)
            {
                var expected = databaseBrokers[i];
                var actual   = brokers[i];

                Assert.That(actual.Id, Is.EqualTo(expected.Id));
                Assert.That(actual.ExternalId, Is.EqualTo(expected.ExternalId));
                Assert.That(actual.Name, Is.EqualTo(expected.Name));
                Assert.That(actual.CreatedOn, Is.EqualTo(expected.CreatedOn));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Executes the specified <see cref="T:TcmCDService.Contracts.BrokerQuery" />
        /// </summary>
        /// <param name="brokerQuery"><see cref="T:TcmCDService.Contracts.BrokerQuery" /></param>
        /// <returns><see cref="I:System.Collections.Generic.IEnumerable{System.String}" /></returns>
        public static IEnumerable <String> Execute(BrokerQuery brokerQuery)
        {
            if (brokerQuery != null)
            {
                List <IDisposable> disposableItems = new List <IDisposable>();
                List <Criteria>    criteria        = new List <Criteria>();

                try
                {
                    // Query for ItemType: Component
                    if (brokerQuery.ItemType != 0)
                    {
                        criteria.Add(new ItemTypeCriteria((int)brokerQuery.ItemType));
                    }

                    // Query for Publication
                    if (!String.IsNullOrEmpty(brokerQuery.Publication))
                    {
                        criteria.Add(new PublicationCriteria(new TcmUri(brokerQuery.Publication).ItemId));
                    }

                    // Query based on Schema
                    if (brokerQuery.SchemaUris != null && brokerQuery.SchemaUris.Any())
                    {
                        criteria.Add(CriteriaFactory.Or(brokerQuery.SchemaUris.Select((u) =>
                        {
                            ItemSchemaCriteria itemSchemaCriteria = new ItemSchemaCriteria(new TcmUri(u).ItemId);
                            disposableItems.Add(itemSchemaCriteria);

                            return(itemSchemaCriteria);
                        }).ToArray()));
                    }

                    // Query based on Component Template
                    if (!String.IsNullOrEmpty(brokerQuery.ComponentTemplateUri))
                    {
                        criteria.Add(new ItemTemplateCriteria(new TcmUri(brokerQuery.ComponentTemplateUri).ItemId));
                    }

                    // Add any SubQuery entries (MetaQuery or KeywordQueries which are specified)
                    if (brokerQuery.SubQueries != null && brokerQuery.SubQueries.Any())
                    {
                        criteria.AddRange(brokerQuery.SubQueries.Where(q => q != null).Select((q) =>
                        {
                            Criteria subCriteria = q is MetaQuery ? ToCriteria(q as MetaQuery) : ToCriteria(q as KeywordQuery);
                            disposableItems.Add(subCriteria);

                            return(subCriteria);
                        }));
                    }

                    using (Tridion.ContentDelivery.DynamicContent.Query.Query query = new Tridion.ContentDelivery.DynamicContent.Query.Query(CriteriaFactory.And(criteria.ToArray())))
                    {
                        // Limit the amount of results
                        using (LimitFilter filter = new LimitFilter(brokerQuery.ResultLimit.GetValueOrDefault(100)))
                        {
                            query.SetResultFilter(filter);
                            query.AddSorting(new SortParameter(SortParameter.ItemModificationDate, SortParameter.Descending));

                            return(query.ExecuteQuery());
                        }
                    }
                }
                finally
                {
                    // Ensure all created Java objects are disposed
                    foreach (Criteria entry in criteria)
                    {
                        if (entry != null)
                        {
                            entry.Dispose();
                        }
                    }

                    foreach (IDisposable entry in disposableItems)
                    {
                        if (entry != null)
                        {
                            entry.Dispose();
                        }
                    }
                }
            }

            return(new String[] { });
        }