using System.Web.Mvc; using Sleis.Models; using Sleis.Service; using Sleis.Utility; using Sleis.Validation.Attribute; using Sleis.ViewModels; using System; using System.Collections.Generic; using Sleis.Utility; namespace Sleis.Controllers { [HandleError] public class AgencyController : BaseController { public IReportService ReportService { get; set; } public PublicHomeService HomeService { get; set; } public CersService CersService { get; set; } public override void Init() { base.Init(); ArgumentValidationUtility.ThrowOnNull(ReportService, "ReportService"); } [Authorize, AppRoleValidation(AppUserRoleType.AgencyAcceptor, AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencyEditor, AppUserRoleType.AgencySubmitter, AppUserRoleType.AgencyViewer)] //[Authorize] public ActionResult Home() { var model = new AgencyHomeView(ReportService.GetSubmittedReportsCount(), ReportService.GetPendingRequestsCount(), ReportService.GetPendingRepudiationsCount(), HomeService.NewsData.GetActiveList()); //clear selected facility. SetSelectedAgencyFacility(0); //base.AppUser.User. return View(model); } public ActionResult ListOpenRequests() { return View(); } [HttpGet] public ActionResult ProcessRequest(int id, string type) { switch (type.ToLower()) { case "repudiation": return View(new ProcessRequestViewModel{ Request = FacilityService.GetRepudiationRequest(id)}); case "amendment": return View(new ProcessRequestViewModel { Request = FacilityService.GetAmendmentRequest(id) }); } return View(new ProcessRequestViewModel()); } [HttpPost, AppRoleValidation(AppUserRoleType.AgencyAcceptor, AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencyEditor, AppUserRoleType.AgencySubmitter)] public ActionResult ProcessRequest(int id, string type, string action, string comment) { ProcessRequestViewModel viewModel = new ProcessRequestViewModel(); if (String.IsNullOrWhiteSpace(comment)) { ModelState.AddModelError("Comment", "Required"); } if (ModelState.IsValid) { bool approved = action.ToLower() == "allow"; try { switch (type.ToLower()) { case "repudiation": viewModel.Request = ReportService.GetRepudiationRequest(id); ReportService.CloseRepudiation(viewModel.Request as RepudiateRequestModel, approved, comment); break; case "amendment": viewModel.Request = ReportService.GetAmendmentRequest(id); ReportService.CloseAmendment(viewModel.Request as AmendmentRequestModel, approved, comment); break; } return RedirectToAction("ListOpenRequests"); } catch (Exception err) { TempData[Constants.GlobalMessageKey] = new SimpleMessage(Constants.SavingDataErrorMessage, true); Log.Error(err); } } switch (type.ToLower()) { case "repudiation": return View(new ProcessRequestViewModel { Request = FacilityService.GetRepudiationRequest(id) }); case "amendment": return View(new ProcessRequestViewModel { Request = FacilityService.GetAmendmentRequest(id) }); } throw new Exception("There has been an error"); } [HttpGet, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencyEditor)] public ActionResult GetOpenRepudiations() { ListView model = new ListView(); model.List = ReportService.GetPendingRepudiations(); return Json(model, JsonRequestBehavior.AllowGet); } [HttpGet, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencyEditor)] public ActionResult GetOpenAmendmentRequests() { ListView model = new ListView(); model.List = ReportService.GetPendingAmendmentRequests(); return Json(model, JsonRequestBehavior.AllowGet); } [HttpGet, Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencyEditor)] public ActionResult Administration() { return View(); } [Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencyEditor)] public ActionResult EpaSubmissions() { Session["SelectedFacilities"] = null; //clear SelectedFacilites. return View(); } [HttpGet, Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencyEditor)] public ActionResult PopulateStagingTables() { PopulateStagingTablesView view = new PopulateStagingTablesView(); //Get possible reporting years List ReportingYears = ReportService.GetStageableReportingYears(); ReportingYears.Reverse(); view.FacilityInventoryYears = new SelectList(ReportingYears); view.PointEmissionsYears = new SelectList(ReportingYears); return View(view); } [HttpPost, Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencyEditor)] public ActionResult PopulateStagingTables(PopulateStagingTablesView view) { try { int result = 0; switch (view.EisCategory) { case EIS4Sleis.EISCategory.FacilityInventory: result = CersService.PopulateStagingTables(view.SelectedFacilities, view.UseMasterFacility, view.SelectedFacilityInventoryYear, SessionUtility.CurrentUser.Email, EIS4Sleis.EISCategory.FacilityInventory); break; case EIS4Sleis.EISCategory.Point: result = CersService.PopulateStagingTables(view.SelectedFacilities, false, view.SelectedPointEmissionsYear, SessionUtility.CurrentUser.Email, EIS4Sleis.EISCategory.Point); break; } if (result < 0) { throw new Exception("Error populating staging tables."); } string message = result == 1 ? "Data successfully staged for 1 facility." : String.Format("Data successfully staged for {0} facilities.", result); TempData[Constants.GlobalMessageKey] = new SimpleMessage(message, false); } catch (Exception err) { TempData[Constants.GlobalMessageKey] = new SimpleMessage(Constants.SavingDataErrorMessage, true); Log.Error(err); } //Populate Dropdowns and use currently selected reporting year. List ReportingYears = ReportService.GetStageableReportingYears(); view.FacilityInventoryYears = new SelectList(ReportingYears, view.SelectedFacilityInventoryYear); view.PointEmissionsYears = new SelectList(ReportingYears, view.SelectedPointEmissionsYear); return View(view); } [HttpGet, Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencyEditor)] public ActionResult SubmitEpaData() { List submissionYears = CersService.GetEmissionYears(EIS4Sleis.EISCategory.FacilityInventory); submissionYears.Sort(); //sort so ordered by ascending submissionYears.Reverse();//order by descending SubmitEpaDataView view = new SubmitEpaDataView(CersService.GetReportingPeriodTypes(), submissionYears); view.SelectedDataCategory = EIS4Sleis.EISCategory.FacilityInventory; view.Password = Properties.Get(Constants.NodeEndpointPassword).ToString(); view.UserName = Properties.Get(Constants.NodeEndpointUsername).ToString(); view.InventoryNodeUrl = Properties.Get(Constants.NodeEndpointUrl).ToString(); view.OrganizationName = Properties.Get(Constants.NodeOrganization).ToString(); return View(view); } [HttpPost, Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencyEditor)] public ActionResult SubmitEpaData(SubmitEpaDataView view) { if (String.IsNullOrWhiteSpace(view.AuthorName)) { ModelState.AddModelError("AuthorName", "Required"); } if (String.IsNullOrWhiteSpace(view.OrganizationName)) { ModelState.AddModelError("OrganizationName", "Required"); } if (String.IsNullOrWhiteSpace(view.SelectedSubmissionType)) { ModelState.AddModelError("SelectedSubmissionType", "Required"); } if (String.IsNullOrWhiteSpace(view.SelectedReportType)) { ModelState.AddModelError("SelectedReportType", "Required"); } if (view.SelectedInventoryYear == 0) { ModelState.AddModelError("SelectedInventoryYear", "Required"); } if (String.IsNullOrWhiteSpace(view.InventoryNodeUrl)) { ModelState.AddModelError("InventoryNodeUrl", "Required"); } if (String.IsNullOrWhiteSpace(view.UserName)) { ModelState.AddModelError("UserName", "Required"); } if (String.IsNullOrWhiteSpace(view.Password)) { ModelState.AddModelError("Password", "Required"); } if (ModelState.IsValid) { EisSubmission submission = new EisSubmission(); try { EIS4Sleis.NodeType nodeType = EIS4Sleis.NodeType.Default; if (Properties.Get(Constants.NodeType) == "v20") { nodeType = EIS4Sleis.NodeType.v20; } else if (Properties.Get(Constants.NodeType) == "v11") { nodeType = EIS4Sleis.NodeType.v11; } bool isProductionSubmission = false; if (view.SelectedSubmissionType == "Production") { isProductionSubmission = true; } EIS4Sleis.DatabaseType databaseType = EIS4Sleis.DatabaseType.SqlServer; if (Properties.Get(Constants.AppDataProvider) == "System.Data.OracleClient") { databaseType = EIS4Sleis.DatabaseType.Oracle; } EIS4Sleis.DatabaseParameters databaseParamters = new EIS4Sleis.DatabaseParameters(databaseType, Properties.Get(Constants.AppConnectionString)); EIS4Sleis.NodeEndpointParameters endpointParameters = new EIS4Sleis.NodeEndpointParameters(nodeType, view.InventoryNodeUrl, view.UserName, view.Password); EIS4Sleis.SubmissionParameters submissionParameters = new EIS4Sleis.SubmissionParameters(view.AuthorName, view.OrganizationName, SessionUtility.CurrentUser.Email, isProductionSubmission, view.SelectedInventoryYear, view.SelectedDataCategory); submissionParameters.AttachmentFolderPath = Properties.Get(Constants.NodeAttachmentFolderPath); CersService.SubmitToEpa(databaseParamters, endpointParameters, submissionParameters, out submission); TempData[Constants.GlobalMessageKey] = new SimpleMessage("Successfully submitted EPA EIS data.", false); //Audit AppEventModel audit = new AppEventModel(EventType.Audit, SessionUtility.CurrentUser.Email, "Successfully submitted EPA EIS data.", Request.UserHostAddress); UserService.Audit(audit); //Save Audit return RedirectToAction("EpaSubmissions"); } catch (EIS4Sleis.EIS4SleisException err) { string errorType = EnumUtility.ToDescription(err.ExceptionType); switch (err.ExceptionType) { case EIS4Sleis.ExceptionType.Uncategorized: TempData[Constants.GlobalMessageKey] = new SimpleMessage(errorType, true); break; case EIS4Sleis.ExceptionType.ValidationException: string downloadUrl = Url.Action("GetEisXml", new{id=submission.Id}); string sHref = String.Format(" {1}", downloadUrl, "Download XML"); //TempData[Constants.GlobalMessageKey] = new SimpleMessage(errorType + " Validation Error File - " + err.ValidationErrorsFilePath, true); TempData[Constants.GlobalMessageKey] = new SimpleMessage(errorType + sHref, true); //TempData[Constants.EisSubmissionId] = submission.Id; break; case EIS4Sleis.ExceptionType.DatabaseException: TempData[Constants.GlobalMessageKey] = new SimpleMessage(errorType, true); break; case EIS4Sleis.ExceptionType.NodeEndpointException: TempData[Constants.GlobalMessageKey] = new SimpleMessage(errorType + err.Message, true); break; case EIS4Sleis.ExceptionType.SerializationException: TempData[Constants.GlobalMessageKey] = new SimpleMessage(errorType, true); break; default: TempData[Constants.GlobalMessageKey] = new SimpleMessage("Error submitting EPA EIS data.", true); break; } //Audit AppEventModel audit = new AppEventModel(EventType.Audit, SessionUtility.CurrentUser.Email,String.Format("Error submitting EPA EIS data. {0}: {1}.", errorType, err.Message), Request.UserHostAddress); UserService.Audit(audit); //Save Audit Log.Error(err); } catch (Exception err) { TempData[Constants.GlobalMessageKey] = new SimpleMessage(Constants.SavingDataErrorMessage, true); AppEventModel audit = new AppEventModel(EventType.Audit, SessionUtility.CurrentUser.Email, String.Format("Error submitting EPA EIS data. {0}.", err.Message), Request.UserHostAddress); UserService.Audit(audit); //Save Audit Log.Error(err); } } view.InitializeReportType(CersService.GetReportingPeriodTypes()); view.InitializeInventoryYear(CersService.GetEmissionYears(view.SelectedDataCategory)); return View(view); } [Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencySubmitter)] public ActionResult ViewSubmissionHistory() { return View(); } [Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencySubmitter)] public ActionResult GetSubmissions() { ListView model = new ListView(); try { model.List = CersService.GetAll(); return Json(model, JsonRequestBehavior.AllowGet); } catch (Exception err) { Log.Error(err); //TempData[Constants.GlobalMessageKey] = new SimpleMessage(err.Message, true); throw err; } } [Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencySubmitter)] public ActionResult GetEisXml(int id) { return File(CersService.CersData.GetById(id).GeneratedXmlZip, "application/octet-stream", String.Format("{0}.zip", id)); } [Authorize, AppRoleValidation(AppUserRoleType.AgencyAdmin, AppUserRoleType.AgencySubmitter)] public ActionResult GetSubmissionDocument(string transactionId) { // CersService.GetSubmissionDocument(transactionId) return File(CersService.GetSubmissionDocument(transactionId).FilePath, "application/octet-stream", String.Format("{0}.zip", transactionId)); } } }