Example #1
0
        public static void UpdatePoliticianAnswerNew(string politicianKey, string questionKey,
                                                     int sequence, string newValue, string source, DateTime dateStamp, string youTubeUrl,
                                                     string youTubeDescription, TimeSpan youTubeRunningTime, string youTubeSource,
                                                     string youTubeSourceUrl, DateTime youTubeDate, string facebookVideoUrl,
                                                     string facebookVideoDescription, TimeSpan facebookVideoRunningTime,
                                                     DateTime facebookVideoDate)
        {
            var questionId = int.Parse(questionKey);

            if (IsNullOrWhiteSpace(newValue) && IsNullOrWhiteSpace(youTubeUrl) &&
                IsNullOrWhiteSpace(facebookVideoUrl))
            // Just delete and be done with it
            {
                Answers2.DeleteByPoliticianKeyQuestionIdSequence(politicianKey, questionId,
                                                                 sequence);
                return;
            }

            var table = Answers2.GetDataByPoliticianKeyQuestionIdSequence(politicianKey,
                                                                          questionId, sequence);
            Answers2Row row;

            if (table.Count == 0)
            {
                row          = table.NewRow();
                row.Sequence = sequence;
            }
            else
            {
                row = table[0];
            }

            row.PoliticianKey      = politicianKey;
            row.QuestionId         = questionId;
            row.Answer             = newValue;
            row.Source             = source;
            row.DateStamp          = dateStamp;
            row.UserName           = UserName;
            row.YouTubeUrl         = youTubeUrl;
            row.YouTubeDescription = youTubeDescription;
            row.YouTubeRunningTime = youTubeRunningTime;
            row.YouTubeSource      = youTubeSource;
            row.YouTubeSourceUrl   = youTubeSourceUrl;
            row.YouTubeDate        = youTubeDate;
            row.YouTubeRefreshTime = IsNullOrWhiteSpace(youTubeUrl)
        ? DefaultDbDate
        : DateTime.UtcNow;
            row.YouTubeAutoDisable       = null;
            row.FacebookVideoUrl         = facebookVideoUrl;
            row.FacebookVideoDescription = facebookVideoDescription;
            row.FacebookVideoRunningTime = facebookVideoRunningTime;
            row.FacebookVideoDate        = facebookVideoDate;
            row.FacebookVideoRefreshTime = IsNullOrWhiteSpace(facebookVideoUrl)
        ? DefaultDbDate
        : DateTime.UtcNow;
            row.FacebookVideoAutoDisable = null;
            if (table.Count == 0)
            {
                table.AddRow(row);
            }
            Answers2.UpdateTable(table);
        }
        private void DoConsolidation()
        {
            try
            {
                var    selectedIndex = ConsolidateSelectedIndex.Value;
                string selectedKey;
                string unselectedKey;
                switch (selectedIndex)
                {
                case "1":
                    selectedKey   = ConsolidateKey1.Value;
                    unselectedKey = ConsolidateKey2.Value;
                    break;

                case "2":
                    selectedKey   = ConsolidateKey2.Value;
                    unselectedKey = ConsolidateKey1.Value;
                    break;

                default: throw new VoteException("Index not 1 or 2");
                }

                // Politicians
                var selectedPolitician   = Politicians.GetData(selectedKey);
                var unselectedPolitician = Politicians.GetData(unselectedKey);
                if (selectedPolitician.Count != 1)
                {
                    throw new VoteException("Politician " + selectedPolitician + " not found");
                }
                if (unselectedPolitician.Count != 1)
                {
                    throw new VoteException("Politician " + unselectedKey + " not found");
                }
                var selectedData = UpdatePoliticians(selectedIndex, selectedPolitician,
                                                     unselectedPolitician);

                // PoliticiansImagesData and PoliticiansImagesBlobs
                var selectedImagesData    = PoliticiansImagesData.GetData(selectedKey);
                var unselectedImagesData  = PoliticiansImagesData.GetData(unselectedKey);
                var selectedImagesBlobs   = PoliticiansImagesBlobs.GetData(selectedKey);
                var unselectedImagesBlobs = PoliticiansImagesBlobs.GetData(unselectedKey);
                UpdateImages(selectedIndex, selectedData, selectedKey, selectedImagesData,
                             selectedImagesBlobs, unselectedImagesData, unselectedImagesBlobs);

                // Answers
                //AnswersTable selectedAnswers = null;
                //AnswersTable unselectedAnswers = null;
                var selectedAnswers2   = Answers.GetActiveDataByPoliticianKeyNew(selectedKey);
                var unselectedAnswers2 = Answers2.GetDataByPoliticianKey(unselectedKey);
                UpdateAnswers2(selectedKey, selectedAnswers2, unselectedAnswers2);

                // ElectionsIncumbentsRemoved
                var selectedIncumbentsRemoved =
                    ElectionsIncumbentsRemoved.GetDataByPoliticianKey(selectedKey);
                var unselectedIncumbentsRemoved =
                    ElectionsIncumbentsRemoved.GetDataByPoliticianKey(unselectedKey);
                UpdateIncumbentsRemoved(selectedKey, unselectedIncumbentsRemoved,
                                        selectedIncumbentsRemoved);

                // ElectionsPoliticians
                var selectedElectionsPoliticians =
                    ElectionsPoliticians.GetDataByPoliticianKey(selectedKey);
                var unselectedElectionsPoliticians =
                    ElectionsPoliticians.GetDataByPoliticianKey(unselectedKey);
                UpdateElectionsPoliticians(selectedKey, unselectedElectionsPoliticians,
                                           selectedElectionsPoliticians);

                // OfficesOfficials
                var selectedOfficesOfficials   = OfficesOfficials.GetDataByPoliticianKey(selectedKey);
                var unselectedOfficesOfficials =
                    OfficesOfficials.GetDataByPoliticianKey(unselectedKey);
                UpdateOfficesOfficials(selectedKey, unselectedOfficesOfficials,
                                       selectedOfficesOfficials);

                // Update everything as one transaction, politicians last
                PoliticiansImagesData.UpdateTable(selectedImagesData);
                PoliticiansImagesData.UpdateTable(unselectedImagesData);
                PoliticiansImagesBlobs.UpdateTable(selectedImagesBlobs);
                PoliticiansImagesBlobs.UpdateTable(unselectedImagesBlobs);
                Answers2.UpdateTable(selectedAnswers2);
                Answers2.UpdateTable(unselectedAnswers2);
                ElectionsIncumbentsRemoved.UpdateTable(unselectedIncumbentsRemoved);
                ElectionsPoliticians.UpdateTable(unselectedElectionsPoliticians);
                OfficesOfficials.UpdateTable(unselectedOfficesOfficials);
                Politicians.UpdateTable(selectedPolitician);
                Politicians.UpdateTable(unselectedPolitician);

                // Log
                LogDataChange.LogUpdate("*ConsolidatePoliticians", "*Various", unselectedKey,
                                        selectedKey, VotePage.UserName, SecurePage.UserSecurityClass, DateTime.UtcNow,
                                        selectedKey);

                // After the main update, refresh the LiveOfficeKey, LiveOfficeStatus and LiveElectionKey
                var view = PoliticiansLiveOfficeKeyView.GetData(selectedKey);
                if (view.Count == 1)
                {
                    var keyAndStatus =
                        PoliticianOfficeStatus.FromLiveOfficeKeyAndStatus(
                            view[0].LiveOfficeKeyAndStatus);
                    selectedPolitician[0].LiveOfficeKey    = keyAndStatus.OfficeKey;
                    selectedPolitician[0].LiveOfficeStatus = keyAndStatus.PoliticianStatus.ToString();
                    selectedPolitician[0].LiveElectionKey  = keyAndStatus.ElectionKey;
                    Politicians.UpdateTable(selectedPolitician);
                }

                ConsolidateReloaded.Value = "ok";
            }
            catch (Exception ex)
            {
                FeedbackConsolidate.AddError("There was an unexpected error: " + ex.Message);
            }
        }
        public static void RefreshYouTubeAnswersNew()
        {
            string message;
            //var expiration = new TimeSpan(3, 0, 0, 0); // 3 days
            // fudge is to account for small differences in run time
            //var fudge = new TimeSpan(1, 0, 0); // 1 hour
            var now = DateTime.UtcNow;

            try
            {
                VotePage.LogInfo("RefreshYouTubeAnswersNew", "Started");

                var answersUpdated = 0;
                //var table = Answers.GetDataForYouTubeRefreshNew(now - expiration + fudge);
                var table = Answers.GetDataForYouTubeRefreshNew2(450);

                foreach (var row in table)
                {
                    row.YouTubeRefreshTime = now;

                    var youTubeId = row.YouTubeUrl.GetYouTubeVideoId();
                    if (IsNullOrWhiteSpace(youTubeId))
                    {
                        if (row.YouTubeAutoDisable != YouTubeVideoInfo.InvalidVideoUrlMessage)
                        {
                            row.YouTubeAutoDisable = YouTubeVideoInfo.InvalidVideoUrlMessage;
                            answersUpdated++;
                        }
                        continue;
                    }

                    var videoInfo = GetVideoInfo(youTubeId, false, 1);
                    if (!videoInfo.IsValid)
                    {
                        if (row.YouTubeAutoDisable != YouTubeVideoInfo.VideoIdNotFoundMessage)
                        {
                            row.YouTubeAutoDisable = YouTubeVideoInfo.VideoIdNotFoundMessage;
                            answersUpdated++;
                        }
                        continue;
                    }

                    if (!videoInfo.IsPublic)
                    {
                        if (row.YouTubeAutoDisable != YouTubeVideoInfo.VideoNotPublicMessage)
                        {
                            row.YouTubeAutoDisable = YouTubeVideoInfo.VideoNotPublicMessage;
                            answersUpdated++;
                        }
                        continue;
                    }

                    var description = videoInfo.Description.Trim();
                    if (IsNullOrWhiteSpace(description) ||
                        description.Length > VideoInfo.MaxVideoDescriptionLength)
                    {
                        description = videoInfo.Title.Trim();
                    }

                    if (description != row.YouTubeDescription ||
                        videoInfo.Duration != row.YouTubeRunningTime ||
                        row.YouTubeAutoDisable != null ||
                        row.YouTubeSource == YouTubeVideoInfo.VideoUploadedByCandidateMessage &&
                        videoInfo.PublishedAt != row.YouTubeDate)
                    {
                        row.YouTubeDescription = description;
                        row.YouTubeRunningTime = videoInfo.Duration;
                        row.YouTubeAutoDisable = null;
                        if (row.YouTubeSource == YouTubeVideoInfo.VideoUploadedByCandidateMessage)
                        {
                            row.YouTubeDate = videoInfo.PublishedAt;
                        }
                        answersUpdated++;
                    }
                }

                Answers2.UpdateTable(table);

                message =
                    $"{table.Count} Expired YouTube answers found, {answersUpdated} YouTube Answers updated";
            }
            catch (Exception ex)
            {
                VotePage.LogException("RefreshYouTubeAnswersNew", ex);
                message = $"Exception: {ex.Message} [see exception log for details]";
            }

            VotePage.LogInfo("RefreshYouTubeAnswersNew", message);
        }