Exemplo n.º 1
0
        protected override void OnStartup(StartupEventArgs e)
        {
            AppDomain.CurrentDomain.UnhandledException += onAppDomainUnhandledException;

            base.OnStartup(e);

            m_connectionString = ConfigurationManager.ConnectionStrings["default"].ConnectionString;

            var window = new MainWindow();
            var mainViewModel = new MainViewModel();
            ILogger logger = mainViewModel;
            var repository = new Repository(m_connectionString, logger );
            var vmRuelect = new RuelectViewModel(logger)
                                {
                                    Repository = repository,
                                    Downloader = new FileDownloader()
                                };
            var vmKartaitogov = new KartaitogovViewModel(logger)
                                    {
                                        Repository = repository,
                                        Downloader = new FileDownloader()
                                    };
            var vmAnalyze = new AnalyzeViewModel(logger)
                                {
                                    Repository = repository
                                };
            window.DataContext = mainViewModel;
            ((TabItem)window.tabs.Items[0]).DataContext = vmRuelect;
            ((TabItem)window.tabs.Items[1]).DataContext = vmKartaitogov;
            ((TabItem)window.tabs.Items[2]).DataContext = vmAnalyze;

            TaskScheduler.UnobservedTaskException += (s, ea) =>
                                                     	{
                                                            logger.LogError("UnobservedTaskException: " + ea.Exception.GetBaseException().ToString());
                                                            // prevent app crashing:
                                                            ea.SetObserved();
                                                     	};

            Application.Current.MainWindow = window;
            runInitialChecks(logger);
            window.Show();
        }
Exemplo n.º 2
0
        public void ParseKartaitogovWebPage()
        {
            var viewModel = new KartaitogovViewModel(new Logger());
            //string resourceName = "Loader.Tests.diff.htm";
            //Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
            var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Kartaitogov", "diff.htm");
            //byte[] webpageContent = Encoding.UTF8.GetBytes(File.ReadAllText(filePath));

            /*
            viewModel.Downloader = new MockDownloader(webpageContent);

            var task = viewModel.downloadImages();
            Assert.IsTrue(task.Wait(TimeSpan.FromSeconds(10)));
            Assert.IsNull(viewModel.LastError, "Error occured: " + viewModel.LastError);

            */

            HtmlDocument htmlDoc = new HtmlDocument();
            Encoding encoding = htmlDoc.DetectEncoding(filePath) ?? Encoding.UTF8;
            htmlDoc.Load(filePath, encoding);

            var reUikNumber = new System.Text.RegularExpressions.Regex(@"\d+");
            using (var con = new SqlConnection("Data Source=.;Initial Catalog = elect;Integrated Security=True"))
            {
                con.Open();
                var cmdRegion = con.CreateCommand();
                cmdRegion.CommandText = "select ObjectID from Region where name = @pName";
                var sqlParamRegName = new SqlParameter("pName", SqlDbType.VarChar);
                cmdRegion.Parameters.Add(sqlParamRegName);

                var cmdComission = con.CreateCommand();
                cmdComission.CommandText = "select ObjectID from Comission where Region = @pRegion and [Number] = @pNumber";
                var sqlParamComNum = new SqlParameter("pNumber", SqlDbType.Int);
                cmdComission.Parameters.Add(sqlParamComNum);
                var sqlParamRegId = new SqlParameter("pRegion", SqlDbType.UniqueIdentifier);
                cmdComission.Parameters.Add(sqlParamRegId);

                string regionName = null;
                Guid regionId = Guid.Empty;
                foreach (HtmlNode headUik in htmlDoc.DocumentNode.SelectNodes("//h3[@class='uik']"))
                {
                    var regionNode = headUik.SelectSingleNode("preceding-sibling::h2[@class='oblast']");
                    var uikText = headUik.InnerText;
                    if (regionNode != null)
                    {
                        var match = reUikNumber.Match(uikText);
                        if (!match.Success)
                        {
                            Console.WriteLine("ERROR: Can't parse UIK number: " + uikText);
                        }
                        else
                        {
                            if (regionName != regionNode.InnerText)
                            {
                                regionName = regionNode.InnerText;
                                sqlParamRegName.Value = regionName;
                                var regionIdRaw = cmdRegion.ExecuteScalar();
                                if (regionIdRaw != null)
                                    regionId = (Guid)regionIdRaw;
                                else
                                {
                                    regionId = Guid.Empty;
                                    Console.WriteLine("WARN: Can't find in DB a region with name: " + regionName);
                                }
                            }

                            sqlParamRegId.Value = regionId;
                            int comissionNum = Int32.Parse(match.Value);
                            sqlParamComNum.Value = comissionNum;
                            var comissionIdRaw = cmdComission.ExecuteScalar();
                            Guid comissionId;
                            if (comissionIdRaw != null)
                                comissionId = (Guid)comissionIdRaw;
                            else
                                comissionId = Guid.Empty;
                            //Console.WriteLine(regionNode.InnerText + " : " + uikText.Substring(uikText.IndexOf('\n', 0, 2)));
                            Console.WriteLine(regionName + "(" + regionId + ")" + " / " + comissionNum + "(" + comissionId + ")");
                        }
                    }
                    else
                    {
                        Console.WriteLine("ERROR: Can't find region node!");
                    }
                }
            }
        }