From 5a89ea0651366855468c523ebd9c7cf356943867 Mon Sep 17 00:00:00 2001 From: andrey_kozlov Date: Mon, 24 Dec 2018 19:15:02 +0500 Subject: [PATCH 1/3] =?UTF-8?q?-=20Fixed=20FK=20violation=20on=20import=20?= =?UTF-8?q?by=20changing=20export=20order=20+=20resolving=20property=20Cat?= =?UTF-8?q?alogId=20and=20CategoryId=20after=20they=20are=20created;=20-?= =?UTF-8?q?=20PropertyEntity.Patch:=20CategoryId=20and=20CatalogId=20are?= =?UTF-8?q?=20filled=20from=20source=20entity=20now;=20-=20Fixed=20Sonar?= =?UTF-8?q?=20warnings=20with=20redundant=20parameters=20and=20string.Form?= =?UTF-8?q?at();=20-=20Replaced=20"..."=20with=20"=E2=80=A6"=20(single=20s?= =?UTF-8?q?ymbol=20alt+0133=20on=20numpad);?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Model/PropertyEntity.cs | 5 +- .../ExportImport/CatalogExportImport.cs | 74 ++++++++++++++----- .../Model/PropertyAssociationInfo.cs | 35 +++++++++ .../VirtoCommerce.CatalogModule.Web.csproj | 1 + 4 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 VirtoCommerce.CatalogModule.Web/Model/PropertyAssociationInfo.cs diff --git a/VirtoCommerce.CatalogModule.Data/Model/PropertyEntity.cs b/VirtoCommerce.CatalogModule.Data/Model/PropertyEntity.cs index 3fbc4dbc9..c94a16ccd 100644 --- a/VirtoCommerce.CatalogModule.Data/Model/PropertyEntity.cs +++ b/VirtoCommerce.CatalogModule.Data/Model/PropertyEntity.cs @@ -1,10 +1,8 @@ using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel.DataAnnotations; using System.Linq; using VirtoCommerce.Domain.Catalog.Model; -using VirtoCommerce.Domain.Catalog.Services; using VirtoCommerce.Platform.Core.Common; namespace VirtoCommerce.CatalogModule.Data.Model @@ -162,6 +160,9 @@ public virtual void Patch(PropertyEntity target) target.TargetType = TargetType; target.Name = Name; + target.CatalogId = CatalogId; + target.CategoryId = CategoryId; + if (!PropertyAttributes.IsNullCollection()) { var attributeComparer = AnonymousComparer.Create((PropertyAttributeEntity x) => x.IsTransient() ? x.PropertyAttributeName : x.Id); diff --git a/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs b/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs index c34a07e14..f1adc178b 100644 --- a/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs +++ b/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs @@ -11,6 +11,7 @@ using VirtoCommerce.Platform.Core.Common; using VirtoCommerce.Platform.Core.ExportImport; using VirtoCommerce.Platform.Core.Settings; +using CatalogModuleModel = VirtoCommerce.CatalogModule.Web.Model; namespace VirtoCommerce.CatalogModule.Web.ExportImport { @@ -76,7 +77,7 @@ private int BatchSize #region Export/Import methods public void DoExport(Stream outStream, PlatformExportManifest manifest, Action progressCallback) { - var progressInfo = new ExportImportProgressInfo { Description = "loading data..." }; + var progressInfo = new ExportImportProgressInfo { Description = "loading data…" }; progressCallback(progressInfo); using (var sw = new StreamWriter(outStream, Encoding.UTF8)) @@ -84,10 +85,11 @@ public void DoExport(Stream outStream, PlatformExportManifest manifest, Action(); using (var streamReader = new StreamReader(stream)) using (var reader = new JsonTextReader(streamReader)) { @@ -111,7 +115,7 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action(reader); - progressInfo.Description = $"{ catalogs.Count() } catalogs are importing..."; + progressInfo.Description = $"{ catalogs.Count() } catalogs are importing…"; progressCallback(progressInfo); _catalogService.Update(catalogs); } @@ -119,7 +123,7 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action(reader); - progressInfo.Description = $"{ categories.Count() } categories are importing..."; + progressInfo.Description = $"{ categories.Count() } categories are importing…"; progressCallback(progressInfo); _categoryService.Update(categories); if (manifest.HandleBinaryData) @@ -130,8 +134,17 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action(reader); - progressInfo.Description = $"{ properties.Count() } properties are importing..."; + properties = _serializer.Deserialize(reader); + foreach (var property in properties) + { + if (property.CategoryId != null || property.CatalogId != null) + { + propertyAssociations.Add(property.Id, new CatalogModuleModel.PropertyAssociationInfo().FromEntity(property)); + property.CategoryId = null; + property.CatalogId = null; + } + } + progressInfo.Description = $"{ properties.Count() } properties are importing…"; progressCallback(progressInfo); _propertyService.Update(properties); } @@ -224,8 +237,8 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action 0) + { + progressInfo.Description = $"Updating {propertyAssociations.Count} property associations…"; + progressCallback(progressInfo); + var propertiesToUpdate = new List(); + foreach (var property in properties) + { + if (propertyAssociations.ContainsKey(property.Id)) + { + propertyAssociations[property.Id].Patch(property); + propertiesToUpdate.Add(property); + } + } + var updatedPropertiesCount = propertiesToUpdate.Count; + for (int i = 0; i < updatedPropertiesCount; i += BatchSize) + { + _propertyService.Update(propertiesToUpdate.Skip(i).Take(BatchSize).ToArray()); + progressInfo.Description = $"{ Math.Min(updatedPropertiesCount, i + BatchSize) } of { updatedPropertiesCount } property associations updated."; + progressCallback(progressInfo); + } + + _propertyService.Update(propertiesToUpdate.ToArray()); + } } #endregion - private void ExportCatalogs(JsonTextWriter writer, JsonSerializer serializer, PlatformExportManifest manifest, ExportImportProgressInfo progressInfo, Action progressCallback) + private void ExportCatalogs(JsonTextWriter writer, JsonSerializer serializer, ExportImportProgressInfo progressInfo, Action progressCallback) { //Catalogs - progressInfo.Description = string.Format("Catalogs are exporting..."); + progressInfo.Description = "Catalogs are exporting…"; progressCallback(progressInfo); var catalogs = _catalogService.GetCatalogsList().ToArray(); @@ -275,7 +313,7 @@ private void ExportCatalogs(JsonTextWriter writer, JsonSerializer serializer, Pl private void ExportCategories(JsonTextWriter writer, JsonSerializer serializer, PlatformExportManifest manifest, ExportImportProgressInfo progressInfo, Action progressCallback) { //Categories - progressInfo.Description = string.Format("Categories are exporting..."); + progressInfo.Description = "Categories are exporting…"; progressCallback(progressInfo); var categorySearchCriteria = new SearchCriteria { WithHidden = true, Skip = 0, Take = 0, ResponseGroup = SearchResponseGroup.WithCategories }; @@ -300,10 +338,10 @@ private void ExportCategories(JsonTextWriter writer, JsonSerializer serializer, progressCallback(progressInfo); } - private void ExportProperties(JsonTextWriter writer, JsonSerializer serializer, PlatformExportManifest manifest, ExportImportProgressInfo progressInfo, Action progressCallback) + private void ExportProperties(JsonTextWriter writer, JsonSerializer serializer, ExportImportProgressInfo progressInfo, Action progressCallback) { //Properties - progressInfo.Description = "Properties exporting..."; + progressInfo.Description = "Properties exporting…"; progressCallback(progressInfo); var properties = _propertyService.GetAllProperties(); @@ -320,9 +358,9 @@ private void ExportProperties(JsonTextWriter writer, JsonSerializer serializer, progressCallback(progressInfo); } - private void ExportPropertiesDictionaryItems(JsonTextWriter writer, JsonSerializer serializer, PlatformExportManifest manifest, ExportImportProgressInfo progressInfo, Action progressCallback) + private void ExportPropertiesDictionaryItems(JsonTextWriter writer, JsonSerializer serializer, ExportImportProgressInfo progressInfo, Action progressCallback) { - progressInfo.Description = "The dictionary items are exporting..."; + progressInfo.Description = "The dictionary items are exporting…"; progressCallback(progressInfo); var criteria = new PropertyDictionaryItemSearchCriteria { Take = 0, Skip = 0 }; @@ -349,7 +387,7 @@ private void ExportPropertiesDictionaryItems(JsonTextWriter writer, JsonSerializ private void ExportProducts(JsonTextWriter writer, JsonSerializer serializer, PlatformExportManifest manifest, ExportImportProgressInfo progressInfo, Action progressCallback) { //Products - progressInfo.Description = string.Format("Products are exporting..."); + progressInfo.Description = "Products are exporting…"; progressCallback(progressInfo); var productSearchCriteria = new SearchCriteria { WithHidden = true, Take = 0, Skip = 0, ResponseGroup = SearchResponseGroup.WithProducts }; diff --git a/VirtoCommerce.CatalogModule.Web/Model/PropertyAssociationInfo.cs b/VirtoCommerce.CatalogModule.Web/Model/PropertyAssociationInfo.cs new file mode 100644 index 000000000..42b60470f --- /dev/null +++ b/VirtoCommerce.CatalogModule.Web/Model/PropertyAssociationInfo.cs @@ -0,0 +1,35 @@ +using System; +using Omu.ValueInjecter; +using CatalogDomainModel = VirtoCommerce.Domain.Catalog.Model; + +namespace VirtoCommerce.CatalogModule.Web.Model +{ + /// + /// Stores and applies Property associations + /// + public class PropertyAssociationInfo + { + public string CatalogId { get; set; } + public string CategoryId { get; set; } + + public PropertyAssociationInfo FromEntity(CatalogDomainModel.Property property) + { + if (property == null) + { + throw new ArgumentNullException(nameof(property)); + } + this.InjectFrom(property); + + return this; + } + + public void Patch(CatalogDomainModel.Property property) + { + if (property == null) + { + throw new ArgumentNullException(nameof(property)); + } + property.InjectFrom(this); + } + } +} diff --git a/VirtoCommerce.CatalogModule.Web/VirtoCommerce.CatalogModule.Web.csproj b/VirtoCommerce.CatalogModule.Web/VirtoCommerce.CatalogModule.Web.csproj index 2b69d7bdb..b96248b89 100644 --- a/VirtoCommerce.CatalogModule.Web/VirtoCommerce.CatalogModule.Web.csproj +++ b/VirtoCommerce.CatalogModule.Web/VirtoCommerce.CatalogModule.Web.csproj @@ -158,6 +158,7 @@ + From 5974f9d34f20ee23d131a15e879a256140861691 Mon Sep 17 00:00:00 2001 From: Eugeny Tatarincev Date: Tue, 25 Dec 2018 14:58:27 +0200 Subject: [PATCH 2/3] Minor code refactoring --- .../ExportImport/CatalogExportImport.cs | 35 +++++++------------ .../Model/PropertyAssociationInfo.cs | 35 ------------------- .../VirtoCommerce.CatalogModule.Web.csproj | 1 - 3 files changed, 12 insertions(+), 59 deletions(-) delete mode 100644 VirtoCommerce.CatalogModule.Web/Model/PropertyAssociationInfo.cs diff --git a/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs b/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs index f1adc178b..da4c24df3 100644 --- a/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs +++ b/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs @@ -102,8 +102,7 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action(); + var propertiesWithForeignKeys = new List(); using (var streamReader = new StreamReader(stream)) using (var reader = new JsonTextReader(streamReader)) { @@ -134,12 +133,13 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action(reader); + var properties = _serializer.Deserialize(reader); foreach (var property in properties) { if (property.CategoryId != null || property.CatalogId != null) { - propertyAssociations.Add(property.Id, new CatalogModuleModel.PropertyAssociationInfo().FromEntity(property)); + propertiesWithForeignKeys.Add(property.Clone() as Property); + //Need to reset property foreign keys to prevent FK violation during inserting into database property.CategoryId = null; property.CatalogId = null; } @@ -240,7 +240,7 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action(); foreach (var pair in associationBackupMap.Skip(i).Take(BatchSize)) @@ -261,28 +261,18 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action 0) + if (propertiesWithForeignKeys.Count > 0) { - progressInfo.Description = $"Updating {propertyAssociations.Count} property associations…"; + progressInfo.Description = $"Updating {propertiesWithForeignKeys.Count} property associations…"; progressCallback(progressInfo); - var propertiesToUpdate = new List(); - foreach (var property in properties) - { - if (propertyAssociations.ContainsKey(property.Id)) - { - propertyAssociations[property.Id].Patch(property); - propertiesToUpdate.Add(property); - } - } - var updatedPropertiesCount = propertiesToUpdate.Count; - for (int i = 0; i < updatedPropertiesCount; i += BatchSize) + + var totalCount = propertiesWithForeignKeys.Count; + for (var i = 0; i < totalCount; i += BatchSize) { - _propertyService.Update(propertiesToUpdate.Skip(i).Take(BatchSize).ToArray()); - progressInfo.Description = $"{ Math.Min(updatedPropertiesCount, i + BatchSize) } of { updatedPropertiesCount } property associations updated."; + _propertyService.Update(propertiesWithForeignKeys.Skip(i).Take(BatchSize).ToArray()); + progressInfo.Description = $"{ Math.Min(totalCount, i + BatchSize) } of { totalCount } property associations updated."; progressCallback(progressInfo); } - - _propertyService.Update(propertiesToUpdate.ToArray()); } } #endregion @@ -546,6 +536,5 @@ private void ImportImages(IHasImages[] haveImagesObjects, ExportImportProgressIn } } } - } } diff --git a/VirtoCommerce.CatalogModule.Web/Model/PropertyAssociationInfo.cs b/VirtoCommerce.CatalogModule.Web/Model/PropertyAssociationInfo.cs deleted file mode 100644 index 42b60470f..000000000 --- a/VirtoCommerce.CatalogModule.Web/Model/PropertyAssociationInfo.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using Omu.ValueInjecter; -using CatalogDomainModel = VirtoCommerce.Domain.Catalog.Model; - -namespace VirtoCommerce.CatalogModule.Web.Model -{ - /// - /// Stores and applies Property associations - /// - public class PropertyAssociationInfo - { - public string CatalogId { get; set; } - public string CategoryId { get; set; } - - public PropertyAssociationInfo FromEntity(CatalogDomainModel.Property property) - { - if (property == null) - { - throw new ArgumentNullException(nameof(property)); - } - this.InjectFrom(property); - - return this; - } - - public void Patch(CatalogDomainModel.Property property) - { - if (property == null) - { - throw new ArgumentNullException(nameof(property)); - } - property.InjectFrom(this); - } - } -} diff --git a/VirtoCommerce.CatalogModule.Web/VirtoCommerce.CatalogModule.Web.csproj b/VirtoCommerce.CatalogModule.Web/VirtoCommerce.CatalogModule.Web.csproj index b96248b89..2b69d7bdb 100644 --- a/VirtoCommerce.CatalogModule.Web/VirtoCommerce.CatalogModule.Web.csproj +++ b/VirtoCommerce.CatalogModule.Web/VirtoCommerce.CatalogModule.Web.csproj @@ -158,7 +158,6 @@ - From 24dfbf355302827561a163121420d4d637997f30 Mon Sep 17 00:00:00 2001 From: Eugeny Tatarincev Date: Tue, 25 Dec 2018 15:02:49 +0200 Subject: [PATCH 3/3] Cleanup --- .../ExportImport/CatalogExportImport.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs b/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs index da4c24df3..f2c5133a8 100644 --- a/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs +++ b/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs @@ -11,7 +11,6 @@ using VirtoCommerce.Platform.Core.Common; using VirtoCommerce.Platform.Core.ExportImport; using VirtoCommerce.Platform.Core.Settings; -using CatalogModuleModel = VirtoCommerce.CatalogModule.Web.Model; namespace VirtoCommerce.CatalogModule.Web.ExportImport { @@ -55,10 +54,12 @@ IProperyDictionaryItemService propertyDictionaryService _propertyDictionarySearchService = propertyDictionarySearchService; _propertyDictionaryService = propertyDictionaryService; - _serializer = new JsonSerializer(); - _serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; - _serializer.Formatting = Formatting.Indented; - _serializer.NullValueHandling = NullValueHandling.Ignore; + _serializer = new JsonSerializer + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + Formatting = Formatting.Indented, + NullValueHandling = NullValueHandling.Ignore + }; } private int BatchSize