private static ResultSet ExecuteSmartTargetQuery(SmartTargetPageModel smartTargetPageModel, Localization localization)
        {
            using (new Tracer(smartTargetPageModel, localization))
            {
                TcmUri pageUri        = new TcmUri(String.Format("tcm:{0}-{1}-64", localization.LocalizationId, smartTargetPageModel.Id));
                TcmUri publicationUri = new TcmUri(0, pageUri.PublicationId, 1);

                ClaimStore claimStore = AmbientDataContext.CurrentClaimStore;
                string     triggers   = AmbientDataHelper.GetTriggers(claimStore);

                QueryBuilder queryBuilder = new QueryBuilder();
                queryBuilder.Parse(triggers);
                queryBuilder.AddCriteria(new PublicationCriteria(publicationUri));
                queryBuilder.AddCriteria(new PageCriteria(pageUri));

                // Adding all the page regions to the query for having only 1 query a page
                foreach (SmartTargetRegion region in smartTargetPageModel.Regions.OfType <SmartTargetRegion>())
                {
                    queryBuilder.AddCriteria(new RegionCriteria(region.Name));
                }

                return(queryBuilder.Execute());
            }
        }
        /// <summary>
        /// Renders each Smart Target region into a list which can be traversed by the caller.
        /// </summary>
        /// <param name="helper">The helper to extend</param>
        /// <param name="regionName">the name of the region to render</param>
        /// <param name="viewName">the name of the component view</param>
        /// <param name="maxItems">the number to limit on</param>
        /// <param name="startIndex">the item index at which to start rendering</param>
        /// <returns>a list of rendered items for a given region across all promotions</returns>
        ///
        public static List <MvcHtmlString> RenderSmartTargetRegionItemsUsingView(this HtmlHelper helper, string regionName, string viewName, int maxItems = 0, int startIndex = 0)
        {
            string publicationId = ConfigurationManager.AppSettings["PublicationId"];

            LOG.Info(string.Format("Calling  RenderSmartTargetRegionItemsUsingView"));
            // First query Fredhopper for the targeted component IDs

            ClaimStore claimStore = AmbientDataContext.CurrentClaimStore;

            string query = AmbientDataHelper.GetTriggers(claimStore);

            var queryBuilder = new QueryBuilder();

            queryBuilder.Parse(query);

            if (maxItems > 0)
            {
                LOG.Info(string.Format("Maxitems ", maxItems));
                queryBuilder.MaxItems = maxItems;
            }
            LOG.Info("maxItems Value: " + maxItems.ToString());

            queryBuilder.StartIndex = startIndex;

            //Add Publication Info
            var pubIdUri = new SM.Utils.TcmUri(publicationId);

            SM.Query.Builder.PublicationCriteria pubCriteria = new SM.Query.Builder.PublicationCriteria(pubIdUri);
            queryBuilder.AddCriteria(pubCriteria);


            //Add Region Info
            RegionCriteria regionCriteria = new RegionCriteria(regionName);

            queryBuilder.AddCriteria(regionCriteria);

            ResultSet fredHopperResultset = queryBuilder.Execute();

            List <string> componentIds = new List <string>();

            foreach (Promotion p in fredHopperResultset.Promotions)
            {
                LOG.Info("Promotion ID " + p.PromotionId.ToString());
                LOG.Info("Promotion ID " + p.Items.Count().ToString());
                foreach (Item i in p.Items)
                {
                    LOG.Info("Component ID " + i.ComponentUri.ToString());
                    LOG.Info("Template ID " + i.TemplateUri.ToString());

                    componentIds.Add(i.ComponentUriAsString + "|" + i.TemplateUriAsString);
                }
            }

            // Next, query the standard Tridion Broker to get the components out.
            // This is because we should use the master source of published content.
            // Using the CP source that has been published to Fredhopper (see API  or service response).
            // is not recommended, so we use the master source of published content, i.e. the Tridion Broker.

            var renderedRegionItemsList = new List <MvcHtmlString>();

            foreach (string s in componentIds)
            {
                string[] compPresIds = s.Split(new char[] { '|' });
                string   compId = compPresIds[0], templateId = compPresIds[1];

                // We now have the Model (i.e. the Component), but we need to call the View, which is the title of the CT.
                // The issue is that the Broker API does not expose (nor store) the title of CTs.  So the only way to get this
                // is to grab it from DD4T's rendered Component Presentation XML.
                IComponent       comp = null;
                ComponentFactory cf   = new ComponentFactory();
                cf.TryGetComponent(compId, out comp, templateId);

                try
                {
                    var renderedCp = helper.Partial(viewName, comp);
                    renderedRegionItemsList.Add(renderedCp);
                }
                catch (Exception ex)
                {
                    LOG.Info("ex : " + ex.Message);
                }
            }
            return(renderedRegionItemsList);
        }