public void ModulesImportBackground(ImportManifest importManifest, ModulePublishingPushNotification pushNotification)
        {
            Action<ImportProcessInfo> progressCallback = (x) =>
            {
                pushNotification.InjectFrom(x);
                pushNotification.Errors = x.Errors;
                _pushNotifier.Upsert(pushNotification);
            };

            try
            {
                _moduleImporter.DoImport(importManifest, progressCallback);
            }
            catch (Exception ex)
            {
                pushNotification.Errors.Add(ex.ToString());
            }
            finally
            {
                pushNotification.Finished = DateTime.UtcNow;
                pushNotification.Description = "Import finished";
                _pushNotifier.Upsert(pushNotification);
            }

        }
        // add not existing products or variations.
        public void DoImport(ImportManifest importManifest, Action<ImportProcessInfo> progressCallback)
        {
            var progressInfo = new ImportProcessInfo();
            progressInfo.Description = "Importing ...";
            progressCallback(progressInfo);

            _defaultCategory = GetCategoriesByCode(importManifest.DefaultCategoryCode, importManifest.CatalogId);
            _catalogProperties = _propertyService.GetAllCatalogProperties(importManifest.CatalogId);
            var zipModulePaths = Directory.GetFiles(importManifest.PackagesPath);
            progressInfo.TotalCount = zipModulePaths.Count();

            foreach (var zipModulePath in zipModulePaths)
            {
                ModuleManifest manifest = null;
                byte[] icon = null;
                using (ZipArchive archive = ZipFile.OpenRead(zipModulePath))
                {
                    var manifestEntry = archive.Entries.FirstOrDefault(x => x.Name == importManifest.ManifestFileName);
                    using (var manifestStream = manifestEntry.Open())
                    {
                        manifest = ManifestReader.Read(manifestStream);
                    }

                    icon = ReadIcon(archive, manifest.IconUrl);
                }

                var publishingResult = Publish(manifest, zipModulePath, icon);

                progressInfo.CreatedCount += publishingResult == PublishingResult.Product ? 1 : 0;
                progressInfo.UpdatedCount += publishingResult == PublishingResult.Variation ? 1 : 0;
                progressInfo.ProcessedCount++;
                progressCallback(progressInfo);
            }
        }
Exemple #3
0
        // add not existing products or variations.
        public void DoImport(ImportManifest importManifest, Action <ImportProcessInfo> progressCallback)
        {
            var progressInfo = new ImportProcessInfo();

            progressInfo.Description = "Importing ...";
            progressCallback(progressInfo);

            _defaultCategory   = GetCategoriesByCode(importManifest.DefaultCategoryCode, importManifest.CatalogId);
            _catalogProperties = _propertyService.GetAllCatalogProperties(importManifest.CatalogId);
            var zipModulePaths = Directory.GetFiles(importManifest.PackagesPath);

            progressInfo.TotalCount = zipModulePaths.Count();

            foreach (var zipModulePath in zipModulePaths)
            {
                ModuleManifest manifest = null;
                byte[]         icon     = null;
                using (ZipArchive archive = ZipFile.OpenRead(zipModulePath))
                {
                    var manifestEntry = archive.Entries.FirstOrDefault(x => x.Name == importManifest.ManifestFileName);
                    using (var manifestStream = manifestEntry.Open())
                    {
                        manifest = ManifestReader.Read(manifestStream);
                    }

                    icon = ReadIcon(archive, manifest.IconUrl);
                }

                var publishingResult = Publish(manifest, zipModulePath, icon);

                progressInfo.CreatedCount += publishingResult == PublishingResult.Product ? 1 : 0;
                progressInfo.UpdatedCount += publishingResult == PublishingResult.Variation ? 1 : 0;
                progressInfo.ProcessedCount++;
                progressCallback(progressInfo);
            }
        }
        public IHttpActionResult Publish(ImportManifest importManifest)
        {
            var settingsManager = ServiceLocator.Current.GetInstance<ISettingsManager>();
            var packagesPath = settingsManager.GetValue("VirtoCommerce.ModulesPublishing.AppStoreImport.PackagesPath", String.Empty);

            importManifest.DefaultCategoryCode = settingsManager.GetValue("VirtoCommerce.ModulesPublishing.AppStoreImport.DefaultCategoryCode", String.Empty);
            importManifest.PackagesPath = HttpContext.Current.Server.MapPath(packagesPath);

            var notification = new ModulePublishingPushNotification(CurrentPrincipal.GetCurrentUserName())
            {
                Title = "Import applications task",
                Description = "Task added and will start soon...."
            };

            if (string.IsNullOrEmpty(importManifest.DefaultCategoryCode))
            {
                notification.Errors.Add("Set 'Category code' setting, before import.");
            }
            if (string.IsNullOrEmpty(packagesPath))
            {
                notification.Errors.Add("Set 'Packages folder' setting, before import.");
            }
            if (!Directory.Exists(importManifest.PackagesPath))
            {
                notification.Errors.Add(string.Format("Path doesn't exists: {0}", importManifest.PackagesPath));
            }

            if (notification.Errors.Count == 0)
            {
                BackgroundJob.Enqueue(() => ModulesImportBackground(importManifest, notification));
            }

            _pushNotifier.Upsert(notification);
            return Ok(notification);

        }