using System; using System.Collections.Generic; using System.Linq; using System.Text; using Disco.Models.Repository; using System.ComponentModel.DataAnnotations; namespace Disco.BI.Extensions { public static class JobFlagExtensions { private static Dictionary> allFlags; private static void CacheAllFlags() { if (allFlags == null) { var fType = typeof(Job.UserManagementFlags); var fMembers = fType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static); var flags = new Dictionary>(); foreach (var f in fMembers) { DisplayAttribute display = (DisplayAttribute)(f.GetCustomAttributes(typeof(DisplayAttribute), false)[0]); string gn = display.GroupName; Dictionary g; if (!flags.TryGetValue(gn, out g)) { g = new Dictionary(); flags.Add(gn, g); } g[(long)f.GetRawConstantValue()] = display.Name; } allFlags = flags; } } public static Dictionary>> ValidFlagsGrouped(this Job j) { Dictionary>> validFlags = new Dictionary>>(); CacheAllFlags(); var currentFlags = (long)(j.Flags ?? 0); foreach (var jt in j.JobSubTypes) { Dictionary g; if (allFlags.TryGetValue(jt.Id, out g)) { validFlags[jt.Id] = g.Select(f => new Tuple(f.Key, f.Value, ((currentFlags & f.Key) == f.Key))).ToList(); } else { validFlags[jt.Id] = null; } } return validFlags; } public static Dictionary> ValidFlags(this Job j) { Dictionary> validFlags = new Dictionary>(); CacheAllFlags(); var currentFlags = (long)(j.Flags ?? 0); foreach (var jt in j.JobSubTypes) { Dictionary g; if (allFlags.TryGetValue(jt.Id, out g)) { foreach (var f in g) validFlags[f.Key] = new Tuple(string.Format("{0}: {1}", jt.Description, f.Value), ((currentFlags & f.Key) == f.Key)); } } return validFlags; } } }