ConfigItemDataModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using GreenTree.Nachtragsmanagement.Services.Configuration;
  2. using GreenTree.Nachtragsmanagement.Web.Extensions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. namespace GreenTree.Nachtragsmanagement.Web.Models.Config
  8. {
  9. public class ConfigItemDataModel
  10. {
  11. public static Dictionary<string, string> FullTypeTranslations = new Dictionary<string, string>
  12. {
  13. { typeof(System.String).FullName, "Text" },
  14. { typeof(System.Int32).FullName, "Ganze Zahl" },
  15. { typeof(System.Int64).FullName, "Große ganze Zahl" },
  16. { typeof(System.Boolean).FullName, "Wahrheitswert" },
  17. { typeof(System.Double).FullName, "Kommazahl" },
  18. { typeof(System.DateTime).FullName, "Datum" },
  19. { typeof(System.Drawing.Color).FullName, "Farbe" },
  20. };
  21. public int Id { get; set; }
  22. public string Name { get; set; }
  23. public string TypeFullName { get; set; }
  24. public string TypeDescription { get; set; }
  25. public string Value { get; set; }
  26. public bool IsValueCollection { get; set; }
  27. public string[] Values { get; set; }
  28. public string Description { get; set; }
  29. public static ConfigItemDataModel FromConfigItem(Core.Domain.Config.ConfigItem configItemEntity, bool newWhenIsNull)
  30. {
  31. if (configItemEntity == null && newWhenIsNull)
  32. return new ConfigItemDataModel
  33. {
  34. Id = -1
  35. };
  36. if (configItemEntity == null && !newWhenIsNull)
  37. throw new ArgumentNullException("configItemEntity", "Cannot create ConfigItemDataModel from NULL configItem entity.");
  38. var configItemModel = new ConfigItemDataModel
  39. {
  40. Id = configItemEntity.Id,
  41. Name = configItemEntity.Name,
  42. TypeFullName = configItemEntity.TypeFullName,
  43. Description = configItemEntity.Description
  44. };
  45. if (FullTypeTranslations.ContainsKey(configItemEntity.TypeFullName))
  46. {
  47. configItemModel.Value = configItemEntity.Value;
  48. configItemModel.TypeDescription = FullTypeTranslations[configItemEntity.TypeFullName];
  49. configItemModel.IsValueCollection = false;
  50. }
  51. else
  52. {
  53. var configurationReference = ConfigurationHelper.GetConfigurationReferenceByName(configItemEntity.TypeFullName);
  54. if (configurationReference != null)
  55. {
  56. configItemModel.TypeDescription = configurationReference.DisplayName;
  57. configItemModel.IsValueCollection = configurationReference.IsMultipleSelection;
  58. configItemModel.Value = configItemEntity.Value;
  59. if (configurationReference.IsMultipleSelection)
  60. configItemModel.Values = configurationReference.TransformValueToValueCollection(configItemEntity.Value);
  61. }
  62. else
  63. {
  64. configItemModel.TypeDescription = "Unbekannt";
  65. }
  66. }
  67. return configItemModel;
  68. }
  69. public Core.Domain.Config.ConfigItem ToConfigItem()
  70. {
  71. var configItem = new Core.Domain.Config.ConfigItem
  72. {
  73. Id = this.Id,
  74. Name = this.Name,
  75. TypeFullName = this.TypeFullName,
  76. Description = this.Description
  77. };
  78. if (FullTypeTranslations.ContainsKey(this.TypeFullName))
  79. {
  80. configItem.Value = this.Value;
  81. }
  82. else
  83. {
  84. var configurationReference = ConfigurationHelper.GetConfigurationReferenceByName(this.TypeFullName);
  85. if (configurationReference != null)
  86. {
  87. if (configurationReference.IsMultipleSelection)
  88. configItem.Value = configurationReference.TransformValueCollectionToValue(this.Values);
  89. else
  90. configItem.Value = this.Value;
  91. }
  92. }
  93. return configItem;
  94. }
  95. }
  96. }