MiscController.cs 11 KB

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