// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved. using System; using System.Diagnostics; using System.IO; using System.Web.Mvc; using Tools.DotNETCommon.XmlHandler; using Tools.CrashReporter.CrashReportCommon; using Tools.CrashReporter.CrashReportWebSite.Models; namespace Tools.CrashReporter.CrashReportWebSite.Controllers { /// /// The controller to handle the crash data. /// [HandleError] public class CrashesController : Controller { private CrashRepository LocalCrashRepository = new CrashRepository(); /// /// An empty constructor. /// public CrashesController() { } /// /// Display a summary list of crashes based on the search criteria. /// /// A form of user data passed up from the client. /// A view to display a list of crash reports. public ActionResult Index( FormCollection CrashesForm ) { // Handle any edits made in the Set form fields foreach( var Entry in CrashesForm ) { int Id = 0; if( int.TryParse( Entry.ToString(), out Id ) ) { Crash CurrentCrash = LocalCrashRepository.GetCrash( Id ); if( CurrentCrash != null ) { if( !string.IsNullOrEmpty( CrashesForm["SetStatus"] ) ) { CurrentCrash.Status = CrashesForm["SetStatus"]; LocalCrashRepository.SetCrashStatus( CurrentCrash.Status, Id ); } if( !string.IsNullOrEmpty( CrashesForm["SetFixedIn"] ) ) { CurrentCrash.FixedChangeList = CrashesForm["SetFixedIn"]; LocalCrashRepository.SetCrashFixedChangeList( CurrentCrash.FixedChangeList, Id ); } if( !string.IsNullOrEmpty( CrashesForm["SetTTP"] ) ) { CurrentCrash.TTPID = CrashesForm["SetTTP"]; LocalCrashRepository.SetCrashTTPID( CurrentCrash.TTPID, Id ); } } } } // Parse the contents of the query string, and populate the form FormHelper FormData = new FormHelper( Request, CrashesForm, "TimeOfCrash" ); CrashesViewModel Result = LocalCrashRepository.GetResults( FormData ); // Add the FromCollection to the CrashesViewModel since we don't need it for the get results function but we do want to post it back to the page. Result.FormCollection = CrashesForm; return View( "Index", Result ); } /// /// Show detailed information about a crash. /// /// A form of user data passed up from the client. /// The unique id of the crash we wish to show the details of. /// A view to show crash details. public ActionResult Show( FormCollection CrashesForm, int? Id ) { if (!Id.HasValue) { return RedirectToAction(""); } CallStackContainer CurrentCallStack = null; // Update the selected crash based on the form contents Crash CurrentCrash = LocalCrashRepository.GetCrash( Id.Value ); if (CurrentCrash == null) { return RedirectToAction( "" ); } string FormValue; FormValue = CrashesForm["SetStatus"]; if (!string.IsNullOrEmpty(FormValue)) { CurrentCrash.Status = FormValue; LocalCrashRepository.SetCrashStatus(FormValue, Id.Value); } FormValue = CrashesForm["SetFixedIn"]; if (!string.IsNullOrEmpty(FormValue)) { CurrentCrash.FixedChangeList = FormValue; LocalCrashRepository.SetCrashFixedChangeList(FormValue, Id.Value); } FormValue = CrashesForm["SetTTP"]; if (!string.IsNullOrEmpty(FormValue)) { CurrentCrash.TTPID = FormValue; LocalCrashRepository.SetCrashTTPID(FormValue, Id.Value); } // Valid to set description to an empty string FormValue = CrashesForm["Description"]; if (FormValue != null) { CurrentCrash.Description = FormValue; LocalCrashRepository.SetCrashDescription(FormValue, Id.Value); } CurrentCallStack = new CallStackContainer( CurrentCrash ); // Set callstack properties CurrentCallStack.bDisplayModuleNames = true; CurrentCallStack.bDisplayFunctionNames = true; CurrentCallStack.bDisplayFileNames = true; CurrentCallStack.bDisplayFilePathNames = true; CurrentCallStack.bDisplayUnformattedCallStack = false; CurrentCrash.CallStackContainer = CurrentCrash.GetCallStack(); // Populate the crash with the correct user data LocalCrashRepository.PopulateUserInfo( CurrentCrash ); return View( "Show", new CrashViewModel { Crash = CurrentCrash, CallStack = CurrentCallStack } ); } /// /// Add a crash passed in the payload as Xml to the database. /// /// Unused. /// The row id of the newly added crash. public ActionResult AddCrash( int id ) { CrashReporterResult NewCrashResult = new CrashReporterResult(); NewCrashResult.ID = -1; try { using( StreamReader Reader = new StreamReader( Request.InputStream, Request.ContentEncoding ) ) { string Result = Reader.ReadToEnd(); CrashDescription NewCrash = XmlHandler.FromXmlString( Result ); NewCrashResult.ID = LocalCrashRepository.AddNewCrash( NewCrash ); NewCrashResult.bSuccess = true; } } catch( Exception Ex ) { NewCrashResult.Message = Ex.ToString(); NewCrashResult.bSuccess = false; } string ReturnResult = XmlHandler.ToXmlString( NewCrashResult ); return Content( ReturnResult, "text/xml" ); } } }