)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » ReportHistory (Ia.Ftn.Cl.Models.Data)

Public general use code classes and xml files that we've compiled and used over the years:

Report History support class for Fixed Telecommunications Network (FTN) data model.

    1: using Microsoft.EntityFrameworkCore;
    2: using System;
    3: using System.Collections.Generic;
    4: using System.Data;
    5: using System.Linq;
    6:  
    7: namespace Ia.Ftn.Cl.Models.Data
    8: {
    9:     ////////////////////////////////////////////////////////////////////////////
   10:  
   11:     /// <summary publish="true">
   12:     /// Report History support class for Fixed Telecommunications Network (FTN) data model.
   13:     /// </summary>
   14:     /// 
   15:     /// <remarks> 
   16:     /// Copyright © 2006-2024 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   17:     /// </remarks> 
   18:     public class ReportHistory
   19:     {
   20:         private static Dictionary<string, List<int>> neglectedAndResolvedReportHistoryDictionary;
   21:  
   22:         private static readonly object objectLock = new object();
   23:  
   24:         ////////////////////////////////////////////////////////////////////////////
   25:  
   26:         /// <summary>
   27:         ///
   28:         /// </summary>
   29:         public ReportHistory() { }
   30:  
   31:         ////////////////////////////////////////////////////////////////////////////
   32:  
   33:         /// <summary>
   34:         ///
   35:         /// </summary>
   36:         public static bool Create(Ia.Ftn.Cl.Models.ReportHistory reportHistory, out string result)
   37:         {
   38:             bool b;
   39:  
   40:             b = false;
   41:             result = string.Empty;
   42:  
   43:             using (var db = new Ia.Ftn.Cl.Db())
   44:             {
   45:                 reportHistory.Report = (from r in db.Reports where r.Id == reportHistory.Report.Id select r).SingleOrDefault();
   46:  
   47:                 reportHistory.StaffIdentityUser = (from s in db.StaffIdentityUsers where s.Id == reportHistory.StaffIdentityUser.Id select s).SingleOrDefault();
   48:  
   49:                 reportHistory.Created = reportHistory.Updated = DateTime.UtcNow.AddHours(3);
   50:  
   51:                 db.ReportHistories.Add(reportHistory);
   52:                 db.SaveChanges();
   53:  
   54:                 DbContextProbablyUpdated();
   55:  
   56:                 b = true;
   57:             }
   58:  
   59:             return b;
   60:         }
   61:  
   62:         ////////////////////////////////////////////////////////////////////////////
   63:  
   64:         /// <summary>
   65:         ///
   66:         /// </summary>
   67:         public static Ia.Ftn.Cl.Models.ReportHistory Read(int reportHistoryId)
   68:         {
   69:             Ia.Ftn.Cl.Models.ReportHistory reportHistory;
   70:  
   71:             using (var db = new Ia.Ftn.Cl.Db())
   72:             {
   73:                 reportHistory = (from rh in db.ReportHistories
   74:                                  where rh.Id == reportHistoryId
   75:                                  select rh).Include(u => u.StaffIdentityUser).Include(u => u.Report).ThenInclude(u => u.ReportHistories).SingleOrDefault();
   76:             }
   77:  
   78:             return reportHistory;
   79:         }
   80:  
   81:         ////////////////////////////////////////////////////////////////////////////
   82:  
   83:         /// <summary>
   84:         ///
   85:         /// </summary>
   86:         public static List<Ia.Ftn.Cl.Models.ReportHistory> ListForReportId(int reportId)
   87:         {
   88:             List<Ia.Ftn.Cl.Models.ReportHistory> reportHistoryList;
   89:  
   90:             using (var db = new Ia.Ftn.Cl.Db())
   91:             {
   92:                 reportHistoryList = (from rh in db.ReportHistories
   93:                                      where rh.Report.Id == reportId
   94:                                      select rh).Include(u => u.StaffIdentityUser).Include(u => u.Report).ThenInclude(u => u.ReportHistories).ToList();
   95:             }
   96:  
   97:             return reportHistoryList;
   98:         }
   99:  
  100:         ////////////////////////////////////////////////////////////////////////////
  101:  
  102:         /// <summary>
  103:         ///
  104:         /// </summary>
  105:         public static Dictionary<string, List<int>> DivisionUserIdToNeglectedAndResolvedReportHistoryCountDictionary()
  106:         {
  107:             if (neglectedAndResolvedReportHistoryDictionary == null || neglectedAndResolvedReportHistoryDictionary.Count == 0)
  108:             {
  109:                 lock (objectLock)
  110:                 {
  111:                     neglectedAndResolvedReportHistoryDictionary = Ia.Ftn.Cl.Models.Data.ReportHistory._NeglectedAndResolvedReportHistoryDictionary();
  112:                 }
  113:             }
  114:  
  115:             return neglectedAndResolvedReportHistoryDictionary;
  116:         }
  117:  
  118:         ////////////////////////////////////////////////////////////////////////////
  119:  
  120:         /// <summary>
  121:         ///
  122:         /// </summary>
  123:         public class ReportHistoryIdResolutionReportIdStaffIdentityUserId
  124:         {
  125:             /// <summary/>
  126:             public int Id;
  127:  
  128:             /// <summary/>
  129:             public int Resolution;
  130:  
  131:             /// <summary/>
  132:             public int ReportId;
  133:  
  134:             /// <summary/>
  135:             public string StaffIdentityUserId;
  136:         }
  137:  
  138:         ////////////////////////////////////////////////////////////////////////////
  139:  
  140:         /// <summary>
  141:         ///
  142:         /// </summary>
  143:         private static Dictionary<string, List<int>> _NeglectedAndResolvedReportHistoryDictionary()
  144:         {
  145:             bool nextIsNegligence, nextIsResolved;
  146:             // id="1026" name="Negligence" arabicName="إهمال"
  147:  
  148:             var negligenceDictionary = new Dictionary<string, int>();
  149:             var resolvedDictionary = new Dictionary<string, int>();
  150:  
  151:             using (var db = new Ia.Ftn.Cl.Db())
  152:             {
  153:                 var list0 = (from rh in db.ReportHistories
  154:                              select new ReportHistoryIdResolutionReportIdStaffIdentityUserId
  155:                              {
  156:                                  Id = rh.Id,
  157:                                  Resolution = rh.Resolution,
  158:                                  ReportId = rh.Report.Id,
  159:                                  StaffIdentityUserId = rh.StaffIdentityUser.Id,
  160:                              }).ToList();
  161:  
  162:                 var dic0 = new Dictionary<int, List<ReportHistoryIdResolutionReportIdStaffIdentityUserId>>();
  163:  
  164:                 foreach (var l in list0)
  165:                 {
  166:                     if (!dic0.ContainsKey(l.ReportId)) dic0[l.ReportId] = new List<ReportHistoryIdResolutionReportIdStaffIdentityUserId>();
  167:  
  168:                     dic0[l.ReportId].Add(l);
  169:                 }
  170:  
  171:                 foreach (var kvp in dic0)
  172:                 {
  173:                     nextIsNegligence = nextIsResolved = false;
  174:                     foreach (var v in kvp.Value.OrderByDescending(u => u.Id))
  175:                     {
  176:                         if (nextIsNegligence)
  177:                         {
  178:                             if (!negligenceDictionary.ContainsKey(v.StaffIdentityUserId)) negligenceDictionary[v.StaffIdentityUserId] = 0;
  179:  
  180:                             negligenceDictionary[v.StaffIdentityUserId]++;
  181:  
  182:                             nextIsNegligence = false;
  183:                         }
  184:                         else if (nextIsResolved)
  185:                         {
  186:                             if (!resolvedDictionary.ContainsKey(v.StaffIdentityUserId)) resolvedDictionary[v.StaffIdentityUserId] = 0;
  187:  
  188:                             resolvedDictionary[v.StaffIdentityUserId]++;
  189:  
  190:                             nextIsResolved = false;
  191:                         }
  192:                         else if (v.Resolution == 1026) nextIsNegligence = true;
  193:                         else nextIsResolved = true;
  194:                     }
  195:                 }
  196:             }
  197:  
  198:             var dictionary = new Dictionary<string, List<int>>();
  199:  
  200:             foreach (var k in resolvedDictionary.Keys.Union(negligenceDictionary.Keys))
  201:             {
  202:                 if (!dictionary.ContainsKey(k))
  203:                 {
  204:                     dictionary[k] = new List<int>();
  205:  
  206:                     dictionary[k].Add(negligenceDictionary.ContainsKey(k) ? negligenceDictionary[k] : 0);
  207:                     dictionary[k].Add(resolvedDictionary.ContainsKey(k) ? resolvedDictionary[k] : 0);
  208:                 }
  209:             }
  210:  
  211:             return dictionary;
  212:         }
  213:  
  214:         ////////////////////////////////////////////////////////////////////////////
  215:  
  216:         /// <summary>
  217:         ///
  218:         /// </summary>
  219:         public static bool UpdateMigratedList(List<Ia.Ftn.Cl.Models.ReportHistory> reportHistoryList, out string result)
  220:         {
  221:             bool b;
  222:             Ia.Ftn.Cl.Models.ReportHistory reportHistory;
  223:  
  224:             b = false;
  225:             result = string.Empty;
  226:  
  227:             using (var db = new Ia.Ftn.Cl.Db())
  228:             {
  229:                 foreach (Ia.Ftn.Cl.Models.ReportHistory updatedReportHistory in reportHistoryList)
  230:                 {
  231:                     reportHistory = (from rh in db.ReportHistories
  232:                                      where rh.Id == updatedReportHistory.Id
  233:                                      select rh).Include(u => u.StaffIdentityUser).Include(u => u.Report).ThenInclude(u => u.ReportHistories).SingleOrDefault();
  234:  
  235:                     if (reportHistory == null)
  236:                     {
  237:                         //updatedReport.Created = updatedReport.Updated = DateTime.UtcNow.AddHours(3);
  238:  
  239:                         db.ReportHistories.Add(updatedReportHistory);
  240:                     }
  241:                     else
  242:                     {
  243:                         // below: copy values from updatedReport to report
  244:  
  245:                         reportHistory.UpdateMigrated(updatedReportHistory);
  246:  
  247:                         db.ReportHistories.Attach(reportHistory);
  248:  
  249:                         db.Entry(reportHistory).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  250:                     }
  251:  
  252:                     b = true;
  253:                 }
  254:  
  255:                 db.SaveChanges();
  256:  
  257:                 DbContextProbablyUpdated();
  258:  
  259:                 b = true;
  260:             }
  261:  
  262:             return b;
  263:         }
  264:  
  265:         ////////////////////////////////////////////////////////////////////////////
  266:  
  267:         /// <summary>
  268:         ///
  269:         /// </summary>
  270:         public static bool NullifyUserId(string userId, out int numberOfRecordsUpdated)
  271:         {
  272:             return MoveUserId(userId, string.Empty, out numberOfRecordsUpdated);
  273:         }
  274:  
  275:         ////////////////////////////////////////////////////////////////////////////
  276:  
  277:         /// <summary>
  278:         ///
  279:         /// </summary>
  280:         public static bool MoveUserId(string fromUserId, string toUserId, out int numberOfRecordsUpdated)
  281:         {
  282:             bool b;
  283:  
  284:             b = false;
  285:             numberOfRecordsUpdated = 0;
  286:  
  287:             using (var db = new Ia.Ftn.Cl.Db())
  288:             {
  289:                 var query = (from rh in db.ReportHistories where rh.StaffIdentityUser.Id == fromUserId select rh).ToList();
  290:  
  291:                 foreach (var v in query)
  292:                 {
  293:                     v.StaffIdentityUser.Id = toUserId;
  294:                     numberOfRecordsUpdated++;
  295:                 }
  296:  
  297:                 db.SaveChanges();
  298:  
  299:                 DbContextProbablyUpdated();
  300:  
  301:                 b = true;
  302:             }
  303:  
  304:             return b;
  305:         }
  306:  
  307:         ////////////////////////////////////////////////////////////////////////////
  308:  
  309:         /// <summary>
  310:         ///
  311:         /// </summary>
  312:         public static bool Delete(int id/*, out string result*/)
  313:         {
  314:             bool b;
  315:  
  316:             b = false;
  317:             //result = string.Empty;
  318:  
  319:             using (var db = new Ia.Ftn.Cl.Db())
  320:             {
  321:                 var v = (from rh in db.ReportHistories where rh.Id == id select rh).FirstOrDefault();
  322:  
  323:                 db.ReportHistories.Remove(v);
  324:                 db.SaveChanges();
  325:  
  326:                 DbContextProbablyUpdated();
  327:  
  328:                 b = true;
  329:             }
  330:  
  331:             return b;
  332:         }
  333:  
  334:         ////////////////////////////////////////////////////////////////////////////
  335:  
  336:         /// <summary>
  337:         ///
  338:         /// </summary>
  339:         public static bool ComplainantNotified(Ia.Ftn.Cl.Models.ReportHistory reportHistory)
  340:         {
  341:             var b = false;
  342:  
  343:             using (var db = new Ia.Ftn.Cl.Db())
  344:             {
  345:                 reportHistory.ComplainantNotified = true;
  346:  
  347:                 reportHistory.Updated = DateTime.UtcNow.AddHours(3);
  348:                 //this.StaffIdentityUser.Id = staff.Id;
  349:                 // above: can't do that because it will remove the name of the record inserter
  350:  
  351:                 db.ReportHistories.Attach(reportHistory);
  352:                 db.Entry(reportHistory).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  353:                 db.SaveChanges();
  354:  
  355:                 b = true;
  356:             }
  357:  
  358:             return b;
  359:         }
  360:  
  361:         ////////////////////////////////////////////////////////////////////////////
  362:  
  363:         /// <summary>
  364:         /// When a report history is updated I will reset local lists to null, and reset the report lists to null, to generate new list
  365:         /// </summary>
  366:         private static void DbContextProbablyUpdated()
  367:         {
  368:             //openStatusOrClosedStatusWithinLast24HourReportList = null;
  369:  
  370:             Ia.Ftn.Cl.Models.Data.Report.OpenStatusOrClosedWithinLast24HourAndResponsibilityAndReadabilityReportListClear();
  371:         }
  372:  
  373:         ////////////////////////////////////////////////////////////////////////////
  374:         ////////////////////////////////////////////////////////////////////////////
  375:     }
  376:  
  377:     ////////////////////////////////////////////////////////////////////////////
  378:     ////////////////////////////////////////////////////////////////////////////   
  379: }