DataCallbackController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using DevExpress.Web.Mvc;
  2. using GreenTree.Nachtragsmanagement.Services.Appendix;
  3. using GreenTree.Nachtragsmanagement.Services.Deviation;
  4. using GreenTree.Nachtragsmanagement.Services.User;
  5. using GreenTree.Nachtragsmanagement.Web.Models.Appendix;
  6. using GreenTree.Nachtragsmanagement.Web.Models.Deviation;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using Newtonsoft.Json.Serialization;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Web;
  15. using System.Web.Mvc;
  16. namespace GreenTree.Nachtragsmanagement.Web.Controllers
  17. {
  18. public class DataCallbackController : Controller
  19. {
  20. private readonly IUserService _userService;
  21. private readonly IDeviationService _deviationService;
  22. private readonly IAppendixService _appendixService;
  23. private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
  24. {
  25. Error = (serializer, ex) => { ex.ErrorContext.Handled = true; }
  26. };
  27. public DataCallbackController(
  28. IUserService userService,
  29. IDeviationService deviationService,
  30. IAppendixService appendixService)
  31. {
  32. _userService = userService;
  33. _deviationService = deviationService;
  34. _appendixService = appendixService;
  35. }
  36. #region User
  37. /// <summary>
  38. /// Returns a DevExpress ComboBox for all roles
  39. /// </summary>
  40. /// <param name="settingsKey">Current comboBox settings key.</param>
  41. /// <param name="model">JSON-serialized model.</param>
  42. /// <param name="type">Type of model.</param>
  43. public ActionResult RolesComboBox(string settingsKey, string model = null, string type = null)
  44. {
  45. return RolesComboBoxExcluded(settingsKey, model, type, null);
  46. }
  47. /// <summary>
  48. /// Returns a DevExpress ComboBox for all roles
  49. /// </summary>
  50. /// <param name="settingsKey">Current comboBox settings key.</param>
  51. /// <param name="model">JSON-serialized model.</param>
  52. /// <param name="type">Type of model.</param>
  53. /// <param name="excludedIdsJson">Ids of the excluded roles.</param>
  54. public ActionResult RolesComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  55. {
  56. var allRoles = _userService.GetAllRoles();
  57. if (excludedIds != null)
  58. {
  59. foreach (var excludedId in excludedIds)
  60. {
  61. var item = allRoles
  62. .FirstOrDefault(s => s.Id == excludedId);
  63. allRoles.Remove(item);
  64. }
  65. }
  66. ViewData["AllRoles"] = allRoles;
  67. ViewData["RolesComboBoxSettings"] = settingsKey;
  68. if (model != null && type != null)
  69. {
  70. var modelType = Type.GetType(type);
  71. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  72. return PartialView("~/Views/Shared/DataEditorTemplates/_RolesComboBox.cshtml", modelObject);
  73. }
  74. return PartialView("~/Views/Shared/DataEditorTemplates/_RolesComboBox.cshtml", null);
  75. }
  76. #endregion
  77. #region Appendix
  78. /// <summary>
  79. /// Returns a DevExpress ComboBox for all appendices
  80. /// </summary>
  81. /// <param name="settingsKey">Current comboBox settings key.</param>
  82. /// <param name="model">JSON-serialized model.</param>
  83. /// <param name="type">Type of model.</param>
  84. public ActionResult AppendicesComboBox(string settingsKey, string model = null, string type = null)
  85. {
  86. var allAppendices = _appendixService.GetAllAppendices();
  87. ViewData["AllAppendices"] = allAppendices;
  88. ViewData["AppendicesComboBoxSettings"] = settingsKey;
  89. if (model != null && type != null)
  90. {
  91. var modelType = Type.GetType(type);
  92. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  93. return PartialView("~/Views/Shared/DataEditorTemplates/_AppendicesComboBox.cshtml", modelObject);
  94. }
  95. return PartialView("~/Views/Shared/DataEditorTemplates/_AppendicesComboBox.cshtml", null);
  96. }
  97. #endregion
  98. #region Deviations
  99. /// <summary>
  100. /// Returns a DevExpress ComboBox for all statuses
  101. /// </summary>
  102. /// <param name="settingsKey">Current comboBox settings key.</param>
  103. /// <param name="model">JSON-serialized model.</param>
  104. /// <param name="type">Type of model.</param>
  105. public ActionResult StatusesComboBox(string settingsKey, string model, string type)
  106. {
  107. return StatusesComboBoxExcluded(settingsKey, model, type, null);
  108. }
  109. /// <summary>
  110. /// Returns a DevExpress ComboBox for all statuses
  111. /// </summary>
  112. /// <param name="settingsKey">Current comboBox settings key.</param>
  113. /// <param name="model">JSON-serialized model.</param>
  114. /// <param name="type">Type of model.</param>
  115. /// <param name="excludedIdsJson">Ids of the excluded statuses.</param>
  116. public ActionResult StatusesComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  117. {
  118. var allStatuses = _deviationService.GetAllStatuses();
  119. if (excludedIds != null)
  120. {
  121. foreach (var excludedId in excludedIds)
  122. {
  123. var item = allStatuses
  124. .FirstOrDefault(s => s.Id == excludedId);
  125. allStatuses.Remove(item);
  126. }
  127. }
  128. ViewData["AllStatuses"] = allStatuses;
  129. ViewData["StatusesComboBoxSettings"] = settingsKey;
  130. if (model != null && type != null)
  131. {
  132. var modelType = Type.GetType(type);
  133. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  134. return PartialView("~/Views/Shared/DataEditorTemplates/_StatusesComboBox.cshtml", modelObject);
  135. }
  136. return PartialView("~/Views/Shared/DataEditorTemplates/_StatusesComboBox.cshtml", null);
  137. }
  138. /// <summary>
  139. /// Returns a DevExpress ComboBox for all disturbances
  140. /// </summary>
  141. /// <param name="settingsKey">Current comboBox settings key.</param>
  142. /// <param name="model">JSON-serialized model.</param>
  143. /// <param name="type">Type of model.</param>
  144. public ActionResult DisturbancesComboBox(string settingsKey, string model, string type)
  145. {
  146. return DisturbancesComboBoxExcluded(settingsKey, model, type, null);
  147. }
  148. /// <summary>
  149. /// Returns a DevExpress ComboBox for all disturbances
  150. /// </summary>
  151. /// <param name="settingsKey">Current comboBox settings key.</param>
  152. /// <param name="model">JSON-serialized model.</param>
  153. /// <param name="type">Type of model.</param>
  154. /// <param name="excludedIdsJson">Ids of the excluded disturbances.</param>
  155. public ActionResult DisturbancesComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  156. {
  157. var allDisturbances = _deviationService.GetAllDisturbances();
  158. if (excludedIds != null)
  159. {
  160. foreach (var excludedId in excludedIds)
  161. {
  162. var item = allDisturbances
  163. .FirstOrDefault(s => s.Id == excludedId);
  164. allDisturbances.Remove(item);
  165. }
  166. }
  167. ViewData["AllDisturbances"] = allDisturbances;
  168. ViewData["DisturbancesComboBoxSettings"] = settingsKey;
  169. if (model != null && type != null)
  170. {
  171. var modelType = Type.GetType(type);
  172. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  173. return PartialView("~/Views/Shared/DataEditorTemplates/_DisturbancesComboBox.cshtml", modelObject);
  174. }
  175. return PartialView("~/Views/Shared/DataEditorTemplates/_DisturbancesComboBox.cshtml", null);
  176. }
  177. /// <summary>
  178. /// Returns a DevExpress ComboBox for all kinds
  179. /// </summary>
  180. /// <param name="settingsKey">Current comboBox settings key.</param>
  181. /// <param name="model">JSON-serialized model.</param>
  182. /// <param name="type">Type of model.</param>
  183. public ActionResult KindsComboBox(string settingsKey, string model, string type)
  184. {
  185. var allKinds = _deviationService.GetAllKinds();
  186. ViewData["AllKinds"] = allKinds;
  187. ViewData["KindsComboBoxSettings"] = settingsKey;
  188. if (model != null && type != null)
  189. {
  190. var modelType = Type.GetType(type);
  191. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  192. return PartialView("~/Views/Shared/DataEditorTemplates/_KindsComboBox.cshtml", modelObject);
  193. }
  194. return PartialView("~/Views/Shared/DataEditorTemplates/_KindsComboBox.cshtml", null);
  195. }
  196. /// <summary>
  197. /// Returns a DevExpress ComboBox for all kinds
  198. /// </summary>
  199. /// <param name="settingsKey">Current comboBox settings key.</param>
  200. /// <param name="model">JSON-serialized model.</param>
  201. /// <param name="type">Type of model.</param>
  202. /// <param name="excludedIdsJson">Ids of the excluded kinds.</param>
  203. public ActionResult KindsComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  204. {
  205. var allKinds = _deviationService.GetAllKinds();
  206. if (excludedIds != null)
  207. {
  208. foreach (var excludedId in excludedIds)
  209. {
  210. var item = allKinds
  211. .FirstOrDefault(s => s.Id == excludedId);
  212. allKinds.Remove(item);
  213. }
  214. }
  215. ViewData["AllKinds"] = allKinds;
  216. ViewData["KindsComboBoxSettings"] = settingsKey;
  217. if (model != null && type != null)
  218. {
  219. var modelType = Type.GetType(type);
  220. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  221. return PartialView("~/Views/Shared/DataEditorTemplates/_KindsComboBox.cshtml", modelObject);
  222. }
  223. return PartialView("~/Views/Shared/DataEditorTemplates/_KindsComboBox.cshtml", null);
  224. }
  225. #endregion
  226. }
  227. }