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

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-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated 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 DateTime staffUserListUpdateDateTime;
  33:          private static Dictionary<string, string> staffIpDictionary = new Dictionary<string, string>();
  34:          private static DateTime staffIpDictionaryLastClearingTime;
  35:   
  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:                  lock (objectLock)
  48:                  {
  49:                      if (staffList == null || staffList.Count == 0 || StaffUserListUpdateDateTime != Ia.Cl.Model.Identity.UserListTimestamp) staffList = Ia.Ngn.Cl.Model.Data.Staff._List;
  50:   
  51:                      return staffList;
  52:                  }
  53:              }
  54:          }
  55:   
  56:          ////////////////////////////////////////////////////////////////////////////
  57:   
  58:          /// <summary>
  59:          ///
  60:          /// </summary>
  61:          private static List<Ia.Ngn.Cl.Model.Staff> _List
  62:          {
  63:              get
  64:              {
  65:                  int administrativeFrameworkId;
  66:                  Guid userId;
  67:                  List<Ia.Ngn.Cl.Model.Staff> list;
  68:                  List<Ia.Cl.Model.Identity.User> userList;
  69:                  List<Ia.Ngn.Cl.Model.Business.Administration.Framework> frameworkList;
  70:   
  71:                  list = new List<Ia.Ngn.Cl.Model.Staff>(initialStaffListLength);
  72:   
  73:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
  74:                  {
  75:                      userList = Ia.Cl.Model.Identity.UserList;
  76:   
  77:                      frameworkList = Ia.Ngn.Cl.Model.Data.Administration.FrameworkList;
  78:   
  79:                      if (userList != null && userList.Count > 0)
  80:                      {
  81:                          list = (from s in db.Staff select s).ToList();
  82:   
  83:                          if (list.Count > 0)
  84:                          {
  85:                              // below: the order of the multiple foreach loops is imporant
  86:   
  87:                              for (int i = 0; i < list.Count; i++)
  88:                              {
  89:                                  administrativeFrameworkId = list[i].AdministrativeFrameworkId;
  90:                                  userId = list[i].UserId;
  91:   
  92:                                  list[i].User = (from u in userList where userId != Guid.Empty && u.ProviderUserKey == userId select u).SingleOrDefault();
  93:   
  94:                                  list[i].Framework = (from u in frameworkList where u.Id == administrativeFrameworkId select u).SingleOrDefault();
  95:                              }
  96:   
  97:                              for (int i = 0; i < list.Count; i++)
  98:                              {
  99:                                  if (list[i].Framework != null && list[i].Framework.Parent != null)
 100:                                  {
 101:                                      list[i].Head = (from s in list
 102:                                                      where s.IsHead == true && (list[i].IsHead == true && s.AdministrativeFrameworkId == list[i].Framework.Parent.Id || list[i].IsHead == false && s.AdministrativeFrameworkId == list[i].Framework.Id)
 103:                                                      select s).SingleOrDefault();
 104:                                  }
 105:                              }
 106:   
 107:                              for (int i = 0; i < list.Count; i++)
 108:                              {
 109:                                  // below: Subordinates
 110:                                  if (list[i].IsHead)
 111:                                  {
 112:                                      list[i].Subordinates = (from _s in list
 113:                                                              where _s.Id != list[i].Id &&
 114:                                                              (_s.Framework == list[i].Framework && _s.IsHead == false)
 115:                                                              ||
 116:                                                              (
 117:                                                              _s.Framework.Parent != null &&
 118:                                                              (_s.Framework.Parent == list[i].Framework || _s.Framework.Parent.Parent != null &&
 119:                                                               (_s.Framework.Parent.Parent == list[i].Framework || _s.Framework.Parent.Parent.Parent != null &&
 120:                                                                (_s.Framework.Parent.Parent.Parent == list[i].Framework || _s.Framework.Parent.Parent.Parent != null &&
 121:                                                                 (_s.Framework.Parent.Parent.Parent.Parent == list[i].Framework)
 122:                                                                )
 123:                                                               )
 124:                                                              )
 125:                                                              )
 126:   
 127:                                                              select _s).ToList();
 128:                                  }
 129:                                  else list[i].Subordinates = null;
 130:   
 131:                                  // below: Colleagues
 132:                                  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();
 133:   
 134:                                  // below: Heads
 135:                                  if (list[i].Head != null)
 136:                                  {
 137:                                      if (list[i].Head.Head != null)
 138:                                      {
 139:                                          if (list[i].Head.Head.Head != null)
 140:                                          {
 141:                                              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();
 142:                                              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();
 143:                                          }
 144:                                          else list[i].Heads = (from s in list where s == list[i].Head || s == list[i].Head.Head select s).ToList();
 145:                                      }
 146:                                      else list[i].Heads = (from s in list where s == list[i].Head select s).ToList();
 147:                                  }
 148:                                  else list[i].Heads = null;
 149:                              }
 150:                          }
 151:   
 152:                          list = (from s in list select s).OrderByDescending(c => c.IsHead).ThenBy(c => c.AdministrativeFrameworkId).ToList();
 153:   
 154:                          staffUserListUpdateDateTime = Ia.Cl.Model.Identity.UserListTimestamp;
 155:                      }
 156:                      else
 157:                      {
 158:                          throw new ArgumentOutOfRangeException(@"Ia.Ngn.Cl.Model.Data.Staff._List(): userList == null or userList.Count == 0");
 159:                      }
 160:   
 161:                      return list;
 162:                  }
 163:              }
 164:          }
 165:   
 166:          ////////////////////////////////////////////////////////////////////////////
 167:   
 168:          /// <summary>
 169:          ///
 170:          /// </summary>
 171:          public static DateTime StaffUserListUpdateDateTime
 172:          {
 173:              get
 174:              {
 175:                  return staffUserListUpdateDateTime;
 176:              }
 177:          }
 178:   
 179:          ////////////////////////////////////////////////////////////////////////////
 180:   
 181:          /// <summary>
 182:          ///
 183:          /// </summary>
 184:          public static bool Create(Ia.Ngn.Cl.Model.Staff newStaff, out Ia.Cl.Model.Result result)
 185:          {
 186:              bool b;
 187:   
 188:              b = false;
 189:              result = new Ia.Cl.Model.Result();
 190:   
 191:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 192:              {
 193:                  newStaff.Created = newStaff.Updated = DateTime.UtcNow.AddHours(3);
 194:   
 195:                  db.Staff.Add(newStaff);
 196:                  db.SaveChanges();
 197:   
 198:                  b = true;
 199:                  result.AddSuccess("Staff " + newStaff.FirstAndMiddleName + " created.");
 200:   
 201:                  staffList = null;
 202:              }
 203:   
 204:              return b;
 205:          }
 206:   
 207:          ////////////////////////////////////////////////////////////////////////////
 208:   
 209:          /// <summary>
 210:          ///
 211:          /// </summary>
 212:          public static Ia.Ngn.Cl.Model.Staff Read(int id)
 213:          {
 214:              Ia.Ngn.Cl.Model.Staff staff;
 215:   
 216:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 217:              {
 218:                  staff = (from s in db.Staff where s.Id == id select s).SingleOrDefault();
 219:              }
 220:   
 221:              return staff;
 222:          }
 223:   
 224:          ////////////////////////////////////////////////////////////////////////////
 225:   
 226:          /// <summary>
 227:          ///
 228:          /// </summary>
 229:          public static List<Ia.Ngn.Cl.Model.Staff> ReadList()
 230:          {
 231:              List<Ia.Ngn.Cl.Model.Staff> staffList;
 232:   
 233:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 234:              {
 235:                  staffList = (from s in db.Staff select s).ToList();
 236:              }
 237:   
 238:              return staffList.ToList();
 239:          }
 240:   
 241:          ////////////////////////////////////////////////////////////////////////////
 242:   
 243:          /// <summary>
 244:          ///
 245:          /// </summary>
 246:          public static bool Update(Ia.Ngn.Cl.Model.Staff updatedStaff, out Ia.Cl.Model.Result result)
 247:          {
 248:              bool b;
 249:              Ia.Ngn.Cl.Model.Staff staff;
 250:   
 251:              b = false;
 252:              result = new Ia.Cl.Model.Result();
 253:   
 254:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 255:              {
 256:                  staff = (from s in db.Staff where s.Id == updatedStaff.Id select s).SingleOrDefault();
 257:   
 258:                  if (staff.Update(updatedStaff))
 259:                  {
 260:                      db.Staff.Attach(staff);
 261:                      db.Entry(staff).State = System.Data.Entity.EntityState.Modified;
 262:                  }
 263:   
 264:                  db.SaveChanges();
 265:   
 266:                  b = true;
 267:                  result.AddSuccess("Staff " + updatedStaff.FirstAndMiddleName + " updated.");
 268:   
 269:                  staffList = null;
 270:              }
 271:   
 272:              return b;
 273:          }
 274:   
 275:          ////////////////////////////////////////////////////////////////////////////
 276:   
 277:          /// <summary>
 278:          ///
 279:          /// </summary>
 280:          public static bool AssignUserId(int staffId, Guid userId)
 281:          {
 282:              bool b;
 283:              Ia.Ngn.Cl.Model.Staff updatedStaff;
 284:   
 285:              b = false;
 286:   
 287:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 288:              {
 289:                  updatedStaff = (from s in db.Staff where s.Id == staffId select s).SingleOrDefault();
 290:   
 291:                  if (updatedStaff != null)
 292:                  {
 293:                      updatedStaff.UserId = userId;
 294:                      updatedStaff.Updated = DateTime.UtcNow.AddHours(3);
 295:   
 296:                      db.Staff.Attach(updatedStaff);
 297:   
 298:                      var v = db.Entry(updatedStaff);
 299:                      v.State = System.Data.Entity.EntityState.Modified;
 300:                      db.SaveChanges();
 301:   
 302:                      b = true;
 303:   
 304:                      staffList = null;
 305:                  }
 306:                  else
 307:                  {
 308:                      b = false;
 309:                  }
 310:              }
 311:   
 312:              return b;
 313:          }
 314:   
 315:          ////////////////////////////////////////////////////////////////////////////
 316:   
 317:          /// <summary>
 318:          ///
 319:          /// </summary>
 320:          public static bool Delete(int id, out Ia.Cl.Model.Result result)
 321:          {
 322:              bool b;
 323:   
 324:              b = false;
 325:              result = new Ia.Cl.Model.Result();
 326:   
 327:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 328:              {
 329:                  var staff = (from s in db.Staff where s.Id == id select s).FirstOrDefault();
 330:   
 331:                  db.Staff.Remove(staff);
 332:                  db.SaveChanges();
 333:   
 334:                  b = true;
 335:                  result.AddSuccess("Staff " + staff.FirstAndMiddleName + " deleted.");
 336:   
 337:                  staffList = null;
 338:              }
 339:   
 340:              return b;
 341:          }
 342:   
 343:          ////////////////////////////////////////////////////////////////////////////
 344:   
 345:          /// <summary>
 346:          ///
 347:          /// </summary>
 348:          public static Ia.Ngn.Cl.Model.Staff MembershipUser
 349:          {
 350:              get
 351:              {
 352:                  Ia.Ngn.Cl.Model.Staff staffMembershipUser;
 353:   
 354:                  if (HttpContext.Current != null && HttpContext.Current.Session["staffMembershipUser"] != null)
 355:                  {
 356:                      staffMembershipUser = HttpContext.Current.Session["staffMembershipUser"] as Ia.Ngn.Cl.Model.Staff;
 357:                  }
 358:                  else
 359:                  {
 360:                      lock (objectLock)
 361:                      {
 362:                          staffMembershipUser = Ia.Ngn.Cl.Model.Data.Staff._MembershipUser;
 363:   
 364:                          if (HttpContext.Current != null) HttpContext.Current.Session["staffMembershipUser"] = staffMembershipUser;
 365:                      }
 366:                  }
 367:   
 368:                  if (staffMembershipUser != null)
 369:                  {
 370:                      if (!staffIpDictionary.ContainsKey(staffMembershipUser.UserId.ToString()))
 371:                      {
 372:                          staffIpDictionary[staffMembershipUser.UserId.ToString()] = Ia.Cl.Model.Default.RequestUserIP();
 373:                      }
 374:                  }
 375:   
 376:                  return staffMembershipUser;
 377:              }
 378:          }
 379:   
 380:          ////////////////////////////////////////////////////////////////////////////
 381:   
 382:          /// <summary>
 383:          ///
 384:          /// </summary>
 385:          public static void ClearStaffMembershipUserSession()
 386:          {
 387:              if (HttpContext.Current != null && HttpContext.Current.Session["staffMembershipUser"] != null)
 388:              {
 389:                  lock (objectLock)
 390:                  {
 391:                      HttpContext.Current.Session.Remove("staffMembershipUser");
 392:                  }
 393:              }
 394:          }
 395:   
 396:          ////////////////////////////////////////////////////////////////////////////
 397:   
 398:          /// <summary>
 399:          ///
 400:          /// </summary>
 401:          private static Ia.Ngn.Cl.Model.Staff _MembershipUser
 402:          {
 403:              get
 404:              {
 405:                  Ia.Ngn.Cl.Model.Staff staffMembershipUser;
 406:   
 407:                  if (Membership.GetUser() != null)
 408:                  {
 409:                      if (Guid.TryParse(Membership.GetUser().ProviderUserKey.ToString(), out Guid guid))
 410:                      {
 411:                          staffMembershipUser = (from s in Ia.Ngn.Cl.Model.Data.Staff.List where s.User != null && s.UserId == guid select s).SingleOrDefault();
 412:                      }
 413:                      else staffMembershipUser = null;
 414:                  }
 415:                  else staffMembershipUser = null;
 416:   
 417:                  return staffMembershipUser;
 418:              }
 419:          }
 420:   
 421:          ////////////////////////////////////////////////////////////////////////////
 422:   
 423:          /// <summary>
 424:          ///
 425:          /// </summary>
 426:          public static Ia.Ngn.Cl.Model.Staff MembershipUser2(Guid userId)
 427:          {
 428:              Ia.Ngn.Cl.Model.Staff staffMembershipUser;
 429:   
 430:              if (userId != Guid.Empty)
 431:              {
 432:                  staffMembershipUser = (from s in Ia.Ngn.Cl.Model.Data.Staff.List where s.UserId == userId select s).SingleOrDefault();
 433:              }
 434:              else staffMembershipUser = null;
 435:   
 436:              return staffMembershipUser;
 437:          }
 438:   
 439:          ////////////////////////////////////////////////////////////////////////////
 440:   
 441:          /// <summary>
 442:          ///
 443:          /// </summary>
 444:          public static bool NullifyUserId(int staffId)
 445:          {
 446:              bool b;
 447:              Ia.Ngn.Cl.Model.Staff staff;
 448:   
 449:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 450:              {
 451:                  staff = (from s in db.Staff where s.Id == staffId select s).SingleOrDefault();
 452:   
 453:                  staff.UserId = Guid.Empty;
 454:   
 455:                  db.Staff.Attach(staff);
 456:                  db.Entry(staff).Property(x => x.UserId).IsModified = true;
 457:   
 458:                  db.SaveChanges();
 459:   
 460:                  b = true;
 461:   
 462:                  staffList = null;
 463:              }
 464:   
 465:              return b;
 466:          }
 467:   
 468:          ////////////////////////////////////////////////////////////////////////////
 469:   
 470:          /// <summary>
 471:          ///
 472:          /// </summary>
 473:          public static void UnlockAllMembershipUsers()
 474:          {
 475:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 476:              {
 477:                  db.Database.ExecuteSqlCommand("update Memberships set IsLockedOut = 0;");
 478:              }
 479:          }
 480:   
 481:          ////////////////////////////////////////////////////////////////////////////
 482:   
 483:          /// <summary>
 484:          ///
 485:          /// </summary>
 486:          public static void SetMembershipIsApprovedToFalseIfUserLastActivityDateIsLessThanDateTime(DateTime dateTime)
 487:          {
 488:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 489:              {
 490:                  db.Database.ExecuteSqlCommand("update Memberships set IsApproved = 0 where UserId in (select u.UserId from Memberships as m inner join Users u on m.UserId = u.UserId where u.LastActivityDate < '" + dateTime.ToString("yyyy-MM-dd") + "'); ");
 491:              }
 492:          }
 493:   
 494:          ////////////////////////////////////////////////////////////////////////////
 495:   
 496:          /// <summary>
 497:          ///
 498:          /// </summary>
 499:          public static void RemoveInactiveStaff(DateTime dateTime)
 500:          {
 501:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 502:              {
 503:                  var v = Membership.GetAllUsers();
 504:                  var list = Ia.Cl.Model.Identity.InactiveSinceDateTimeUserList(dateTime);
 505:   
 506:                  //db.Database.ExecuteSqlCommand("update Memberships set IsLockedOut = 0;");
 507:              }
 508:          }
 509:   
 510:          ////////////////////////////////////////////////////////////////////////////
 511:          ////////////////////////////////////////////////////////////////////////////
 512:   
 513:          /// <summary>
 514:          ///
 515:          /// </summary>
 516:          public static Dictionary<string, string> StaffIpDictionary
 517:          {
 518:              get
 519:              {
 520:                  DateTime now;
 521:   
 522:                  now = DateTime.UtcNow.AddHours(3);
 523:   
 524:                  if (now.AddDays(-1) > staffIpDictionaryLastClearingTime)
 525:                  {
 526:                      staffIpDictionary.Clear();
 527:                      staffIpDictionaryLastClearingTime = now;
 528:                  }
 529:   
 530:                  return staffIpDictionary;
 531:              }
 532:          }
 533:   
 534:          ////////////////////////////////////////////////////////////////////////////
 535:          ////////////////////////////////////////////////////////////////////////////
 536:      }
 537:   
 538:      ////////////////////////////////////////////////////////////////////////////
 539:      ////////////////////////////////////////////////////////////////////////////
 540:  }