Skip to content

Commit

Permalink
Resolves #755 Some methods still loading all products in one go
Browse files Browse the repository at this point in the history
  • Loading branch information
mgesing committed Oct 19, 2015
1 parent 2322c74 commit f999743
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* Tax provider and payment method were automatically activated when there were no active provider\method
* #784 Biz-Importer: Name of delivery time must not be imported empty
* #776 Preview: Manufacturer and Product in Multi Store
* #755 Some methods still loading all products in one go


## SmartStore.NET 2.2.2
Expand Down
30 changes: 19 additions & 11 deletions src/Libraries/SmartStore.Services/Seo/SitemapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,31 @@ private void WriteManufacturers(UrlHelper urlHelper)

private void WriteProducts(UrlHelper urlHelper)
{
var ctx = new ProductSearchContext()
var protocol = _securitySettings.ForceSslForAllPages ? "https" : "http";

var ctx = new ProductSearchContext
{
OrderBy = ProductSortingEnum.CreatedOn,
PageSize = int.MaxValue,
PageSize = 500,
StoreId = _storeContext.CurrentStoreIdIfMultiStoreMode,
VisibleIndividuallyOnly = true
};

var products = _productService.SearchProducts(ctx);
var protocol = _securitySettings.ForceSslForAllPages ? "https" : "http";
foreach (var product in products)
{
var url = urlHelper.RouteUrl("Product", new { SeName = product.GetSeName() }, protocol);
var updateFrequency = UpdateFrequency.Weekly;
var updateTime = product.UpdatedOnUtc;
WriteUrlLocation(url, updateFrequency, updateTime);
}
for (ctx.PageIndex = 0; ctx.PageIndex < 9999999; ++ctx.PageIndex)
{
var products = _productService.SearchProducts(ctx);

foreach (var product in products)
{
var url = urlHelper.RouteUrl("Product", new { SeName = product.GetSeName() }, protocol);
var updateFrequency = UpdateFrequency.Weekly;
var updateTime = product.UpdatedOnUtc;
WriteUrlLocation(url, updateFrequency, updateTime);
}

if (!products.HasNextPage)
break;
}
}

private void WriteTopics(UrlHelper urlHelper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Mime;using System.Text;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
using Autofac;
using Newtonsoft.Json;
using SmartStore.Admin.Models.Catalog;
Expand Down Expand Up @@ -2836,35 +2834,45 @@ public ActionResult ExportPdf(bool all, string selectedIds = null)
NotifyInfo(T("Admin.Common.ExportNoData"));
return RedirectToAction("List");
}

IList<Product> products;

const int maxProducts = 500;
int totalCount = 0;
IPagedList<Product> products;

using (var scope = new DbContextScope(_dbContext, autoDetectChanges: false, forceNoTracking: true))
{
if (all)
var searchContext = new ProductSearchContext
{
products = _productService.SearchProducts(new ProductSearchContext
{
LanguageId = _workContext.WorkingLanguage.Id,
OrderBy = ProductSortingEnum.Position,
PageSize = int.MaxValue,
ShowHidden = true
});
LanguageId = _workContext.WorkingLanguage.Id,
OrderBy = ProductSortingEnum.Position,
PageSize = 1,
ShowHidden = true
};

if (!all)
{
searchContext.ProductIds = selectedIds.ToIntArray().ToList();
}
else

products = _productService.SearchProducts(searchContext);

totalCount = products.TotalCount;

if (totalCount > 0 && totalCount <= maxProducts)
{
var ids = selectedIds.ToIntArray();
products = _productService.GetProductsByIds(ids);
searchContext.PageSize = int.MaxValue;

products = _productService.SearchProducts(searchContext);
}
}

if (products.Count == 0)
if (totalCount == 0)
{
NotifyInfo(T("Admin.Common.ExportNoData"));
return RedirectToAction("List");
}

if (products.Count > 500)
if (totalCount > maxProducts)
{
NotifyWarning(T("Admin.Common.ExportToPdf.TooManyItems"));
return RedirectToAction("List");
Expand Down

0 comments on commit f999743

Please sign in to comment.