Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Report

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

Report Entity Framework class for Next Generation Network (NGN) entity model.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Web.Security;
   6:  using System.ComponentModel.DataAnnotations;
   7:  using System.ComponentModel.DataAnnotations.Schema;
   8:  using System.Data.Entity;
   9:   
  10:  namespace Ia.Ngn.Cl.Model
  11:  {
  12:      ////////////////////////////////////////////////////////////////////////////
  13:   
  14:      /// <summary publish="true">
  15:      /// Report Entity Framework class for Next Generation Network (NGN) entity model.
  16:      /// </summary>
  17:      /// 
  18:      /// <remarks> 
  19:      /// Copyright © 2006-2017 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  20:      ///
  21:      /// 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
  22:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  23:      ///
  24:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  25:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  26:      /// 
  27:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  28:      /// 
  29:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  30:      /// </remarks> 
  31:      public partial class Report
  32:      {
  33:          /// <summary/>
  34:          public Report()
  35:          {
  36:              // this.ReportHistories = new List<ReportHistory>();
  37:          }
  38:   
  39:          /// <summary/>
  40:          public int Id { get; set; }
  41:   
  42:          /// <summary/>
  43:          public string Service { get; set; }
  44:   
  45:          /// <summary/>
  46:          public int ServiceType { get; set; }
  47:   
  48:          /// <summary/>
  49:          public int State { get; set; }
  50:   
  51:          /// <summary/>
  52:          public int Status { get; set; }
  53:   
  54:          /// <summary/>
  55:          public int Category { get; set; }
  56:   
  57:          /// <summary/>
  58:          public int Area { get; set; }
  59:   
  60:          /// <summary/>
  61:          public int Priority { get; set; }
  62:   
  63:          /// <summary/>
  64:          public int Severity { get; set; }
  65:   
  66:          /// <summary/>
  67:          public string Detail { get; set; }
  68:   
  69:          /// <summary/>
  70:          public string Contact { get; set; }
  71:   
  72:          /// <summary/>
  73:          public string Note { get; set; }
  74:   
  75:          /// <summary/>
  76:          public DateTime Created { get; set; }
  77:   
  78:          /// <summary/>
  79:          public DateTime Updated { get; set; }
  80:   
  81:          /// <summary/>
  82:          public System.Guid UserId { get; set; }
  83:   
  84:          /// <summary/>
  85:          public virtual ICollection<ReportHistory> ReportHistories { get; set; }
  86:   
  87:          /// <summary/>
  88:          [NotMapped]
  89:          public bool StatusIsOpen 
  90:          {
  91:              get
  92:              {
  93:                  return this.Status == (int)Ia.Ngn.Cl.Model.Business.Report.Status.Open;
  94:              }
  95:          }
  96:   
  97:          ////////////////////////////////////////////////////////////////////////////
  98:   
  99:          /// <summary>
 100:          ///
 101:          /// </summary>
 102:          public static int Create(Report report, out string result)
 103:          {
 104:              int id;
 105:   
 106:              id = -1;
 107:              result = "";
 108:   
 109:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 110:              {
 111:                  report.Created = report.Updated = DateTime.UtcNow.AddHours(3);
 112:   
 113:                  db.Reports.Add(report);
 114:                  db.SaveChanges();
 115:   
 116:                  id = report.Id;
 117:              }
 118:   
 119:              return id;
 120:          }
 121:   
 122:          ////////////////////////////////////////////////////////////////////////////
 123:   
 124:          /// <summary>
 125:          ///
 126:          /// </summary>
 127:          public static Report Read(int reportId)
 128:          {
 129:              Report report;
 130:   
 131:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 132:              {
 133:                  report = (from r in db.Reports where r.Id == reportId select r).Include(r => r.ReportHistories).SingleOrDefault();
 134:              }
 135:   
 136:              return report;
 137:          }
 138:   
 139:          ////////////////////////////////////////////////////////////////////////////
 140:   
 141:          /// <summary>
 142:          ///
 143:          /// </summary>
 144:          public static List<Ia.Ngn.Cl.Model.Report> ReadList(string service)
 145:          {
 146:              List<Ia.Ngn.Cl.Model.Report> reportList;
 147:   
 148:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 149:              {
 150:                  try
 151:                  {
 152:                      reportList = (from r in db.Reports where r.Service == service select r).Include(r => r.ReportHistories).ToList();
 153:                  }
 154:                  catch
 155:                  {
 156:                      reportList = null;
 157:                  }
 158:              }
 159:   
 160:              return reportList;
 161:          }
 162:   
 163:          ////////////////////////////////////////////////////////////////////////////
 164:   
 165:          /// <summary>
 166:          ///
 167:          /// </summary>
 168:          public static List<Ia.Ngn.Cl.Model.Report> ReadListForServiceFromReportId(int reportId)
 169:          {
 170:              List<Ia.Ngn.Cl.Model.Report> reportList;
 171:   
 172:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 173:              {
 174:                  try
 175:                  {
 176:                      reportList = (from _r in db.Reports join r in db.Reports on _r.Service equals r.Service where r.Id == reportId select r).Include(r => r.ReportHistories).ToList();
 177:                  }
 178:                  catch
 179:                  {
 180:                      reportList = null;
 181:                  }
 182:              }
 183:   
 184:              return reportList;
 185:          }
 186:   
 187:          ////////////////////////////////////////////////////////////////////////////
 188:   
 189:          /// <summary>
 190:          ///
 191:          /// </summary>
 192:          [NotMapped]
 193:          public ReportHistory LastReportHistory
 194:          {
 195:              get
 196:              {
 197:                  ReportHistory reportHistory;
 198:   
 199:                  reportHistory = null;
 200:   
 201:                  if (this.ReportHistories != null)
 202:                  {
 203:                      if (this.ReportHistories.Count > 0)
 204:                      {
 205:                          reportHistory = this.ReportHistories.Skip(this.ReportHistories.Count - 1).FirstOrDefault();
 206:                      }
 207:                      else reportHistory = null;
 208:                  }
 209:                  else reportHistory = null;
 210:   
 211:                  return reportHistory;
 212:              }
 213:          }
 214:   
 215:          ////////////////////////////////////////////////////////////////////////////
 216:   
 217:          /// <summary>
 218:          ///
 219:          /// </summary>
 220:          [NotMapped]
 221:          public ReportHistory SecondToLastReportHistory
 222:          {
 223:              get
 224:              {
 225:                  ReportHistory reportHistory;
 226:   
 227:                  reportHistory = null;
 228:   
 229:                  if (this.ReportHistories != null)
 230:                  {
 231:                      if (this.ReportHistories.Count > 1)
 232:                      {
 233:                          reportHistory = this.ReportHistories.Skip(this.ReportHistories.Count - 2).FirstOrDefault();
 234:                      }
 235:                      else reportHistory = null;
 236:                  }
 237:                  else reportHistory = null;
 238:   
 239:                  return reportHistory;
 240:              }
 241:          }
 242:   
 243:          ////////////////////////////////////////////////////////////////////////////
 244:   
 245:          /// <summary>
 246:          ///
 247:          /// </summary>
 248:          public bool CloseStatus(Ia.Ngn.Cl.Model.Staff staff)
 249:          {
 250:              bool b;
 251:   
 252:              b = false;
 253:   
 254:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 255:              {
 256:                  this.Status = 2; // <status id="2" name="Closed" ...>
 257:   
 258:                  this.Updated = DateTime.UtcNow.AddHours(3);
 259:                  //this.UserId = staff.UserId;
 260:                  // above: can't do that because it will remove the name of the record inserter
 261:   
 262:                  db.Reports.Attach(this);
 263:                  db.Entry(this).State = System.Data.Entity.EntityState.Modified;
 264:                  db.SaveChanges();
 265:   
 266:                  b = true;
 267:              }
 268:   
 269:              return b;
 270:          }
 271:   
 272:          ////////////////////////////////////////////////////////////////////////////
 273:   
 274:          /// <summary>
 275:          ///
 276:          /// </summary>
 277:          public bool OpenStatus(Ia.Ngn.Cl.Model.Staff staff)
 278:          {
 279:              bool b;
 280:   
 281:              b = false;
 282:   
 283:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 284:              {
 285:                  this.Status = 1; // <status id="1" name="Opened" ...>
 286:   
 287:                  this.Updated = DateTime.UtcNow.AddHours(3);
 288:                  //this.UserId = staff.UserId;
 289:                  // above: can't do that because it will remove the name of the record inserter
 290:   
 291:                  db.Reports.Attach(this);
 292:                  db.Entry(this).State = System.Data.Entity.EntityState.Modified;
 293:                  db.SaveChanges();
 294:   
 295:                  b = true;
 296:              }
 297:   
 298:              return b;
 299:          }
 300:   
 301:          ////////////////////////////////////////////////////////////////////////////
 302:   
 303:          /// <summary>
 304:          ///
 305:          /// </summary>
 306:          public static bool UpdateMigratedList(List<Report> reportList, out string result)
 307:          {
 308:              bool b;
 309:              Report report;
 310:   
 311:              b = false;
 312:              result = "";
 313:   
 314:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 315:              {
 316:                  foreach (Report updatedReport in reportList)
 317:                  {
 318:                      report = (from r in db.Reports where r.Id == updatedReport.Id select r).SingleOrDefault();
 319:   
 320:                      if (report == null)
 321:                      {
 322:                          //updatedReport.Created = updatedReport.Updated = DateTime.UtcNow.AddHours(3);
 323:   
 324:                          db.Reports.Add(updatedReport);
 325:                      }
 326:                      else
 327:                      {
 328:                          // below: copy values from updatedReport to report
 329:   
 330:                          report.UpdateMigrated(updatedReport);
 331:   
 332:                          db.Reports.Attach(report);
 333:   
 334:                          db.Entry(report).State = System.Data.Entity.EntityState.Modified;
 335:                      }
 336:   
 337:                      b = true;
 338:   
 339:                  }
 340:   
 341:                  db.SaveChanges();
 342:   
 343:                  b = true;
 344:              }
 345:   
 346:              return b;
 347:          }
 348:   
 349:          ////////////////////////////////////////////////////////////////////////////
 350:   
 351:          /// <summary>
 352:          ///
 353:          /// </summary>
 354:          public bool UpdateMigrated(Report updatedReport)
 355:          {
 356:              // below: this will not update Id, Created
 357:              bool updated;
 358:   
 359:              updated = false;
 360:   
 361:              if (this.Area != updatedReport.Area) { this.Area = updatedReport.Area; updated = true; }
 362:              if (this.Category != updatedReport.Category) { this.Category = updatedReport.Category; updated = true; }
 363:              if (this.Contact != updatedReport.Contact) { this.Contact = updatedReport.Contact; updated = true; }
 364:              if (this.Detail != updatedReport.Detail) { this.Detail = updatedReport.Detail; updated = true; }
 365:              if (this.Note != updatedReport.Note) { this.Note = updatedReport.Note; updated = true; }
 366:              if (this.Priority != updatedReport.Priority) { this.Priority = updatedReport.Priority; updated = true; }
 367:              if (this.Service != updatedReport.Service) { this.Service = updatedReport.Service; updated = true; }
 368:   
 369:              if (this.ServiceType != updatedReport.ServiceType) { this.ServiceType = updatedReport.ServiceType; updated = true; }
 370:              if (this.ReportHistories != updatedReport.ReportHistories) { this.ReportHistories = updatedReport.ReportHistories; updated = true; }
 371:              if (this.Severity != updatedReport.Severity) { this.Severity = updatedReport.Severity; updated = true; }
 372:              if (this.State != updatedReport.State) { this.State = updatedReport.State; updated = true; }
 373:              if (this.Status != updatedReport.Status) { this.Status = updatedReport.Status; updated = true; }
 374:   
 375:              if (this.UserId != updatedReport.UserId) { this.UserId = updatedReport.UserId; updated = true; }
 376:   
 377:              // below: this is an update of migrated data
 378:              if (this.Created != updatedReport.Created) { this.Created = updatedReport.Created; updated = true; }
 379:              if (this.Updated != updatedReport.Updated) { this.Updated = updatedReport.Updated; updated = true; }
 380:   
 381:              //if (updated) this.Updated = DateTime.UtcNow.AddHours(3);
 382:   
 383:              return updated;
 384:          }
 385:   
 386:          ////////////////////////////////////////////////////////////////////////////
 387:   
 388:          /// <summary>
 389:          ///
 390:          /// </summary>
 391:          public static bool NullifyUserId(Guid userId, out int numberOfRecordsUpdated)
 392:          {
 393:              return MoveUserId(userId, Guid.Empty, out numberOfRecordsUpdated);
 394:          }
 395:   
 396:          ////////////////////////////////////////////////////////////////////////////
 397:   
 398:          /// <summary>
 399:          ///
 400:          /// </summary>
 401:          public static bool MoveUserId(Guid fromUserId, Guid toUserId, out int numberOfRecordsUpdated)
 402:          {
 403:              bool b;
 404:   
 405:              b = false;
 406:              numberOfRecordsUpdated = 0;
 407:   
 408:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 409:              {
 410:                  var query = (from r in db.Reports where r.UserId == fromUserId select r).ToList();
 411:   
 412:                  foreach (var v in query)
 413:                  {
 414:                      v.UserId = toUserId;
 415:                      numberOfRecordsUpdated++;
 416:                  }
 417:   
 418:                  db.SaveChanges();
 419:   
 420:                  b = true;
 421:              }
 422:   
 423:              return b;
 424:          }
 425:   
 426:          ////////////////////////////////////////////////////////////////////////////
 427:   
 428:          /// <summary>
 429:          ///
 430:          /// </summary>
 431:          public static bool Delete(int id/*, out string result*/)
 432:          {
 433:              bool b;
 434:   
 435:              b = false;
 436:              //result = "";
 437:   
 438:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 439:              {
 440:                  var v = (from r in db.Reports where r.Id == id select r).FirstOrDefault();
 441:   
 442:                  db.Reports.Remove(v);
 443:                  db.SaveChanges();
 444:   
 445:                  b = true;
 446:              }
 447:   
 448:              return b;
 449:          }
 450:   
 451:          ////////////////////////////////////////////////////////////////////////////
 452:          ////////////////////////////////////////////////////////////////////////////
 453:      }
 454:  }