MiscController.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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.Configuration;
  12. using GreenTree.Nachtragsmanagement.Services.Deviation;
  13. using GreenTree.Nachtragsmanagement.Services.Logging;
  14. using GreenTree.Nachtragsmanagement.Services.Misc;
  15. using GreenTree.Nachtragsmanagement.Services.Scheduling;
  16. using GreenTree.Nachtragsmanagement.Services.User;
  17. using GreenTree.Nachtragsmanagement.Web.Framework.Authorization;
  18. using GreenTree.Nachtragsmanagement.Web.Models.Config;
  19. using GreenTree.Nachtragsmanagement.Web.Models.Global;
  20. using GreenTree.Nachtragsmanagement.Web.Models.Misc;
  21. using Newtonsoft.Json;
  22. using System;
  23. using System.Collections.Generic;
  24. using System.IO;
  25. using System.Linq;
  26. using System.Web;
  27. using System.Web.Mvc;
  28. using System.Web.UI;
  29. using System.Web.UI.WebControls;
  30. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  31. {
  32. public class MiscController : Controller
  33. {
  34. private readonly IMiscService _miscService;
  35. private readonly IUserService _userService;
  36. private readonly INotificationService _notificationService;
  37. private readonly INotificationScheduler _notificationScheduler;
  38. private readonly IConfigurationService _configurationService;
  39. private readonly IUserHelper _userHelper;
  40. private readonly ILogger _logger;
  41. public MiscController(
  42. IMiscService miscService,
  43. IUserService userService,
  44. INotificationService notificationService,
  45. INotificationScheduler notificationScheduler,
  46. IConfigurationService configurationService,
  47. IUserHelper userHelper,
  48. ILogger logger)
  49. {
  50. _miscService = miscService;
  51. _userService = userService;
  52. _notificationService = notificationService;
  53. _notificationScheduler = notificationScheduler;
  54. _configurationService = configurationService;
  55. _userHelper = userHelper;
  56. _logger = logger;
  57. ViewData["AllUsers"] = _userService.GetAllUsers();
  58. ViewData["AllUsersWithRole"] =
  59. _userService.GetAllUsers()
  60. .Select(u => new
  61. {
  62. Id = u.Id,
  63. Description = String.Format("{0} - {1}", u.Lastname,
  64. String.Join(", ", u.Roles
  65. .Select(r => r.Description)))
  66. })
  67. .ToList();
  68. ViewData["AllNotificationPlugins"] =
  69. _notificationService.GetNotificationPlugins();
  70. }
  71. #region MailNotifications
  72. /// <summary>
  73. /// Basic mailNotification view function
  74. /// </summary>
  75. [FunctionAuthorize(true, "Misc-MailNotifications")]
  76. public ActionResult ViewMailNotifications()
  77. {
  78. var mailNotifications = _miscService.GetAllMailNotifications();
  79. var mailNotificationModels = mailNotifications
  80. .Select(u => MailNotificationDataModel.FromMailNotification(
  81. u, false, _notificationService, _notificationScheduler))
  82. .ToList();
  83. return View("~/Views/Misc/MailNotifications.cshtml", mailNotificationModels);
  84. }
  85. /// <summary>
  86. /// Get JSON data of specific mailNotification
  87. /// </summary>
  88. /// <param name="id">MailNotification id.</param>
  89. public ActionResult GetMailNotification(int id = -1)
  90. {
  91. var mailNotification = _miscService.GetMailNotificationById(id);
  92. if (mailNotification == null)
  93. return new JsonResult
  94. {
  95. Data = "notFound",
  96. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  97. };
  98. var mailNotificationModel = MailNotificationDataModel.FromMailNotification(
  99. mailNotification, false, _notificationService, _notificationScheduler);
  100. return new JsonResult
  101. {
  102. Data = JsonConvert.SerializeObject(mailNotificationModel),
  103. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  104. };
  105. }
  106. /// <summary>
  107. /// Callback result for mailNotification grid
  108. /// </summary>
  109. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  110. public ActionResult PartialMailNotifications(int scrollHeight = -1)
  111. {
  112. var mailNotifications = _miscService.GetAllMailNotifications();
  113. var mailNotificationModels = mailNotifications
  114. .Select(u => MailNotificationDataModel.FromMailNotification(
  115. u, false, _notificationService, _notificationScheduler))
  116. .ToList();
  117. ViewData["ScrollHeight"] = scrollHeight;
  118. return PartialView("~/Views/Misc/_MailNotificationGridPartial.cshtml", mailNotificationModels);
  119. }
  120. /// <summary>
  121. /// Callback result for mailNotification job combobox
  122. /// </summary>
  123. /// <param name="pluginSystemName">The system name of the corresponding notification plugin.</param>
  124. public ActionResult PartialNotificationPluginJobs(string pluginSystemName)
  125. {
  126. var notificationPlugin = _notificationService.GetNotificationPlugin(pluginSystemName);
  127. if (notificationPlugin == null)
  128. return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", null);
  129. var mailNotificationModel = new MailNotificationDataModel
  130. {
  131. NotificationPlugin = notificationPlugin
  132. };
  133. return PartialView("~/Views/Misc/_MailNotificationPluginJobsPartial.cshtml", mailNotificationModel);
  134. }
  135. /// <summary>
  136. /// Export result for mailNotification grid
  137. /// </summary>
  138. [HttpPost]
  139. public ActionResult ExportPartialMailNotifications(GridViewExportFormat exportformat)
  140. {
  141. if (exportformat == null || String.IsNullOrEmpty(exportformat.Format))
  142. return new EmptyResult();
  143. var mailNotifications = _miscService.GetAllMailNotifications();
  144. var mailNotificationModels = mailNotifications
  145. .Select(u => MailNotificationDataModel.FromMailNotification(
  146. u, false, _notificationService, _notificationScheduler))
  147. .ToList();
  148. var viewContext = new ViewContext();
  149. var viewPage = new ViewPage();
  150. var htmlHelper = new HtmlHelper(viewContext, viewPage);
  151. var gridViewSettings = Extensions.GridViewSettingsHelper.MailNotificationGridViewSettings(htmlHelper);
  152. switch (exportformat.Format.ToLower())
  153. {
  154. case "xlsx":
  155. return GridViewExtension.ExportToXlsx(gridViewSettings, mailNotificationModels);
  156. case "xls":
  157. return GridViewExtension.ExportToXls(gridViewSettings, mailNotificationModels);
  158. case "pdf":
  159. return GridViewExtension.ExportToPdf(gridViewSettings, mailNotificationModels);
  160. default:
  161. return new EmptyResult();
  162. }
  163. }
  164. /// <summary>
  165. /// Partial edit for editing of existing or for new mailNotification
  166. /// </summary>
  167. /// <param name="id">Id for existing mailNotification, otherweise -1.</param>
  168. public ActionResult EditMailNotification(int id = -1)
  169. {
  170. var mailNotification = _miscService.GetMailNotificationById(id);
  171. var mailNotificationModel = MailNotificationDataModel.FromMailNotification(
  172. mailNotification, true, _notificationService, _notificationScheduler);
  173. return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
  174. }
  175. /// <summary>
  176. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  177. /// </summary>
  178. /// <param name="mailNotificationModel">MailNotification model to be saved.</param>
  179. [HttpPost, ValidateInput(false)]
  180. public ActionResult EditMailNotification(MailNotificationDataModel mailNotificationModel)
  181. {
  182. try
  183. {
  184. if (!ModelState.IsValid)
  185. {
  186. foreach (var role in mailNotificationModel.UserValues)
  187. mailNotificationModel.UserDescriptions.Add(
  188. ((IList<User>)ViewData["AllUsers"])
  189. .First(r => r.Id == role).Lastname);
  190. var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotificationModel.NotificationPluginSystemName);
  191. if (notificationPlugin != null)
  192. mailNotificationModel.NotificationPlugin = notificationPlugin;
  193. return PartialView("~/Views/Misc/_MailNotificationEditPartial.cshtml", mailNotificationModel);
  194. }
  195. if (mailNotificationModel.CronExpression.Split(' ').Length == 5)
  196. mailNotificationModel.CronExpression = mailNotificationModel.CronExpression + " *";
  197. var selectedUsers = _userService.GetUsersByIds(mailNotificationModel.UserValues.ToArray());
  198. if (mailNotificationModel.Id == -1)
  199. {
  200. var mailNotification = mailNotificationModel.ToMailNotification();
  201. mailNotification.SetUsers(selectedUsers);
  202. _miscService.InsertMailNotification(mailNotification);
  203. _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  204. }
  205. else
  206. {
  207. var mailNotification = _miscService.GetMailNotificationById(mailNotificationModel.Id);
  208. mailNotification.CronExpression = mailNotificationModel.CronExpression;
  209. mailNotification.NotificationPluginSystemName = mailNotificationModel.NotificationPluginSystemName;
  210. mailNotification.NotificationJobSystemName = mailNotificationModel.NotificationJobSystemName;
  211. mailNotification.SetUsers(selectedUsers);
  212. _miscService.UpdateMailNotification(mailNotification);
  213. _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  214. }
  215. _notificationScheduler.Start();
  216. return new JsonResult
  217. {
  218. Data = "success"
  219. };
  220. }
  221. catch (Exception ex)
  222. {
  223. _logger.Error("Fehler bei Speicherung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
  224. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  225. }
  226. }
  227. /// <summary>
  228. /// Simple JSON result for deleting a specific mailNotification
  229. /// </summary>
  230. /// <param name="id">MailNotification id.</param>
  231. [HttpPost]
  232. public ActionResult DeleteMailNotification(int id)
  233. {
  234. try
  235. {
  236. var mailNotification = _miscService.GetMailNotificationById(id);
  237. if (mailNotification != null)
  238. _miscService.DeleteMailNotification(mailNotification);
  239. _logger.Entity(mailNotification, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  240. return new JsonResult
  241. {
  242. Data = "success"
  243. };
  244. }
  245. catch (Exception ex)
  246. {
  247. _logger.Error("Fehler bei Löschung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
  248. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  249. }
  250. }
  251. /// <summary>
  252. /// Processes the specific mailNotification
  253. /// </summary>
  254. /// <param name="id">MailNotification id.</param>
  255. [HttpPost]
  256. public ActionResult ProcessMailNotification(int id)
  257. {
  258. try
  259. {
  260. var mailNotification = _miscService.GetMailNotificationById(id);
  261. if (mailNotification != null)
  262. {
  263. var notificationPlugin = _notificationService.GetNotificationPlugin(mailNotification.NotificationPluginSystemName);
  264. notificationPlugin.ProcessNotifications(new[] { mailNotification });
  265. }
  266. return new JsonResult
  267. {
  268. Data = "success"
  269. };
  270. }
  271. catch (Exception ex)
  272. {
  273. _logger.Error("Fehler bei Ausführung einer Mail-Benachrichtigung.", ex, _userHelper.FromCookies());
  274. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  275. }
  276. }
  277. #endregion
  278. #region Logs
  279. /// <summary>
  280. /// Basic log view function
  281. /// </summary>
  282. [FunctionAuthorize(true, "Misc-Logs")]
  283. public ActionResult ViewLogs()
  284. {
  285. var logs = _logger.GetAllLogs();
  286. var logModels = logs
  287. .Select(u => LogDataModel.FromLog(u, false))
  288. .ToList();
  289. return View("~/Views/Misc/Logs.cshtml", logModels);
  290. }
  291. /// <summary>
  292. /// Get JSON data of specific log
  293. /// </summary>
  294. /// <param name="id">Log id.</param>
  295. public ActionResult GetLog(int id = -1)
  296. {
  297. var log = _logger.GetLogById(id);
  298. if (log == null)
  299. return new JsonResult
  300. {
  301. Data = "notFound",
  302. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  303. };
  304. var logModel = LogDataModel.FromLog(log, false);
  305. return new JsonResult
  306. {
  307. Data = JsonConvert.SerializeObject(logModel),
  308. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  309. };
  310. }
  311. /// <summary>
  312. /// Callback result for log grid
  313. /// </summary>
  314. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  315. public ActionResult PartialLogs(int scrollHeight = -1)
  316. {
  317. var logs = _logger.GetAllLogs();
  318. var logModels = logs
  319. .Select(u => LogDataModel.FromLog(u, false))
  320. .ToList();
  321. ViewData["ScrollHeight"] = scrollHeight;
  322. return PartialView("~/Views/Misc/_LogGridPartial.cshtml", logModels);
  323. }
  324. /// <summary>
  325. /// Export result for log grid
  326. /// </summary>
  327. [HttpPost]
  328. public ActionResult ExportPartialLogs(GridViewExportFormat exportformat)
  329. {
  330. if (exportformat == null || String.IsNullOrEmpty(exportformat.Format))
  331. return new EmptyResult();
  332. var logs = _logger.GetAllLogs();
  333. var logModels = logs
  334. .Select(u => LogDataModel.FromLog(u, false))
  335. .ToList();
  336. var viewContext = new ViewContext();
  337. var viewPage = new ViewPage();
  338. var htmlHelper = new HtmlHelper(viewContext, viewPage);
  339. var gridViewSettings = Extensions.GridViewSettingsHelper.LogGridViewSettings(htmlHelper);
  340. switch (exportformat.Format.ToLower())
  341. {
  342. case "xlsx":
  343. return GridViewExtension.ExportToXlsx(gridViewSettings, logModels);
  344. case "xls":
  345. return GridViewExtension.ExportToXls(gridViewSettings, logModels);
  346. case "pdf":
  347. return GridViewExtension.ExportToPdf(gridViewSettings, logModels);
  348. default:
  349. return new EmptyResult();
  350. }
  351. }
  352. /// <summary>
  353. /// Partial edit for editing of existing or for new log
  354. /// </summary>
  355. /// <param name="id">Id for existing log, otherweise -1.</param>
  356. public ActionResult ViewLog(int id = -1)
  357. {
  358. var log = _logger.GetLogById(id);
  359. var logModel = LogDataModel.FromLog(log, true);
  360. return PartialView("~/Views/Misc/_LogViewPartial.cshtml", logModel);
  361. }
  362. /// <summary>
  363. /// Simple JSON result for deleting a specific log
  364. /// </summary>
  365. /// <param name="id">Log id.</param>
  366. [HttpPost]
  367. public ActionResult DeleteLog(int id)
  368. {
  369. try
  370. {
  371. var log = _logger.GetLogById(id);
  372. if (log != null)
  373. _logger.DeleteLog(log);
  374. return new JsonResult
  375. {
  376. Data = "success"
  377. };
  378. }
  379. catch (Exception ex)
  380. {
  381. _logger.Error("Fehler bei Löschung eines Logs.", ex, _userHelper.FromCookies());
  382. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  383. }
  384. }
  385. #endregion
  386. #region ConfigItems
  387. /// <summary>
  388. /// Basic configItem view function
  389. /// </summary>
  390. [FunctionAuthorize(true, "Misc-ConfigItems")]
  391. public ActionResult ViewConfigItems()
  392. {
  393. var configItems = _configurationService.GetAllConfigItems();
  394. var configItemModels = configItems
  395. .Select(u => ConfigItemDataModel.FromConfigItem(u, false))
  396. .ToList();
  397. return View("~/Views/Config/View.cshtml", configItemModels);
  398. }
  399. /// <summary>
  400. /// Get JSON data of specific configItem
  401. /// </summary>
  402. /// <param name="id">ConfigItem id.</param>
  403. public ActionResult GetConfigItem(int id = -1)
  404. {
  405. var configItem = _configurationService.GetConfigItemById(id);
  406. if (configItem == null)
  407. return new JsonResult
  408. {
  409. Data = "notFound",
  410. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  411. };
  412. var configItemModel = ConfigItemDataModel.FromConfigItem(configItem, false);
  413. return new JsonResult
  414. {
  415. Data = JsonConvert.SerializeObject(configItemModel),
  416. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  417. };
  418. }
  419. /// <summary>
  420. /// Callback result for configItem grid
  421. /// </summary>
  422. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  423. public ActionResult PartialConfigItems(int scrollHeight = -1)
  424. {
  425. var configItems = _configurationService.GetAllConfigItems();
  426. var configItemModels = configItems
  427. .Select(u => ConfigItemDataModel.FromConfigItem(u, false))
  428. .ToList();
  429. ViewData["ScrollHeight"] = scrollHeight;
  430. return PartialView("~/Views/Config/_ConfigItemGridPartial.cshtml", configItemModels);
  431. }
  432. /// <summary>
  433. /// Partial edit for config item value
  434. /// </summary>
  435. /// <param name="typeFullName">Value type.</param>
  436. public ActionResult GetValueTypePartialEdit(string typeFullName)
  437. {
  438. var model = new ConfigItemDataModel
  439. {
  440. TypeFullName = typeFullName
  441. };
  442. return PartialView("~/Views/Config/_ConfigItemValueEditPartial.cshtml", model);
  443. }
  444. /// <summary>
  445. /// Partial edit for editing of existing or for new configItem
  446. /// </summary>
  447. /// <param name="id">Id for existing configItem, otherweise -1.</param>
  448. public ActionResult EditConfigItem(int id = -1)
  449. {
  450. var configItem = _configurationService.GetConfigItemById(id);
  451. var configItemModel = ConfigItemDataModel.FromConfigItem(configItem, true);
  452. return PartialView("~/Views/Config/_ConfigItemEditPartial.cshtml", configItemModel);
  453. }
  454. /// <summary>
  455. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  456. /// </summary>
  457. /// <param name="configItemModel">ConfigItem model to be saved.</param>
  458. [HttpPost, ValidateInput(false)]
  459. public ActionResult EditConfigItem(ConfigItemDataModel configItemModel)
  460. {
  461. try
  462. {
  463. if (!ModelState.IsValid)
  464. return PartialView("~/Views/Config/_ConfigItemEditPartial.cshtml", configItemModel);
  465. if (configItemModel.Id == -1)
  466. {
  467. var configItem = configItemModel.ToConfigItem();
  468. _configurationService.InsertConfigItem(configItem);
  469. _logger.Entity(configItem, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  470. }
  471. else
  472. {
  473. var configItem = _configurationService.GetConfigItemById(configItemModel.Id);
  474. configItem.Name = configItemModel.Name;
  475. configItem.TypeFullName = configItemModel.TypeFullName;
  476. configItem.Value = configItemModel.Value;
  477. configItem.Description = configItemModel.Description;
  478. _configurationService.UpdateConfigItem(configItem);
  479. _logger.Entity(configItem, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  480. }
  481. _notificationScheduler.Start();
  482. return new JsonResult
  483. {
  484. Data = "success"
  485. };
  486. }
  487. catch (Exception ex)
  488. {
  489. _logger.Error("Fehler bei Speicherung einer Einstellung.", ex, _userHelper.FromCookies());
  490. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  491. }
  492. }
  493. /// <summary>
  494. /// Simple JSON result for deleting a specific configItem
  495. /// </summary>
  496. /// <param name="id">ConfigItem id.</param>
  497. [HttpPost]
  498. public ActionResult DeleteConfigItem(int id)
  499. {
  500. try
  501. {
  502. var configItem = _configurationService.GetConfigItemById(id);
  503. if (configItem != null)
  504. _configurationService.DeleteConfigItem(configItem);
  505. _logger.Entity(configItem, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  506. return new JsonResult
  507. {
  508. Data = "success"
  509. };
  510. }
  511. catch (Exception ex)
  512. {
  513. _logger.Error("Fehler bei Löschung einer Einstellung.", ex, _userHelper.FromCookies());
  514. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  515. }
  516. }
  517. #endregion
  518. }
  519. }