using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Web.Mvc; using System.Web.Script.Serialization; using System.Web.UI; using AutoMapper; using DR.AcCommonUI.Helpers.LivePage; using DR.FV2016.Service.Helpers; using DR.FV2016.Service.Interfaces; using DR.FV2016.Service.Models; using DR.FV2016.Web.Models; using DR.FV2016.Web.Models.Candidate; using DR.FV2016.Web.Models.Results; using DR.Sosy.Contract; using DR.Web.UI.Model; using Newtonsoft.Json; namespace DR.FV2016.Web.Controllers { public class ExternalModulesController : Controller { private readonly ICompositeCandidateService _candidateService; private readonly IScribbleService _scribbleService; private readonly IFVConfigurationService _configurationService; private readonly ILivePageService _livePageService; private readonly IArticleService _articleService; private readonly IResultsService _resultService; private static readonly List _redBlock = new List() { "Å", "B", "F", "Ø", "A" }; private static readonly List _blueBlock = new List() { "V", "O", "C", "I", "K" }; private const int ScribbleId = 819424; public ExternalModulesController(ICompositeCandidateService candidateService, IScribbleService scribbleService, IFVConfigurationService configurationService, ILivePageService livePageService, IArticleService articleService, IResultsService resultService) { if (candidateService == null) throw new ArgumentNullException("candidateService"); if (scribbleService == null) throw new ArgumentNullException("scribbleService"); if (configurationService == null) throw new ArgumentNullException("configurationService"); if (articleService == null) throw new ArgumentNullException("articleService"); _candidateService = candidateService; _scribbleService = scribbleService; _configurationService = configurationService; _livePageService = livePageService; _articleService = articleService; _resultService = resultService; } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 60)] public ActionResult TopBandCandidates() { if (ConfigurationManager.AppSettings["ExternalBannerDisabled"] == "true") return HttpNotFound(); var bannerConfig = _configurationService.Get("topbanner-config"); var model = new TopBannerViewModel { BannerConfig = bannerConfig ?? new BannerConfig { Config = new Config { EnableVideo = false }, Video = new Video() { Description = "LIVE fra valget på DR 1", SelectedStreamingChannel = "dr1" } } }; if (model.BannerConfig != null) { if (!model.BannerConfig.Config.EnableVideo) { var scribble = _scribbleService.GetEvent(ScribbleId); model.Scribble = scribble; } } var bannerCandidateLists = _candidateService.GetBannerCandidates(); var partyLeaders = bannerCandidateLists.FirstOrDefault(); if (partyLeaders != null && partyLeaders.Any()) { model.PartyLeadersList.Items = Mapper.Map>(bannerCandidateLists.First().ToList()); } var tinyCandidates = bannerCandidateLists.Select(cl => cl.Select(Mapper.Map)); model.CandidateJson = new JavaScriptSerializer().Serialize(tinyCandidates); return PartialView("Banners/TopBarExternal", model); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 60)] public ActionResult TopBannerJson() { if (ConfigurationManager.AppSettings["ExternalBannerDisabled"] == "true") return HttpNotFound(); var bannerConfigUrl = ConfigurationManager.AppSettings["BannerConfigUrl"]; if (string.IsNullOrEmpty(bannerConfigUrl)) throw new ConfigurationErrorsException("The key BannerConfigUrl is missing or empty in web.config!"); var bannerConfig = _configurationService.Get("topbanner-config"); var topBannerModel = new TopBannerViewModel(); var scribble = _scribbleService.GetEvent(ScribbleId); topBannerModel.Scribble = scribble; var bannerCandidateLists = _candidateService.GetBannerCandidates(); var partyLeaders = bannerCandidateLists.FirstOrDefault(); if (partyLeaders != null && partyLeaders.Any()) { topBannerModel.PartyLeadersList.Items = Mapper.Map>(bannerCandidateLists.First().ToList()); } var tinyCandidates = bannerCandidateLists.Select(cl => cl.Select(Mapper.Map)); //topBannerModel.CandidateJson = new JavaScriptSerializer().Serialize(tinyCandidates); var model = topBannerModel; model.BannerConfig = bannerConfig; return Content(JsonConvert.SerializeObject(new { ImageUrl = "//www.altinget.dk/images/kd/", CandidateUrl = "/nyheder/politik/valg2016/kandidater/", CandidateTestUrl = "/nyheder/politik/valg2016/kandidat-testen/", Servertime = DateTime.Now, Streaming = model.BannerConfig != null ? model.BannerConfig.Video : null, Scribble = model.Scribble, Candidates = tinyCandidates, }), "application/json", Encoding.UTF8); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 60)] public JsonResult TimeStampJson() { var unixTimestamp = (Int64)DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; var unixTimestampUTC = (Int64)DateTime.Now.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds; return Json(new { timestamp = unixTimestamp * 1000, timestampUTC = unixTimestampUTC * 1000 }, JsonRequestBehavior.AllowGet); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 30)] public ActionResult Banner(string jumpTo) { if (ConfigurationManager.AppSettings["ExternalBannerDisabled"] == "true") return HttpNotFound(); HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*"); var currentTime = DateTime.Now; //#if DEBUG if (!string.IsNullOrEmpty(jumpTo)) { var electionDay = new DateTime(2015, 6, 18, 9, 5, 0); var pollsClosed = new DateTime(2015, 6, 18, 20, 1, 0); var electionClosed = new DateTime(2015, 6, 20, 9, 0, 0); switch (jumpTo.ToUpper()) { case "ELECTIONDAY": currentTime = electionDay; break; case "RESULTSIN": currentTime = pollsClosed; break; case "ELECTIONCLOSED": currentTime = electionClosed; break; default: currentTime = DateTime.Now; break; } } //#endif var bannerResult = _resultService.GetBannerData(currentTime); var model = new BannerViewModel(); switch (bannerResult.ElectionState) { case ElectionState.PreElection: case ElectionState.ElectionDay: model = BuildPreElectionBannerViewmodel(model); break; case ElectionState.ResultsIn: model.Result = Mapper.Map(bannerResult); break; case ElectionState.ElectedCandidatesIn: model.Result = Mapper.Map(bannerResult); model.Result.Results.Where(r => r.Results.Any()).ToList().ForEach(c => c.Blocks.Add("red", c.Results.Where(r => _redBlock.Contains(r.Letter.ToUpper())).OrderBy(p => p.SharePercent))); model.Result.Results.Where(r => r.Results.Any()).ToList().ForEach(c => c.Blocks.Add("blue", c.Results.Where(r => _blueBlock.Contains(r.Letter.ToUpper())).OrderByDescending(p => p.SharePercent))); break; } model.ElectionState = bannerResult.ElectionState; return PartialView(model); //return Content(JsonConvert.SerializeObject(model), "application/json", Encoding.UTF8); } private BannerViewModel BuildPreElectionBannerViewmodel(BannerViewModel model) { var bannerConfig = _configurationService.Get("topbanner-config"); model.Servertime = DateTime.Now; model.StreamingImageUrl = string.Format("//dr-scr-capture.cloudapp.net/{0}.jpg", bannerConfig.Video.SelectedStreamingChannel); model.CandidateTestUrl = "/nyheder/politik/valg2016/kandidat-testen/"; //var scribble = _scribbleService.GetEvent(1268260); var scribble = _scribbleService.GetEvent(1075171); model.ScribblePost = scribble.Posts.FirstOrDefault(x => x.Type == DR.Sosy.Model.ScribblePostType.Text); var candidates = _candidateService.GetBannerCandidates().SelectMany(c => c); var bannerCandidateLists = Mapper.Map>(candidates); model.CandidatesJson = JsonConvert.SerializeObject(bannerCandidateLists); model.InitialCandidates = bannerCandidateLists.Take(10).ToList(); return model; } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 30)] public ActionResult BannerHtml(string jumpTo) { if (ConfigurationManager.AppSettings["ExternalBannerDisabled"] == "true") return HttpNotFound(); HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*"); var currentTime = DateTime.Now; //#if DEBUG //reinstate after rehearsals //if (!TimeHelper.ElectionDayHasStarted()) //{ if (!string.IsNullOrEmpty(jumpTo)) { switch (jumpTo.ToUpper()) { case "ELECTIONDAY": currentTime = TimeHelper.RealElectionDayStart; break; case "RESULTSIN": currentTime = TimeHelper.PollStationsClosedTime; break; case "ELECTIONCLOSED": currentTime = TimeHelper.ExternalElectionContentDisableTime; break; default: currentTime = TimeHelper.Now(); break; } } //} //#endif var bannerResult = _resultService.GetBannerData(currentTime); var model = new BannerViewModel(); switch (bannerResult.ElectionState) { case ElectionState.PreElection: case ElectionState.ElectionDay: model = BuildPreElectionBannerViewmodel(model); break; case ElectionState.ResultsIn: model.Result = Mapper.Map(bannerResult); break; case ElectionState.ElectedCandidatesIn: BuildPreElectionBannerViewmodel(model); model.Result = Mapper.Map(bannerResult); model.Result.Results.Where(r => r.Results.Any()).ToList().ForEach(c => c.Blocks.Add("red", c.Results.Where(r => _redBlock.Contains(r.Letter.ToUpper())).OrderBy(p => p.SharePercent))); model.Result.Results.Where(r => r.Results.Any()).ToList().ForEach(c => c.Blocks.Add("blue", c.Results.Where(r => _blueBlock.Contains(r.Letter.ToUpper())).OrderByDescending(p => p.SharePercent))); break; } model.ElectionState = bannerResult.ElectionState; return PartialView(model); } [OutputCache(Location = OutputCacheLocation.ServerAndClient, Duration = 15)] public ActionResult BannerMobile(string jumpTo) { var model = new BannerViewModel(); model.Servertime = DateTime.Now; var candidates = _candidateService.GetBannerCandidates().SelectMany(c => c); var bannerCandidateLists = Mapper.Map>(candidates); model.CandidatesJson = JsonConvert.SerializeObject(bannerCandidateLists); model.InitialCandidates = bannerCandidateLists.Take(10).ToList(); var currentTime = DateTime.Now; #if DEBUG if (!string.IsNullOrEmpty(jumpTo)) { var electionDay = new DateTime(2015, 6, 18, 9, 5, 0); var pollsClosed = new DateTime(2015, 6, 18, 20, 1, 0); var electionClosed = new DateTime(2015, 6, 20, 9, 0, 0); switch (jumpTo.ToUpper()) { case "ELECTIONDAY": currentTime = electionDay; break; case "RESULTSIN": currentTime = pollsClosed; break; case "ELECTIONCLOSED": currentTime = electionClosed; break; default: currentTime = DateTime.Now; break; } } #endif var bannerResult = _resultService.GetBannerData(currentTime); switch (bannerResult.ElectionState) { case ElectionState.PreElection: case ElectionState.ElectionDay: model = BuildPreElectionBannerViewmodel(model); break; case ElectionState.ResultsIn: model.Result = Mapper.Map(bannerResult); break; case ElectionState.ElectedCandidatesIn: model.Result = Mapper.Map(bannerResult); model.Result.Results.Where(r => r.Results.Any()).ToList().ForEach(c => c.Blocks.Add("red", c.Results.Where(r => _redBlock.Contains(r.Letter.ToUpper())).OrderBy(p => p.SharePercent))); model.Result.Results.Where(r => r.Results.Any()).ToList().ForEach(c => c.Blocks.Add("blue", c.Results.Where(r => _blueBlock.Contains(r.Letter.ToUpper())).OrderByDescending(p => p.SharePercent))); break; } model.ElectionState = bannerResult.ElectionState; return PartialView("BannerForecastsResultsMobile", model); //return Content(JsonConvert.SerializeObject(model), "application/json", Encoding.UTF8); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 60)] public ActionResult GetTopBannerCandidates() { if (ConfigurationManager.AppSettings["ExternalBannerDisabled"] == "true") return HttpNotFound(); var candidateLists = _candidateService.GetBannerCandidates(); var tinyCandidates = candidateLists.Select(cl => cl.Select(Mapper.Map)); return Json(tinyCandidates, JsonRequestBehavior.AllowGet); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 15)] public ActionResult GetFv15ScribbleData(int limit = 3) { if (ConfigurationManager.AppSettings["ExternalBannerDisabled"] == "true") return HttpNotFound(); var scribble = _scribbleService.GetEvent(ScribbleId); return Json(scribble.Posts.Take(limit), JsonRequestBehavior.AllowGet); } [OutputCache(Location = OutputCacheLocation.Downstream, Duration = 15)] public JsonResult LivePageConfiguration() { HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*"); var livepagePostingGuid = new Guid("E34B21AD-4F72-46F3-8704-1ECD9D070EA3"); var articleWrapper = _articleService.GetArticleWrapper("/live"); var model = LivePageHelper.GetLivePageConfiguration(livepagePostingGuid, _livePageService, Mapper.Map>(articleWrapper.Images)); return Json(model, JsonRequestBehavior.AllowGet); } } }