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

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » ReportHistory

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

Report History support class for Optical Fiber Network (OFN) 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.Ngn.Cl.Model.Data
   8:  {
   9:      ////////////////////////////////////////////////////////////////////////////
  10:   
  11:      /// <summary publish="true">
  12:      /// Report History support class for Optical Fiber Network (OFN) 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<Guid, 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.Ngn.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.Ngn.Cl.Model.Ngn())
  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.Ngn.Cl.Model.ReportHistory Read(int reportHistoryId)
  76:          {
  77:              Ia.Ngn.Cl.Model.ReportHistory reportHistory;
  78:   
  79:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
  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.Ngn.Cl.Model.ReportHistory> ListForReportId(int reportId)
  95:          {
  96:              List<Ia.Ngn.Cl.Model.ReportHistory> reportHistoryList;
  97:   
  98:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
  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<Guid, List<int>> DivisionUserIdToNeglectedAndResolvedReportHistoryCountDictionary()
 114:          {
 115:              if (neglectedAndResolvedReportHistoryDictionary == null || neglectedAndResolvedReportHistoryDictionary.Count == 0)
 116:              {
 117:                  lock (objectLock)
 118:                  {
 119:                      neglectedAndResolvedReportHistoryDictionary = Ia.Ngn.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:              public int Id;
 134:              public int Resolution;
 135:              public int ReportId;
 136:              public Guid UserId;
 137:          }
 138:   
 139:          ////////////////////////////////////////////////////////////////////////////
 140:   
 141:          /// <summary>
 142:          ///
 143:          /// </summary>
 144:          private static Dictionary<Guid, List<int>> _NeglectedAndResolvedReportHistoryDictionary()
 145:          {
 146:              bool nextIsNegligence, nextIsResolved;
 147:              // id="1026" name="Negligence" arabicName="إهمال"
 148:   
 149:              var negligenceDictionary = new Dictionary<Guid, int>();
 150:              var resolvedDictionary = new Dictionary<Guid, int>();
 151:   
 152:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 153:              {
 154:                  var list0 = (from rh in db.ReportHistories
 155:                               select new ReportHistoryIdResolutionReportIdUserId
 156:                               {
 157:                                   Id = rh.Id,
 158:                                   Resolution = rh.Resolution,
 159:                                   ReportId = rh.Report.Id,
 160:                                   UserId = rh.UserId,
 161:                               }).ToList();
 162:   
 163:                  var dic0 = new Dictionary<int, List<ReportHistoryIdResolutionReportIdUserId>>();
 164:   
 165:                  foreach (var l in list0)
 166:                  {
 167:                      if (!dic0.ContainsKey(l.ReportId)) dic0[l.ReportId] = new List<ReportHistoryIdResolutionReportIdUserId>();
 168:   
 169:                      dic0[l.ReportId].Add(l);
 170:                  }
 171:   
 172:                  foreach (var kvp in dic0)
 173:                  {
 174:                      nextIsNegligence = nextIsResolved = false;
 175:                      foreach (var v in kvp.Value.OrderByDescending(u => u.Id))
 176:                      {
 177:                          if (nextIsNegligence)
 178:                          {
 179:                              if (!negligenceDictionary.ContainsKey(v.UserId)) negligenceDictionary[v.UserId] = 0;
 180:   
 181:                              negligenceDictionary[v.UserId]++;
 182:   
 183:                              nextIsNegligence = false;
 184:                          }
 185:                          else if (nextIsResolved)
 186:                          {
 187:                              if (!resolvedDictionary.ContainsKey(v.UserId)) resolvedDictionary[v.UserId] = 0;
 188:   
 189:                              resolvedDictionary[v.UserId]++;
 190:   
 191:                              nextIsResolved = false;
 192:                          }
 193:                          else if (v.Resolution == 1026) nextIsNegligence = true;
 194:                          else nextIsResolved = true;
 195:                      }
 196:                  }
 197:              }
 198:   
 199:              var dictionary = new Dictionary<Guid, List<int>>();
 200:   
 201:              foreach (var k in resolvedDictionary.Keys.Union(negligenceDictionary.Keys))
 202:              {
 203:                  if (!dictionary.ContainsKey(k))
 204:                  {
 205:                      dictionary[k] = new List<int>();
 206:   
 207:                      dictionary[k].Add(negligenceDictionary.ContainsKey(k) ? negligenceDictionary[k] : 0);
 208:                      dictionary[k].Add(resolvedDictionary.ContainsKey(k) ? resolvedDictionary[k] : 0);
 209:                  }
 210:              }
 211:   
 212:              return dictionary;
 213:          }
 214:   
 215:          ////////////////////////////////////////////////////////////////////////////
 216:   
 217:          /// <summary>
 218:          ///
 219:          /// </summary>
 220:          public static bool UpdateMigratedList(List<Ia.Ngn.Cl.Model.ReportHistory> reportHistoryList, out string result)
 221:          {
 222:              bool b;
 223:              Ia.Ngn.Cl.Model.ReportHistory reportHistory;
 224:   
 225:              b = false;
 226:              result = string.Empty;
 227:   
 228:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 229:              {
 230:                  foreach (Ia.Ngn.Cl.Model.ReportHistory updatedReportHistory in reportHistoryList)
 231:                  {
 232:                      reportHistory = (from rh in db.ReportHistories
 233:                                       where rh.Id == updatedReportHistory.Id
 234:                                       select rh).Include(u => u.Report).ThenInclude(u => u.ReportHistories).SingleOrDefault();
 235:   
 236:                      if (reportHistory == null)
 237:                      {
 238:                          //updatedReport.Created = updatedReport.Updated = DateTime.UtcNow.AddHours(3);
 239:   
 240:                          db.ReportHistories.Add(updatedReportHistory);
 241:                      }
 242:                      else
 243:                      {
 244:                          // below: copy values from updatedReport to report
 245:   
 246:                          reportHistory.UpdateMigrated(updatedReportHistory);
 247:   
 248:                          db.ReportHistories.Attach(reportHistory);
 249:   
 250:                          db.Entry(reportHistory).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
 251:                      }
 252:   
 253:                      b = true;
 254:                  }
 255:   
 256:                  db.SaveChanges();
 257:   
 258:                  DbContextProbablyUpdated();
 259:   
 260:                  b = true;
 261:              }
 262:   
 263:              return b;
 264:          }
 265:   
 266:          ////////////////////////////////////////////////////////////////////////////
 267:   
 268:          /// <summary>
 269:          ///
 270:          /// </summary>
 271:          public static bool NullifyUserId(Guid userId, out int numberOfRecordsUpdated)
 272:          {
 273:              return MoveUserId(userId, Guid.Empty, out numberOfRecordsUpdated);
 274:          }
 275:   
 276:          ////////////////////////////////////////////////////////////////////////////
 277:   
 278:          /// <summary>
 279:          ///
 280:          /// </summary>
 281:          public static bool MoveUserId(Guid fromUserId, Guid toUserId, out int numberOfRecordsUpdated)
 282:          {
 283:              bool b;
 284:   
 285:              b = false;
 286:              numberOfRecordsUpdated = 0;
 287:   
 288:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 289:              {
 290:                  var query = (from rh in db.ReportHistories where rh.UserId == fromUserId select rh).ToList();
 291:   
 292:                  foreach (var v in query)
 293:                  {
 294:                      v.UserId = toUserId;
 295:                      numberOfRecordsUpdated++;
 296:                  }
 297:   
 298:                  db.SaveChanges();
 299:   
 300:                  DbContextProbablyUpdated();
 301:   
 302:                  b = true;
 303:              }
 304:   
 305:              return b;
 306:          }
 307:   
 308:          ////////////////////////////////////////////////////////////////////////////
 309:   
 310:          /// <summary>
 311:          ///
 312:          /// </summary>
 313:          public static bool Delete(int id/*, out string result*/)
 314:          {
 315:              bool b;
 316:   
 317:              b = false;
 318:              //result = string.Empty;
 319:   
 320:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 321:              {
 322:                  var v = (from rh in db.ReportHistories where rh.Id == id select rh).FirstOrDefault();
 323:   
 324:                  db.ReportHistories.Remove(v);
 325:                  db.SaveChanges();
 326:   
 327:                  DbContextProbablyUpdated();
 328:   
 329:                  b = true;
 330:              }
 331:   
 332:              return b;
 333:          }
 334:   
 335:          ////////////////////////////////////////////////////////////////////////////
 336:   
 337:          /// <summary>
 338:          /// When a report history is updated I will reset local lists to null, and reset the report lists to null, to generate new list
 339:          /// </summary>
 340:          private static void DbContextProbablyUpdated()
 341:          {
 342:              //openStatusOrClosedStatusWithinLast24HourReportList = null;
 343:   
 344:              Ia.Ngn.Cl.Model.Data.Report.OpenStatusOrClosedWithinLast24HourAndResponsibilityAndReadabilityReportListClear();
 345:          }
 346:   
 347:          ////////////////////////////////////////////////////////////////////////////
 348:          ////////////////////////////////////////////////////////////////////////////
 349:      }
 350:   
 351:      ////////////////////////////////////////////////////////////////////////////
 352:      ////////////////////////////////////////////////////////////////////////////   
 353:  }