Skip to content

Commit 05d6074

Browse files
committed
Excel Import: completely rewritten Excel import routines to make it smarter and faster
* now it's magnitudes faster than before * the sort order of columns does not matter anymore * the only mandatory column for new records is "Name" * empty cells are not processed anymore, so smarter data updates can be run now * TBD: collect errors and warnings and push it to a log (DB or FS?) * TBD: run import async in background
1 parent e02c1c9 commit 05d6074

6 files changed

Lines changed: 815 additions & 495 deletions

File tree

src/Libraries/SmartStore.Core/Data/DbContextScope.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public DbContextScope(string alias = null, bool? autoDetectChanges = null, bool?
2828
ctx.ValidateOnSaveEnabled = validateOnSave.Value;
2929
}
3030

31+
public int Commit()
32+
{
33+
var ctx = EngineContext.Current.Resolve<IDbContext>(_alias);
34+
return ctx.SaveChanges();
35+
}
36+
3137
public void Dispose()
3238
{
3339
var ctx = EngineContext.Current.Resolve<IDbContext>(_alias);

src/Libraries/SmartStore.Core/Extensions/ConversionExtensions.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,29 @@ public static object Convert(this object value, Type to, CultureInfo culture)
112112
// see if source or target types have a TypeConverter that converts between the two
113113
TypeConverter toConverter = TypeDescriptor.GetConverter(fromType);
114114

115-
if (toConverter != null && toConverter.CanConvertTo(to))
115+
Type nonNullableTo = to.GetNonNullableType();
116+
bool isNullableTo = to != nonNullableTo;
117+
118+
if (toConverter != null && toConverter.CanConvertTo(nonNullableTo))
116119
{
117-
return toConverter.ConvertTo(null, culture, value, to);
120+
object result = toConverter.ConvertTo(null, culture, value, nonNullableTo);
121+
return isNullableTo ? Activator.CreateInstance(typeof(Nullable<>).MakeGenericType(nonNullableTo), result) : result;
118122
}
119123

120-
TypeConverter fromConverter = TypeDescriptor.GetConverter(to);
124+
TypeConverter fromConverter = TypeDescriptor.GetConverter(nonNullableTo);
121125

122126
if (fromConverter != null && fromConverter.CanConvertFrom(fromType))
123127
{
124-
return fromConverter.ConvertFrom(null, culture, value);
128+
object result = fromConverter.ConvertFrom(null, culture, value);
129+
return isNullableTo ? Activator.CreateInstance(typeof(Nullable<>).MakeGenericType(nonNullableTo), result) : result;
125130
}
131+
132+
// TypeConverter doesn't like Double to Decimal
133+
if (fromType == typeof(double) && nonNullableTo == typeof(decimal))
134+
{
135+
decimal result = new Decimal((double)value);
136+
return isNullableTo ? Activator.CreateInstance(typeof(Nullable<>).MakeGenericType(nonNullableTo), result) : result;
137+
}
126138

127139
throw Error.InvalidCast(fromType, to);
128140

src/Libraries/SmartStore.Services/Catalog/ProductService.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,8 +1094,10 @@ public virtual void UpdateHasTierPricesProperty(Product product)
10941094
if (product == null)
10951095
throw new ArgumentNullException("product");
10961096

1097+
var prevValue = product.HasTierPrices;
10971098
product.HasTierPrices = product.TierPrices.Count > 0;
1098-
UpdateProduct(product);
1099+
if (prevValue != product.HasTierPrices)
1100+
UpdateProduct(product);
10991101
}
11001102

11011103
/// <summary>
@@ -1107,8 +1109,10 @@ public virtual void UpdateHasDiscountsApplied(Product product)
11071109
if (product == null)
11081110
throw new ArgumentNullException("product");
11091111

1112+
var prevValue = product.HasDiscountsApplied;
11101113
product.HasDiscountsApplied = product.AppliedDiscounts.Count > 0;
1111-
UpdateProduct(product);
1114+
if (prevValue != product.HasDiscountsApplied)
1115+
UpdateProduct(product);
11121116
}
11131117

11141118
#endregion

0 commit comments

Comments
 (0)