using Autofac;
using GreenTree.Nachtragsmanagement.Core;
using GreenTree.Nachtragsmanagement.Core.Domain.Misc;
using GreenTree.Nachtragsmanagement.Data;
using GreenTree.Nachtragsmanagement.Services.Logging;
using GreenTree.Nachtragsmanagement.Services.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace GreenTree.Nachtragsmanagement.Web.Update
{
public class UpdateProcess
{
///
/// Run all actions required for the current version
///
public void RunUpdateProcess()
{
RunUpdateActionsForVersion(AppendixVersion.CurrentVersion);
}
///
/// Run all actions required for a specific update version
///
/// Update version.
public void RunUpdateProcess(string version)
{
RunUpdateActionsForVersion(version);
}
///
/// Run all actions for a specific update version
///
/// Update version.
private void RunUpdateActionsForVersion(string version)
{
if (String.IsNullOrEmpty(version)) return;
if (CheckExistingDbContextSpecForVersion(version)) return;
switch (version)
{
case "0.9.1.0":
{
var logger = Singleton.Instance.Resolve();
logger.Information("UpdateProcess Test - " + version);
}
break;
default:
return;
}
CreateDbContextSpecForVersion(version);
}
///
/// Checks the specified update version for an existing DbContextSpec entity to check wether the actions has already been done
///
/// Update version.
/// True when action already done otherwise False.
private bool CheckExistingDbContextSpecForVersion(string version)
{
if (String.IsNullOrEmpty(version)) return true;
var dbContext = Singleton.Instance.Resolve();
if (dbContext == null) return true;
var versionSpecName = String.Format("UpdateRun-{0}", version);
var versionSpec = dbContext.Get()
.FirstOrDefault(d => d.Name == versionSpecName);
if (versionSpec != null) return true;
return false;
}
///
/// Sets a new DbContextSpec entity to determine wether the actions has already been done
///
/// Update version.
private void CreateDbContextSpecForVersion(string version)
{
if (String.IsNullOrEmpty(version)) return;
if (CheckExistingDbContextSpecForVersion(version)) return;
var dbContext = Singleton.Instance.Resolve();
if (dbContext == null) return;
var versionSpec = new DbContextSpec
{
Name = String.Format("UpdateRun-{0}", version),
Value = "True"
};
dbContext.Get().Add(versionSpec);
dbContext.SaveChanges();
}
}
}