| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- using DevExpress.Utils;
- using DevExpress.Web;
- using DevExpress.Web.ASPxThemes;
- using DevExpress.Web.Mvc;
- using DevExpress.XtraPrinting;
- using DevExpress.XtraScheduler;
- using GreenTree.Nachtragsmanagement.Core.Authentication;
- using GreenTree.Nachtragsmanagement.Core.Domain.Deviation;
- using GreenTree.Nachtragsmanagement.Core.Domain.User;
- using GreenTree.Nachtragsmanagement.Services.Appendix;
- using GreenTree.Nachtragsmanagement.Services.Deviation;
- using GreenTree.Nachtragsmanagement.Services.Logging;
- using GreenTree.Nachtragsmanagement.Services.Misc;
- using GreenTree.Nachtragsmanagement.Services.Scheduling;
- using GreenTree.Nachtragsmanagement.Services.User;
- using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
- using GreenTree.Nachtragsmanagement.Web.Models.Global;
- using GreenTree.Nachtragsmanagement.Web.Models.Misc;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace GreenTree.Nachtragsmanagement.Web.Controllers
- {
- public class MiscController : Controller
- {
- private readonly IMiscService _miscService;
- private readonly IUserService _userService;
- private readonly INotificationService _notificationService;
- private readonly INotificationScheduler _notificationScheduler;
- private readonly IUserHelper _userHelper;
- private readonly ILogger _logger;
- public MiscController(
- IMiscService miscService,
- IUserService userService,
- INotificationService notificationService,
- INotificationScheduler notificationScheduler,
- IUserHelper userHelper,
- ILogger logger)
- {
- _miscService = miscService;
- _userService = userService;
- _notificationService = notificationService;
- _notificationScheduler = notificationScheduler;
- _userHelper = userHelper;
- _logger = logger;
- ViewData["AllUsers"] = _userService.GetAllUsers();
- ViewData["AllUsersWithRole"] =
- _userService.GetAllUsers()
- .Select(u => new
- {
- Id = u.Id,
- Description = String.Format("{0} - {1}", u.Lastname,
- String.Join(", ", u.Roles
- .Select(r => r.Description)))
- })
- .ToList();
- ViewData["AllNotificationPlugins"] =
- _notificationService.GetNotificationPlugins();
- }
- #region MailNotifications
- /// <summary>
- /// Basic mailNotification view function
- /// </summary>
- [FunctionAuthorize(true, "Misc-MailNotifications")]
- public ActionResult ViewMailNotifications()
- {
- var mailNotifications = _miscService.GetAllMailNotifications();
- var mailNotificationModels = mailNotifications
- .Select(u => MailNotificationDataModel.FromMailNotification(
- u, false, _notificationService, _notificationScheduler))
- .ToList();
- return View("~/Views/Misc/MailNotifications.cshtml", mailNotificationModels);
- }
- /// <summary>
- /// Get JSON data of specific mailNotification
- /// </summary>
- /// <param name="id">MailNotification id.</param>
- public ActionResult GetMailNotification(int id = -1)
- {
- var mailNotification = _miscService.GetMailNotificationById(id);
- if (mailNotification == null)
- return new JsonResult
- {
- Data = "notFound",
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- var mailNotificationModel = MailNotificationDataModel.FromMailNotification(
- mailNotification, false, _notificationService, _notificationScheduler);
-
- return new JsonResult
- {
- Data = JsonConvert.SerializeObject(mailNotificationModel),
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- }
- /// <summary>
- /// Callback result for mailNotification grid
- /// </summary>
- /// <param name="scrollHeight">The height of the grid scrollable component.</param>
- public ActionResult PartialMailNotifications(int scrollHeight = -1)
- {
- var mailNotifications = _miscService.GetAllMailNotifications();
- var mailNotificationModels = mailNotifications
- .Select(u => MailNotificationDataModel.FromMailNotification(
- u, false, _notificationService, _notificationScheduler))
- .ToList();
- ViewData["ScrollHeight"] = scrollHeight;
- return PartialView("~/Views/Misc/_MailNotificationGridPartial.cshtml", mailNotificationModels);
- }
- /// <summary>
- /// Callback result for mailNotification job combobox
- /// </summary>
- /// <param name="pluginSystemName">The system name of the corresponding notification plugin.</param>
- public ActionResult PartialNotificationPluginJobs(string pluginSystemName)
- {
- var notificationPlugin = _notificationService.GetNotificationPlugin(pluginSystemName);
- if (notificationPlugin == null)
- return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", null);
- var mailNotificationModel = new MailNotificationDataModel
- {
- NotificationPlugin = notificationPlugin
- };
- return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", mailNotificationModel);
- }
- /// <summary>
- /// Export result for mailNotification grid
- /// </summary>
- [HttpPost]
- public ActionResult ExportPartialMailNotifications(GridViewExportFormat exportformat)
- {
- if (exportformat == null || String.IsNullOrEmpty(exportformat.Format))
- return new EmptyResult();
- var mailNotifications = _miscService.GetAllMailNotifications();
- var mailNotificationModels = mailNotifications
- .Select(u => MailNotificationDataModel.FromMailNotification(
- u, false, _notificationService, _notificationScheduler))
- .ToList();
- var viewContext = new ViewContext();
- var viewPage = new ViewPage();
- var htmlHelper = new HtmlHelper(viewContext, viewPage);
- var gridViewSettings = Extensions.GridViewSettingsHelper.MailNotificationGridViewSettings(htmlHelper);
- switch (exportformat.Format.ToLower())
- {
- case "xlsx":
- return GridViewExtension.ExportToXlsx(gridViewSettings, mailNotificationModels);
- case "xls":
- return GridViewExtension.ExportToXls(gridViewSettings, mailNotificationModels);
- case "pdf":
- return GridViewExtension.ExportToPdf(gridViewSettings, mailNotificationModels);
- default:
- return new EmptyResult();
- }
- }
- /// <summary>
- /// Partial edit for editing of existing or for new mailNotification
- /// </summary>
- /// <param name="id">Id for existing mailNotification, otherweise -1.</param>
- public ActionResult EditMailNotification(int id = -1)
- {
- var mailNotification = _miscService.GetMailNotificationById(id);
- var mailNotificationModel = MailNotificationDataModel.FromMailNotification(
- mailNotification, true, _notificationService, _notificationScheduler);
- return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
- }
- /// <summary>
- /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
- /// </summary>
- /// <param name="mailNotificationModel">MailNotification model to be saved.</param>
- [HttpPost, ValidateInput(false)]
- public ActionResult EditMailNotification(MailNotificationDataModel mailNotificationModel)
- {
- try
- {
- if (!ModelState.IsValid)
- {
- foreach (var role in mailNotificationModel.UserValues)
- mailNotificationModel.UserDescriptions.Add(
- ((IList<User>)ViewData["AllUsers"])
- .First(r => r.Id == role).Lastname);
- var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotificationModel.NotificationPluginSystemName);
- if (notificationPlugin != null)
- mailNotificationModel.NotificationPlugin = notificationPlugin;
- return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
- }
- if (mailNotificationModel.CronExpression.Split(' ').Length == 5)
- mailNotificationModel.CronExpression = mailNotificationModel.CronExpression + " *";
- var selectedUsers = _userService.GetUsersByIds(mailNotificationModel.UserValues.ToArray());
- if (mailNotificationModel.Id == -1)
- {
- var mailNotification = mailNotificationModel.ToMailNotification();
- mailNotification.SetUsers(selectedUsers);
- _miscService.InsertMailNotification(mailNotification);
- _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
- }
- else
- {
- var mailNotification = _miscService.GetMailNotificationById(mailNotificationModel.Id);
- mailNotification.CronExpression = mailNotificationModel.CronExpression;
- mailNotification.NotificationPluginSystemName = mailNotificationModel.NotificationPluginSystemName;
- mailNotification.NotificationJobSystemName = mailNotificationModel.NotificationJobSystemName;
- mailNotification.SetUsers(selectedUsers);
- _miscService.UpdateMailNotification(mailNotification);
- _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
- }
- _notificationScheduler.Start();
- return new JsonResult
- {
- Data = "success"
- };
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Speicherung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- }
- /// <summary>
- /// Simple JSON result for deleting a specific mailNotification
- /// </summary>
- /// <param name="id">MailNotification id.</param>
- [HttpPost]
- public ActionResult DeleteMailNotification(int id)
- {
- try
- {
- var mailNotification = _miscService.GetMailNotificationById(id);
- if (mailNotification != null)
- _miscService.DeleteMailNotification(mailNotification);
- _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
- return new JsonResult
- {
- Data = "success"
- };
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Löschung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- }
- /// <summary>
- /// Processes the specific mailNotification
- /// </summary>
- /// <param name="id">MailNotification id.</param>
- [HttpPost]
- public ActionResult ProcessMailNotification(int id)
- {
- try
- {
- var mailNotification = _miscService.GetMailNotificationById(id);
- if (mailNotification != null)
- {
- var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotification.NotificationPluginSystemName);
- notificationPlugin.ProcessNotifications(new[] { mailNotification });
- }
- return new JsonResult
- {
- Data = "success"
- };
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Ausführung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- }
- #endregion
- #region Logs
- /// <summary>
- /// Basic log view function
- /// </summary>
- [FunctionAuthorize(true, "Misc-Logs")]
- public ActionResult ViewLogs()
- {
- var logs = _logger.GetAllLogs();
- var logModels = logs
- .Select(u => LogDataModel.FromLog(u, false))
- .ToList();
- return View("~/Views/Misc/Logs.cshtml", logModels);
- }
- /// <summary>
- /// Get JSON data of specific log
- /// </summary>
- /// <param name="id">Log id.</param>
- public ActionResult GetLog(int id = -1)
- {
- var log = _logger.GetLogById(id);
- if (log == null)
- return new JsonResult
- {
- Data = "notFound",
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- var logModel = LogDataModel.FromLog(log, false);
- return new JsonResult
- {
- Data = JsonConvert.SerializeObject(logModel),
- JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- }
- /// <summary>
- /// Callback result for log grid
- /// </summary>
- /// <param name="scrollHeight">The height of the grid scrollable component.</param>
- public ActionResult PartialLogs(int scrollHeight = -1)
- {
- var logs = _logger.GetAllLogs();
- var logModels = logs
- .Select(u => LogDataModel.FromLog(u, false))
- .ToList();
- ViewData["ScrollHeight"] = scrollHeight;
- return PartialView("~/Views/Misc/_LogGridPartial.cshtml", logModels);
- }
- /// <summary>
- /// Export result for log grid
- /// </summary>
- [HttpPost]
- public ActionResult ExportPartialLogs(GridViewExportFormat exportformat)
- {
- if (exportformat == null || String.IsNullOrEmpty(exportformat.Format))
- return new EmptyResult();
- var logs = _logger.GetAllLogs();
- var logModels = logs
- .Select(u => LogDataModel.FromLog(u, false))
- .ToList();
- var viewContext = new ViewContext();
- var viewPage = new ViewPage();
- var htmlHelper = new HtmlHelper(viewContext, viewPage);
- var gridViewSettings = Extensions.GridViewSettingsHelper.LogGridViewSettings(htmlHelper);
- switch (exportformat.Format.ToLower())
- {
- case "xlsx":
- return GridViewExtension.ExportToXlsx(gridViewSettings, logModels);
- case "xls":
- return GridViewExtension.ExportToXls(gridViewSettings, logModels);
- case "pdf":
- return GridViewExtension.ExportToPdf(gridViewSettings, logModels);
- default:
- return new EmptyResult();
- }
- }
- /// <summary>
- /// Partial edit for editing of existing or for new log
- /// </summary>
- /// <param name="id">Id for existing log, otherweise -1.</param>
- public ActionResult ViewLog(int id = -1)
- {
- var log = _logger.GetLogById(id);
- var logModel = LogDataModel.FromLog(log, true);
- return PartialView("~/Views/Misc/_LogViewPartial.cshtml", logModel);
- }
- /// <summary>
- /// Simple JSON result for deleting a specific log
- /// </summary>
- /// <param name="id">Log id.</param>
- [HttpPost]
- public ActionResult DeleteLog(int id)
- {
- try
- {
- var log = _logger.GetLogById(id);
- if (log != null)
- _logger.DeleteLog(log);
- return new JsonResult
- {
- Data = "success"
- };
- }
- catch (Exception ex)
- {
- _logger.Error("Fehler bei Löschung eines Logs.", ex, _userHelper.FromCookies());
- return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
- }
- }
- #endregion
- }
- }
|