UpdateProcess.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Autofac;
  2. using GreenTree.Nachtragsmanagement.Core;
  3. using GreenTree.Nachtragsmanagement.Core.Domain.Misc;
  4. using GreenTree.Nachtragsmanagement.Data;
  5. using GreenTree.Nachtragsmanagement.Services.Logging;
  6. using GreenTree.Nachtragsmanagement.Services.User;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Web;
  11. namespace GreenTree.Nachtragsmanagement.Web.Update
  12. {
  13. public class UpdateProcess
  14. {
  15. /// <summary>
  16. /// Run all actions required for the current version
  17. /// </summary>
  18. public void RunUpdateProcess()
  19. {
  20. RunUpdateActionsForVersion(AppendixVersion.CurrentVersion);
  21. }
  22. /// <summary>
  23. /// Run all actions required for a specific update version
  24. /// </summary>
  25. /// <param name="version">Update version.</param>
  26. public void RunUpdateProcess(string version)
  27. {
  28. RunUpdateActionsForVersion(version);
  29. }
  30. /// <summary>
  31. /// Run all actions for a specific update version
  32. /// </summary>
  33. /// <param name="version">Update version.</param>
  34. private void RunUpdateActionsForVersion(string version)
  35. {
  36. if (String.IsNullOrEmpty(version)) return;
  37. if (CheckExistingDbContextSpecForVersion(version)) return;
  38. switch (version)
  39. {
  40. case "0.9.1.0":
  41. {
  42. var logger = Singleton<IContainer>.Instance.Resolve<IServiceLogger>();
  43. logger.Information("UpdateProcess Test - " + version);
  44. }
  45. break;
  46. default:
  47. return;
  48. }
  49. CreateDbContextSpecForVersion(version);
  50. }
  51. /// <summary>
  52. /// Checks the specified update version for an existing DbContextSpec entity to check wether the actions has already been done
  53. /// </summary>
  54. /// <param name="version">Update version.</param>
  55. /// <returns>True when action already done otherwise False.</returns>
  56. private bool CheckExistingDbContextSpecForVersion(string version)
  57. {
  58. if (String.IsNullOrEmpty(version)) return true;
  59. var dbContext = Singleton<IContainer>.Instance.Resolve<IDbContext>();
  60. if (dbContext == null) return true;
  61. var versionSpecName = String.Format("UpdateRun-{0}", version);
  62. var versionSpec = dbContext.Get<DbContextSpec>()
  63. .FirstOrDefault(d => d.Name == versionSpecName);
  64. if (versionSpec != null) return true;
  65. return false;
  66. }
  67. /// <summary>
  68. /// Sets a new DbContextSpec entity to determine wether the actions has already been done
  69. /// </summary>
  70. /// <param name="version">Update version.</param>
  71. private void CreateDbContextSpecForVersion(string version)
  72. {
  73. if (String.IsNullOrEmpty(version)) return;
  74. if (CheckExistingDbContextSpecForVersion(version)) return;
  75. var dbContext = Singleton<IContainer>.Instance.Resolve<IDbContext>();
  76. if (dbContext == null) return;
  77. var versionSpec = new DbContextSpec
  78. {
  79. Name = String.Format("UpdateRun-{0}", version),
  80. Value = "True"
  81. };
  82. dbContext.Get<DbContextSpec>().Add(versionSpec);
  83. dbContext.SaveChanges();
  84. }
  85. }
  86. }