Skip to content

Commit 36a15b0

Browse files
committed
Import: more refactoring
1 parent e577924 commit 36a15b0

10 files changed

Lines changed: 207 additions & 130 deletions

File tree

src/Libraries/SmartStore.Services/Catalog/Importer/CategoryImporter.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public CategoryImporter(
6565
_dataExchangeSettings = dataExchangeSettings;
6666
}
6767

68-
protected virtual int ProcessSlugs(IImportExecuteContext context, ImportRow<Category>[] batch)
68+
protected virtual int ProcessSlugs(IImportExecuteContext context, IEnumerable<ImportRow<Category>> batch)
6969
{
7070
var entityName = typeof(Category).Name;
7171
var slugMap = new Dictionary<string, UrlRecord>();
@@ -136,7 +136,7 @@ protected virtual int ProcessSlugs(IImportExecuteContext context, ImportRow<Cate
136136
return _urlRecordRepository.Context.SaveChanges();
137137
}
138138

139-
protected virtual int ProcessLocalizations(IImportExecuteContext context, ImportRow<Category>[] batch)
139+
protected virtual int ProcessLocalizations(IImportExecuteContext context, IEnumerable<ImportRow<Category>> batch)
140140
{
141141
foreach (var row in batch)
142142
{
@@ -165,8 +165,9 @@ protected virtual int ProcessLocalizations(IImportExecuteContext context, Import
165165
return num;
166166
}
167167

168-
protected virtual int ProcessParentMappings(IImportExecuteContext context,
169-
ImportRow<Category>[] batch,
168+
protected virtual int ProcessParentMappings(
169+
IImportExecuteContext context,
170+
IEnumerable<ImportRow<Category>> batch,
170171
Dictionary<int, ImportCategoryMapping> srcToDestId)
171172
{
172173
foreach (var row in batch)
@@ -198,7 +199,7 @@ protected virtual int ProcessParentMappings(IImportExecuteContext context,
198199

199200
protected virtual int ProcessPictures(
200201
IImportExecuteContext context,
201-
ImportRow<Category>[] batch,
202+
IEnumerable<ImportRow<Category>> batch,
202203
Dictionary<int, ImportCategoryMapping> srcToDestId)
203204
{
204205
Picture picture = null;
@@ -271,7 +272,7 @@ protected virtual int ProcessPictures(
271272
return num;
272273
}
273274

274-
protected virtual int ProcessStoreMappings(IImportExecuteContext context, ImportRow<Category>[] batch)
275+
protected virtual int ProcessStoreMappings(IImportExecuteContext context, IEnumerable<ImportRow<Category>> batch)
275276
{
276277
_storeMappingRepository.AutoCommitEnabled = false;
277278

@@ -290,7 +291,7 @@ protected virtual int ProcessStoreMappings(IImportExecuteContext context, Import
290291

291292
protected virtual int ProcessCategories(
292293
IImportExecuteContext context,
293-
ImportRow<Category>[] batch,
294+
IEnumerable<ImportRow<Category>> batch,
294295
Dictionary<string, int> templateViewPaths,
295296
Dictionary<int, ImportCategoryMapping> srcToDestId)
296297
{
@@ -445,15 +446,15 @@ protected override void Import(IImportExecuteContext context)
445446

446447
using (var scope = new DbContextScope(ctx: _categoryRepository.Context, autoDetectChanges: false, proxyCreation: false, validateOnSave: false))
447448
{
448-
var segmenter = context.GetSegmenter<Category>();
449+
var segmenter = context.CreateSegmenter();
449450

450451
Init(context, _dataExchangeSettings);
451452

452453
context.Result.TotalRecords = segmenter.TotalRows;
453454

454455
while (context.Abort == DataExchangeAbortion.None && segmenter.ReadNextBatch())
455456
{
456-
var batch = segmenter.CurrentBatch;
457+
var batch = segmenter.GetCurrentBatch<Category>();
457458

458459
// Perf: detach all entities
459460
_categoryRepository.Context.DetachAll(false);
@@ -544,8 +545,7 @@ protected override void Import(IImportExecuteContext context)
544545

545546
while (context.Abort == DataExchangeAbortion.None && segmenter.ReadNextBatch())
546547
{
547-
var batch = segmenter.CurrentBatch;
548-
548+
var batch = segmenter.GetCurrentBatch<Category>();
549549
_categoryRepository.Context.DetachAll(false);
550550

551551
try

src/Libraries/SmartStore.Services/Catalog/Importer/ProductImporter.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public ProductImporter(
107107

108108
protected virtual int ProcessProductMappings(
109109
IImportExecuteContext context,
110-
ImportRow<Product>[] batch,
110+
IEnumerable<ImportRow<Product>> batch,
111111
Dictionary<int, ImportProductMapping> srcToDestId)
112112
{
113113
_productRepository.AutoCommitEnabled = false;
@@ -137,7 +137,7 @@ protected virtual int ProcessProductMappings(
137137
return num;
138138
}
139139

140-
protected virtual void ProcessProductPictures(IImportExecuteContext context, ImportRow<Product>[] batch)
140+
protected virtual void ProcessProductPictures(IImportExecuteContext context, IEnumerable<ImportRow<Product>> batch)
141141
{
142142
// true, cause pictures must be saved and assigned an id prior adding a mapping.
143143
_productPictureRepository.AutoCommitEnabled = true;
@@ -244,7 +244,7 @@ protected virtual void ProcessProductPictures(IImportExecuteContext context, Imp
244244
}
245245
}
246246

247-
protected virtual int ProcessProductManufacturers(IImportExecuteContext context, ImportRow<Product>[] batch)
247+
protected virtual int ProcessProductManufacturers(IImportExecuteContext context, IEnumerable<ImportRow<Product>> batch)
248248
{
249249
_productManufacturerRepository.AutoCommitEnabled = false;
250250

@@ -295,7 +295,7 @@ protected virtual int ProcessProductManufacturers(IImportExecuteContext context,
295295
return num;
296296
}
297297

298-
protected virtual int ProcessProductCategories(IImportExecuteContext context, ImportRow<Product>[] batch)
298+
protected virtual int ProcessProductCategories(IImportExecuteContext context, IEnumerable<ImportRow<Product>> batch)
299299
{
300300
_productCategoryRepository.AutoCommitEnabled = false;
301301

@@ -347,8 +347,8 @@ protected virtual int ProcessProductCategories(IImportExecuteContext context, Im
347347
}
348348

349349
protected virtual int ProcessLocalizations(
350-
IImportExecuteContext context,
351-
ImportRow<Product>[] batch,
350+
IImportExecuteContext context,
351+
IEnumerable<ImportRow<Product>> batch,
352352
string[] localizedProperties)
353353
{
354354
if (localizedProperties.Length == 0)
@@ -385,7 +385,7 @@ protected virtual int ProcessLocalizations(
385385
return 0;
386386
}
387387

388-
protected virtual int ProcessSlugs(IImportExecuteContext context, ImportRow<Product>[] batch, string[] columnIndexes)
388+
protected virtual int ProcessSlugs(IImportExecuteContext context, IEnumerable<ImportRow<Product>> batch, string[] columnIndexes)
389389
{
390390
var entityName = typeof(Product).Name;
391391
var slugMap = new Dictionary<string, UrlRecord>();
@@ -461,7 +461,7 @@ protected virtual int ProcessSlugs(IImportExecuteContext context, ImportRow<Prod
461461
return _urlRecordRepository.Context.SaveChanges();
462462
}
463463

464-
protected virtual int ProcessStoreMappings(IImportExecuteContext context, ImportRow<Product>[] batch)
464+
protected virtual int ProcessStoreMappings(IImportExecuteContext context, IEnumerable<ImportRow<Product>> batch)
465465
{
466466
_storeMappingRepository.AutoCommitEnabled = false;
467467

@@ -480,7 +480,7 @@ protected virtual int ProcessStoreMappings(IImportExecuteContext context, Import
480480

481481
protected virtual int ProcessProducts(
482482
IImportExecuteContext context,
483-
ImportRow<Product>[] batch,
483+
IEnumerable<ImportRow<Product>> batch,
484484
Dictionary<string, int> templateViewPaths,
485485
Dictionary<int, ImportProductMapping> srcToDestId)
486486
{
@@ -715,7 +715,7 @@ public static string[] DefaultKeyFields
715715
}
716716
}
717717

718-
private IEnumerable<string> ResolveLocalizedProperties(ImportDataSegmenter<Product> segmenter)
718+
private IEnumerable<string> ResolveLocalizedProperties(ImportDataSegmenter segmenter)
719719
{
720720
// Perf: determine whether our localizable properties actually have
721721
// counterparts in the source BEFORE import begins. This way we spare ourself
@@ -737,7 +737,7 @@ protected override void Import(IImportExecuteContext context)
737737

738738
using (var scope = new DbContextScope(ctx: _productRepository.Context, autoDetectChanges: false, proxyCreation: false, validateOnSave: false))
739739
{
740-
var segmenter = context.GetSegmenter<Product>();
740+
var segmenter = context.CreateSegmenter();
741741
Init(context, _dataExchangeSettings);
742742

743743
var localizedProperties = ResolveLocalizedProperties(segmenter).ToArray();
@@ -746,7 +746,7 @@ protected override void Import(IImportExecuteContext context)
746746

747747
while (context.Abort == DataExchangeAbortion.None && segmenter.ReadNextBatch())
748748
{
749-
var batch = segmenter.CurrentBatch;
749+
var batch = segmenter.GetCurrentBatch<Product>();
750750

751751
// Perf: detach all entities
752752
_productRepository.Context.DetachAll(false);
@@ -877,7 +877,7 @@ protected override void Import(IImportExecuteContext context)
877877

878878
while (context.Abort == DataExchangeAbortion.None && segmenter.ReadNextBatch())
879879
{
880-
var batch = segmenter.CurrentBatch;
880+
var batch = segmenter.GetCurrentBatch<Product>();
881881

882882
_productRepository.Context.DetachAll(false);
883883

src/Libraries/SmartStore.Services/Common/AddressExtentions.cs

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Collections.Generic;
34
using System.Text;
45
using SmartStore.Core.Domain.Common;
@@ -7,50 +8,13 @@ namespace SmartStore.Services.Common
78
{
89
public static class AddressExtentions
910
{
10-
/// <summary>
11-
/// Find an address
12-
/// </summary>
13-
/// <param name="source">Source</param>
14-
/// <param name="firstName">First name</param>
15-
/// <param name="lastName">Last name</param>
16-
/// <param name="phoneNumber">Phone number</param>
17-
/// <param name="email">Email</param>
18-
/// <param name="faxNumber">Fax number</param>
19-
/// <param name="company">Company</param>
20-
/// <param name="address1">Address 1</param>
21-
/// <param name="address2">Address 2</param>
22-
/// <param name="city">City</param>
23-
/// <param name="stateProvinceId">State/province identifier</param>
24-
/// <param name="zipPostalCode">Zip postal code</param>
25-
/// <param name="countryId">Country identifier</param>
26-
/// <returns>Address</returns>
27-
public static Address FindAddress(this List<Address> source,
28-
string firstName, string lastName, string phoneNumber,
29-
string email, string faxNumber, string company, string address1,
30-
string address2, string city, int? stateProvinceId,
31-
string zipPostalCode, int? countryId)
32-
{
33-
return source.Find((a) => ((String.IsNullOrEmpty(a.FirstName) && String.IsNullOrEmpty(firstName)) || a.FirstName == firstName) &&
34-
((String.IsNullOrEmpty(a.LastName) && String.IsNullOrEmpty(lastName)) || a.LastName == lastName) &&
35-
((String.IsNullOrEmpty(a.PhoneNumber) && String.IsNullOrEmpty(phoneNumber)) || a.PhoneNumber == phoneNumber) &&
36-
((String.IsNullOrEmpty(a.Email) && String.IsNullOrEmpty(email)) || a.Email == email) &&
37-
((String.IsNullOrEmpty(a.FaxNumber) && String.IsNullOrEmpty(faxNumber)) || a.FaxNumber == faxNumber) &&
38-
((String.IsNullOrEmpty(a.Company) && String.IsNullOrEmpty(company)) || a.Company == company) &&
39-
((String.IsNullOrEmpty(a.Address1) && String.IsNullOrEmpty(address1)) || a.Address1 == address1) &&
40-
((String.IsNullOrEmpty(a.Address2) && String.IsNullOrEmpty(address2)) || a.Address2 == address2) &&
41-
((String.IsNullOrEmpty(a.City) && String.IsNullOrEmpty(city)) || a.City == city) &&
42-
((a.StateProvinceId.IsNullOrDefault() && stateProvinceId.IsNullOrDefault()) || a.StateProvinceId == stateProvinceId) &&
43-
((String.IsNullOrEmpty(a.ZipPostalCode) && String.IsNullOrEmpty(zipPostalCode)) || a.ZipPostalCode == zipPostalCode) &&
44-
((a.CountryId.IsNullOrDefault() && countryId.IsNullOrDefault()) || a.CountryId == countryId));
45-
}
46-
4711
/// <summary>
48-
/// Find first occurrence of address
12+
/// Find first occurrence of an address
4913
/// </summary>
5014
/// <param name="source">Addresses to be searched</param>
5115
/// <param name="address">Address to find</param>
5216
/// <returns>First occurrence of address</returns>
53-
public static Address FindAddress(this List<Address> source, Address address)
17+
public static Address FindAddress(this ICollection<Address> source, Address address)
5418
{
5519
return source.FindAddress(
5620
address.FirstName,
@@ -68,6 +32,57 @@ public static Address FindAddress(this List<Address> source, Address address)
6832
);
6933
}
7034

35+
/// <summary>
36+
/// Find an address
37+
/// </summary>
38+
/// <param name="source">Source</param>
39+
/// <param name="firstName">First name</param>
40+
/// <param name="lastName">Last name</param>
41+
/// <param name="phoneNumber">Phone number</param>
42+
/// <param name="email">Email</param>
43+
/// <param name="faxNumber">Fax number</param>
44+
/// <param name="company">Company</param>
45+
/// <param name="address1">Address 1</param>
46+
/// <param name="address2">Address 2</param>
47+
/// <param name="city">City</param>
48+
/// <param name="stateProvinceId">State/province identifier</param>
49+
/// <param name="zipPostalCode">Zip postal code</param>
50+
/// <param name="countryId">Country identifier</param>
51+
/// <returns>Address</returns>
52+
public static Address FindAddress(
53+
this ICollection<Address> source,
54+
string firstName,
55+
string lastName,
56+
string phoneNumber,
57+
string email,
58+
string faxNumber,
59+
string company,
60+
string address1,
61+
string address2,
62+
string city,
63+
int? stateProvinceId,
64+
string zipPostalCode,
65+
int? countryId)
66+
{
67+
Func<Address, bool> addressMatcher = (x) =>
68+
{
69+
return x.Email.IsCaseInsensitiveEqual(email)
70+
&& x.LastName.IsCaseInsensitiveEqual(lastName)
71+
&& x.FirstName.IsCaseInsensitiveEqual(firstName)
72+
&& x.Address1.IsCaseInsensitiveEqual(address1)
73+
&& x.Address2.IsCaseInsensitiveEqual(address2)
74+
&& x.Company.IsCaseInsensitiveEqual(company)
75+
&& x.ZipPostalCode.IsCaseInsensitiveEqual(zipPostalCode)
76+
&& x.City.IsCaseInsensitiveEqual(city)
77+
&& x.PhoneNumber.IsCaseInsensitiveEqual(phoneNumber)
78+
&& x.FaxNumber.IsCaseInsensitiveEqual(faxNumber)
79+
&& x.StateProvinceId == stateProvinceId
80+
&& x.CountryId == countryId;
81+
};
82+
83+
return source.FirstOrDefault(addressMatcher);
84+
}
85+
7186
/// <summary>Returns the full name of the address.</summary>
7287
public static string GetFullName(this Address address)
7388
{

0 commit comments

Comments
 (0)