public void SampleDataImportBackground(string filePath, SampleDataImportPushNotification pushNotification)
        {
            Action <ExportImportProgressInfo> progressCallback = (x) =>
            {
                pushNotification.InjectFrom(x);
                _pushNotifier.Upsert(pushNotification);
            };

            try
            {
                using (var stream = File.Open(filePath, FileMode.Open))
                {
                    var manifest = _platformExportManager.ReadExportManifest(stream);
                    if (manifest != null)
                    {
                        _platformExportManager.Import(stream, manifest, progressCallback);
                    }
                }
            }
            catch (Exception ex)
            {
                pushNotification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                pushNotification.Finished = DateTime.UtcNow;
                _pushNotifier.Upsert(pushNotification);
            }
        }
예제 #2
0
        public async Task SampleDataImportBackgroundAsync(Uri url, SampleDataImportPushNotification pushNotification, IJobCancellationToken cancellationToken, PerformContext context)
        {
            void progressCallback(ExportImportProgressInfo x)
            {
                pushNotification.Path(x);
                pushNotification.JobId = context.BackgroundJob.Id;
                _pushNotifier.Send(pushNotification);
            }

            try
            {
                pushNotification.Description = "Start downloading from " + url;

                await _pushNotifier.SendAsync(pushNotification);

                var tmpPath = Path.GetFullPath(_platformOptions.LocalUploadFolderPath);
                if (!Directory.Exists(tmpPath))
                {
                    Directory.CreateDirectory(tmpPath);
                }

                var tmpFilePath = Path.Combine(tmpPath, Path.GetFileName(url.ToString()));
                using (var client = new WebClient())
                {
                    client.DownloadProgressChanged += async(sender, args) =>
                    {
                        pushNotification.Description = string.Format("Sample data {0} of {1} downloading...", args.BytesReceived.ToHumanReadableSize(), args.TotalBytesToReceive.ToHumanReadableSize());
                        await _pushNotifier.SendAsync(pushNotification);
                    };
                    var task = client.DownloadFileTaskAsync(url, tmpFilePath);
                    task.Wait();
                }
                using (var stream = new FileStream(tmpFilePath, FileMode.Open))
                {
                    var manifest = _platformExportManager.ReadExportManifest(stream);
                    if (manifest != null)
                    {
                        await _platformExportManager.ImportAsync(stream, manifest, progressCallback, new JobCancellationTokenWrapper(cancellationToken));
                    }
                }
            }
            catch (JobAbortedException)
            {
                //do nothing
            }
            catch (Exception ex)
            {
                pushNotification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                _settingsManager.SetValue(PlatformConstants.Settings.Setup.SampleDataState.Name, SampleDataState.Completed);
                pushNotification.Description = "Import finished";
                pushNotification.Finished    = DateTime.UtcNow;
                await _pushNotifier.SendAsync(pushNotification);
            }
        }
        public void SampleDataImportBackground(Uri url, string tmpPath, SampleDataImportPushNotification pushNotification)
        {
            Action <ExportImportProgressInfo> progressCallback = x =>
            {
                pushNotification.InjectFrom(x);
                _pushNotifier.Upsert(pushNotification);
            };

            try
            {
                pushNotification.Description = "Start downloading from " + url;
                _pushNotifier.Upsert(pushNotification);

                if (!Directory.Exists(tmpPath))
                {
                    Directory.CreateDirectory(tmpPath);
                }

                var tmpFilePath = Path.Combine(tmpPath, Path.GetFileName(url.ToString()));
                using (var client = new WebClient())
                {
                    client.DownloadProgressChanged += (sender, args) =>
                    {
                        pushNotification.Description = string.Format("Sample data {0} of {1} downloading...", args.BytesReceived.ToHumanReadableSize(), args.TotalBytesToReceive.ToHumanReadableSize());
                        _pushNotifier.Upsert(pushNotification);
                    };
                    var task = client.DownloadFileTaskAsync(url, tmpFilePath);
                    task.Wait();
                }
                using (var stream = File.Open(tmpFilePath, FileMode.Open))
                {
                    var manifest = _platformExportManager.ReadExportManifest(stream);
                    if (manifest != null)
                    {
                        _platformExportManager.Import(stream, manifest, progressCallback);
                    }
                }
            }
            catch (Exception ex)
            {
                pushNotification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                _settingsManager.SetValue(_sampledataStateSetting, SampleDataState.Completed);
                pushNotification.Description = "Import finished";
                pushNotification.Finished    = DateTime.UtcNow;
                _pushNotifier.Upsert(pushNotification);
            }
        }
