public async Task <ActionResult> Preview(CornerViewModel cornerViewModel) { #region validate input if (!ModelState.IsValid) { TempData["error"] = "There was a problem with your input. Please try again."; return(RedirectToAction("Index")); } #endregion var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["PLSS"].ConnectionString); PDFDocument pdf = null; try { await connection.OpenAsync(); var user = CommandExecutor.ExecuteCommand(new GetUserCommand(connection, User.Identity.Name)); if (user == null) { TempData["error"] = "You must log in to submit a corner"; return(RedirectToRoute("", new { Controller = "Home", Action = "Index" })); } cornerViewModel.User = user; var corner = new Corner(cornerViewModel); var photos = new Photo(cornerViewModel); var model = new TieSheetPdfModel(cornerViewModel, corner, photos); var pdfService = new PlssPdfService("Assets\\pdf"); pdf = pdfService.HydratePdfForm("MonumentStatusTemplate.pdf", model); #if DEBUG pdf.FlattenFormFields(); #endif return(File(pdf.GetPDFAsByteArray(), MediaTypeNames.Application.Pdf, $"{model.BlmPointId}-preview.pdf")); } catch (Exception ex) { Log.LogException(LogLevel.Fatal, $"problem previewing pdf for {cornerViewModel}", ex); TempData["error"] = $"There was a problem generating your preview. {ex.Message}"; return(RedirectToRoute("", new { Controller = "Home", Action = "Index" })); } finally { pdf?.Dispose(); connection.Close(); connection.Dispose(); } }
public PDFDocument HydratePdfForm(string path, TieSheetPdfModel model) { var pdfDoc = GetPdfForm(path); return(HydratePdfForm(pdfDoc, model)); }
public async Task <ActionResult> New(CornerViewModel cornerViewModel) { #region validate input if (!ModelState.IsValid) { TempData["error"] = ModelState.ToErrors(); return(RedirectToRoute("", new { Controller = "Home", Action = "Index" })); } #endregion if (Request.Form["submitType"] == "preview") { Log.Info("Showing preview for {0}", cornerViewModel.BlmPointId); return(await Preview(cornerViewModel)); } var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["PLSS"].ConnectionString); try { await connection.OpenAsync(); var user = CommandExecutor.ExecuteCommand(new GetUserCommand(connection, User.Identity.Name)); if (user == null) { Log.Info("Could not find user {0} redirecting to home.", User.Identity.Name); TempData["error"] = "You must log in to submit a corner"; return(RedirectToRoute("", new { Controller = "Home", Action = "Index" })); } Log.Info("Submitting tiesheet for {0}", user.Name); cornerViewModel.User = user; var corner = new Corner(cornerViewModel); var grid = new Grid(cornerViewModel.Grid); var coordinate = new Coordinate(cornerViewModel.Coordinate); var formInfo = new FormInfo(user.Name, corner.BlmPointId); var photos = new Photo(cornerViewModel); corner.CoordinateId = coordinate.CoordinateId; corner.GridId = grid.GridId; corner.FormInfoId = formInfo.FormInfoId; corner.PhotoId = photos.PhotoId; var gInserts = connection.Execute(Grid.InsertString, grid); Debug.Assert(gInserts == 1, "inserted into grid successfully"); var cInserts = connection.Execute(Coordinate.InsertString, coordinate); Debug.Assert(cInserts == 1, "inserted into coords successfully"); var fInserts = connection.Execute(FormInfo.InsertString, formInfo); Debug.Assert(fInserts == 1, "inserted into form successfully"); var pInserts = connection.Execute(Photo.InsertString, photos); Debug.Assert(pInserts == 1, "inserted into photo successfully"); var cornInserts = connection.Execute(Corner.InsertString, corner); Debug.Assert(cornInserts == 1, "inserted into corners successfully"); var model = new TieSheetPdfModel(cornerViewModel, corner, photos); var pdfService = new PlssPdfService("Assets\\pdf"); var pdf = pdfService.HydratePdfForm("MonumentStatusTemplate.pdf", model); #if DEBUG pdf.FlattenFormFields(); #endif Log.Info("finished created database models"); var actualPath = Path.Combine(Config.Global.Get <string>("SharePath"), formInfo.Path); Log.Info($"Writing PDF to: {actualPath}"); var success = FileSaver.SaveFile(actualPath, pdf.GetPDFAsByteArray()); if (!success) { Log.Fatal($"problem saving pdf for {cornerViewModel}"); //do nothing, email will get sent about issue and we'll rebuild pdf form later. Log.Info("Sending failed notification email to {0}", string.Join(", ", App.AdminEmails)); CommandExecutor.ExecuteCommand(new UserSubmitionFailedEmailCommand( new UserSubmitionFailedEmailCommand.MailTemplate(App.AdminEmails, new[] { user. UserName }, user.Name, model.BlmPointId, model. CollectionDate))); } else { CommandExecutor.ExecuteCommand(new UserSubmittedEmailCommand( new UserSubmittedEmailCommand.MailTemplate(App.AdminEmails, new[] { user.UserName }, user.Name, model.BlmPointId, model.CollectionDate, actualPath))); } Log.Info("updating forminfoes table path: {0}", actualPath, success); var cUpdate = connection.Execute( "update FormInfoes set " + "path = @actualpath, " + "uploadedSuccessfully = @success " + "where forminfoid = @FormInfoId", new { actualPath, formInfo.FormInfoId, success }); Debug.Assert(cUpdate == 1, "updated form infos correctly"); } catch (Exception ex) { Log.LogException(LogLevel.Fatal, $"problem saving new corner for {cornerViewModel}", ex); TempData["error"] = ex.Message; return(RedirectToRoute("", new { Controller = "Home", Action = "Index" })); } finally { connection.Close(); connection.Dispose(); } return(RedirectToRoute("", new { Controller = "Home", Action = "Index" })); }