Ejemplo n.º 1
0
        private BudgetIfContext AddToContext(BudgetIfContext context, Vote entity, int count, int commitCount, bool recreateContext)
        {
            context.Set <Vote>().Add(entity);

            if (count % commitCount == 0)
            {
                context.SaveChanges();
                if (recreateContext)
                {
                    context.Dispose();
                    context = new BudgetIfContext();
                    context.Configuration.AutoDetectChangesEnabled = false;
                }
            }

            return(context);
        }
Ejemplo n.º 2
0
        private void GetPoll(int pollNumber)
        {
            var str      = _webConnector.Get($"http://w1.c1.rada.gov.ua/pls/radan_gs09/ns_golos?g_id={pollNumber}", null, Encoding.GetEncoding(1251));
            var printStr = _webConnector.Get($"http://w1.c1.rada.gov.ua/pls/radan_gs09/ns_golos_print?g_id={pollNumber}&vid=1", null, Encoding.GetEncoding(1251));

            var title = GetMatch(str, titleRegex, "name");

            if (string.IsNullOrWhiteSpace(title))
            {
                return;
            }
            var pollEntry = _db.Polls.FirstOrDefault(p => p.PageId == pollNumber);

            if (pollEntry == null)
            {
                pollEntry         = new Poll();
                pollEntry.Subject = title;

                var number = GetMatch(title, numberRegex, "number");
                pollEntry.SubjectNo = number;


                var date = GetMatch(str, dateRegex, "date");
                logger.Debug("Date: " + date);

                var yes      = GetMatch(str, totalVoteRegex, "yes");
                var no       = GetMatch(str, totalVoteRegex, "no");
                var abstain  = GetMatch(str, totalVoteRegex, "abstain");
                var notvoted = GetMatch(str, totalVoteRegex, "notvoted");

                pollEntry.VoteDate   = DateTime.Parse(date, provider);
                pollEntry.Yes        = Convert.ToInt32(yes);
                pollEntry.No         = Convert.ToInt32(no);
                pollEntry.Abstain    = Convert.ToInt32(abstain);
                pollEntry.NotVoted   = Convert.ToInt32(notvoted);
                pollEntry.IsAccepted = str.Contains("<FONT COLOR=Green>Рішення прийнято</FONT>");

                var session = GetMatch(printStr, sessionRegex, "session");
                pollEntry.SessionNo = Convert.ToInt32(session);
                var vrNumber = GetMatch(printStr, vrNumberRegex, "number");
                pollEntry.VoteInVRId = Convert.ToInt32(vrNumber);

                pollEntry.PageId = pollNumber;
                _db.Polls.Add(pollEntry);
                _db.SaveChanges();
            }

            var table = GetMatch(printStr, tableRegex, "name");

            var rowMatches = rowRegex.Matches(table);

            Fraction currentFractionEntry = null;

            foreach (Match match in rowMatches)
            {
                var value = match.Groups[0].Value;
                if (value.Contains("colspan=4"))
                {
                    var fraction = GetMatch(value, fractionRegex, "fraction");
                    if (!string.IsNullOrWhiteSpace(fraction))
                    {
                        currentFractionEntry = fractionCache.FirstOrDefault(p => p.Name == fraction);
                        if (currentFractionEntry == null)
                        {
                            currentFractionEntry = new Fraction()
                            {
                                Name = fraction
                            };
                            _db.Fractions.Add(currentFractionEntry);
                            _db.SaveChanges();
                            fractionCache.Add(currentFractionEntry);
                        }
                    }
                }
            }
            logger.Debug("Add Votes");
            var voteList = new List <Vote>();

            foreach (Match match in rowMatches)
            {
                var value = match.Groups[0].Value;
                if (!value.Contains("colspan=4"))
                {
                    var voteMatches = voteRegex.Matches(value);
                    foreach (Match voteMatch in voteMatches)
                    {
                        var name = voteMatch.Groups["name"].Value;
                        var vote = voteMatch.Groups["vote"].Value.Trim();

                        var deputatEntry = deputatCache.FirstOrDefault(p => p.NameWithInitials == name);
                        if (deputatEntry == null)
                        {
                            deputatEntry = new Deputat()
                            {
                                NameWithInitials = name
                            };
                            _db.Deputats.Add(deputatEntry);
                            _db.SaveChanges();
                            deputatCache.Add(deputatEntry);
                        }
                        var answer = 0;
                        switch (vote)
                        {
                        case "Проти":
                            answer = 0;
                            break;

                        case "За":
                            answer = 1;
                            break;

                        case "Утримався":
                        case "Утрималась":
                            answer = 2;
                            break;

                        case "Не голосував":
                        case "Не голосувала":
                            answer = 3;
                            break;

                        case "Відсутня":
                        case "Відсутній":
                            answer = 4;
                            break;
                        }
                        var voteEntry = new Vote()
                        {
                            DeputatId  = deputatEntry.Id,
                            FractionId = currentFractionEntry.Id,
                            PollId     = pollEntry.Id,
                            Answer     = answer
                        };
                        voteList.Add(voteEntry);
                    }
                }
            }

            using (TransactionScope scope = new TransactionScope())
            {
                BudgetIfContext context = null;
                try
                {
                    context = new BudgetIfContext();
                    context.Configuration.AutoDetectChangesEnabled = false;
                    int count = 0;
                    foreach (var entityToInsert in voteList)
                    {
                        ++count;
                        context = AddToContext(context, entityToInsert, count, 100, true);
                    }
                    context.SaveChanges();
                }
                finally
                {
                    if (context != null)
                    {
                        context.Dispose();
                    }
                }

                scope.Complete();
            }
            logger.Debug("Votes added");
        }