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