Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Staff

Public general use code classes and xml files that we've compiled and used over the years:

Staff support class for Next Generation Network (NGN) data model.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Xml.Linq;
   5:  using System.Web;
   6:  using System.Web.Security;
   7:  using System.ComponentModel.DataAnnotations.Schema;
   8:   
   9:  namespace Ia.Ngn.Cl.Model.Data
  10:  {
  11:      ////////////////////////////////////////////////////////////////////////////
  12:   
  13:      /// <summary publish="true">
  14:      /// Staff support class for Next Generation Network (NGN) data model.
  15:      /// </summary>
  16:      /// 
  17:      /// <remarks> 
  18:      /// Copyright © 2006-2019 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  19:      ///
  20:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  21:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  22:      ///
  23:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  24:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  25:      /// 
  26:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  27:      /// 
  28:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  29:      /// </remarks> 
  30:      public partial class Staff
  31:      {
  32:          private const int initialStaffListLength = 200;
  33:          private static List<Ia.Ngn.Cl.Model.Staff> staffList;
  34:          private static Dictionary<string, string> staffIpDictionary = new Dictionary<string, string>();
  35:          private static object objectLock = new object();
  36:   
  37:          ////////////////////////////////////////////////////////////////////////////
  38:   
  39:          /// <summary>
  40:          ///
  41:          /// </summary>
  42:          public static List<Ia.Ngn.Cl.Model.Staff> List
  43:          {
  44:              get
  45:              {
  46:                  if (staffList == null || staffList.Count == 0)
  47:                  {
  48:                      if (HttpContext.Current != null && HttpContext.Current.Application["staffList"] != null)
  49:                      {
  50:                          staffList = HttpContext.Current.Application["staffList"] as List<Ia.Ngn.Cl.Model.Staff>;
  51:                          //staffList = (List<Ia.Ngn.Cl.Model.Staff>)HttpContext.Current.Application["staffList"];
  52:                      }
  53:                      else
  54:                      {
  55:                          staffList = Ia.Ngn.Cl.Model.Data.Staff._List;
  56:   
  57:                          if (HttpContext.Current != null) HttpContext.Current.Application["staffList"] = staffList;
  58:                      }
  59:                  }
  60:   
  61:                  return staffList;
  62:              }
  63:          }
  64:   
  65:          ////////////////////////////////////////////////////////////////////////////
  66:   
  67:          /// <summary>
  68:          ///
  69:          /// </summary>
  70:          public static void RefreshStaffList()
  71:          {
  72:              staffList = null;
  73:          }
  74:   
  75:          ////////////////////////////////////////////////////////////////////////////
  76:   
  77:          /// <summary>
  78:          ///
  79:          /// </summary>
  80:          public static List<Ia.Ngn.Cl.Model.Staff> _List
  81:          {
  82:              get
  83:              {
  84:                  int administrativeFrameworkId;
  85:                  Guid userId;
  86:                  List<Ia.Ngn.Cl.Model.Staff> list;
  87:                  List<Ia.Cl.Model.Identity.User> userList;
  88:                  List<Ia.Ngn.Cl.Model.Data.Administration.Framework> frameworkList;
  89:   
  90:                  list = new List<Ia.Ngn.Cl.Model.Staff>(initialStaffListLength);
  91:   
  92:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
  93:                  {
  94:                      lock (objectLock) userList = Ia.Cl.Model.Identity.UserList;
  95:   
  96:                      lock (objectLock) frameworkList = Ia.Ngn.Cl.Model.Data.Administration.FrameworkList;
  97:   
  98:                      if (userList != null && userList.Count > 0)
  99:                      {
 100:                          list = (from s in db.Staff select s).ToList();
 101:   
 102:                          if (list.Count > 0)
 103:                          {
 104:                              // below: the order of the multiple foreach loops is imporant
 105:   
 106:                              for (int i = 0; i < list.Count; i++)
 107:                              {
 108:                                  administrativeFrameworkId = list[i].AdministrativeFrameworkId;
 109:                                  userId = list[i].UserId;
 110:   
 111:                                  try
 112:                                  {
 113:                                      list[i].User = (from u in userList where userId != Guid.Empty && u.ProviderUserKey == userId select u).SingleOrDefault();
 114:                                  }
 115:                                  catch (Exception ex)
 116:                                  {
 117:                                      string s;
 118:   
 119:                                      s = string.Empty;
 120:   
 121:                                      foreach (var u in userList)
 122:                                      {
 123:                                          s += "[" + u.Email + ":" + u.ProviderUserKey + ":" + u.UserName + "]";
 124:                                      }
 125:   
 126:                                      throw new Exception("Exception: " + ex.ToString() + ":[" + s + "]");
 127:                                  }
 128:   
 129:                                  lock (objectLock)
 130:                                  {
 131:                                      list[i].Framework = (from u in frameworkList where u.Id == administrativeFrameworkId select u).SingleOrDefault();
 132:                                  }
 133:                              }
 134:   
 135:                              for (int i = 0; i < list.Count; i++)
 136:                              {
 137:                                  if (list[i].Framework != null && list[i].Framework.Parent != null)
 138:                                  {
 139:                                      list[i].Head = (from s in list
 140:                                                      where s.IsHead == true && (
 141:                                                      list[i].IsHead == true && s.AdministrativeFrameworkId == list[i].Framework.Parent.Id
 142:                                                      || list[i].IsHead == false && s.AdministrativeFrameworkId == list[i].Framework.Id)
 143:                                                      select s).SingleOrDefault();
 144:                                  }
 145:                              }
 146:   
 147:                              for (int i = 0; i < list.Count; i++)
 148:                              {
 149:                                  // below: Subordinates
 150:                                  if (list[i].IsHead)
 151:                                  {
 152:                                      try
 153:                                      {
 154:                                          list[i].Subordinates = (from _s in list
 155:                                                                  where _s.Id != list[i].Id &&
 156:                                                                  (_s.Framework == list[i].Framework && _s.IsHead == false)
 157:                                                                  ||
 158:                                                                  (
 159:                                                                  _s.Framework.Parent != null &&
 160:                                                                  (_s.Framework.Parent == list[i].Framework || _s.Framework.Parent.Parent != null &&
 161:                                                                   (_s.Framework.Parent.Parent == list[i].Framework || _s.Framework.Parent.Parent.Parent != null &&
 162:                                                                    (_s.Framework.Parent.Parent.Parent == list[i].Framework || _s.Framework.Parent.Parent.Parent != null &&
 163:                                                                     (_s.Framework.Parent.Parent.Parent.Parent == list[i].Framework)
 164:                                                                    )
 165:                                                                   )
 166:                                                                  )
 167:                                                                  )
 168:   
 169:                                                                  select _s).ToList();
 170:                                      }
 171:                                      catch (Exception ex)
 172:                                      {
 173:                                          list[i].FirstName = "Exception: _staffList[i].Subordinates = [" + ex.ToString() + "]";
 174:                                      }
 175:                                  }
 176:                                  else list[i].Subordinates = null;
 177:   
 178:                                  // below: Colleagues
 179:                                  list[i].Colleagues = (from s in list where s.Id != list[i].Id && s.Head != null && list[i].Head != null && s.Head == list[i].Head select s).ToList();
 180:   
 181:                                  // below: Heads
 182:                                  if (list[i].Head != null)
 183:                                  {
 184:                                      if (list[i].Head.Head != null)
 185:                                      {
 186:                                          if (list[i].Head.Head.Head != null)
 187:                                          {
 188:                                              if (list[i].Head.Head.Head.Head != null) list[i].Heads = (from s in list where s == list[i].Head || s == list[i].Head.Head || s == list[i].Head.Head.Head || s == list[i].Head.Head.Head.Head select s).ToList();
 189:                                              else list[i].Heads = (from s in list where s == list[i].Head || s == list[i].Head.Head || s == list[i].Head.Head.Head select s).ToList();
 190:                                          }
 191:                                          else list[i].Heads = (from s in list where s == list[i].Head || s == list[i].Head.Head select s).ToList();
 192:                                      }
 193:                                      else list[i].Heads = (from s in list where s == list[i].Head select s).ToList();
 194:                                  }
 195:                                  else list[i].Heads = null;
 196:                              }
 197:                          }
 198:   
 199:                          list = (from s in list select s).OrderByDescending(c => c.IsHead).ThenBy(c => c.AdministrativeFrameworkId).ToList();
 200:                      }
 201:                      else
 202:                      {
 203:                          throw new ArgumentOutOfRangeException(@"Ia.Ngn.Cl.Model.Data.Staff._List(): userList == null || userList.Count == 0");
 204:                      }
 205:   
 206:                      return list.ToList();
 207:                  }
 208:              }
 209:          }
 210:   
 211:          ////////////////////////////////////////////////////////////////////////////
 212:   
 213:          /// <summary>
 214:          ///
 215:          /// </summary>
 216:          public static Ia.Ngn.Cl.Model.Staff MembershipUser
 217:          {
 218:              get
 219:              {
 220:                  Ia.Ngn.Cl.Model.Staff staffMembershipUser;
 221:   
 222:                  if (HttpContext.Current != null && HttpContext.Current.Session["staffMembershipUser"] != null)
 223:                  {
 224:                      staffMembershipUser = (Ia.Ngn.Cl.Model.Staff)HttpContext.Current.Session["staffMembershipUser"];
 225:                  }
 226:                  else
 227:                  {
 228:                      staffMembershipUser = Ia.Ngn.Cl.Model.Data.Staff._MembershipUser;
 229:   
 230:                      if (HttpContext.Current != null) HttpContext.Current.Session["staffMembershipUser"] = staffMembershipUser;
 231:                  }
 232:   
 233:                  if (staffMembershipUser != null)
 234:                  {
 235:                      if (!staffIpDictionary.ContainsKey(staffMembershipUser.UserId.ToString()))
 236:                      {
 237:                          staffIpDictionary[staffMembershipUser.UserId.ToString()] = Ia.Cl.Model.Default.RequestUserIP();
 238:                      }
 239:                  }
 240:   
 241:                  return staffMembershipUser;
 242:              }
 243:          }
 244:   
 245:          ////////////////////////////////////////////////////////////////////////////
 246:   
 247:          /// <summary>
 248:          ///
 249:          /// </summary>
 250:          public static void ClearStaffMembershipUserSession()
 251:          {
 252:              if (HttpContext.Current != null && HttpContext.Current.Session["staffMembershipUser"] != null)
 253:              {
 254:                  HttpContext.Current.Session.Remove("staffMembershipUser");
 255:              }
 256:          }
 257:   
 258:          ////////////////////////////////////////////////////////////////////////////
 259:   
 260:          /// <summary>
 261:          ///
 262:          /// </summary>
 263:          private static Ia.Ngn.Cl.Model.Staff _MembershipUser
 264:          {
 265:              get
 266:              {
 267:                  Ia.Ngn.Cl.Model.Staff staffMembershipUser;
 268:   
 269:                  if (Membership.GetUser() != null)
 270:                  {
 271:                      if (Guid.TryParse(Membership.GetUser().ProviderUserKey.ToString(), out Guid guid))
 272:                      {
 273:                          staffMembershipUser = (from s in Ia.Ngn.Cl.Model.Data.Staff.List where s.User != null && s.UserId == guid select s).SingleOrDefault();
 274:                      }
 275:                      else staffMembershipUser = null;
 276:                  }
 277:                  else staffMembershipUser = null;
 278:   
 279:                  return staffMembershipUser;
 280:              }
 281:          }
 282:   
 283:          ////////////////////////////////////////////////////////////////////////////
 284:   
 285:          /// <summary>
 286:          ///
 287:          /// </summary>
 288:          public static Ia.Ngn.Cl.Model.Staff MembershipUser2(Guid userId)
 289:          {
 290:              Ia.Ngn.Cl.Model.Staff staffMembershipUser;
 291:   
 292:              if (userId != Guid.Empty)
 293:              {
 294:                  staffMembershipUser = (from s in Ia.Ngn.Cl.Model.Data.Staff.List where s.UserId == userId select s).SingleOrDefault();
 295:              }
 296:              else staffMembershipUser = null;
 297:   
 298:              return staffMembershipUser;
 299:          }
 300:   
 301:          ////////////////////////////////////////////////////////////////////////////
 302:   
 303:          /// <summary>
 304:          ///
 305:          /// </summary>
 306:          public static bool NullifyUserId(int staffId)
 307:          {
 308:              bool b;
 309:              Ia.Ngn.Cl.Model.Staff staff;
 310:   
 311:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 312:              {
 313:                  staff = (from s in db.Staff where s.Id == staffId select s).SingleOrDefault();
 314:   
 315:                  staff.UserId = Guid.Empty;
 316:   
 317:                  db.Staff.Attach(staff);
 318:                  db.Entry(staff).Property(x => x.UserId).IsModified = true;
 319:   
 320:                  db.SaveChanges();
 321:   
 322:                  b = true;
 323:              }
 324:   
 325:              return b;
 326:          }
 327:   
 328:          ////////////////////////////////////////////////////////////////////////////
 329:   
 330:          /// <summary>
 331:          ///
 332:          /// </summary>
 333:          public static void UnlockAllMembershipUsers()
 334:          {
 335:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 336:              {
 337:                  db.Database.ExecuteSqlCommand("update Memberships set IsLockedOut = 0;");
 338:              }
 339:          }
 340:   
 341:          ////////////////////////////////////////////////////////////////////////////
 342:   
 343:          /// <summary>
 344:          ///
 345:          /// </summary>
 346:          public static void RemoveInactiveStaff(DateTime dateTime)
 347:          {
 348:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 349:              {
 350:                  var v = Membership.GetAllUsers();
 351:                  var list = Ia.Cl.Model.Identity.InactiveSinceDateTimeUserList(dateTime);
 352:   
 353:                  //db.Database.ExecuteSqlCommand("update Memberships set IsLockedOut = 0;");
 354:              }
 355:          }
 356:   
 357:          ////////////////////////////////////////////////////////////////////////////
 358:          ////////////////////////////////////////////////////////////////////////////
 359:   
 360:          /// <summary>
 361:          ///
 362:          /// </summary>
 363:          public static Dictionary<string, string> StaffIpDictionary
 364:          {
 365:              get
 366:              {
 367:                  if (staffIpDictionary == null || staffIpDictionary.Count == 0)
 368:                  {
 369:                      if (HttpContext.Current != null && HttpContext.Current.Application["staffIpDictionary"] != null)
 370:                      {
 371:                          staffIpDictionary = HttpContext.Current.Application["staffIpDictionary"] as Dictionary<string, string>;
 372:                      }
 373:                      else
 374:                      {
 375:                          staffIpDictionary = Ia.Ngn.Cl.Model.Data.Staff._StaffIpDictionary;
 376:   
 377:                          if (HttpContext.Current != null) HttpContext.Current.Application["staffIpDictionary"] = staffIpDictionary;
 378:                      }
 379:                  }
 380:   
 381:                  return staffIpDictionary;
 382:              }
 383:   
 384:              set
 385:              {
 386:                  staffIpDictionary = value;
 387:              }
 388:          }
 389:   
 390:          ////////////////////////////////////////////////////////////////////////////
 391:   
 392:          /// <summary>
 393:          ///
 394:          /// </summary>
 395:          public static Dictionary<string, string> _StaffIpDictionary
 396:          {
 397:              get
 398:              {
 399:                  return staffIpDictionary;
 400:              }
 401:          }
 402:   
 403:          ////////////////////////////////////////////////////////////////////////////
 404:   
 405:          /// <summary>
 406:          ///
 407:          /// </summary>
 408:          public static void ClearStaffIpDictionaryApplication()
 409:          {
 410:              /*
 411:              if (HttpContext.Current != null && HttpContext.Current.Application["staffIpDictionary"] != null)
 412:              {
 413:                  HttpContext.Current.Application.Remove("staffIpDictionary");
 414:              }
 415:              */
 416:   
 417:              staffIpDictionary = new Dictionary<string, string>();
 418:          }
 419:   
 420:          ////////////////////////////////////////////////////////////////////////////
 421:          ////////////////////////////////////////////////////////////////////////////
 422:      }
 423:   
 424:      ////////////////////////////////////////////////////////////////////////////
 425:      ////////////////////////////////////////////////////////////////////////////
 426:  }