Skip to content

Commit f32bd3d

Browse files
committed
Swap source and mapped name when persisting import column mappings
1 parent 36a15b0 commit f32bd3d

9 files changed

Lines changed: 264 additions & 12 deletions

File tree

src/Libraries/SmartStore.Data/Migrations/201605061916117_SwapColumnMappingValues.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
namespace SmartStore.Data.Migrations
2+
{
3+
using System.Collections.Generic;
4+
using System.Data.Entity.Migrations;
5+
using System.Linq;
6+
using Core.Domain;
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Linq;
9+
using Setup;
10+
11+
public partial class SwapColumnMappingValues : DbMigration, IDataSeeder<SmartObjectContext>
12+
{
13+
public override void Up()
14+
{
15+
}
16+
17+
public override void Down()
18+
{
19+
}
20+
21+
public bool RollbackOnFailure
22+
{
23+
get { return false; }
24+
}
25+
26+
public void Seed(SmartObjectContext context)
27+
{
28+
var importProfiles = context.Set<ImportProfile>().ToList();
29+
30+
foreach (var profile in importProfiles.Where(x => x.ColumnMapping.HasValue()))
31+
{
32+
var dic = new Dictionary<string, Dictionary<string, string>>();
33+
var storeMapping = true;
34+
35+
var json = JObject.Parse(profile.ColumnMapping);
36+
37+
foreach (var kvp in json)
38+
{
39+
dynamic value = kvp.Value;
40+
41+
var mappedName = (string)value.MappedName;
42+
var property = (string)value.Property;
43+
var defaultValue = (string)value.Default;
44+
45+
if (mappedName.HasValue())
46+
{
47+
// break migration because data is already migrated
48+
storeMapping = false;
49+
break;
50+
}
51+
else if (property.HasValue())
52+
{
53+
if (!kvp.Key.IsCaseInsensitiveEqual(property) || defaultValue.HasValue())
54+
{
55+
// swap value
56+
dic.Add(property, new Dictionary<string, string>
57+
{
58+
{ "MappedName", kvp.Key },
59+
{ "Default", defaultValue }
60+
});
61+
}
62+
else
63+
{
64+
// ignore because persisting not required anymore
65+
}
66+
}
67+
else
68+
{
69+
// explicitly ignored property
70+
dic.Add(property, new Dictionary<string, string>
71+
{
72+
{ "MappedName", property },
73+
{ "Default", "[IGNOREPROPERTY]" }
74+
});
75+
}
76+
}
77+
78+
if (storeMapping)
79+
{
80+
if (dic.Any())
81+
profile.ColumnMapping = JsonConvert.SerializeObject(dic);
82+
else
83+
profile.ColumnMapping = null;
84+
}
85+
}
86+
87+
context.SaveChanges();
88+
}
89+
}
90+
}

src/Libraries/SmartStore.Data/Migrations/201605061916117_SwapColumnMappingValues.resx

Lines changed: 126 additions & 0 deletions
Large diffs are not rendered by default.

src/Libraries/SmartStore.Data/SmartStore.Data.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@
388388
<Compile Include="Migrations\201605020640016_GtinMpnIndex.Designer.cs">
389389
<DependentUpon>201605020640016_GtinMpnIndex.cs</DependentUpon>
390390
</Compile>
391+
<Compile Include="Migrations\201605061916117_SwapColumnMappingValues.cs" />
392+
<Compile Include="Migrations\201605061916117_SwapColumnMappingValues.Designer.cs">
393+
<DependentUpon>201605061916117_SwapColumnMappingValues.cs</DependentUpon>
394+
</Compile>
391395
<Compile Include="Setup\Builder\ActivityLogTypeMigrator.cs" />
392396
<Compile Include="Setup\Builder\PermissionMigrator.cs" />
393397
<Compile Include="Setup\Builder\SettingsBuilder.cs" />
@@ -724,6 +728,9 @@
724728
<EmbeddedResource Include="Migrations\201605020640016_GtinMpnIndex.resx">
725729
<DependentUpon>201605020640016_GtinMpnIndex.cs</DependentUpon>
726730
</EmbeddedResource>
731+
<EmbeddedResource Include="Migrations\201605061916117_SwapColumnMappingValues.resx">
732+
<DependentUpon>201605061916117_SwapColumnMappingValues.cs</DependentUpon>
733+
</EmbeddedResource>
727734
<EmbeddedResource Include="Sql\Indexes.sql" />
728735
<EmbeddedResource Include="Sql\StoredProcedures.sql" />
729736
</ItemGroup>

