MiscController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using DevExpress.Utils;
  2. using DevExpress.Web;
  3. using DevExpress.Web.ASPxThemes;
  4. using DevExpress.Web.Mvc;
  5. using DevExpress.XtraPrinting;
  6. using DevExpress.XtraScheduler;
  7. using GreenTree.Nachtragsmanagement.Core.Domain.Deviation;
  8. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  9. using GreenTree.Nachtragsmanagement.Services.Appendix;
  10. using GreenTree.Nachtragsmanagement.Services.Deviation;
  11. using GreenTree.Nachtragsmanagement.Services.Misc;
  12. using GreenTree.Nachtragsmanagement.Services.Scheduling;
  13. using GreenTree.Nachtragsmanagement.Services.User;
  14. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  15. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  16. using GreenTree.Nachtragsmanagement.Web.Models.Misc;
  17. using Newtonsoft.Json;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Web;
  23. using System.Web.Mvc;
  24. using System.Web.UI;
  25. using System.Web.UI.WebControls;
  26. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  27. {
  28. public class MiscController : Controller
  29. {
  30. private readonly IMiscService _miscService;
  31. private readonly IUserService _userService;
  32. private readonly INotificationService _notificationService;
  33. private readonly INotificationScheduler _notificationScheduler;
  34. public MiscController(
  35. IMiscService miscService,
  36. IUserService userService,
  37. INotificationService notificationService,
  38. INotificationScheduler notificationScheduler)
  39. {
  40. _miscService = miscService;
  41. _userService = userService;
  42. _notificationService = notificationService;
  43. _notificationScheduler = notificationScheduler;
  44. ViewData["AllUsers"] = _userService.GetAllUsers();
  45. ViewData["AllUsersWithRole"] =
  46. _userService.GetAllUsers()
  47. .Select(u => new
  48. {
  49. Id = u.Id,
  50. Description = String.Format("{0} - {1}", u.Lastname,
  51. String.Join(", ", u.Roles
  52. .Select(r => r.Description)))
  53. })
  54. .ToList();
  55. ViewData["AllNotificationPlugins"] =
  56. _notificationService.GetNotificationPlugins();
  57. }
  58. #region MailNotifications
  59. /// <summary>
  60. /// Basic mailNotification view function
  61. /// </summary>
  62. [FunctionAuthorize(true, "Misc-MailNotifications")]
  63. public ActionResult ViewMailNotifications()
  64. {
  65. var mailNotifications = _miscService.GetAllMailNotifications();
  66. var mailNotificationModels = mailNotifications
  67. .Select(u => MailNotificationDataModel.FromMailNotification(
  68. u, false, _notificationService, _notificationScheduler))
  69. .ToList();
  70. return View("~/Views/Misc/MailNotifications.cshtml", mailNotificationModels);
  71. }
  72. /// <summary>
  73. /// Get JSON data of specific mailNotification
  74. /// </summary>
  75. /// <param name="id">MailNotification id.</param>
  76. public ActionResult GetMailNotification(int id = -1)
  77. {
  78. var mailNotification = _miscService.GetMailNotificationById(id);
  79. if (mailNotification == null)
  80. return new JsonResult
  81. {
  82. Data = "notFound",
  83. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  84. };
  85. var mailNotificationModel = MailNotificationDataModel.FromMailNotification(
  86. mailNotification, false, _notificationService, _notificationScheduler);
  87. return new JsonResult
  88. {
  89. Data = JsonConvert.SerializeObject(mailNotificationModel),
  90. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  91. };
  92. }
  93. /// <summary>
  94. /// Callback result for mailNotification grid
  95. /// </summary>
  96. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  97. public ActionResult PartialMailNotifications(int scrollHeight = -1)
  98. {
  99. var mailNotifications = _miscService.GetAllMailNotifications();
  100. var mailNotificationModels = mailNotifications
  101. .Select(u => MailNotificationDataModel.FromMailNotification(
  102. u, false, _notificationService, _notificationScheduler))
  103. .ToList();
  104. ViewData["ScrollHeight"] = scrollHeight;
  105. return PartialView("~/Views/Misc/_MailNotificationGridPartial.cshtml", mailNotificationModels);
  106. }
  107. /// <summary>
  108. /// Callback result for mailNotification job combobox
  109. /// </summary>
  110. /// <param name="pluginSystemName">The system name of the corresponding notification plugin.</param>
  111. public ActionResult PartialNotificationPluginJobs(string pluginSystemName)
  112. {
  113. var notificationPlugin = _notificationService.GetNotificationPlugin(pluginSystemName);
  114. if (notificationPlugin == null)
  115. return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", null);
  116. var mailNotificationModel = new MailNotificationDataModel
  117. {
  118. NotificationPlugin = notificationPlugin
  119. };
  120. return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", mailNotificationModel);
  121. }
  122. /// <summary>
  123. /// Export result for mailNotification grid
  124. /// </summary>
  125. [HttpPost]
  126. public ActionResult ExportPartialMailNotifications(GridViewExportFormat exportformat)
  127. {
  128. if (exportformat == null || String.IsNullOrEmpty(exportformat.Format))
  129. return new EmptyResult();
  130. var mailNotifications = _miscService.GetAllMailNotifications();
  131. var mailNotificationModels = mailNotifications
  132. .Select(u => MailNotificationDataModel.FromMailNotification(
  133. u, false, _notificationService, _notificationScheduler))
  134. .ToList();
  135. var viewContext = new ViewContext();
  136. var viewPage = new ViewPage();
  137. var htmlHelper = new HtmlHelper(viewContext, viewPage);
  138. var gridViewSettings = Extensions.GridViewSettingsHelper.MailNotificationGridViewSettings(htmlHelper);
  139. switch (exportformat.Format.ToLower())
  140. {
  141. case "xlsx":
  142. return GridViewExtension.ExportToXlsx(gridViewSettings, mailNotificationModels);
  143. case "xls":
  144. return GridViewExtension.ExportToXls(gridViewSettings, mailNotificationModels);
  145. case "pdf":
  146. return GridViewExtension.ExportToPdf(gridViewSettings, mailNotificationModels);
  147. default:
  148. return new EmptyResult();
  149. }
  150. }
  151. /// <summary>
  152. /// Partial edit for editing of existing or for new mailNotification
  153. /// </summary>
  154. /// <param name="id">Id for existing mailNotification, otherweise -1.</param>
  155. public ActionResult EditMailNotification(int id = -1)
  156. {
  157. var mailNotification = _miscService.GetMailNotificationById(id);
  158. var mailNotificationModel = MailNotificationDataModel.FromMailNotification(
  159. mailNotification, true, _notificationService, _notificationScheduler);
  160. return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
  161. }
  162. /// <summary>
  163. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  164. /// </summary>
  165. /// <param name="mailNotificationModel">MailNotification model to be saved.</param>
  166. [HttpPost, ValidateInput(false)]
  167. public ActionResult EditMailNotification(MailNotificationDataModel mailNotificationModel)
  168. {
  169. if (!ModelState.IsValid)
  170. {
  171. foreach (var role in mailNotificationModel.UserValues)
  172. mailNotificationModel.UserDescriptions.Add(
  173. ((IList<User>)ViewData["AllUsers"])
  174. .First(r => r.Id == role).Lastname);
  175. var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotificationModel.NotificationPluginSystemName);
  176. if (notificationPlugin != null)
  177. mailNotificationModel.NotificationPlugin = notificationPlugin;
  178. return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
  179. }
  180. if (mailNotificationModel.CronExpression.Split(' ').Length == 5)
  181. mailNotificationModel.CronExpression = mailNotificationModel.CronExpression + " *";
  182. var selectedUsers = _userService.GetUsersByIds(mailNotificationModel.UserValues.ToArray());
  183. if (mailNotificationModel.Id == -1)
  184. {
  185. var mailNotification = mailNotificationModel.ToMailNotification();
  186. mailNotification.SetUsers(selectedUsers);
  187. _miscService.InsertMailNotification(mailNotification);
  188. }
  189. else
  190. {
  191. var mailNotification = _miscService.GetMailNotificationById(mailNotificationModel.Id);
  192. mailNotification.CronExpression = mailNotificationModel.CronExpression;
  193. mailNotification.NotificationPluginSystemName = mailNotificationModel.NotificationPluginSystemName;
  194. mailNotification.NotificationJobSystemName = mailNotificationModel.NotificationJobSystemName;
  195. mailNotification.SetUsers(selectedUsers);
  196. _miscService.UpdateMailNotification(mailNotification);
  197. }
  198. _notificationScheduler.Start();
  199. return new JsonResult
  200. {
  201. Data = "success"
  202. };
  203. }
  204. /// <summary>
  205. /// Simple JSON result for deleting a specific mailNotification
  206. /// </summary>
  207. /// <param name="id">MailNotification id.</param>
  208. [HttpPost]
  209. public ActionResult DeleteMailNotification(int id)
  210. {
  211. var mailNotification = _miscService.GetMailNotificationById(id);
  212. if (mailNotification != null)
  213. _miscService.DeleteMailNotification(mailNotification);
  214. return new JsonResult
  215. {
  216. Data = "success"
  217. };
  218. }
  219. /// <summary>
  220. /// Processes the specific mailNotification
  221. /// </summary>
  222. /// <param name="id">MailNotification id.</param>
  223. [HttpPost]
  224. public ActionResult ProcessMailNotification(int id)
  225. {
  226. var mailNotification = _miscService.GetMailNotificationById(id);
  227. if (mailNotification != null)
  228. {
  229. var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotification.NotificationPluginSystemName);
  230. notificationPlugin.ProcessNotifications(new[] { mailNotification });
  231. }
  232. return new JsonResult
  233. {
  234. Data = "success"
  235. };
  236. }
  237. #endregion
  238. }
  239. }