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