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);
        }
        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);
        }