src/Libraries/SmartStore.Services/DataExchange/Import/ColumnMapping/ColumnMap.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ public bool AddMapping(string sourceName, string index, string mappedName, strin
7676
Guard.ArgumentNotEmpty(() => sourceName);
7777
Guard.ArgumentNotEmpty(() => mappedName);
7878

79-
var isAlreadyMapped = (mappedName.HasValue() && _map.Any(x => x.Value.Property.IsCaseInsensitiveEqual(mappedName)));
79+
var isAlreadyMapped = (mappedName.HasValue() && _map.Any(x => x.Value.MappedName.IsCaseInsensitiveEqual(mappedName)));
8080

8181
if (isAlreadyMapped)
8282
return false;
8383

8484
_map[CreateSourceName(sourceName, index)] = new ColumnMappingValue
8585
{
86-
Property = mappedName,
86+
MappedName = mappedName,
8787
Default = defaultValue
8888
};
8989

@@ -115,7 +115,7 @@ public ColumnMappingValue GetMapping(string sourceName)
115115
return result;
116116
}
117117

118-
return new ColumnMappingValue { Property = sourceName };
118+
return new ColumnMappingValue { MappedName = sourceName };
119119
}
120120
}
121121

@@ -127,7 +127,7 @@ public class ColumnMappingValue
127127
/// The mapped name
128128
/// </summary>
129129
[JsonProperty]
130-
public string Property { get; set; }
130+
public string MappedName { get; set; }
131131

132132
/// <summary>
133133
/// An optional default value

src/Libraries/SmartStore.Services/DataExchange/Import/ColumnMapping/ColumnMapConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override object ConvertFrom(CultureInfo culture, object value)
3232

3333
foreach (var kvp in dict)
3434
{
35-
map.AddMapping(kvp.Key, null, kvp.Value.Property, kvp.Value.Default);
35+
map.AddMapping(kvp.Key, null, kvp.Value.MappedName, kvp.Value.Default);
3636
}
3737

3838
return map;

src/Libraries/SmartStore.Services/DataExchange/Import/ImportDataSegmenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public bool HasColumn(string name, bool withAnyIndex = false)
125125
/// </remarks>
126126
public bool HasColumn(string name, string index)
127127
{
128-
return _table.HasColumn(_columnMap.GetMapping(name, index).Property);
128+
return _table.HasColumn(_columnMap.GetMapping(name, index).MappedName);
129129
}
130130

131131
/// <summary>

src/Libraries/SmartStore.Services/DataExchange/Import/ImportRow.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public bool HasDataValue(string columnName, string index)
152152
var mapping = _segmenter.ColumnMap.GetMapping(columnName, index);
153153

154154
object value;
155-
return (_row.TryGetValue(mapping.Property, out value) && value != null && value != DBNull.Value);
155+
return (_row.TryGetValue(mapping.MappedName, out value) && value != null && value != DBNull.Value);
156156
}
157157

158158
public TProp GetDataValue<TProp>(string columnName)
@@ -165,7 +165,7 @@ public TProp GetDataValue<TProp>(string columnName, string index)
165165
object value;
166166
var mapping = _segmenter.ColumnMap.GetMapping(columnName, index);
167167

168-
if (_row.TryGetValue(mapping.Property, out value) && value != null && value != DBNull.Value)
168+
if (_row.TryGetValue(mapping.MappedName, out value) && value != null && value != DBNull.Value)
169169
{
170170
return value.Convert<TProp>(_segmenter.Culture);
171171
}
@@ -219,7 +219,7 @@ public bool SetProperty<TProp>(
219219
{
220220
// explicitly ignore this property
221221
}
222-
else if (_row.TryGetValue(mapping.Property, out value) && (value != null && value != DBNull.Value))
222+
else if (_row.TryGetValue(mapping.MappedName, out value) && (value != null && value != DBNull.Value))
223223
{
224224
// source contains field value. Set it.
225225
TProp converted;

src/Presentation/SmartStore.Web/Administration/Controllers/ImportController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ private void PrepareProfileModel(ImportProfileModel model, ImportProfile profile
224224
{
225225
var mapping = new ColumnMappingItemModel
226226
{
227-
Column = x.Key,
228-
Property = x.Value.Property,
227+
Column = x.Value.MappedName,
228+
Property = x.Key,
229229
Default = x.Value.Default
230230
};
231231

@@ -451,7 +451,7 @@ public ActionResult Edit(ImportProfileModel model, bool continueEditing, FormCol
451451
if (defaultValue.HasValue() && GetDisabledDefaultFieldNames(profile).Contains(property))
452452
defaultValue = null;
453453

454-
result = map.AddMapping(column, null, property, defaultValue);
454+
result = map.AddMapping(property, null, column, defaultValue);
455455
}
456456

457457
if (!result)

0 commit comments

Comments
 (0)