DataCallbackController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 Deviations
  78. /// <summary>
  79. /// Returns a DevExpress ComboBox for all statuses
  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 StatusesComboBox(string settingsKey, string model, string type)
  85. {
  86. return StatusesComboBoxExcluded(settingsKey, model, type, null);
  87. }
  88. /// <summary>
  89. /// Returns a DevExpress ComboBox for all statuses
  90. /// </summary>
  91. /// <param name="settingsKey">Current comboBox settings key.</param>
  92. /// <param name="model">JSON-serialized model.</param>
  93. /// <param name="type">Type of model.</param>
  94. /// <param name="excludedIdsJson">Ids of the excluded statuses.</param>
  95. public ActionResult StatusesComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  96. {
  97. var allStatuses = _deviationService.GetAllStatuses();
  98. if (excludedIds != null)
  99. {
  100. foreach (var excludedId in excludedIds)
  101. {
  102. var item = allStatuses
  103. .FirstOrDefault(s => s.Id == excludedId);
  104. allStatuses.Remove(item);
  105. }
  106. }
  107. ViewData["AllStatuses"] = allStatuses;
  108. ViewData["StatusesComboBoxSettings"] = settingsKey;
  109. if (model != null && type != null)
  110. {
  111. var modelType = Type.GetType(type);
  112. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  113. return PartialView("~/Views/Shared/DataEditorTemplates/_StatusesComboBox.cshtml", modelObject);
  114. }
  115. return PartialView("~/Views/Shared/DataEditorTemplates/_StatusesComboBox.cshtml", null);
  116. }
  117. /// <summary>
  118. /// Returns a DevExpress ComboBox for all disturbances
  119. /// </summary>
  120. /// <param name="settingsKey">Current comboBox settings key.</param>
  121. /// <param name="model">JSON-serialized model.</param>
  122. /// <param name="type">Type of model.</param>
  123. public ActionResult DisturbancesComboBox(string settingsKey, string model, string type)
  124. {
  125. return DisturbancesComboBoxExcluded(settingsKey, model, type, null);
  126. }
  127. /// <summary>
  128. /// Returns a DevExpress ComboBox for all disturbances
  129. /// </summary>
  130. /// <param name="settingsKey">Current comboBox settings key.</param>
  131. /// <param name="model">JSON-serialized model.</param>
  132. /// <param name="type">Type of model.</param>
  133. /// <param name="excludedIdsJson">Ids of the excluded disturbances.</param>
  134. public ActionResult DisturbancesComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  135. {
  136. var allDisturbances = _deviationService.GetAllDisturbances();
  137. if (excludedIds != null)
  138. {
  139. foreach (var excludedId in excludedIds)
  140. {
  141. var item = allDisturbances
  142. .FirstOrDefault(s => s.Id == excludedId);
  143. allDisturbances.Remove(item);
  144. }
  145. }
  146. ViewData["AllDisturbances"] = allDisturbances;
  147. ViewData["DisturbancesComboBoxSettings"] = settingsKey;
  148. if (model != null && type != null)
  149. {
  150. var modelType = Type.GetType(type);
  151. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  152. return PartialView("~/Views/Shared/DataEditorTemplates/_DisturbancesComboBox.cshtml", modelObject);
  153. }
  154. return PartialView("~/Views/Shared/DataEditorTemplates/_DisturbancesComboBox.cshtml", null);
  155. }
  156. /// <summary>
  157. /// Returns a DevExpress ComboBox for all kinds
  158. /// </summary>
  159. /// <param name="settingsKey">Current comboBox settings key.</param>
  160. /// <param name="model">JSON-serialized model.</param>
  161. /// <param name="type">Type of model.</param>
  162. public ActionResult KindsComboBox(string settingsKey, string model, string type)
  163. {
  164. var allKinds = _deviationService.GetAllKinds();
  165. ViewData["AllKinds"] = allKinds;
  166. ViewData["KindsComboBoxSettings"] = settingsKey;
  167. if (model != null && type != null)
  168. {
  169. var modelType = Type.GetType(type);
  170. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  171. return PartialView("~/Views/Shared/DataEditorTemplates/_KindsComboBox.cshtml", modelObject);
  172. }
  173. return PartialView("~/Views/Shared/DataEditorTemplates/_KindsComboBox.cshtml", null);
  174. }
  175. /// <summary>
  176. /// Returns a DevExpress ComboBox for all kinds
  177. /// </summary>
  178. /// <param name="settingsKey">Current comboBox settings key.</param>
  179. /// <param name="model">JSON-serialized model.</param>
  180. /// <param name="type">Type of model.</param>
  181. /// <param name="excludedIdsJson">Ids of the excluded kinds.</param>
  182. public ActionResult KindsComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  183. {
  184. var allKinds = _deviationService.GetAllKinds();
  185. if (excludedIds != null)
  186. {
  187. foreach (var excludedId in excludedIds)
  188. {
  189. var item = allKinds
  190. .FirstOrDefault(s => s.Id == excludedId);
  191. allKinds.Remove(item);
  192. }
  193. }
  194. ViewData["AllKinds"] = allKinds;
  195. ViewData["KindsComboBoxSettings"] = settingsKey;
  196. if (model != null && type != null)
  197. {
  198. var modelType = Type.GetType(type);
  199. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  200. return PartialView("~/Views/Shared/DataEditorTemplates/_KindsComboBox.cshtml", modelObject);
  201. }
  202. return PartialView("~/Views/Shared/DataEditorTemplates/_KindsComboBox.cshtml", null);
  203. }
  204. #endregion
  205. #region Appendices
  206. /// <summary>
  207. /// Returns a DevExpress ComboBox for all states
  208. /// </summary>
  209. /// <param name="settingsKey">Current comboBox settings key.</param>
  210. /// <param name="model">JSON-serialized model.</param>
  211. /// <param name="type">Type of model.</param>
  212. public ActionResult StatesComboBox(string settingsKey, string model, string type)
  213. {
  214. return StatesComboBoxExcluded(settingsKey, model, type, null);
  215. }
  216. /// <summary>
  217. /// Returns a DevExpress ComboBox for all states
  218. /// </summary>
  219. /// <param name="settingsKey">Current comboBox settings key.</param>
  220. /// <param name="model">JSON-serialized model.</param>
  221. /// <param name="type">Type of model.</param>
  222. /// <param name="excludedIdsJson">Ids of the excluded states.</param>
  223. public ActionResult StatesComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  224. {
  225. var allStates = _appendixService.GetAllStates();
  226. if (excludedIds != null)
  227. {
  228. foreach (var excludedId in excludedIds)
  229. {
  230. var item = allStates
  231. .FirstOrDefault(s => s.Id == excludedId);
  232. allStates.Remove(item);
  233. }
  234. }
  235. ViewData["AllStates"] = allStates;
  236. ViewData["StatesComboBoxSettings"] = settingsKey;
  237. if (model != null && type != null)
  238. {
  239. var modelType = Type.GetType(type);
  240. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  241. return PartialView("~/Views/Shared/DataEditorTemplates/_StatesComboBox.cshtml", modelObject);
  242. }
  243. return PartialView("~/Views/Shared/DataEditorTemplates/_StatesComboBox.cshtml", null);
  244. }
  245. #endregion
  246. }
  247. }