UpdateProcess.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. default:
  41. return;
  42. }
  43. CreateDbContextSpecForVersion(version);
  44. }
  45. /// <summary>
  46. /// Checks the specified update version for an existing DbContextSpec entity to check wether the actions has already been done
  47. /// </summary>
  48. /// <param name="version">Update version.</param>
  49. /// <returns>True when action already done otherwise False.</returns>
  50. private bool CheckExistingDbContextSpecForVersion(string version)
  51. {
  52. if (String.IsNullOrEmpty(version)) return true;
  53. var dbContext = Singleton<IContainer>.Instance.Resolve<IDbContext>();
  54. if (dbContext == null) return true;
  55. var versionSpecName = String.Format("UpdateRun-{0}", version);
  56. var versionSpec = dbContext.Get<DbContextSpec>()
  57. .FirstOrDefault(d => d.Name == versionSpecName);
  58. if (versionSpec != null) return true;
  59. return false;
  60. }
  61. /// <summary>
  62. /// Sets a new DbContextSpec entity to determine wether the actions has already been done
  63. /// </summary>
  64. /// <param name="version">Update version.</param>
  65. private void CreateDbContextSpecForVersion(string version)
  66. {
  67. if (String.IsNullOrEmpty(version)) return;
  68. if (CheckExistingDbContextSpecForVersion(version)) return;
  69. var dbContext = Singleton<IContainer>.Instance.Resolve<IDbContext>();
  70. if (dbContext == null) return;
  71. var versionSpec = new DbContextSpec
  72. {
  73. Name = String.Format("UpdateRun-{0}", version),
  74. Value = "True"
  75. };
  76. dbContext.Get<DbContextSpec>().Add(versionSpec);
  77. dbContext.SaveChanges();
  78. }
  79. }
  80. }