UpdateProcess.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Autofac;
  2. using GreenTree.Nachtragsmanagement.Core;
  3. using GreenTree.Nachtragsmanagement.Core.Domain.Config;
  4. using GreenTree.Nachtragsmanagement.Core.Domain.Misc;
  5. using GreenTree.Nachtragsmanagement.Data;
  6. using GreenTree.Nachtragsmanagement.Services.Configuration;
  7. using GreenTree.Nachtragsmanagement.Services.Logging;
  8. using GreenTree.Nachtragsmanagement.Services.User;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Web;
  13. namespace GreenTree.Nachtragsmanagement.Web.Update
  14. {
  15. public class UpdateProcess
  16. {
  17. /// <summary>
  18. /// Run all actions required for the current version
  19. /// </summary>
  20. public void RunUpdateProcess()
  21. {
  22. RunUpdateActionsForVersion(AppendixVersion.CurrentVersion);
  23. }
  24. /// <summary>
  25. /// Run all actions required for a specific update version
  26. /// </summary>
  27. /// <param name="version">Update version.</param>
  28. public void RunUpdateProcess(string version)
  29. {
  30. RunUpdateActionsForVersion(version);
  31. }
  32. /// <summary>
  33. /// Run all actions for a specific update version
  34. /// </summary>
  35. /// <param name="version">Update version.</param>
  36. private void RunUpdateActionsForVersion(string version)
  37. {
  38. if (String.IsNullOrEmpty(version)) return;
  39. if (CheckExistingDbContextSpecForVersion(version)) return;
  40. switch (version)
  41. {
  42. case "1.0.0.1":
  43. var configurationService = Singleton<IContainer>.Instance.Resolve<IConfigurationService>();
  44. if (configurationService == null) return;
  45. var existingConfigItem =
  46. configurationService.GetConfigItemByName(
  47. "GreenTree.Nachtragsmanagement.DeviationGrid.ClosedDeviationStatuses");
  48. if (existingConfigItem == null)
  49. {
  50. var newConfigItem = new ConfigItem
  51. {
  52. Name = "GreenTree.Nachtragsmanagement.DeviationGrid.ClosedDeviationStatuses",
  53. Description = "Alle Vertragsabweichungsstati, die als \"Geschlossen\" gelten",
  54. TypeFullName = "ConfigurationReference.StatusConfigurationReference",
  55. Value = "2, 4"
  56. };
  57. configurationService.InsertConfigItem(newConfigItem);
  58. }
  59. else
  60. {
  61. existingConfigItem.Name = "GreenTree.Nachtragsmanagement.DeviationGrid.ClosedDeviationStatuses";
  62. existingConfigItem.Description = "Alle Vertragsabweichungsstati, die als \"Geschlossen\" gelten";
  63. existingConfigItem.TypeFullName = "ConfigurationReference.StatusConfigurationReference";
  64. existingConfigItem.Value = "2, 4";
  65. configurationService.UpdateConfigItem(existingConfigItem);
  66. }
  67. break;
  68. default:
  69. return;
  70. }
  71. CreateDbContextSpecForVersion(version);
  72. }
  73. /// <summary>
  74. /// Checks the specified update version for an existing DbContextSpec entity to check wether the actions has already been done
  75. /// </summary>
  76. /// <param name="version">Update version.</param>
  77. /// <returns>True when action already done otherwise False.</returns>
  78. private bool CheckExistingDbContextSpecForVersion(string version)
  79. {
  80. if (String.IsNullOrEmpty(version)) return true;
  81. var dbContext = Singleton<IContainer>.Instance.Resolve<IDbContext>();
  82. if (dbContext == null) return true;
  83. var versionSpecName = String.Format("UpdateRun-{0}", version);
  84. var versionSpec = dbContext.Get<DbContextSpec>()
  85. .FirstOrDefault(d => d.Name == versionSpecName);
  86. if (versionSpec != null) return true;
  87. return false;
  88. }
  89. /// <summary>
  90. /// Sets a new DbContextSpec entity to determine wether the actions has already been done
  91. /// </summary>
  92. /// <param name="version">Update version.</param>
  93. private void CreateDbContextSpecForVersion(string version)
  94. {
  95. if (String.IsNullOrEmpty(version)) return;
  96. if (CheckExistingDbContextSpecForVersion(version)) return;
  97. var dbContext = Singleton<IContainer>.Instance.Resolve<IDbContext>();
  98. if (dbContext == null) return;
  99. var versionSpec = new DbContextSpec
  100. {
  101. Name = String.Format("UpdateRun-{0}", version),
  102. Value = "True"
  103. };
  104. dbContext.Get<DbContextSpec>().Add(versionSpec);
  105. dbContext.SaveChanges();
  106. }
  107. }
  108. }