MiscController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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.Authentication;
  8. using GreenTree.Nachtragsmanagement.Core.Domain.Deviation;
  9. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  10. using GreenTree.Nachtragsmanagement.Services.Appendix;
  11. using GreenTree.Nachtragsmanagement.Services.Deviation;
  12. using GreenTree.Nachtragsmanagement.Services.Logging;
  13. using GreenTree.Nachtragsmanagement.Services.Misc;
  14. using GreenTree.Nachtragsmanagement.Services.Scheduling;
  15. using GreenTree.Nachtragsmanagement.Services.User;
  16. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  17. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  18. using GreenTree.Nachtragsmanagement.Web.Models.Misc;
  19. using Newtonsoft.Json;
  20. using System;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using System.Linq;
  24. using System.Web;
  25. using System.Web.Mvc;
  26. using System.Web.UI;
  27. using System.Web.UI.WebControls;
  28. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  29. {
  30. public class MiscController : Controller
  31. {
  32. private readonly IMiscService _miscService;
  33. private readonly IUserService _userService;
  34. private readonly INotificationService _notificationService;
  35. private readonly INotificationScheduler _notificationScheduler;
  36. private readonly IUserHelper _userHelper;
  37. private readonly ILogger _logger;
  38. public MiscController(
  39. IMiscService miscService,
  40. IUserService userService,
  41. INotificationService notificationService,
  42. INotificationScheduler notificationScheduler,
  43. IUserHelper userHelper,
  44. ILogger logger)
  45. {
  46. _miscService = miscService;
  47. _userService = userService;
  48. _notificationService = notificationService;
  49. _notificationScheduler = notificationScheduler;
  50. _userHelper = userHelper;
  51. _logger = logger;
  52. ViewData["AllUsers"] = _userService.GetAllUsers();
  53. ViewData["AllUsersWithRole"] =
  54. _userService.GetAllUsers()
  55. .Select(u => new
  56. {
  57. Id = u.Id,
  58. Description = String.Format("{0} - {1}", u.Lastname,
  59. String.Join(", ", u.Roles
  60. .Select(r => r.Description)))
  61. })
  62. .ToList();
  63. ViewData["AllNotificationPlugins"] =
  64. _notificationService.GetNotificationPlugins();
  65. }
  66. #region MailNotifications
  67. /// <summary>
  68. /// Basic mailNotification view function
  69. /// </summary>
  70. [FunctionAuthorize(true, "Misc-MailNotifications")]
  71. public ActionResult ViewMailNotifications()
  72. {
  73. var mailNotifications = _miscService.GetAllMailNotifications();
  74. var mailNotificationModels = mailNotifications
  75. .Select(u => MailNotificationDataModel.FromMailNotification(
  76. u, false, _notificationService, _notificationScheduler))
  77. .ToList();
  78. return View("~/Views/Misc/MailNotifications.cshtml", mailNotificationModels);
  79. }
  80. /// <summary>
  81. /// Get JSON data of specific mailNotification
  82. /// </summary>
  83. /// <param name="id">MailNotification id.</param>
  84. public ActionResult GetMailNotification(int id = -1)
  85. {
  86. var mailNotification = _miscService.GetMailNotificationById(id);
  87. if (mailNotification == null)
  88. return new JsonResult
  89. {
  90. Data = "notFound",
  91. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  92. };
  93. var mailNotificationModel = MailNotificationDataModel.FromMailNotification(
  94. mailNotification, false, _notificationService, _notificationScheduler);
  95. return new JsonResult
  96. {
  97. Data = JsonConvert.SerializeObject(mailNotificationModel),
  98. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  99. };
  100. }
  101. /// <summary>
  102. /// Callback result for mailNotification grid
  103. /// </summary>
  104. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  105. public ActionResult PartialMailNotifications(int scrollHeight = -1)
  106. {
  107. var mailNotifications = _miscService.GetAllMailNotifications();
  108. var mailNotificationModels = mailNotifications
  109. .Select(u => MailNotificationDataModel.FromMailNotification(
  110. u, false, _notificationService, _notificationScheduler))
  111. .ToList();
  112. ViewData["ScrollHeight"] = scrollHeight;
  113. return PartialView("~/Views/Misc/_MailNotificationGridPartial.cshtml", mailNotificationModels);
  114. }
  115. /// <summary>
  116. /// Callback result for mailNotification job combobox
  117. /// </summary>
  118. /// <param name="pluginSystemName">The system name of the corresponding notification plugin.</param>
  119. public ActionResult PartialNotificationPluginJobs(string pluginSystemName)
  120. {
  121. var notificationPlugin = _notificationService.GetNotificationPlugin(pluginSystemName);
  122. if (notificationPlugin == null)
  123. return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", null);
  124. var mailNotificationModel = new MailNotificationDataModel
  125. {
  126. NotificationPlugin = notificationPlugin
  127. };
  128. return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", mailNotificationModel);
  129. }
  130. /// <summary>
  131. /// Export result for mailNotification grid
  132. /// </summary>
  133. [HttpPost]
  134. public ActionResult ExportPartialMailNotifications(GridViewExportFormat exportformat)
  135. {
  136. if (exportformat == null || String.IsNullOrEmpty(exportformat.Format))
  137. return new EmptyResult();
  138. var mailNotifications = _miscService.GetAllMailNotifications();
  139. var mailNotificationModels = mailNotifications
  140. .Select(u => MailNotificationDataModel.FromMailNotification(
  141. u, false, _notificationService, _notificationScheduler))
  142. .ToList();
  143. var viewContext = new ViewContext();
  144. var viewPage = new ViewPage();
  145. var htmlHelper = new HtmlHelper(viewContext, viewPage);
  146. var gridViewSettings = Extensions.GridViewSettingsHelper.MailNotificationGridViewSettings(htmlHelper);
  147. switch (exportformat.Format.ToLower())
  148. {
  149. case "xlsx":
  150. return GridViewExtension.ExportToXlsx(gridViewSettings, mailNotificationModels);
  151. case "xls":
  152. return GridViewExtension.ExportToXls(gridViewSettings, mailNotificationModels);
  153. case "pdf":
  154. return GridViewExtension.ExportToPdf(gridViewSettings, mailNotificationModels);
  155. default:
  156. return new EmptyResult();
  157. }
  158. }
  159. /// <summary>
  160. /// Partial edit for editing of existing or for new mailNotification
  161. /// </summary>
  162. /// <param name="id">Id for existing mailNotification, otherweise -1.</param>
  163. public ActionResult EditMailNotification(int id = -1)
  164. {
  165. var mailNotification = _miscService.GetMailNotificationById(id);
  166. var mailNotificationModel = MailNotificationDataModel.FromMailNotification(
  167. mailNotification, true, _notificationService, _notificationScheduler);
  168. return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
  169. }
  170. /// <summary>
  171. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  172. /// </summary>
  173. /// <param name="mailNotificationModel">MailNotification model to be saved.</param>
  174. [HttpPost, ValidateInput(false)]
  175. public ActionResult EditMailNotification(MailNotificationDataModel mailNotificationModel)
  176. {
  177. try
  178. {
  179. if (!ModelState.IsValid)
  180. {
  181. foreach (var role in mailNotificationModel.UserValues)
  182. mailNotificationModel.UserDescriptions.Add(
  183. ((IList<User>)ViewData["AllUsers"])
  184. .First(r => r.Id == role).Lastname);
  185. var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotificationModel.NotificationPluginSystemName);
  186. if (notificationPlugin != null)
  187. mailNotificationModel.NotificationPlugin = notificationPlugin;
  188. return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
  189. }
  190. if (mailNotificationModel.CronExpression.Split(' ').Length == 5)
  191. mailNotificationModel.CronExpression = mailNotificationModel.CronExpression + " *";
  192. var selectedUsers = _userService.GetUsersByIds(mailNotificationModel.UserValues.ToArray());
  193. if (mailNotificationModel.Id == -1)
  194. {
  195. var mailNotification = mailNotificationModel.ToMailNotification();
  196. mailNotification.SetUsers(selectedUsers);
  197. _miscService.InsertMailNotification(mailNotification);
  198. _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  199. }
  200. else
  201. {
  202. var mailNotification = _miscService.GetMailNotificationById(mailNotificationModel.Id);
  203. mailNotification.CronExpression = mailNotificationModel.CronExpression;
  204. mailNotification.NotificationPluginSystemName = mailNotificationModel.NotificationPluginSystemName;
  205. mailNotification.NotificationJobSystemName = mailNotificationModel.NotificationJobSystemName;
  206. mailNotification.SetUsers(selectedUsers);
  207. _miscService.UpdateMailNotification(mailNotification);
  208. _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  209. }
  210. _notificationScheduler.Start();
  211. return new JsonResult
  212. {
  213. Data = "success"
  214. };
  215. }
  216. catch (Exception ex)
  217. {
  218. _logger.Error("Fehler bei Speicherung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
  219. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  220. }
  221. }
  222. /// <summary>
  223. /// Simple JSON result for deleting a specific mailNotification
  224. /// </summary>
  225. /// <param name="id">MailNotification id.</param>
  226. [HttpPost]
  227. public ActionResult DeleteMailNotification(int id)
  228. {
  229. try
  230. {
  231. var mailNotification = _miscService.GetMailNotificationById(id);
  232. if (mailNotification != null)
  233. _miscService.DeleteMailNotification(mailNotification);
  234. _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  235. return new JsonResult
  236. {
  237. Data = "success"
  238. };
  239. }
  240. catch (Exception ex)
  241. {
  242. _logger.Error("Fehler bei Löschung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
  243. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  244. }
  245. }
  246. /// <summary>
  247. /// Processes the specific mailNotification
  248. /// </summary>
  249. /// <param name="id">MailNotification id.</param>
  250. [HttpPost]
  251. public ActionResult ProcessMailNotification(int id)
  252. {
  253. try
  254. {
  255. var mailNotification = _miscService.GetMailNotificationById(id);
  256. if (mailNotification != null)
  257. {
  258. var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotification.NotificationPluginSystemName);
  259. notificationPlugin.ProcessNotifications(new[] { mailNotification });
  260. }
  261. return new JsonResult
  262. {
  263. Data = "success"
  264. };
  265. }
  266. catch (Exception ex)
  267. {
  268. _logger.Error("Fehler bei Ausführung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
  269. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  270. }
  271. }
  272. #endregion
  273. #region Logs
  274. /// <summary>
  275. /// Basic log view function
  276. /// </summary>
  277. [FunctionAuthorize(true, "Misc-Logs")]
  278. public ActionResult ViewLogs()
  279. {
  280. var logs = _logger.GetAllLogs();
  281. var logModels = logs
  282. .Select(u => LogDataModel.FromLog(u, false))
  283. .ToList();
  284. return View("~/Views/Misc/Logs.cshtml", logModels);
  285. }
  286. /// <summary>
  287. /// Get JSON data of specific log
  288. /// </summary>
  289. /// <param name="id">Log id.</param>
  290. public ActionResult GetLog(int id = -1)
  291. {
  292. var log = _logger.GetLogById(id);
  293. if (log == null)
  294. return new JsonResult
  295. {
  296. Data = "notFound",
  297. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  298. };
  299. var logModel = LogDataModel.FromLog(log, false);
  300. return new JsonResult
  301. {
  302. Data = JsonConvert.SerializeObject(logModel),
  303. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  304. };
  305. }
  306. /// <summary>
  307. /// Callback result for log grid
  308. /// </summary>
  309. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  310. public ActionResult PartialLogs(int scrollHeight = -1)
  311. {
  312. var logs = _logger.GetAllLogs();
  313. var logModels = logs
  314. .Select(u => LogDataModel.FromLog(u, false))
  315. .ToList();
  316. ViewData["ScrollHeight"] = scrollHeight;
  317. return PartialView("~/Views/Misc/_LogGridPartial.cshtml", logModels);
  318. }
  319. /// <summary>
  320. /// Export result for log grid
  321. /// </summary>
  322. [HttpPost]
  323. public ActionResult ExportPartialLogs(GridViewExportFormat exportformat)
  324. {
  325. if (exportformat == null || String.IsNullOrEmpty(exportformat.Format))
  326. return new EmptyResult();
  327. var logs = _logger.GetAllLogs();
  328. var logModels = logs
  329. .Select(u => LogDataModel.FromLog(u, false))
  330. .ToList();
  331. var viewContext = new ViewContext();
  332. var viewPage = new ViewPage();
  333. var htmlHelper = new HtmlHelper(viewContext, viewPage);
  334. var gridViewSettings = Extensions.GridViewSettingsHelper.LogGridViewSettings(htmlHelper);
  335. switch (exportformat.Format.ToLower())
  336. {
  337. case "xlsx":
  338. return GridViewExtension.ExportToXlsx(gridViewSettings, logModels);
  339. case "xls":
  340. return GridViewExtension.ExportToXls(gridViewSettings, logModels);
  341. case "pdf":
  342. return GridViewExtension.ExportToPdf(gridViewSettings, logModels);
  343. default:
  344. return new EmptyResult();
  345. }
  346. }
  347. /// <summary>
  348. /// Partial edit for editing of existing or for new log
  349. /// </summary>
  350. /// <param name="id">Id for existing log, otherweise -1.</param>
  351. public ActionResult ViewLog(int id = -1)
  352. {
  353. var log = _logger.GetLogById(id);
  354. var logModel = LogDataModel.FromLog(log, true);
  355. return PartialView("~/Views/Misc/_LogViewPartial.cshtml", logModel);
  356. }
  357. /// <summary>
  358. /// Simple JSON result for deleting a specific log
  359. /// </summary>
  360. /// <param name="id">Log id.</param>
  361. [HttpPost]
  362. public ActionResult DeleteLog(int id)
  363. {
  364. try
  365. {
  366. var log = _logger.GetLogById(id);
  367. if (log != null)
  368. _logger.DeleteLog(log);
  369. return new JsonResult
  370. {
  371. Data = "success"
  372. };
  373. }
  374. catch (Exception ex)
  375. {
  376. _logger.Error("Fehler bei Löschung eines Logs.", ex, _userHelper.FromCookies());
  377. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  378. }
  379. }
  380. #endregion
  381. }
  382. }