Beispiel #1
0
        public void Start(Security security, Type dataType, object arg, IEnumerableEx values, object path)
        {
            if (dataType == null)
            {
                throw new ArgumentNullException("dataType");
            }

            if (security == null && dataType != typeof(NewsMessage) && dataType != typeof(SecurityMessage))
            {
                throw new ArgumentNullException("security");
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            var currProgress = 5;

            var valuesPerPercent = (values.Count / (100 - currProgress)).Max(1);
            var valuesPerCount   = (valuesPerPercent / 10).Max(1);
            var currCount        = 0;
            var valuesCount      = 0;

            Func <int, bool> isCancelled = count =>
            {
                var isCancelling = _worker.CancellationPending;

                if (!isCancelling)
                {
                    valuesCount += count;

                    if (valuesCount / valuesPerPercent > currProgress)
                    {
                        currProgress = valuesCount / valuesPerPercent;
                        _worker.ReportProgress(currProgress);
                    }

                    if (valuesCount > currCount)
                    {
                        currCount = valuesCount + valuesPerCount;
                        this.GuiAsync(() => UpdateCount(valuesCount));
                    }
                }

                return(isCancelling);
            };

            string       fileName;
            BaseExporter exporter;

            switch (_button.ExportType)
            {
            case ExportTypes.Excel:
                fileName = (string)path;
                exporter = new ExcelExporter(security, arg, isCancelled, fileName, () => GuiDispatcher.GlobalDispatcher.AddSyncAction(TooManyValues));
                break;

            case ExportTypes.Xml:
                fileName = (string)path;
                exporter = new XmlExporter(security, arg, isCancelled, fileName);
                break;

            case ExportTypes.Txt:
                var wnd = new ExportTxtPreviewWindow
                {
                    DataType = dataType,
                    Arg      = arg
                };

                var registry = ((HydraEntityRegistry)ConfigManager.GetService <IEntityRegistry>()).Settings.TemplateTxtRegistry;

                if (dataType == typeof(SecurityMessage))
                {
                    wnd.TxtTemplate = registry.TemplateTxtSecurity;
                }
                else if (dataType == typeof(NewsMessage))
                {
                    wnd.TxtTemplate = registry.TemplateTxtNews;
                }
                else if (dataType.IsSubclassOf(typeof(CandleMessage)))
                {
                    wnd.TxtTemplate = registry.TemplateTxtCandle;
                }
                else if (dataType == typeof(Level1ChangeMessage))
                {
                    wnd.TxtTemplate = registry.TemplateTxtLevel1;
                }
                else if (dataType == typeof(QuoteChangeMessage))
                {
                    wnd.TxtTemplate = registry.TemplateTxtDepth;
                }
                else if (dataType == typeof(ExecutionMessage))
                {
                    if (arg == null)
                    {
                        throw new ArgumentNullException("arg");
                    }

                    switch ((ExecutionTypes)arg)
                    {
                    case ExecutionTypes.Tick:
                        wnd.TxtTemplate = registry.TemplateTxtTick;
                        break;

                    case ExecutionTypes.Order:
                    case ExecutionTypes.Trade:
                        wnd.TxtTemplate = registry.TemplateTxtTransaction;
                        break;

                    case ExecutionTypes.OrderLog:
                        wnd.TxtTemplate = registry.TemplateTxtOrderLog;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                else
                {
                    throw new ArgumentOutOfRangeException("dataType", dataType, LocalizedStrings.Str721);
                }


                if (!wnd.ShowModal(this))
                {
                    return;
                }

                if (dataType == typeof(SecurityMessage))
                {
                    registry.TemplateTxtSecurity = wnd.TxtTemplate;
                }
                else if (dataType == typeof(NewsMessage))
                {
                    registry.TemplateTxtNews = wnd.TxtTemplate;
                }
                else if (dataType.IsSubclassOf(typeof(CandleMessage)))
                {
                    registry.TemplateTxtCandle = wnd.TxtTemplate;
                }
                else if (dataType == typeof(Level1ChangeMessage))
                {
                    registry.TemplateTxtLevel1 = wnd.TxtTemplate;
                }
                else if (dataType == typeof(QuoteChangeMessage))
                {
                    registry.TemplateTxtDepth = wnd.TxtTemplate;
                }
                else if (dataType == typeof(ExecutionMessage))
                {
                    if (arg == null)
                    {
                        throw new ArgumentNullException("arg");
                    }

                    switch ((ExecutionTypes)arg)
                    {
                    case ExecutionTypes.Tick:
                        registry.TemplateTxtTick = wnd.TxtTemplate;
                        break;

                    case ExecutionTypes.Order:
                    case ExecutionTypes.Trade:
                        registry.TemplateTxtTransaction = wnd.TxtTemplate;
                        break;

                    case ExecutionTypes.OrderLog:
                        registry.TemplateTxtOrderLog = wnd.TxtTemplate;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                else
                {
                    throw new ArgumentOutOfRangeException("dataType", dataType, LocalizedStrings.Str721);
                }

                fileName = (string)path;
                exporter = new TextExporter(security, arg, isCancelled, fileName, wnd.TxtTemplate, wnd.TxtHeader);
                break;

            case ExportTypes.Sql:
                fileName = null;
                exporter = new DatabaseExporter(security, arg, isCancelled, (DatabaseConnectionPair)path)
                {
                    CheckUnique = false
                };
                break;

            case ExportTypes.Bin:
                var drive = (IMarketDataDrive)path;
                fileName = drive is LocalMarketDataDrive ? drive.Path : null;
                exporter = new BinExporter(security, arg, isCancelled, drive);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            CreateWorker(values.Count, fileName);

            _worker.DoWork += (s, e) =>
            {
                _worker.ReportProgress(currProgress);

                exporter.Export(dataType, values);

                _worker.ReportProgress(100);
                Thread.Sleep(500);
                _worker.ReportProgress(0);
            };

            _worker.RunWorkerAsync();
        }
        public void Start(Security security, Type dataType, object arg, IEnumerableEx values, object path)
        {
            if (security == null && dataType != typeof(News) && dataType != typeof(Security))
            {
                throw new ArgumentNullException("security");
            }

            if (dataType == null)
            {
                throw new ArgumentNullException("dataType");
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            var currProgress = 5;

            var valuesPerPercent = (values.Count / (100 - currProgress)).Max(1);
            var valuesPerCount   = (valuesPerPercent / 10).Max(1);
            var currCount        = 0;
            var valuesCount      = 0;

            Func <int, bool> isCancelled = count =>
            {
                var isCancelling = _worker.CancellationPending;

                if (!isCancelling)
                {
                    valuesCount += count;

                    if (valuesCount / valuesPerPercent > currProgress)
                    {
                        currProgress = valuesCount / valuesPerPercent;
                        _worker.ReportProgress(currProgress);
                    }

                    if (valuesCount > currCount)
                    {
                        currCount = valuesCount + valuesPerCount;
                        this.GuiAsync(() => UpdateCount(valuesCount));
                    }
                }

                return(isCancelling);
            };

            string       fileName;
            BaseExporter exporter;

            switch (_button.ExportType)
            {
            case ExportTypes.Excel:
                fileName = (string)path;
                exporter = new ExcelExporter(security, arg, isCancelled, fileName, () => GuiDispatcher.GlobalDispatcher.AddSyncAction(TooManyValues));
                break;

            case ExportTypes.Xml:
                fileName = (string)path;
                exporter = new XmlExporter(security, arg, isCancelled, fileName);
                break;

            case ExportTypes.Txt:
                fileName = (string)path;
                exporter = new TextExporter(security, arg, isCancelled, fileName);
                break;

            case ExportTypes.Sql:
                fileName = null;
                exporter = new DatabaseExporter(security, arg, isCancelled, (DatabaseConnectionPair)path)
                {
                    CheckUnique = false
                };
                break;

            case ExportTypes.Bin:
                var drive = (IMarketDataDrive)path;
                fileName = drive is LocalMarketDataDrive ? drive.Path : null;
                exporter = new BinExporter(security, arg, isCancelled, drive);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            CreateWorker(values.Count, fileName);

            _worker.DoWork += (s, e) =>
            {
                _worker.ReportProgress(currProgress);

                exporter.Export(dataType, values);

                _worker.ReportProgress(100);
                Thread.Sleep(500);
                _worker.ReportProgress(0);
            };

            _worker.RunWorkerAsync();
        }