예제 #1
0
        public string RefreshElections(string electionYear, string stateCode)
        {
            var jsonObj = VoteSmart.GetJsonAsDictionary("Election.getElectionByYearState",
                                                        "year=" + electionYear + "&stateId=" + stateCode);

            if (!jsonObj.ContainsKey("elections"))
            {
                return("No elections found in " + electionYear + " for " +
                       StateCache.GetStateName(stateCode));
            }

            VsElections.DeleteByElectionYearStateCode(electionYear, stateCode);

            var elections = jsonObj["elections"] as Dictionary <string, object>;

            Debug.Assert(elections != null, "elections != null");
            var election = VoteSmart.AsArrayList(elections["election"]);

            foreach (var e in election.Cast <Dictionary <string, object> >())
            {
                var electionId = Convert.ToInt32(e["electionId"]);
                var stage      = VoteSmart.AsArrayList(e["stage"]);
                foreach (var s in stage.Cast <Dictionary <string, object> >())
                {
                    var stageId = s["stageId"] as string;
                    VsElectionsCandidates.DeleteByElectionIdStageId(electionId, stageId);
                    VsElections.Insert(electionId, stageId, electionYear,
                                       stateCode, e["officeTypeId"] as string, e["special"] as string,
                                       e["name"] as string, s["name"] as string,
                                       DateTime.Parse(s["electionDate"] as string),
                                       VotePage.DefaultDbDate);
                }
            }

            if (VsElectionYearState.ElectionYearStateCodeExists(electionYear, stateCode))
            {
                VsElectionYearState.UpdateLastRefreshTime(DateTime.UtcNow, electionYear,
                                                          stateCode);
            }
            else
            {
                VsElectionYearState.Insert(electionYear, stateCode, DateTime.UtcNow);
            }

            return(string.Empty);
        }
예제 #2
0
        public string RefreshCandidates(string vsElectionKey)
        {
            var electionId =
                int.Parse(vsElectionKey.Substring(0, vsElectionKey.Length - 1));
            var stageId = vsElectionKey.Substring(vsElectionKey.Length - 1, 1);

            VsElectionsCandidates.DeleteByElectionIdStageId(electionId, stageId);
            VsElections.UpdateCandidateListRefreshTime(DateTime.UtcNow, electionId, stageId);

            var jsonObj = VoteSmart.GetJsonAsDictionary("Candidates.getByElection",
                                                        "electionId=" + electionId + "&stageId=" + stageId);

            if (jsonObj.ContainsKey("candidateList"))
            {
                var candidateList = jsonObj["candidateList"] as Dictionary <string, object>;
                // ReSharper disable once PossibleNullReferenceException
                var candidateArray = VoteSmart.AsArrayList(candidateList["candidate"]);
                foreach (
                    var candidate in candidateArray.OfType <Dictionary <string, object> >())
                {
                    try
                    {
                        VsElectionsCandidates.Insert(electionId, stageId,
                                                     Convert.ToInt32(candidate["candidateId"]),
                                                     VsDecode(candidate["ballotName"]), candidate["lastName"] as string,
                                                     candidate["firstName"] as string);
                    }
                    catch (MySqlException e)
                    {
                        if (!e.Message.StartsWith("Duplicate entry",
                                                  StringComparison.OrdinalIgnoreCase))
                        {
                            throw;
                        }
                    }
                }
            }

            return(string.Empty);
        }
예제 #3
0
        public string RefreshCandidate(string vsCandidateId)
        {
            var candidateId = int.Parse(vsCandidateId);

            VsCandidates.DeleteByCandidateId(candidateId);
            VsCandidatesItems.DeleteByCandidateId(candidateId);
            VsCandidatesWebAddresses.DeleteByCandidateId(candidateId);

            var jsonObj = VoteSmart.GetJsonAsDictionary("CandidateBio.getBio",
                                                        "candidateId=" + candidateId);

            if (jsonObj.ContainsKey("bio"))
            {
                var bio = jsonObj["bio"] as Dictionary <string, object>;
                if (bio?.ContainsKey("candidate") == true)
                {
                    var      candidate = bio["candidate"] as Dictionary <string, object>;
                    DateTime birthDate;
                    Debug.Assert(candidate != null, "candidate != null");
                    if (!DateTime.TryParse(candidate["birthDate"] as string, out birthDate))
                    {
                        birthDate = VotePage.DefaultDbDate;
                    }
                    VsCandidates.Insert(candidateId, DateTime.UtcNow,
                                        candidate["firstName"] as string,
                                        candidate["middleName"] as string,
                                        candidate["nickName"] as string,
                                        candidate["lastName"] as string,
                                        candidate["suffix"] as string,
                                        birthDate,
                                        candidate["birthPlace"] as string,
                                        candidate["pronunciation"] as string,
                                        candidate["gender"] as string,
                                        candidate["family"] as string,
                                        candidate["photo"] as string,
                                        candidate["homeCity"] as string,
                                        candidate["homeState"] as string,
                                        candidate["education"] as string,
                                        candidate["profession"] as string,
                                        candidate["political"] as string,
                                        candidate["religion"] as string,
                                        candidate["congMembership"] as string,
                                        candidate["orgMembership"] as string,
                                        candidate["specialMsg"] as string);
                }
            }

            var itemsJsonObj = VoteSmart.GetJsonAsDictionary("CandidateBio.getAddlBio",
                                                             "candidateId=" + candidateId);

            if (itemsJsonObj.ContainsKey("addlBio"))
            {
                var addlBio = itemsJsonObj["addlBio"] as Dictionary <string, object>;
                if (addlBio?.ContainsKey("additional") == true)
                {
                    var additional = addlBio["additional"] as Dictionary <string, object>;
                    if (additional != null)
                    {
                        var item = VoteSmart.AsArrayList(additional["item"]);
                        if (item != null)
                        {
                            foreach (var i in item.Cast <Dictionary <string, object> >())
                            {
                                VsCandidatesItems.Insert(candidateId, i["name"] as string,
                                                         i["data"] as string);
                            }
                        }
                    }
                }
            }

            var wasonObj = VoteSmart.GetJsonAsDictionary("Address.getOfficeWebAddress",
                                                         "candidateId=" + candidateId);

            if (wasonObj.ContainsKey("webaddress"))
            {
                var webAddress = wasonObj["webaddress"] as Dictionary <string, object>;
                if (webAddress?.ContainsKey("address") == true)
                {
                    var address = VoteSmart.AsArrayList(webAddress["address"]);
                    if (address != null)
                    {
                        foreach (var a in address.Cast <Dictionary <string, object> >())
                        {
                            VsCandidatesWebAddresses.Insert(candidateId,
                                                            a["webAddressTypeId"] as string, a["webAddress"] as string);
                        }
                    }
                }
            }

            return(string.Empty);
        }