Exemplo n.º 1
0
 private void onSelectedProtocolChanged(PollProtocol protocol)
 {
 }
Exemplo n.º 2
0
 public bool EqualsTo(PollProtocol protocol)
 {
     // TODO
     return true;
 }
Exemplo n.º 3
0
        public IEnumerable<PollProtocol> LoadProtocols(IRegionResolver regionResolver)
        {
            while (Reader.ReadNextRecord())
            {
                string regionName = Reader[ColumnIndexRegion];
                Int32 commision;

                Region region = regionResolver.GetOrCreate(regionName);

                if (!Int32.TryParse(Reader[ColumnIndexComission], out commision))
                    throw new InvalidDataException("Can't parse as Int32: " + Reader[ColumnIndexComission] + ". Row #" + Reader.CurrentRecordIndex);
                var results = new int[ResultColumnsCount];

                for (int i = 0; i < ResultColumnsCount; i++)
                {
                    results[i] = Int32.Parse(Reader[i + FirstResultColumnIndex]);
                }
                // Images
                List<PollProtocolImage> images = null;
                if (FirstImageUriColumnIndex > -1)
                {
                    images = new List<PollProtocolImage>();
                    Uri uri;
                    for (int i = FirstImageUriColumnIndex; i < Reader.FieldCount; i++)
                    {
                        string value = Reader[i];
                        if (string.IsNullOrWhiteSpace(value))
                            break;
                        if (!Uri.TryCreate(value, UriKind.Absolute, out uri))
                            throw new InvalidDataException("Can't parse as Uri: " + value + ". Row #" + Reader.CurrentRecordIndex);

                        images.Add(new PollProtocolImage { Uri = uri.ToString() });
                    }
                }

                var result = new PollProtocol
                             	{
                             		Region = region,
                             		Comission = commision,
                             		Results = results,
                             		Images = images
                             	};
                yield return result;
            }
        }
Exemplo n.º 4
0
        protected void saveProtocol(PollProtocol protocol, bool isNewProvider, ref int countTotal, ref int countNew, ref int countUpdated)
        {
            PollProtocol existingProtocol = null;
            bool needSave = true;
            if (!isNewProvider)
            {
                existingProtocol = Repository.LoadProtocol(protocol);
            }
            if (existingProtocol != null)
            {
                needSave = !existingProtocol.EqualsTo(protocol);
                if (needSave)
                    countUpdated++;
                protocol.Id = existingProtocol.Id;
            }
            else
                countNew++;

            if (needSave)
            {
                Repository.SaveProtocol(protocol,
                                        new ProtocolSaveOption
                                        {
                                            UnknownRegionAction = UnknownRegionAction,
                                            UnknownComissionAction = UnknownComissionAction,
                                            DownloadNotifier = new DownloadNotifier(Logger),
                                            CancellationToken = Tcs.Token
                                        });

                log(string.Format("#{0} protocol saved. Region: {1}, comission: {2}. Images: {3}",
                                  countTotal, protocol.Region.Name, protocol.Comission, protocol.Images.Count));
            }
            else
            {
                log(string.Format("#{0} protocol skipped. Region: {1}, comission: {2}. Images: {3}",
                                  countTotal, protocol.Region.Name, protocol.Comission, protocol.Images.Count));
            }
            countTotal++;
        }
Exemplo n.º 5
0
 private PollProtocol assertProtocol(SqlConnection con, Guid providerId, Guid comissionId)
 {
     var cmd = con.CreateCommand();
     cmd.CommandText = "select * from Protocol where Provider='" + providerId + "' and Comission='" + comissionId + "'";
     using (var reader = cmd.ExecuteReader())
     {
         Assert.IsTrue(reader.Read());
         Assert.AreEqual(3+18, reader.FieldCount);	// ObjectId, Provider, Comissin + Value1..Value18
         var protocol = new PollProtocol()
                        	{
                        		Id = reader.GetGuid(0),
                        		Results = new int[18]
                        	};
         for (int i = 0; i < 18; i++)
         {
             protocol.Results[i] = reader.GetInt32(i + 3);
         }
         return protocol;
     }
 }