예제 #4
0
        public IHttpActionResult TryToImportSampleData([FromUri] string url = null)
        {
            lock (_lockObject)
            {
                //Sample data initialization
                if (Uri.IsWellFormedUriString(url, UriKind.Absolute) && !_settingsManager.GetValue("VirtoCommerce:SampleDataInstalled", false))
                {
                    _settingsManager.SetValue("VirtoCommerce:SampleDataInstalled", true);
                    var pushNotification = new SampleDataImportPushNotification("System");
                    _pushNotifier.Upsert(pushNotification);
                    BackgroundJob.Enqueue(() => SampleDataImportBackground(new Uri(url), HostingEnvironment.MapPath("~/App_Data/Uploads/"), pushNotification));

                    return(Ok(pushNotification));
                }
            }
            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult TryToImportSampleData()
        {
            lock (_lockObject)
            {
                //Sample data initialization
                var sampleDataPath = ConfigurationManager.AppSettings.GetValue("VirtoCommerce:SampleDataPath", string.Empty);
                if (!String.IsNullOrEmpty(sampleDataPath) && !_settingsManager.GetValue("VirtoCommerce:SampleDataInstalled", false))
                {
                    _settingsManager.SetValue("VirtoCommerce:SampleDataInstalled", true);

                    var pushNotification = new SampleDataImportPushNotification("System");
                    _pushNotifier.Upsert(pushNotification);
                    BackgroundJob.Enqueue(() => SampleDataImportBackground(HostingEnvironment.MapPath(sampleDataPath), pushNotification));

                    return(Ok(pushNotification));
                }
            }
            return(StatusCode(HttpStatusCode.NoContent));
        }
예제 #6
0
        public ActionResult <SampleDataImportPushNotification> ImportSampleData([FromQuery] string url = null)
        {
            lock (_lockObject)
            {
                var sampleDataState = EnumUtility.SafeParse(_settingsManager.GetValue(PlatformConstants.Settings.Setup.SampleDataState.Name, SampleDataState.Undefined.ToString()), SampleDataState.Undefined);
                if (sampleDataState == SampleDataState.Undefined && Uri.IsWellFormedUriString(url, UriKind.Absolute))
                {
                    _settingsManager.SetValue(PlatformConstants.Settings.Setup.SampleDataState.Name, SampleDataState.Processing);
                    var pushNotification = new SampleDataImportPushNotification(User.Identity.Name);
                    _pushNotifier.Send(pushNotification);
                    var jobId = BackgroundJob.Enqueue(() => SampleDataImportBackgroundAsync(new Uri(url), Path.GetFullPath(_platformOptions.LocalUploadFolderPath), pushNotification, JobCancellationToken.Null, null));
                    pushNotification.JobId = jobId;

                    return(Ok(pushNotification));
                }
            }

            return(Ok());
        }
        public IHttpActionResult ImportSampleData([FromUri] string url = null)
        {
            lock (_lockObject)
            {
                var sampleDataState = EnumUtility.SafeParse(_settingsManager.GetValue(_sampledataStateSetting, SampleDataState.Undefined.ToString()), SampleDataState.Undefined);
                if (sampleDataState == SampleDataState.Undefined)
                {
                    //Sample data initialization
                    if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
                    {
                        _settingsManager.SetValue(_sampledataStateSetting, SampleDataState.Processing);
                        var pushNotification = new SampleDataImportPushNotification("System");
                        _pushNotifier.Upsert(pushNotification);
                        BackgroundJob.Enqueue(() => SampleDataImportBackground(new Uri(url), HostingEnvironment.MapPath(Startup.VirtualRoot + "/App_Data/Uploads/"), pushNotification));

                        return(Ok(pushNotification));
                    }
                }
            }
            return(StatusCode(HttpStatusCode.NoContent));
        }