MiscController.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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, string exportType)
  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. if (exportType != "print")
  330. {
  331. switch (exportformat.ToLower())
  332. {
  333. case "xlsx":
  334. return GridViewExtension.ExportToXlsx(settings, logModels);
  335. case "xls":
  336. return GridViewExtension.ExportToXls(settings, logModels);
  337. case "pdf":
  338. generator.WritePdfToResponse(Response, "Logliste.pdf", DispositionTypeNames.Attachment.ToString());
  339. break;
  340. }
  341. }
  342. }
  343. return new EmptyResult();
  344. }
  345. else
  346. return new EmptyResult();
  347. }
  348. /// <summary>
  349. /// Customize created columns
  350. /// </summary>
  351. private void logGenerator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
  352. {
  353. foreach (var column in e.ColumnsInfo)
  354. {
  355. if (column.ColumnCaption == "#") { column.IsVisible = false; }
  356. if (column.FieldName == "LogLevelDescription") { column.ColumnWidth = 60; }
  357. if (column.FieldName == "ShortMessage") { column.ColumnWidth = 300; }
  358. if (column.FieldName == "FullMessage") { column.IsVisible = false; column.IsDetail = true; }
  359. if (column.FieldName == "IpAddress") { column.ColumnWidth = 70; }
  360. if (column.FieldName == "UserDescription") { column.ColumnWidth = 70; }
  361. if (column.FieldName == "EntityType") { column.ColumnWidth = 60; }
  362. if (column.FieldName == "EntityId") { column.ColumnWidth = 50; }
  363. if (column.FieldName == "CreatedOnUtc") { column.ColumnWidth = 70; }
  364. }
  365. }
  366. /// <summary>
  367. /// Partial edit for editing of existing or for new log
  368. /// </summary>
  369. /// <param name="id">Id for existing log, otherweise -1.</param>
  370. public ActionResult ViewLog(int id = -1)
  371. {
  372. var log = _logger.GetLogById(id);
  373. var logModel = LogDataModel.FromLog(log, true);
  374. return PartialView("~/Views/Misc/_LogViewPartial.cshtml", logModel);
  375. }
  376. /// <summary>
  377. /// Simple JSON result for deleting a specific log
  378. /// </summary>
  379. /// <param name="id">Log id.</param>
  380. [HttpPost]
  381. public ActionResult DeleteLog(int id)
  382. {
  383. try
  384. {
  385. var log = _logger.GetLogById(id);
  386. if (log != null)
  387. _logger.DeleteLog(log);
  388. return new JsonResult
  389. {
  390. Data = "success"
  391. };
  392. }
  393. catch (Exception ex)
  394. {
  395. _logger.Error("Fehler bei Löschung eines Logs.", ex, _userHelper.FromCookies());
  396. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  397. }
  398. }
  399. #endregion
  400. #region ConfigItems
  401. /// <summary>
  402. /// Basic configItem view function
  403. /// </summary>
  404. [FunctionAuthorize(true, "Misc-ConfigItems")]
  405. public ActionResult ViewConfigItems()
  406. {
  407. var configItems = _configurationService.GetAllConfigItems();
  408. var configItemModels = configItems
  409. .Select(u => ConfigItemDataModel.FromConfigItem(u, false))
  410. .ToList();
  411. return View("~/Views/Config/View.cshtml", configItemModels);
  412. }
  413. /// <summary>
  414. /// Get JSON data of specific configItem
  415. /// </summary>
  416. /// <param name="id">ConfigItem id.</param>
  417. public ActionResult GetConfigItem(int id = -1)
  418. {
  419. var configItem = _configurationService.GetConfigItemById(id);
  420. if (configItem == null)
  421. return new JsonResult
  422. {
  423. Data = "notFound",
  424. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  425. };
  426. var configItemModel = ConfigItemDataModel.FromConfigItem(configItem, false);
  427. return new JsonResult
  428. {
  429. Data = JsonConvert.SerializeObject(configItemModel),
  430. JsonRequestBehavior = JsonRequestBehavior.AllowGet
  431. };
  432. }
  433. /// <summary>
  434. /// Callback result for configItem grid
  435. /// </summary>
  436. /// <param name="scrollHeight">The height of the grid scrollable component.</param>
  437. public ActionResult PartialConfigItems(int scrollHeight = -1)
  438. {
  439. var configItems = _configurationService.GetAllConfigItems();
  440. var configItemModels = configItems
  441. .Select(u => ConfigItemDataModel.FromConfigItem(u, false))
  442. .ToList();
  443. ViewData["ScrollHeight"] = scrollHeight;
  444. return PartialView("~/Views/Config/_ConfigItemGridPartial.cshtml", configItemModels);
  445. }
  446. /// <summary>
  447. /// Partial edit for config item value
  448. /// </summary>
  449. /// <param name="typeFullName">Value type.</param>
  450. public ActionResult GetValueTypePartialEdit(string typeFullName)
  451. {
  452. var model = new ConfigItemDataModel
  453. {
  454. TypeFullName = typeFullName
  455. };
  456. return PartialView("~/Views/Config/_ConfigItemValueEditPartial.cshtml", model);
  457. }
  458. /// <summary>
  459. /// Partial edit for editing of existing or for new configItem
  460. /// </summary>
  461. /// <param name="id">Id for existing configItem, otherweise -1.</param>
  462. public ActionResult EditConfigItem(int id = -1)
  463. {
  464. var configItem = _configurationService.GetConfigItemById(id);
  465. var configItemModel = ConfigItemDataModel.FromConfigItem(configItem, true);
  466. return PartialView("~/Views/Config/_ConfigItemEditPartial.cshtml", configItemModel);
  467. }
  468. /// <summary>
  469. /// Partial edit result if ModelState is valid, otherwise simple JSON result for success
  470. /// </summary>
  471. /// <param name="configItemModel">ConfigItem model to be saved.</param>
  472. [HttpPost, ValidateInput(false)]
  473. public ActionResult EditConfigItem(ConfigItemDataModel configItemModel)
  474. {
  475. try
  476. {
  477. if (!ModelState.IsValid)
  478. return PartialView("~/Views/Config/_ConfigItemEditPartial.cshtml", configItemModel);
  479. if (configItemModel.Id == -1)
  480. {
  481. var configItem = configItemModel.ToConfigItem();
  482. _configurationService.InsertConfigItem(configItem);
  483. _logger.Entity(configItem, Core.Domain.Logging.LogEntityActivity.Insert, _userHelper.FromCookies());
  484. }
  485. else
  486. {
  487. var configItem = _configurationService.GetConfigItemById(configItemModel.Id);
  488. configItem.Name = configItemModel.Name;
  489. configItem.TypeFullName = configItemModel.TypeFullName;
  490. configItem.Value = configItemModel.Value;
  491. configItem.Description = configItemModel.Description;
  492. _configurationService.UpdateConfigItem(configItem);
  493. _logger.Entity(configItem, Core.Domain.Logging.LogEntityActivity.Update, _userHelper.FromCookies());
  494. }
  495. _notificationScheduler.Start();
  496. return new JsonResult
  497. {
  498. Data = "success"
  499. };
  500. }
  501. catch (Exception ex)
  502. {
  503. _logger.Error("Fehler bei Speicherung einer Einstellung.", ex, _userHelper.FromCookies());
  504. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  505. }
  506. }
  507. /// <summary>
  508. /// Simple JSON result for deleting a specific configItem
  509. /// </summary>
  510. /// <param name="id">ConfigItem id.</param>
  511. [HttpPost]
  512. public ActionResult DeleteConfigItem(int id)
  513. {
  514. try
  515. {
  516. var configItem = _configurationService.GetConfigItemById(id);
  517. if (configItem != null)
  518. _configurationService.DeleteConfigItem(configItem);
  519. _logger.Entity(configItem, Core.Domain.Logging.LogEntityActivity.Delete, _userHelper.FromCookies());
  520. return new JsonResult
  521. {
  522. Data = "success"
  523. };
  524. }
  525. catch (Exception ex)
  526. {
  527. _logger.Error("Fehler bei Löschung einer Einstellung.", ex, _userHelper.FromCookies());
  528. return PartialView("~/Views/Shared/_PopupError.cshtml", ex);
  529. }
  530. }
  531. #endregion
  532. }
  533. }