MiscController.cs 24 KB

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