Log Import for CreateLog

This commit is contained in:
2014-02-05 15:02:10 -05:00
parent 7976fe04c3
commit 042d3dd476
13 changed files with 349 additions and 27 deletions
+189 -1
View File
@@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.IO;
using System.Linq;
using System.Security;
using System.Web.Mvc;
using System.Web.Routing;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Models;
using MileageTraker.Web.ViewModels;
using MileageTraker.Web.ViewModels.CreateLog;
using MileageTraker.Web.ViewModels.Log;
namespace MileageTraker.Web.Controllers
{
@@ -160,5 +164,189 @@ namespace MileageTraker.Web.Controllers
}
};
}
#region Import
public ActionResult ImportUpload()
{
return View();
}
[HttpPost]
public ActionResult Import(ImportUploadViewModel viewModel)
{
if (ModelState.IsValid && viewModel.File != null && viewModel.File.ContentLength > 0)
{
var fileName = Path.GetFileName(viewModel.File.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
viewModel.File.SaveAs(path);
try
{
var logImports = LogImporter.Import(path);
// todo: delete file?
return View(logImports.ToList());
}
catch (Exception ex)
{
TempData["StatusMessage"] = "Problem reading excel document: " + ex.Message;
TempData["StatusMessage-Type"] = "alert-error";
}
}
return RedirectToAction("ImportUpload");
}
enum ImportStatus
{
Success,
Failure,
Duplicate
}
[HttpPost]
[ActionLog]
public ActionResult ImportCreate(LogImportViewModel viewModel)
{
var buttonAttributes = new Dictionary<string, object> { { "class", "btn btn-mini" }, { "target", "_blank" } };
try
{
// verify purpose
var purposeType = DataService.FindPurposeType(viewModel.Purpose);
if (purposeType == null)
{
return Json(new
{
Status = ImportStatus.Failure.ToString(),
Message = "Invalid Purpose: " + viewModel.Purpose,
Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet);
}
if (!ModelState.IsValid)
{
var errorList = GetModelStateErrorList(ModelState);
// remove the object name from the errors
errorList = errorList.Select(s => s.Replace("viewModel.", ""));
return Json(new
{
Status = ImportStatus.Failure.ToString(),
Message = string.Join(", ", errorList),
Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet);
}
var log = viewModel.GetLog();
log.User = DataService.FindUserByUsername(User.Identity.Name);
log.Source = HttpContext.Request.Url.AbsolutePath;
log.UserHostAddress = HttpContext.Request.UserHostAddress;
log.UserAgent = HttpContext.Request.UserAgent;
log.Purpose = purposeType;
// check duplicate
var duplicateLogs = DataService.GetDuplicateLogs(log).ToList();
if (duplicateLogs.Any())
{
var link = HtmlHelper.GenerateLink(
ControllerContext.RequestContext, RouteTable.Routes,
"Edit", "Default", "EditPast", "CreateLog",
new RouteValueDictionary(new { id = duplicateLogs.First().LogId }),
buttonAttributes);
return Json(new
{
Status = ImportStatus.Duplicate.ToString(),
Message = "This log has been previously entered",
Action = link
}, JsonRequestBehavior.AllowGet);
}
// verify vehicle exists
if (DataService.GetVehicle(log.VehicleId) == null)
{
return Json(new
{
Status = ImportStatus.Failure.ToString(),
Message = "Vehile with supplied ID does not exist",
Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet);
}
// SAVE IT!
DataService.AddLog(log);
var successLink = HtmlHelper.GenerateLink(
ControllerContext.RequestContext, RouteTable.Routes,
"Edit", "Default", "EditPast", "CreateLog",
new RouteValueDictionary(new { id = log.LogId }),
buttonAttributes);
return Json(new
{
Status = ImportStatus.Success.ToString(),
Action = successLink
}, JsonRequestBehavior.AllowGet);
}
catch (DbEntityValidationException ex)
{
// Retrieve the error messages as a list of strings.
var errorMessages = ex.EntityValidationErrors
.SelectMany(x => x.ValidationErrors)
.Select(x => x.ErrorMessage);
// Join the list to a single string.
var fullErrorMessage = string.Join(", ", errorMessages);
return Json(new
{
Status = ImportStatus.Failure.ToString(),
Message = fullErrorMessage,
Action = GetFixLink(viewModel)
},
JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
var message = ex.Message;
return Json(new
{
Status = ImportStatus.Failure.ToString(),
Message = message,
Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet);
}
}
private string GetFixLink(LogImportViewModel viewModel)
{
try
{
viewModel.GetLogViewModel();
}
catch
{
return string.Empty;
}
return RenderRazorViewToString("ImportFix", viewModel);
}
[HttpPost]
public ActionResult ImportFix(LogImportViewModel viewModel)
{
var createGetLogViewModel = viewModel.GetCreateLogViewModel();
createGetLogViewModel.Purpose = new SelectListViewModel
{
Available = GetPurposeTypesSelectList()
};
return View("Index", createGetLogViewModel);
}
public ActionResult ImportTemplate()
{
var template = LogImportTemplateWriter.Write();
return File(template, "application/ms-excel", "MileageTraker_ImportTemplate.xls");
}
#endregion
}
}