public virtual void DoExport(StoreExportConfiguration exportConfiguration, ExportNotification notification) { //Notification notification.Description = "loading ..."; _notifier.Upsert(notification); try { var store = _storeService.GetById(exportConfiguration.StoreId); var settings = new List <SettingEntry>(); var backup = new Backup(_blobStorageProvider, _blobUrlResolver); // Add objects to backup backup.AddEntry(_stopeXmlName, store); // Add collections of the same types elements if (!exportConfiguration.IsDisableLanguages) { backup.AddEntry(_storeLanguagesXmlName, store.Languages.ToArray()); } if (!exportConfiguration.IsDisableCurrencies) { backup.AddEntry(_storeCurrenciesXmlName, store.Currencies.ToArray()); } if (!exportConfiguration.IsDisableSeo) { backup.AddEntry(_storeSeoXmlName, store.SeoInfos.ToArray()); } // Add collections of different types elements if (!exportConfiguration.IsDisablePamentMethods) { foreach (var paymentMethod in store.PaymentMethods.Where(x => x.IsActive)) { //don't export Settings because security backup.AddEntry(string.Format("{0}{1}.xml", _paymentMethodXmlNamePrefix, paymentMethod.Code), paymentMethod); } } if (!exportConfiguration.IsDisableShipmentMethods) { foreach (var shippingMethod in store.ShippingMethods.Where(x => x.IsActive)) { backup.AddEntry(string.Format("{0}{1}.xml", _shippingMethodXmlNamePrefix, shippingMethod.Code), shippingMethod); settings.AddRange(shippingMethod.Settings); } } settings.AddRange(store.Settings); backup.AddEntry(_settingsXmlName, settings.ToArray()); // Create backup file var zipUrl = backup.Save("Store-" + (store.Name ?? store.Id) + ".zip"); notification.DownloadUrl = zipUrl; notification.Description = "Export finished"; } catch (Exception ex) { notification.Description = "Export error"; notification.ErrorCount++; notification.Errors.Add(ex.ToString()); } finally { notification.Finished = DateTime.UtcNow; _notifier.Upsert(notification); } }
public virtual void DoImport(StoreImportConfiguration importConfiguration, ImportNotification notification) { if (string.IsNullOrEmpty(importConfiguration.FileUrl)) { throw new Exception("FileUrl is null or empty. Can't import."); } //Notification notification.Description = "loading ..."; _notifier.Upsert(notification); try { var backup = new Backup(_blobStorageProvider, null); backup.OpenBackup(importConfiguration.FileUrl); // Extract objects from backup var store = backup.LoadObject <Store>(_stopeXmlName); if (store != null) { store.Languages = backup.LoadObject <string[]>(_storeLanguagesXmlName); store.Currencies = backup.LoadObject <CurrencyCodes[]>(_storeCurrenciesXmlName); store.SeoInfos = backup.LoadObject <SeoInfo[]>(_storeSeoXmlName); store.PaymentMethods = backup.LoadObjectsByMask <PaymentMethod>(_paymentMethodXmlNamePrefix).ToArray(); store.ShippingMethods = backup.LoadObjectsByMask <ShippingMethod>(_shippingMethodXmlNamePrefix).ToArray(); var settings = backup.LoadObject <SettingEntry[]>(_settingsXmlName); var storeSettings = settings.Where(x => x.ObjectId == store.Id).ToArray(); // Clear ids of collections to prevent dublicate ids //todo check exists or not added payment and shipping modules if (store.PaymentMethods != null) { store.PaymentMethods.ForEach(x => x.Id = null); } if (store.ShippingMethods != null) { store.ShippingMethods.ForEach(x => x.Id = null); } if (store.SeoInfos != null) { store.SeoInfos.ForEach(x => x.Id = null); } if (!string.IsNullOrEmpty(importConfiguration.NewStoreName)) { store.Name = importConfiguration.NewStoreName; } if (!string.IsNullOrEmpty(importConfiguration.NewStoreId)) { store.Id = importConfiguration.NewStoreId; } if (_storeService.GetById(store.Id) != null) { //todo change generation code store.Id = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture).GenerateSlug(); } SaveStore(store); storeSettings.ForEach(x => x.ObjectId = store.Id); _settingsManager.SaveSettings(storeSettings); } backup.CloseBackup(); } catch (Exception ex) { notification.Description = "Export error"; notification.ErrorCount++; notification.Errors.Add(ex.ToString()); } finally { notification.Finished = DateTime.UtcNow; notification.Description = "Import finished" + (notification.Errors.Any() ? " with errors" : " successfully"); _notifier.Upsert(notification); } }