172 lines
7.1 KiB
C#
172 lines
7.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Kreta.Core;
|
|
using Kreta.DataAccessManual;
|
|
using Kreta.DataAccessManual.Interfaces;
|
|
using Kreta.Enums;
|
|
using Kreta.Framework.Caching;
|
|
using Kreta.KretaServer.Exceptions;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Kreta.KretaServer.Caching
|
|
{
|
|
public class SystemSettingsCache : GenericCache<SystemSettingsCache.SystemSettingsData>
|
|
{
|
|
private static readonly string SystemSettingsCacheKeyPrefix = $"{Constants.Cache.CacheKeyPrefix}{nameof(SystemSettingsCache)}_";
|
|
private static readonly string SystemSettingsRegionPrefix = $"{Constants.Cache.CacheKeyPrefix}{nameof(SystemSettingsCache)}Region_";
|
|
|
|
public SystemSettingsCache(Framework.Caching.CacheManager cacheManager) : base(cacheManager, nameof(SystemSettingsCache))
|
|
{
|
|
}
|
|
|
|
[Serializable]
|
|
public class SystemSettingsData
|
|
{
|
|
public RendszerBeallitasTipusEnum BeallitasTipus { get; set; }
|
|
public SystemSettingsControlTypeEnum Type { get; set; }
|
|
public string Json { get; set; }
|
|
public bool IsDisabled { get; set; }
|
|
}
|
|
|
|
private string GetCacheKey(string keyPrefix, RendszerBeallitasTipusEnum beallitasTipus, string intezmenyAzonosito, int tanevId)
|
|
{
|
|
return $"{keyPrefix}{intezmenyAzonosito.ToLower()}.{tanevId}.{(int)beallitasTipus}";
|
|
}
|
|
|
|
private static string GetRegionKey(string intezmenyAzonosito) => $"{SystemSettingsRegionPrefix}{intezmenyAzonosito}";
|
|
|
|
private SystemSettingsData LoadSystemSettingByType(IRendszerBeallitasDAL dal, RendszerBeallitasTipusEnum beallitasTipus, int tanevId)
|
|
{
|
|
var rb = dal.Get(beallitasTipus, tanevId);
|
|
if (rb != null)
|
|
{
|
|
return new SystemSettingsData
|
|
{
|
|
Type = (SystemSettingsControlTypeEnum)rb.ErtekTipus,
|
|
Json = rb.Ertek,
|
|
BeallitasTipus = (RendszerBeallitasTipusEnum)rb.BeallitasTipus,
|
|
IsDisabled = rb.IsDisabled
|
|
};
|
|
}
|
|
|
|
throw new KretaException($"System setting not found: {beallitasTipus}");
|
|
}
|
|
|
|
private SystemSettingsData LoadSystemSettingByType(RendszerBeallitasTipusEnum beallitasTipus, string intezmenyAzonosito, int tanevId)
|
|
{
|
|
return Dal.OrganizationConnection.Run(intezmenyAzonosito, h =>
|
|
{
|
|
return LoadSystemSettingByType(h.RendszerBeallitas(), beallitasTipus, tanevId);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Visszaad egy rendszerbeállítást
|
|
/// </summary>
|
|
public SystemSettingsData GetSystemSetting(RendszerBeallitasTipusEnum beallitasTipus, string intezmenyAzonosito, int tanevId)
|
|
{
|
|
var key = GetCacheKey(SystemSettingsCacheKeyPrefix, beallitasTipus, intezmenyAzonosito, tanevId);
|
|
var region = GetRegionKey(intezmenyAzonosito);
|
|
|
|
var result = GetOrAddWithRegion(key, region, (k, r) => LoadSystemSettingByType(beallitasTipus, intezmenyAzonosito, tanevId));
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Visszaadja az összes rendszerbeállítást
|
|
/// </summary>
|
|
public List<SystemSettingsData> GetSystemSettings(string intezmenyAzonosito, int tanevId)
|
|
{
|
|
return Dal.OrganizationConnection.Run(intezmenyAzonosito, h =>
|
|
{
|
|
List<SystemSettingsData> result = new List<SystemSettingsData>();
|
|
var dal = h.RendszerBeallitas();
|
|
foreach (RendszerBeallitasTipusEnum beallitasTipus in Enum.GetValues(typeof(RendszerBeallitasTipusEnum)))
|
|
{
|
|
var key = GetCacheKey(SystemSettingsCacheKeyPrefix, beallitasTipus, intezmenyAzonosito, tanevId);
|
|
var region = GetRegionKey(intezmenyAzonosito);
|
|
|
|
result.Add(GetOrAddWithRegion(key, region, (k, r) => LoadSystemSettingByType(dal, beallitasTipus, tanevId)));
|
|
}
|
|
return result;
|
|
});
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ment egy rendszerbeállítás
|
|
/// </summary>
|
|
public void SetSystemSettings(RendszerBeallitasTipusEnum beallitasTipus, string intezmenyAzonosito, int tanevId, object settingobject, int? kovTanevId)
|
|
{
|
|
Dal.OrganizationConnection.Run(intezmenyAzonosito, h =>
|
|
{
|
|
var dal = h.RendszerBeallitas();
|
|
var rb = dal.Get(beallitasTipus, tanevId);
|
|
|
|
if (rb != null)
|
|
{
|
|
string json = JsonConvert.SerializeObject(settingobject);
|
|
rb.Ertek = json;
|
|
dal.FullUpdate(rb);
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentException("Invalid System settings type given");
|
|
}
|
|
|
|
var key = GetCacheKey(SystemSettingsCacheKeyPrefix, beallitasTipus, intezmenyAzonosito, tanevId);
|
|
var region = GetRegionKey(intezmenyAzonosito);
|
|
|
|
var value = LoadSystemSettingByType(dal, beallitasTipus, tanevId);
|
|
AddOrUpdateWithRegion(key, region, value, k => value);
|
|
|
|
if (kovTanevId.IsEntityId())
|
|
{
|
|
try
|
|
{
|
|
key = GetCacheKey(SystemSettingsCacheKeyPrefix, beallitasTipus, intezmenyAzonosito, (int)kovTanevId);
|
|
region = GetRegionKey(intezmenyAzonosito);
|
|
|
|
value = LoadSystemSettingByType(dal, beallitasTipus, (int)kovTanevId);
|
|
AddOrUpdateWithRegion(key, region, value, k => value);
|
|
}
|
|
catch (KretaException)
|
|
{
|
|
// Köv tanév esetén nem foglalkozunk azzal a hibával, hogy nem található a rendszerbeállítás,
|
|
// mivel új rendszerbeállítás felvételekor nem tudjuk beszúrni a rendszerbeállítás táblába az értéket,
|
|
// mert nem feltétlen szerepel tanévrendje események köv tanévhez.
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public bool ResetSystemSettings(string intezmenyAzonosito, int? intezmenyID, int? tanevID, List<int> exceptSystemSettings)
|
|
{
|
|
return Dal.OrganizationConnection.Run(intezmenyAzonosito, h =>
|
|
{
|
|
try
|
|
{
|
|
h.RendszerBeallitas().ResetSystemSettings(intezmenyID, tanevID, exceptSystemSettings);
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var region = GetRegionKey(intezmenyAzonosito);
|
|
ClearRegion(region);
|
|
return true;
|
|
|
|
});
|
|
}
|
|
|
|
public void RemoveSystemSetting(RendszerBeallitasTipusEnum beallitasTipus, string intezmenyAzonosito, int tanevId)
|
|
{
|
|
var key = GetCacheKey(SystemSettingsCacheKeyPrefix, beallitasTipus, intezmenyAzonosito, tanevId);
|
|
var region = GetRegionKey(intezmenyAzonosito);
|
|
|
|
RemoveFromRegion(key, region);
|
|
}
|
|
}
|
|
}
|