using System; using System.Linq; using System.Web.Mvc; using Hangfire; using log4net; using LeafWeb.Core.Entities; using LeafWeb.Core.Utility; using LeafWeb.WebCms.App_Start; using LeafWeb.WebCms.Models; using LeafWeb.WebCms.Services; using LeafWeb.WebCms.Services.PiscalQueue; namespace LeafWeb.WebCms.Controllers { public class QueueController : BaseController { public ActionResult Index() { var resultItems = DataService.GetLeafInputs() .OrderByDescending(f => f.Id) .ToList() .Select(leafInput => new QueueItemViewModel(leafInput)); string serviceDescription; try { serviceDescription = new PiscalService().ServiceDescription; } catch (Exception) { serviceDescription = "Exception while initializing"; } var queueViewModel = new QueueViewModel { Items = resultItems, ServerDescription = serviceDescription }; return View(queueViewModel); } public ActionResult Details(int id) { var leafInput = DataService.GetLeafInput(id); if (leafInput == null) RedirectToUmbracoPage(LeafWebPageIds.ManageQueue); var viewModel = new LeafInputDetails(leafInput); return View(viewModel); } public ActionResult DownloadInput(int id) { return GetInputZip(id); } public ActionResult DownloadOutputToUser(int id) { return GetOutputZip(id, LeafOutputFileType.ToUser); } public ActionResult DownloadOutputNotToUser(int id) { return GetOutputZip(id, LeafOutputFileType.NotToUser); } public ActionResult DownloadOutputCleanedInput(int id) { return GetOutputZip(id, LeafOutputFileType.CleanedInput); } private ActionResult GetOutputZip(int id, LeafOutputFileType type) { var leafInput = DataService.GetLeafInput(id); if (leafInput == null) return View("DownloadNotFound"); var zip = leafInput.GetOutputFileZip(type); var filename = $"{leafInput.Identifier.FilterValidFilename()}_{type}.zip"; return new FileContentResult(zip, "application/zip") { FileDownloadName = filename }; } private ActionResult GetInputZip(int id) { var leafInput = DataService.GetLeafInput(id); if (leafInput == null) return View("DownloadNotFound"); var zip = leafInput.GetInputFileZip(); var filename = $"{leafInput.Identifier.FilterValidFilename()}_Input.zip"; return new FileContentResult(zip, "application/zip") { FileDownloadName = filename }; } [ActionLog] public ActionResult Delete(int id) { var leafInput = DataService.GetLeafInput(id); if (leafInput == null) { SetStatusMessage($"LeafInput '${id}' not found, perhaps it is already deleted?"); return RedirectToUmbracoPage(LeafWebPageIds.ManageQueue); } // don't allow currently running LeafInput to be deleted if (leafInput.IsInProgress) { SetStatusMessage($"LeafInput '{leafInput.Identifier}' is currently running!", StatusType.Error); return RedirectToCurrentUmbracoUrl(); } DataService.DeleteLeafInput(leafInput); SetStatusMessage($"LeafInput '{leafInput.Identifier}' deleted", StatusType.Success); return RedirectToUmbracoPage(LeafWebPageIds.ManageQueue); } [ActionLog] public ActionResult Cancel(int id) { var leafInput = DataService.GetLeafInput(id); if (leafInput == null) { SetStatusMessage($"LeafInput '${id}' not found, may have been deleted?"); return RedirectToUmbracoPage(LeafWebPageIds.ManageQueue); } if (leafInput.IsPending) { LogManager.GetLogger(LoggerName(RouteData)).DebugFormat("LeafInput: {0}, Set Cancelled from Pending", leafInput.Id); DataService.SetLeafInputStatus(leafInput, LeafInputStatusType.Cancelled, "Emailing cancellation notification to user", $"Email: \'{leafInput.Email}\'"); // send notification immediately BackgroundJob.Enqueue(email => email.SendLeafWebCancelled(leafInput.Id)); SetStatusMessage($"Cancelling LeafInput '{leafInput.Identifier}'", StatusType.Success); } else if (leafInput.IsRunning) { DataService.SetLeafInputStatus(leafInput, LeafInputStatusType.CancelPending); SetStatusMessage($"Cancelling LeafInput '{leafInput.Identifier}'", StatusType.Success); HangfireStartup.TriggerPiscalProcessQueue(); } else { // don't allow to be cancelled if it isn't currently running SetStatusMessage($"LeafInput '{leafInput.Identifier}' is not currently running!", StatusType.Error); } return RedirectToCurrentUmbracoUrl(); } [ActionLog] public ActionResult SendUserDownloadLink(int id) { var leafInput = DataService.GetLeafInput(id); if (!leafInput.IsComplete) { SetStatusMessage($"LeafInput '{leafInput.Identifier}' is not complete!", StatusType.Error); } else { var leafInputId = leafInput.Id; BackgroundJob.Enqueue(e => e.SendLeafWebComplete(leafInputId)); SetStatusMessage($"LeafInput '{leafInput.Identifier}' download link sent", StatusType.Success); } return RedirectToCurrentUmbracoUrl(); } [ActionLog] public ActionResult SendUserChartLink(int id) { throw new System.NotImplementedException(); } } }