شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

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