using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using HRM.BO; using Microsoft.Extensions.Caching.Memory; namespace HRM.DA { /// /// Server side of <app-virtual-grid>: turns any List<T> into one window of rows (search, sort, /// skip/take, total count). A page endpoint calls it with its existing service method: /// /// [HttpPost("getDiscontinueEmployeePage")] /// public ActionResult GetDiscontinueEmployeePage(VirtualPageRequest request) /// { /// CurrentUser user = CurrentUser.GetCurrentUser(HttpContext.User); /// DateTime fromDate = request.GetDate("fromDate", "From Date"); /// DateTime toDate = request.GetDate("toDate", "To Date"); /// return Ok(VirtualPaging.Page(request, user, /// () => _employeeService.GetDiscontinuedEmployeeDetails(fromDate, toDate, user.PayrollTypeID.Value))); /// } /// /// The list is loaded on Preview (request.Refresh) and kept in memory for a few minutes, per user, /// endpoint and filters, so scrolling, sorting, searching and Export to Excel do not call the /// service again. See README.md next to this file. /// public static class VirtualPaging { private static readonly MemoryCache Cache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 1000000 }); // max rows kept, all users together private static readonly TimeSpan KeepFor = TimeSpan.FromMinutes(10); // since last use private static readonly TimeSpan KeepAtMost = TimeSpan.FromMinutes(30); // since loaded private const int MaxRowsPerPage = 1000; public static VirtualPageResult Page(VirtualPageRequest request, CurrentUser user, Func> loadData, [CallerFilePath] string callerFile = "", [CallerMemberName] string callerName = "") where T : class { if (request == null) throw new ArgumentException("Invalid request."); // 1. The list: from memory, or from the service on Preview / when it has expired. string key = CacheKey(request, user, callerFile + "|" + callerName + "|" + typeof(T).FullName); KeptList kept; if (request.Refresh || !Cache.TryGetValue(key, out kept)) { kept = new KeptList(loadData() ?? new List()); Cache.Set(key, kept, new MemoryCacheEntryOptions { Size = Math.Max(1, kept.Items.Count), SlidingExpiration = KeepFor, AbsoluteExpirationRelativeToNow = KeepAtMost, }); } List fields = GetFields(request.Fields); IEnumerable rows = Enumerable.Range(0, kept.Items.Count); // 2. Search: the text the grid shows, in any of the grid's columns. string search = (request.Search ?? "").Trim(); if (search.Length > 0) { List texts = kept.SearchTexts(fields); rows = rows.Where(i => texts[i].IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0); } List matched = rows.ToList(); // 3. Sort: only by one of the grid's columns. PropertyInfo sortBy = fields.FirstOrDefault(p => string.Equals(p.Name, request.SortField, StringComparison.OrdinalIgnoreCase)); if (sortBy != null) { bool desc = string.Equals(request.SortDir, "desc", StringComparison.OrdinalIgnoreCase); matched = desc ? matched.OrderByDescending(i => sortBy.GetValue(kept.Items[i]), ValueComparer.Instance).ToList() : matched.OrderBy(i => sortBy.GetValue(kept.Items[i]), ValueComparer.Instance).ToList(); } // 4. The window: Take 0 = every row (Export to Excel). int skip = Math.Max(0, request.Skip); int take = request.Take <= 0 ? matched.Count : Math.Min(request.Take, MaxRowsPerPage); var result = new VirtualPageResult { Total = matched.Count, TotalWithoutSearch = kept.Items.Count }; foreach (int i in matched.Skip(skip).Take(take)) { var row = new Dictionary(fields.Count); foreach (PropertyInfo p in fields) row[CamelCase(p.Name)] = p.GetValue(kept.Items[i]); result.Data.Add(row); } return result; } #region Helpers /// A loaded list, plus the search text of each row (worked out once, on the first search). private sealed class KeptList { private readonly ConcurrentDictionary> _searchTexts = new ConcurrentDictionary>(); public KeptList(List items) { Items = items.Where(x => x != null).ToList(); } public List Items { get; } public List SearchTexts(List fields) { string key = string.Join(",", fields.Select(p => p.Name)); return _searchTexts.GetOrAdd(key, _ => Items .Select(item => string.Join("\u0001", fields.Select(p => DisplayText(p.GetValue(item))))) .ToList()); } } /// The requested fields that are readable properties of T (unknown names are ignored). None requested = all. private static List GetFields(List names) { // When a class hides a base property with "new", keep the most derived one. var properties = new Dictionary(StringComparer.OrdinalIgnoreCase); for (Type t = typeof(T); t != null && t != typeof(object); t = t.BaseType) { foreach (PropertyInfo p in t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { if (p.CanRead && p.GetIndexParameters().Length == 0 && !properties.ContainsKey(p.Name)) properties[p.Name] = p; } } if (names == null || names.Count == 0) return properties.Values.ToList(); var result = new List(); foreach (string name in names.Distinct(StringComparer.OrdinalIgnoreCase)) { PropertyInfo p; if (name != null && properties.TryGetValue(name, out p)) result.Add(p); } return result; } /// The text the grid shows for a value: dates "dd MMM yyyy", decimals "1,234.56". private static string DisplayText(object value) { if (value == null) return string.Empty; if (value is DateTime d) return d.ToString("dd MMM yyyy", CultureInfo.InvariantCulture); if (value is decimal || value is double || value is float) { decimal number = Convert.ToDecimal(value, CultureInfo.InvariantCulture); return number.ToString("N2", CultureInfo.InvariantCulture) + "\u0001" + number.ToString(CultureInfo.InvariantCulture); } if (value is Enum) return value + "\u0001" + Convert.ToInt32(value, CultureInfo.InvariantCulture); return Convert.ToString(value, CultureInfo.InvariantCulture); } private static string CacheKey(VirtualPageRequest request, CurrentUser user, string endpoint) { string filters = string.Join("&", (request.Filters ?? new Dictionary()) .OrderBy(f => f.Key, StringComparer.OrdinalIgnoreCase) .Select(f => f.Key.ToLowerInvariant() + "=" + f.Value)); return endpoint + "|" + user?.UserID + "|" + user?.PayrollTypeID + "|" + filters; } private static string CamelCase(string name) => string.IsNullOrEmpty(name) ? name : char.ToLowerInvariant(name[0]) + name.Substring(1); /// Empty values first; numbers, dates and text compared as such (text ignoring case). private sealed class ValueComparer : IComparer { public static readonly ValueComparer Instance = new ValueComparer(); public int Compare(object x, object y) { bool xEmpty = x == null || (x is string xs && xs.Length == 0); bool yEmpty = y == null || (y is string ys && ys.Length == 0); if (xEmpty || yEmpty) return xEmpty == yEmpty ? 0 : (xEmpty ? -1 : 1); if (x is string a && y is string b) return StringComparer.InvariantCultureIgnoreCase.Compare(a, b); if (IsNumber(x) && IsNumber(y)) return Convert.ToDecimal(x, CultureInfo.InvariantCulture).CompareTo(Convert.ToDecimal(y, CultureInfo.InvariantCulture)); if (x.GetType() == y.GetType() && x is IComparable comparable) return comparable.CompareTo(y); return StringComparer.InvariantCultureIgnoreCase.Compare( Convert.ToString(x, CultureInfo.InvariantCulture), Convert.ToString(y, CultureInfo.InvariantCulture)); } private static bool IsNumber(object v) => v is decimal || v is double || v is float || v is int || v is long || v is short || v is byte; } #endregion } }