init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
227
Framework/Util/FrameworkEnumExtensions.cs
Normal file
227
Framework/Util/FrameworkEnumExtensions.cs
Normal file
|
@ -0,0 +1,227 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Kreta.Framework.Caching;
|
||||
|
||||
namespace Kreta.Framework.Util
|
||||
{
|
||||
public static class FrameworkEnumExtensions
|
||||
{
|
||||
private static readonly ManualEnumCache ManualEnumCache = SDAServer.Instance.CacheManager.AquireCache<ManualEnumCache>();
|
||||
|
||||
private static readonly DictionaryTableCache DictionaryTableCache = SDAServer.Instance.CacheManager.AquireCache<DictionaryTableCache>();
|
||||
|
||||
public static string GetItemNameFromCache(this int value, int tanevId, string intezmenyAzonosito = null)
|
||||
=> DictionaryTableCache.GetItemName(tanevId, value, intezmenyAzonosito);
|
||||
|
||||
public static int GetDictionaryTypeId(this int value, int tanevId, string intezmenyAzonosito = null)
|
||||
=> DictionaryTableCache.GetById(tanevId, value, intezmenyAzonosito).DictionaryTypeId;
|
||||
|
||||
public static void RemoveFromCache(this int tipusId, int tanevId, int? id = null, string intezmenyAzonosito = null)
|
||||
=> DictionaryTableCache.RemoveKey(tanevId, tipusId, id, intezmenyAzonosito);
|
||||
|
||||
public static List<DictionaryItem> GetItemsByType(this int typeId, int tanevId, bool visibleOnly = false, string intezmenyAzonosito = null)
|
||||
=> DictionaryTableCache.GetByType(tanevId, typeId, visibleOnly, intezmenyAzonosito);
|
||||
|
||||
public static int? GetItemIdByTypeAndName(this int typeId, string name, int tanevId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
return new int?();
|
||||
}
|
||||
|
||||
name = name.ToUpper();
|
||||
|
||||
var dictionaryItems = GetItemsByType(typeId, tanevId).ToDictionary(k => k.Name.ToUpper(), v => v.Id);
|
||||
|
||||
if (dictionaryItems.TryGetValue(name, out int value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
return new int?();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EnumToList
|
||||
/// </summary>
|
||||
/// <param name="tipusId"></param>
|
||||
/// <param name="tanevId"></param>
|
||||
/// <param name="emptyItem">Ha true, hozzáad a listához egy üres elemet </param>
|
||||
/// <param name="topItems">A topItems-ben szereplő ID-val rendelkező enumok kerülnek a lista elejére</param>
|
||||
/// <param name="toLower"></param>
|
||||
/// <param name="visibleOnly">Az Enum list feltöltésekor szabályozható, hogy a C_VISIBLE értéket figyelembe vegye-e</param>
|
||||
/// <returns>IDictionary<string, string></returns>
|
||||
public static IDictionary<string, string> EnumToList(int tipusId, int tanevId, bool emptyItem = false, List<int> topItems = null, bool toLower = false, bool visibleOnly = true)
|
||||
{
|
||||
var enums = tipusId.GetItemsByType(tanevId, visibleOnly).ToDictionary(di => di.Id.ToString(), di => di.Name);
|
||||
|
||||
return enums.SetDictionaryItemsAdditionalSettings(tanevId, emptyItem, topItems, toLower);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EnumItemListToDictionary
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="enumItemList"></param>
|
||||
/// <param name="tanevId"></param>
|
||||
/// <param name="emptyItem">Ha true, hozzáad a listához egy üres elemet </param>
|
||||
/// <param name="topItems">A topItems-ben szereplő ID-val rendelkező enumok kerülnek a lista elejére</param>
|
||||
/// <param name="toLower"></param>
|
||||
/// <param name="intezmenyAzonosito"></param>
|
||||
/// <returns>IDictionary<string, string></returns>
|
||||
public static IDictionary<string, string> EnumItemListToDictionary<T>(this List<T> enumItemList, int tanevId, bool emptyItem = false, List<int> topItems = null, bool toLower = false, string intezmenyAzonosito = null) where T : Enum
|
||||
{
|
||||
var enums = new Dictionary<string, string>();
|
||||
|
||||
foreach (var genericEnumItem in enumItemList)
|
||||
{
|
||||
var enumItem = Enum.Parse(typeof(T), genericEnumItem.ToString()) as Enum;
|
||||
var enumId = Convert.ToInt32(enumItem);
|
||||
var dictionaryItem = DictionaryTableCache.GetById(tanevId, enumId, intezmenyAzonosito);
|
||||
enums.Add(dictionaryItem.Id.ToString(), dictionaryItem.Name);
|
||||
}
|
||||
|
||||
return enums.SetDictionaryItemsAdditionalSettings(tanevId, emptyItem, topItems, toLower);
|
||||
}
|
||||
|
||||
private static IDictionary<string, string> SetDictionaryItemsAdditionalSettings(this Dictionary<string, string> enums, int tanevId, bool emptyItem, List<int> topItems, bool toLower, string intezmenyAzonosito = null)
|
||||
{
|
||||
if (toLower)
|
||||
{
|
||||
enums = enums.ToDictionary(
|
||||
item => item.Key,
|
||||
item => item.Value.ToLower());
|
||||
}
|
||||
|
||||
if (topItems != null)
|
||||
{
|
||||
var topItemEnums = topItems.Where(x => enums.ContainsKey(x.ToString())).Select(id => DictionaryTableCache.GetById(tanevId, id, intezmenyAzonosito)).ToList();
|
||||
var topItemDictionary = topItemEnums.ToDictionary(a => a.Id.ToString(), b => b.Name);
|
||||
|
||||
enums = topItemDictionary.Union(enums).ToDictionary(k => k.Key, v => v.Value);
|
||||
}
|
||||
|
||||
return SetEmptyItem(emptyItem, enums);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Csak manuális enumok esetén használandó.
|
||||
/// NotSupportedException ha nem manual enum.
|
||||
/// ArgumentException ha nem enum.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="emptyItem"></param>
|
||||
/// <returns></returns>
|
||||
public static IDictionary<string, string> EnumToListManual<T>(bool emptyItem = false) where T : Enum
|
||||
{
|
||||
var enumType = typeof(T);
|
||||
if (!enumType.AssemblyQualifiedName.Contains("Kreta.Enums.ManualEnums"))
|
||||
{
|
||||
throw new NotSupportedException("The given enum is not a manual Enum");
|
||||
}
|
||||
|
||||
//// Can't use type constraints on value types, so have to do check like this
|
||||
if (enumType.BaseType != typeof(Enum))
|
||||
{
|
||||
throw new ArgumentException("T must be of type System.Enum");
|
||||
}
|
||||
|
||||
var enums =
|
||||
ManualEnumCache.GetOrAdd(
|
||||
enumType.FullName,
|
||||
_ =>
|
||||
Enum.GetNames(enumType)
|
||||
.ToDictionary(
|
||||
e => Convert.ToInt32(e.EnumParse<T>()).ToString(),
|
||||
e => GetDisplayName(e.EnumParse<T>())
|
||||
)
|
||||
);
|
||||
|
||||
return SetEmptyItem(emptyItem, enums);
|
||||
}
|
||||
|
||||
private static string GetDisplayName<T>(T enumValue)
|
||||
{
|
||||
string result;
|
||||
//NOTE: Ha van Display attribútum, akkor annak a szövegét szedjük elő!
|
||||
var fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
|
||||
var displayAttributeArray = (DisplayAttribute[])fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false);
|
||||
|
||||
if (displayAttributeArray.Length > 0)
|
||||
{
|
||||
result = displayAttributeArray[0].GetName();
|
||||
return result;
|
||||
}
|
||||
|
||||
//NOTE: Ha nincs Display attribútum, akkor a StringResourcesId attribútum paramétere alapján a StringResources.kres-ben lévő szöveget szedjük elő!
|
||||
var stringResourcesIdAttribute = enumValue
|
||||
.GetType()
|
||||
.GetMember(enumValue.ToString())[0]
|
||||
.GetCustomAttribute<StringResourcesIdAttribute>();
|
||||
|
||||
if (stringResourcesIdAttribute != null)
|
||||
{
|
||||
//NOTE: OBSOLETE!!! Amint kiírtottuk az összes manual enu
|
||||
result = StringResourcesUtil.GetString(stringResourcesIdAttribute.StringResourcesId);
|
||||
return result;
|
||||
}
|
||||
|
||||
//NOTE: Ha egyik attribútum sincs, akkor az enum-nak a ToString-jével térünk vissza!
|
||||
result = enumValue.ToString();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string GetManualEnumNameFromCache<T>(int enumValue) where T : Enum
|
||||
{
|
||||
var names = EnumToListManual<T>();
|
||||
|
||||
foreach (var n in names)
|
||||
{
|
||||
if (n.Key == enumValue.ToString())
|
||||
{
|
||||
return n.Value;
|
||||
}
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private static IDictionary<string, string> SetEmptyItem(bool emptyItem, Dictionary<string, string> enums)
|
||||
{
|
||||
if (emptyItem)
|
||||
{
|
||||
var lista = new Dictionary<string, string>
|
||||
{
|
||||
{"", StringResourcesUtil.GetString(364)}
|
||||
};
|
||||
|
||||
return lista.Union(enums).ToDictionary(k => k.Key, v => v.Value);
|
||||
}
|
||||
|
||||
return enums;
|
||||
}
|
||||
|
||||
public static T EnumParse<T>(this string value) where T : Enum
|
||||
=> EnumParse<T>(value, false);
|
||||
|
||||
public static T EnumParse<T>(this string value, bool ignoreCase) where T : Enum
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
throw new ArgumentException("Must specify valid information for parsing in the string.", nameof(value));
|
||||
}
|
||||
|
||||
var t = typeof(T);
|
||||
|
||||
if (!t.IsEnum)
|
||||
{
|
||||
throw new ArgumentException("Type provided must be an Enum.", "T");
|
||||
}
|
||||
|
||||
return (T)Enum.Parse(t, value, ignoreCase);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue