DataCallbackController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. var defaultStatusId =
  108. allStatuses
  109. .Any(s => s.IsDefault)
  110. ? allStatuses
  111. .First(s => s.IsDefault).Id
  112. : -1;
  113. ViewData["DefaultStatus"] = defaultStatusId;
  114. ViewData["AllStatuses"] = allStatuses;
  115. ViewData["StatusesComboBoxSettings"] = settingsKey;
  116. if (model != null && type != null)
  117. {
  118. var modelType = Type.GetType(type);
  119. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  120. return PartialView("~/Views/Shared/DataEditorTemplates/_StatusesComboBox.cshtml", modelObject);
  121. }
  122. return PartialView("~/Views/Shared/DataEditorTemplates/_StatusesComboBox.cshtml", null);
  123. }
  124. /// <summary>
  125. /// Returns a DevExpress ComboBox for all disturbances
  126. /// </summary>
  127. /// <param name="settingsKey">Current comboBox settings key.</param>
  128. /// <param name="model">JSON-serialized model.</param>
  129. /// <param name="type">Type of model.</param>
  130. public ActionResult DisturbancesComboBox(string settingsKey, string model, string type)
  131. {
  132. return DisturbancesComboBoxExcluded(settingsKey, model, type, null);
  133. }
  134. /// <summary>
  135. /// Returns a DevExpress ComboBox for all disturbances
  136. /// </summary>
  137. /// <param name="settingsKey">Current comboBox settings key.</param>
  138. /// <param name="model">JSON-serialized model.</param>
  139. /// <param name="type">Type of model.</param>
  140. /// <param name="excludedIdsJson">Ids of the excluded disturbances.</param>
  141. public ActionResult DisturbancesComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  142. {
  143. var allDisturbances = _deviationService.GetAllDisturbances();
  144. if (excludedIds != null)
  145. {
  146. foreach (var excludedId in excludedIds)
  147. {
  148. var item = allDisturbances
  149. .FirstOrDefault(s => s.Id == excludedId);
  150. allDisturbances.Remove(item);
  151. }
  152. }
  153. ViewData["AllDisturbances"] = allDisturbances;
  154. ViewData["DisturbancesComboBoxSettings"] = settingsKey;
  155. if (model != null && type != null)
  156. {
  157. var modelType = Type.GetType(type);
  158. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  159. return PartialView("~/Views/Shared/DataEditorTemplates/_DisturbancesComboBox.cshtml", modelObject);
  160. }
  161. return PartialView("~/Views/Shared/DataEditorTemplates/_DisturbancesComboBox.cshtml", null);
  162. }
  163. /// <summary>
  164. /// Returns a DevExpress ComboBox for all kinds
  165. /// </summary>
  166. /// <param name="settingsKey">Current comboBox settings key.</param>
  167. /// <param name="model">JSON-serialized model.</param>
  168. /// <param name="type">Type of model.</param>
  169. public ActionResult KindsComboBox(string settingsKey, string model, string type)
  170. {
  171. return KindsComboBoxExcluded(settingsKey, model, type, null);
  172. }
  173. /// <summary>
  174. /// Returns a DevExpress ComboBox for all kinds
  175. /// </summary>
  176. /// <param name="settingsKey">Current comboBox settings key.</param>
  177. /// <param name="model">JSON-serialized model.</param>
  178. /// <param name="type">Type of model.</param>
  179. /// <param name="excludedIdsJson">Ids of the excluded kinds.</param>
  180. public ActionResult KindsComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  181. {
  182. var allKinds = _deviationService.GetAllKinds();
  183. if (excludedIds != null)
  184. {
  185. foreach (var excludedId in excludedIds)
  186. {
  187. var item = allKinds
  188. .FirstOrDefault(s => s.Id == excludedId);
  189. allKinds.Remove(item);
  190. }
  191. }
  192. var defaultKindId =
  193. allKinds
  194. .Any(s => s.IsDefault)
  195. ? allKinds
  196. .First(s => s.IsDefault).Id
  197. : -1;
  198. ViewData["DefaultKind"] = defaultKindId;
  199. ViewData["AllKinds"] = allKinds;
  200. ViewData["KindsComboBoxSettings"] = settingsKey;
  201. if (model != null && type != null)
  202. {
  203. var modelType = Type.GetType(type);
  204. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  205. return PartialView("~/Views/Shared/DataEditorTemplates/_KindsComboBox.cshtml", modelObject);
  206. }
  207. return PartialView("~/Views/Shared/DataEditorTemplates/_KindsComboBox.cshtml", null);
  208. }
  209. #endregion
  210. #region Appendices
  211. /// <summary>
  212. /// Returns a DevExpress ComboBox for all states
  213. /// </summary>
  214. /// <param name="settingsKey">Current comboBox settings key.</param>
  215. /// <param name="model">JSON-serialized model.</param>
  216. /// <param name="type">Type of model.</param>
  217. public ActionResult StatesComboBox(string settingsKey, string model, string type)
  218. {
  219. return StatesComboBoxExcluded(settingsKey, model, type, null);
  220. }
  221. /// <summary>
  222. /// Returns a DevExpress ComboBox for all states
  223. /// </summary>
  224. /// <param name="settingsKey">Current comboBox settings key.</param>
  225. /// <param name="model">JSON-serialized model.</param>
  226. /// <param name="type">Type of model.</param>
  227. /// <param name="excludedIdsJson">Ids of the excluded states.</param>
  228. public ActionResult StatesComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  229. {
  230. var allStates = _appendixService.GetAllStates();
  231. if (excludedIds != null)
  232. {
  233. foreach (var excludedId in excludedIds)
  234. {
  235. var item = allStates
  236. .FirstOrDefault(s => s.Id == excludedId);
  237. allStates.Remove(item);
  238. }
  239. }
  240. var defaultStateId =
  241. allStates
  242. .Any(s => s.IsDefault)
  243. ? allStates
  244. .First(s => s.IsDefault).Id
  245. : -1;
  246. ViewData["DefaultState"] = defaultStateId;
  247. ViewData["AllStates"] = allStates;
  248. ViewData["StatesComboBoxSettings"] = settingsKey;
  249. if (model != null && type != null)
  250. {
  251. var modelType = Type.GetType(type);
  252. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  253. return PartialView("~/Views/Shared/DataEditorTemplates/_StatesComboBox.cshtml", modelObject);
  254. }
  255. return PartialView("~/Views/Shared/DataEditorTemplates/_StatesComboBox.cshtml", null);
  256. }
  257. /// <summary>
  258. /// Returns a DevExpress ComboBox for all categories
  259. /// </summary>
  260. /// <param name="settingsKey">Current comboBox settings key.</param>
  261. /// <param name="model">JSON-serialized model.</param>
  262. /// <param name="type">Type of model.</param>
  263. public ActionResult CategoriesComboBox(string settingsKey, string model, string type)
  264. {
  265. return CategoriesComboBoxExcluded(settingsKey, model, type, null);
  266. }
  267. /// <summary>
  268. /// Returns a DevExpress ComboBox for all categories
  269. /// </summary>
  270. /// <param name="settingsKey">Current comboBox settings key.</param>
  271. /// <param name="model">JSON-serialized model.</param>
  272. /// <param name="type">Type of model.</param>
  273. /// <param name="excludedIdsJson">Ids of the excluded categories.</param>
  274. public ActionResult CategoriesComboBoxExcluded(string settingsKey, string model, string type, int[] excludedIds)
  275. {
  276. var allCategories = _appendixService.GetAllCategories();
  277. if (excludedIds != null)
  278. {
  279. foreach (var excludedId in excludedIds)
  280. {
  281. var item = allCategories
  282. .FirstOrDefault(s => s.Id == excludedId);
  283. allCategories.Remove(item);
  284. }
  285. }
  286. ViewData["AllCategories"] = allCategories;
  287. ViewData["CategoriesComboBoxSettings"] = settingsKey;
  288. if (model != null && type != null)
  289. {
  290. var modelType = Type.GetType(type);
  291. var modelObject = JsonConvert.DeserializeObject(model, modelType, _serializerSettings);
  292. return PartialView("~/Views/Shared/DataEditorTemplates/_CategoriesComboBox.cshtml", modelObject);
  293. }
  294. return PartialView("~/Views/Shared/DataEditorTemplates/_CategoriesComboBox.cshtml", null);
  295. }
  296. #endregion
  297. }
  298. }