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..f2c5133a8 100644 --- a/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs +++ b/VirtoCommerce.CatalogModule.Web/ExportImport/CatalogExportImport.cs @@ -54,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 @@ -76,7 +78,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 +86,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) @@ -131,7 +135,17 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action(reader); - progressInfo.Description = $"{ properties.Count() } properties are importing..."; + foreach (var property in properties) + { + if (property.CategoryId != null || property.CatalogId != null) + { + 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; + } + } + progressInfo.Description = $"{ properties.Count() } properties are importing…"; progressCallback(progressInfo); _propertyService.Update(properties); } @@ -224,10 +238,10 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action(); foreach (var pair in associationBackupMap.Skip(i).Take(BatchSize)) @@ -246,13 +260,28 @@ public void DoImport(Stream stream, PlatformExportManifest manifest, Action 0) + { + progressInfo.Description = $"Updating {propertiesWithForeignKeys.Count} property associations…"; + progressCallback(progressInfo); + + var totalCount = propertiesWithForeignKeys.Count; + for (var i = 0; i < totalCount; i += BatchSize) + { + _propertyService.Update(propertiesWithForeignKeys.Skip(i).Take(BatchSize).ToArray()); + progressInfo.Description = $"{ Math.Min(totalCount, i + BatchSize) } of { totalCount } property associations updated."; + progressCallback(progressInfo); + } + } } #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 +304,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 +329,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 +349,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 +378,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 }; @@ -508,6 +537,5 @@ private void ImportImages(IHasImages[] haveImagesObjects, ExportImportProgressIn } } } - } }