Exemplo n.º 6
0
        public IEnumerable<PollProtocol> LoadProtocols(IRegionResolver regionResolver)
        {
            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(m_pageContent);

            var reUikNumber = new Regex(@"\d+", RegexOptions.Compiled | RegexOptions.Multiline);

            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)
                {
                    m_logger.LogError("Can't find region node in the html");
                }
                else
                {
                    var match = reUikNumber.Match(uikText);
                    if (!match.Success)
                    {
                        m_logger.LogError("Can't parse comission number: " + uikText);
                        continue;
                    }
                    int comissionNum;
                    if (!Int32.TryParse(match.Value, out comissionNum))
                    {
                        m_logger.LogError("Can't parse parse comission number as integer: " + match.Value);
                        continue;
                    }

                    string regionName = regionNode.InnerText;
                    Region region = regionResolver.GetOrCreate(regionName);

                    // Results
                    var tableResults = headUik.SelectSingleNode("following-sibling::table[@class='observers_data']");
                    if (tableResults == null)
                    {
                        m_logger.LogError(String.Format("{0}/{1}: Can't find table with protocols results (table[@class='observers_data'])", regionName, comissionNum));
                        continue;
                    }
                    var trAltResults = tableResults.SelectSingleNode("descendant::tr[@class='obs']");
                    if (trAltResults == null)
                    {
                        m_logger.LogError(String.Format("{0}/{1}: Can't find table row with alternative protocol results (tr[@class='obs'])", regionName, comissionNum));
                        continue;
                    }

                    var tdResults = trAltResults.SelectNodes("td");
                    var results = new int[25];
                    if (tdResults.Count != 26)
                    {
                        m_logger.LogError(String.Format("{0}/{1}: Number of columns in row with protocol result doesn't equal expected value (26), but equals to {2}", regionName, comissionNum, tdResults.Count));
                        continue;
                    }

                    bool needBreak = false;
                    for (int i = 1; i < 26; i++)
                    {
                        string votesRaw = tdResults[i].InnerText;
                        int votes;
                        if (!Int32.TryParse(votesRaw, out votes))
                        {
                            m_logger.LogError(String.Format("{0}/{1}: Can't parse votes count as integer: {2}", regionName, comissionNum, tdResults.Count));
                            needBreak = true;
                            break;
                        }
                        results[i - 1] = votes;
                    }
                    if (needBreak)
                        continue;

                    // Images:
                    var imagesNode = headUik.SelectSingleNode("following-sibling::div[@class='photo_names']");
                    if (imagesNode == null)
                    {
                        m_logger.LogError(String.Format("{0}/{1}: Can't find div with protocol images (div[@class='photo_names'])", regionName, comissionNum));
                        continue;
                    }
                    var images = new List<PollProtocolImage>();
                    var imageAnchorNodes = imagesNode.SelectNodes("descendant::a");
                    if (imageAnchorNodes == null || imageAnchorNodes.Count == 0)
                        m_logger.LogWarning(String.Format("{0}/{1}: Can't find hyperlinks to protocol images (a)", regionName, comissionNum));
                    else
                    {
                        foreach (HtmlNode anchorNode in imageAnchorNodes)
                        {
                            string uri = anchorNode.GetAttributeValue("href", "");
                            // remove uri parameters (everything after "?")
                            var idx = uri.IndexOf("?");
                            if (idx > -1)
                                uri = uri.Substring(0, idx);
                            string filePath;
                            byte[] imageBytes = null;
                            if (m_imageFilesMap.TryGetValue(uri, out filePath))
                                imageBytes = File.ReadAllBytes(filePath);
                            var protocolImage = new PollProtocolImage
                                                    {
                                                        Uri = uri,
                                                        Image = imageBytes
                                                    };
                            images.Add(protocolImage);
                        }
                    }

                    var protocol = new PollProtocol
                                   	{
                                   		Region = region,
                                   		Comission = comissionNum,
                                   		Images = images,
                                   		Results = results
                                   	};

                    yield return protocol;
                }
            }
        }