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

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