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(); private static readonly DictionaryTableCache DictionaryTableCache = SDAServer.Instance.CacheManager.AquireCache(); 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 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?(); } /// /// EnumToList /// /// /// /// Ha true, hozzáad a listához egy üres elemet /// A topItems-ben szereplő ID-val rendelkező enumok kerülnek a lista elejére /// /// Az Enum list feltöltésekor szabályozható, hogy a C_VISIBLE értéket figyelembe vegye-e /// IDictionary<string, string> public static IDictionary EnumToList(int tipusId, int tanevId, bool emptyItem = false, List 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); } /// /// EnumItemListToDictionary /// /// /// /// /// Ha true, hozzáad a listához egy üres elemet /// A topItems-ben szereplő ID-val rendelkező enumok kerülnek a lista elejére /// /// /// IDictionary<string, string> public static IDictionary EnumItemListToDictionary(this List enumItemList, int tanevId, bool emptyItem = false, List topItems = null, bool toLower = false, string intezmenyAzonosito = null) where T : Enum { var enums = new Dictionary(); 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 SetDictionaryItemsAdditionalSettings(this Dictionary enums, int tanevId, bool emptyItem, List 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); } /// /// Csak manuális enumok esetén használandó. /// NotSupportedException ha nem manual enum. /// ArgumentException ha nem enum. /// /// /// /// public static IDictionary EnumToListManual(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()).ToString(), e => GetDisplayName(e.EnumParse()) ) ); return SetEmptyItem(emptyItem, enums); } private static string GetDisplayName(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(); 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(int enumValue) where T : Enum { var names = EnumToListManual(); foreach (var n in names) { if (n.Key == enumValue.ToString()) { return n.Value; } } return string.Empty; } private static IDictionary SetEmptyItem(bool emptyItem, Dictionary enums) { if (emptyItem) { var lista = new Dictionary { {"", StringResourcesUtil.GetString(364)} }; return lista.Union(enums).ToDictionary(k => k.Key, v => v.Value); } return enums; } public static T EnumParse(this string value) where T : Enum => EnumParse(value, false); public static T EnumParse(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); } } }