Files
LeafWeb/Web/Controllers/LeafInputController.cs
T
poprhythm 10cd2986cf Organize Piscal Service items
Add nlog, PiscalQueueManager

	modified:   Core.Tests/Remote/PiscalSshClientTests.cs
	modified:   Core/DAL/DataService.cs
	modified:   Core/Remote/IPiscalClient.cs
	modified:   Core/Remote/PiscalSshClient.cs
	new file:   Web/Attributes/ActionLogAttribute.cs
	modified:   Web/Controllers/ControllerBase.cs
	modified:   Web/Controllers/LeafInputController.cs
	modified:   Web/Controllers/LeafOutputController.cs
	new file:   Web/NLog.config
	new file:   Web/NLog.xsd
	new file:   Web/Services/PiscalQueueManager.cs
	modified:   Web/Services/PiscalService.cs
	modified:   Web/Startup.cs
	modified:   Web/Web.csproj
	modified:   Web/packages.config

Ignore logs

Organize piscal queue manager

	modified:   Core/Entities/LeafInputFile.cs
	modified:   Web/Controllers/LeafInputController.cs
	renamed:    Web/Startup.cs -> Web/HangfireStartup.cs
	modified:   Web/Services/PiscalQueueManager.cs
	modified:   Web/Web.csproj

cleanup usings in leafinputcontroller
2016-03-01 07:35:58 -05:00

118 lines
3.3 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeafWeb.Core.Entities;
using LeafWeb.Web.Attributes;
using LeafWeb.Web.ViewModels;
using LeafWeb.Web.ViewModels.LeafInput;
using NLog;
namespace LeafWeb.Web.Controllers
{
public class LeafInputController : ControllerBase
{
public ActionResult Index()
{
// initialize the session storage to retain SessionID between requests
Session["placeholder"] = 0;
var viewModel = new CreateViewModel();
HydrateCreateViewModel(viewModel);
return View(viewModel);
}
[HttpParamAction]
[HttpPost]
public ActionResult Index(CreateViewModel viewModel)
{
// directory name is the sessionID
var files = GetBackloadDirectoryFiles(Session.SessionID);
if (!files.Any())
ModelState.AddModelError("Files", "Must select at least one file");
if (ModelState.IsValid && !IsHttpParamActionMatch()) // HttpParamMatch indicates it's backing out from Confirm
{
// Go to confirmation
var confirmViewModel = new ConfirmViewModel(viewModel, files.Select(f => f.Name).ToArray());
HydrateCreateViewModel(confirmViewModel);
return View("Confirm", confirmViewModel);
}
HydrateCreateViewModel(viewModel);
return View("Index", viewModel);
}
private void HydrateCreateViewModel(dynamic viewModel)
{
if (viewModel.PhotosynthesisType == null)
viewModel.PhotosynthesisType = new SelectListViewModel();
if (viewModel.PhotosynthesisType.ListItems == null)
viewModel.PhotosynthesisType.ListItems = GetPhotosynthesisTypeSelectList();
}
[HttpParamAction]
[HttpPost]
[ActionLog]
public ActionResult Confirm(CreateViewModel viewModel)
{
// directory name is the sessionID
var files = GetBackloadDirectoryFiles(Session.SessionID);
if (!files.Any())
{
ModelState.AddModelError("Files", "Must select at least one file");
}
if (ModelState.IsValid)
{
// convert viewModel into Model
var leafInput = viewModel.GetFileInput(DataService);
// load files into LeafInputFile
leafInput.Files =
(from f in files
let bytes = System.IO.File.ReadAllBytes(f.FullName)
select new LeafInputFile {Filename = f.Name, Contents = bytes}).ToList();
// Save to db
DataService.AddLeafInput(leafInput);
DeleteBackloadDirectory(Session.SessionID);
var msg = $"A data set has submitted for '{viewModel.Identifier}' from '{viewModel.SiteId}'. " + Environment.NewLine
+ $"When complete, an email will be delivered to {viewModel.Name} <{viewModel.Email}> with results.";
SetStatusMessage(HttpUtility.HtmlEncode(msg),StatusType.Success);
LogManager.GetCurrentClassLogger().Info(msg);
HangfireStartup.TriggerPiscalProcessQueue();
return RedirectToAction("Index");
}
HydrateCreateViewModel(viewModel);
return View("Index", viewModel);
}
private FileInfo[] GetBackloadDirectoryFiles(string directoryName)
{
var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\");
var directory = new DirectoryInfo(path);
return
!directory.Exists
? new FileInfo[] { }
: directory.GetFiles();
}
private void DeleteBackloadDirectory(string directoryName)
{
var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\");
var directory = new DirectoryInfo(path);
if (directory.Exists)
directory.Delete(true);
}
}
}