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

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