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