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

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » ServiceRequest

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

Service Request support class for Next Generation Network (NGN) data model.

   1:  using System;
   2:  using System.Collections;
   3:  using System.Collections.Generic;
   4:  using System.Xml.Linq;
   5:  using System.Linq;
   6:  using System.Reflection;
   7:  using System.IO;
   8:  using System.Globalization;
   9:  using System.Text.RegularExpressions;
  10:  using System.Data;
  11:  using System.Data.Entity;
  12:  using Ia.Ngn.Cl.Model.Business; // Needed for ServerExtension
  13:  using System.Text;
  14:   
  15:  namespace Ia.Ngn.Cl.Model.Data
  16:  {
  17:      ////////////////////////////////////////////////////////////////////////////
  18:   
  19:      /// <summary publish="true">
  20:      /// Service Request support class for Next Generation Network (NGN) data model.
  21:      /// </summary>
  22:      /// 
  23:      /// <remarks> 
  24:      /// Copyright © 2006-2017 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  25:      ///
  26:      /// 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
  27:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  28:      ///
  29:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  30:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  31:      /// 
  32:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  33:      /// 
  34:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  35:      /// </remarks> 
  36:      public partial class ServiceRequest
  37:      {
  38:          private static int serviceRequestIdStartEndRangeBufferedListIndex, serviceRequestIdLatestStartEndRangeBufferedListIndex;
  39:          private static SortedList serviceCategorySortedList, serviceSortedList, customerCategorySortedList, statusSortedList;
  40:          private static XDocument xDocument;
  41:          private static List<int> serviceIdAllowedForProcessingList;
  42:          private static Dictionary<int, string> systemCode;
  43:          private static List<Tuple<int, int>> serviceRequestIdStartEndRangeBufferedTupleList;
  44:   
  45:          ////////////////////////////////////////////////////////////////////////////
  46:   
  47:          /// <summary>
  48:          ///
  49:          /// </summary>
  50:          public ServiceRequest() { }
  51:   
  52:          ////////////////////////////////////////////////////////////////////////////
  53:   
  54:          /// <summary>
  55:          ///
  56:          /// </summary>
  57:          public static Dictionary<int, string> SystemCode
  58:          {
  59:              get
  60:              {
  61:                  if (systemCode == null || systemCode.Count == 0)
  62:                  {
  63:                      // select code_id, code_name from system_codes
  64:   
  65:                      systemCode = new Dictionary<int, string>(100);
  66:   
  67:                      systemCode[6008] = "قطع مطالبات";
  68:                      systemCode[6009] = "قطع إدارى";
  69:                      systemCode[4007] = "إفراج";
  70:                      systemCode[4008] = "قيد التجديد";
  71:                      systemCode[4009] = "قيد إخطار الجهة";
  72:                      systemCode[4010] = "تسيل جزئي";
  73:                      systemCode[1001] = "جديد";
  74:                      systemCode[1002] = "مطلوب";
  75:                      systemCode[1003] = "متاح";
  76:                      systemCode[2001] = "قيد الموافقة الفنية";
  77:                      systemCode[2002] = "قيد الدفع";
  78:                      systemCode[2003] = "قيد التنفيذ";
  79:                      systemCode[2004] = "تعذر التنفيذ";
  80:                      systemCode[2005] = "تم التنفيذ";
  81:                      systemCode[3001] = "نقل من رصيد خدمة - دائن";
  82:                      systemCode[3002] = "نقل من رصيد خدمة - مدين";
  83:                      systemCode[4001] = "سارية";
  84:                      systemCode[4002] = "منتهية";
  85:                      systemCode[4003] = "مستردة";
  86:                      systemCode[4004] = "تسييل";
  87:                      systemCode[5001] = "سارية";
  88:                      systemCode[5002] = "منتهية";
  89:                      systemCode[5003] = "ملغاة";
  90:                      systemCode[4005] = "ملغاة";
  91:                      systemCode[5004] = "مبدئية";
  92:                      systemCode[6001] = "قيد التنفيذ";
  93:                      systemCode[6002] = "تم التنفيذ";
  94:                      systemCode[6003] = "تعذر التنفيذ";
  95:                      systemCode[4006] = "جديدة";
  96:                      systemCode[7001] = "يعمل";
  97:                      systemCode[7002] = "قطع مؤقت";
  98:                      systemCode[7003] = "قطع مبرمج";
  99:                      systemCode[1004] = "مقطوع";
 100:                      systemCode[5005] = "متأخرة السداد";
 101:                      systemCode[7004] = "قيد التنفيذ";
 102:                      systemCode[8001] = "قيد التنفيذ";
 103:                      systemCode[8002] = "تم التنفيذ";
 104:                      systemCode[8003] = "تعذر التنفيذ";
 105:                      systemCode[1005] = "يعمل";
 106:                      systemCode[7005] = "تعذر التنفيذ";
 107:                      systemCode[9001] = "دائن";
 108:                      systemCode[9002] = "مدين";
 109:                      systemCode[7006] = "رفع نهائي";
 110:                      systemCode[1203] = "مبدئية";
 111:                      systemCode[1201] = "سارية";
 112:                      systemCode[1202] = "ملغاة";
 113:                      systemCode[1101] = "انهاء";
 114:                      systemCode[1102] = "قطع مؤقت";
 115:                      systemCode[1301] = "قطع مبرمج";
 116:                      systemCode[1302] = "أمر عمل";
 117:                      systemCode[7007] = "رفع محاسبة";
 118:                      systemCode[1401] = "نص";
 119:                      systemCode[1402] = "رقم";
 120:                      systemCode[1403] = "تاريخ";
 121:                      systemCode[1404] = "تاريخ/وقت";
 122:                      systemCode[7008] = "قطع معاكسة";
 123:                      systemCode[1501] = "خدمة أساسية";
 124:                      systemCode[1502] = "تشغيل خدمة فرعية";
 125:                      systemCode[1503] = "إيقاف خدمة فرعية";
 126:                      systemCode[1504] = "عملية";
 127:                      systemCode[1505] = "منتج";
 128:                      systemCode[1510] = "خدمات متنوعة";
 129:                      systemCode[1103] = "إعادة";
 130:                      systemCode[1506] = "خدمة اساسية - مجموعة";
 131:                      systemCode[1507] = "خدمة تابعة";
 132:                      systemCode[1508] = "خدمة مدمجة";
 133:                      systemCode[2101] = "جديد";
 134:                      systemCode[2102] = "تم الحساب";
 135:                      systemCode[2103] = "نسخة جديدة";
 136:                      systemCode[2201] = "ملف المكالمات الدولية";
 137:                      systemCode[6004] = "إعادة الحالة";
 138:                      systemCode[1601] = "مبدئية";
 139:                      systemCode[1602] = "نهائية";
 140:                      systemCode[1603] = "ملغاة";
 141:                      systemCode[2006] = "ملغاة";
 142:                      systemCode[7009] = "ملغى";
 143:                      systemCode[7010] = "لا يعمل";
 144:                      systemCode[2104] = "مفقود";
 145:                      systemCode[5006] = "سارية - مؤقتة";
 146:                      systemCode[5007] = "مرحلة";
 147:                      systemCode[1701] = "عادي";
 148:                      systemCode[1702] = "نطاق";
 149:                      systemCode[1509] = "إيقاف خدمة أساسية";
 150:                  }
 151:   
 152:                  return systemCode;
 153:              }
 154:          }
 155:   
 156:          ////////////////////////////////////////////////////////////////////////////
 157:   
 158:          /// <summary>
 159:          ///
 160:          /// </summary>
 161:          public static SortedList ServiceCategorySortedList
 162:          {
 163:              get
 164:              {
 165:                  int id;
 166:   
 167:                  if (serviceCategorySortedList == null)
 168:                  {
 169:                      serviceCategorySortedList = new SortedList(10);
 170:   
 171:                      foreach (XElement x in XDocument.Element("serviceRequest").Elements("service").Elements("categoryList").Elements("category"))
 172:                      {
 173:                          id = int.Parse(x.Attribute("id").Value);
 174:   
 175:                          serviceCategorySortedList[id] = x.Attribute("arabicName").Value;
 176:                      }
 177:                  }
 178:   
 179:                  return serviceCategorySortedList;
 180:              }
 181:          }
 182:   
 183:          ////////////////////////////////////////////////////////////////////////////
 184:   
 185:          /// <summary>
 186:          ///
 187:          /// </summary>
 188:          public static SortedList ServiceSortedList
 189:          {
 190:              get
 191:              {
 192:                  int id;
 193:   
 194:                  if (serviceSortedList == null)
 195:                  {
 196:                      serviceSortedList = new SortedList(10);
 197:   
 198:                      foreach (XElement x in XDocument.Element("serviceRequest").Elements("service").Elements("serviceList").Elements("service"))
 199:                      {
 200:                          id = int.Parse(x.Attribute("id").Value);
 201:   
 202:                          serviceSortedList[id] = x.Attribute("arabicName").Value;
 203:                      }
 204:                  }
 205:   
 206:                  return serviceSortedList;
 207:              }
 208:          }
 209:   
 210:          ////////////////////////////////////////////////////////////////////////////
 211:   
 212:          /// <summary>
 213:          ///
 214:          /// </summary>
 215:          public static SortedList CustomerCategorySortedList
 216:          {
 217:              get
 218:              {
 219:                  int id;
 220:   
 221:                  if (customerCategorySortedList == null)
 222:                  {
 223:                      customerCategorySortedList = new SortedList(10);
 224:   
 225:                      foreach (XElement x in XDocument.Element("serviceRequest").Elements("customer").Elements("categoryList").Elements("category"))
 226:                      {
 227:                          id = int.Parse(x.Attribute("id").Value);
 228:   
 229:                          customerCategorySortedList[id] = x.Attribute("arabicName").Value;
 230:                      }
 231:                  }
 232:   
 233:                  return customerCategorySortedList;
 234:              }
 235:          }
 236:   
 237:          ////////////////////////////////////////////////////////////////////////////
 238:   
 239:          /// <summary>
 240:          ///
 241:          /// </summary>
 242:          public static SortedList StatusSortedList
 243:          {
 244:              get
 245:              {
 246:                  int id;
 247:   
 248:                  if (statusSortedList == null)
 249:                  {
 250:                      statusSortedList = new SortedList(10);
 251:   
 252:                      foreach (XElement x in XDocument.Element("serviceRequest").Elements("statusList").Elements("status"))
 253:                      {
 254:                          id = int.Parse(x.Attribute("id").Value);
 255:   
 256:                          statusSortedList[id] = x.Attribute("arabicName").Value;
 257:                      }
 258:                  }
 259:   
 260:                  return statusSortedList;
 261:              }
 262:          }
 263:   
 264:          ////////////////////////////////////////////////////////////////////////////
 265:   
 266:          /// <summary>
 267:          ///
 268:          /// </summary>
 269:          public static List<int> ServiceIdAllowedForProcessingList
 270:          {
 271:              get
 272:              {
 273:                  int id;
 274:   
 275:                  if (serviceIdAllowedForProcessingList == null)
 276:                  {
 277:                      serviceIdAllowedForProcessingList = new List<int>(100);
 278:   
 279:                      foreach (XElement x in XDocument.Element("serviceRequest").Elements("service").Elements("serviceList").Elements("service"))
 280:                      {
 281:                          if (x.HasAttributes && x.Attribute("allowProcessing") != null)
 282:                          {
 283:                              if (x.Attribute("allowProcessing").Value == "true")
 284:                              {
 285:                                  id = int.Parse(x.Attribute("id").Value);
 286:                                  serviceIdAllowedForProcessingList.Add(id);
 287:                              }
 288:                          }
 289:                      }
 290:                  }
 291:   
 292:                  return serviceIdAllowedForProcessingList;
 293:              }
 294:          }
 295:   
 296:          ////////////////////////////////////////////////////////////////////////////    
 297:   
 298:          /// <summary>
 299:          ///
 300:          /// </summary>
 301:          public static string OracleSqlCommandForServiceRequestIdRange(Tuple<int, int> startEndRange)
 302:          {
 303:              return OracleSqlCommandForServiceRequestIdRange(startEndRange.Item1, startEndRange.Item2);
 304:          }
 305:   
 306:          ////////////////////////////////////////////////////////////////////////////    
 307:   
 308:          /// <summary>
 309:          ///
 310:          /// </summary>
 311:          private static string OracleSqlCommandForServiceRequestIdRange(int start, int end)
 312:          {
 313:              string sql;
 314:   
 315:              // select * from SRV_REQ_FIPER where SRV_REQ_ID >= 110000 and SRV_REQ_ID <= 110200 order by REQ_DATE asc, SRV_REQ_ID asc
 316:              sql = @"select * from SRV_REQ_FIPER where SRV_REQ_ID >= " + start + " and SRV_REQ_ID <= " + end + " order by SRV_REQ_ID asc";
 317:   
 318:              return sql;
 319:          }
 320:   
 321:          ////////////////////////////////////////////////////////////////////////////    
 322:   
 323:          /// <summary>
 324:          ///
 325:          /// </summary>
 326:          private static string OracleSqlCommandForGivenDateTime(DateTime dateTime)
 327:          {
 328:              string sql;
 329:   
 330:              //sql = @"select * from SRV_REQ_FIPER LEFT OUTER JOIN SRV_REQ_FIPER_TECH ON SRV_REQ_FIPER_TECH.SRV_REQ_ID = SRV_REQ_FIPER.SRV_REQ_ID where REQ_DATE >= '" + dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) + "' and REQ_DATE < '" + dateTime.AddDays(1).ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) + "' order by REQ_DATE asc, SRV_REQ_ID asc";
 331:              sql = @"select * from SRV_REQ_FIPER where REQ_DATE >= '" + dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) + "' and REQ_DATE < '" + dateTime.AddDays(1).ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) + "' order by SRV_REQ_ID asc";
 332:   
 333:              return sql;
 334:          }
 335:   
 336:          ////////////////////////////////////////////////////////////////////////////    
 337:   
 338:          /// <summary>
 339:          ///
 340:          /// </summary>
 341:          private static string OracleSqlCommandForSingleRandomDateTimeWithinTheLastNDays(int rangeOfPastDays, out DateTime selectedDate)
 342:          {
 343:              // below:
 344:              int i;
 345:              string sql;
 346:   
 347:              i = Ia.Cl.Model.Default.Random(rangeOfPastDays);
 348:   
 349:              selectedDate = DateTime.UtcNow.AddDays(-i);
 350:   
 351:              sql = OracleSqlCommandForGivenDateTime(selectedDate);
 352:   
 353:              return sql;
 354:          }
 355:   
 356:          ////////////////////////////////////////////////////////////////////////////    
 357:   
 358:          /// <summary>
 359:          ///
 360:          /// </summary>
 361:          private static string OracleSqlCommandForSingleDateTimeUsingDayIndexBetweenNowAndEarliestDate(ref int index, out DateTime selectedDate)
 362:          {
 363:              // below: select a date between now and the earliest date using an variable index value
 364:              string sql;
 365:              DateTime now;
 366:   
 367:              now = DateTime.UtcNow.AddHours(3);
 368:   
 369:              // below: check that inIndex is an index of a day between earliest day and now, and reset it to 0 if it is bigger than now
 370:              if (DateTime.Compare(Ia.Ngn.Cl.Model.Business.Administration.EarliestServiceRequestDate.AddDays(index), now) < 0)
 371:              {
 372:                  // below: within range
 373:              }
 374:              else index = 0;
 375:   
 376:              selectedDate = Ia.Ngn.Cl.Model.Business.Administration.EarliestServiceRequestDate.AddDays(index++);
 377:   
 378:              sql = OracleSqlCommandForGivenDateTime(selectedDate);
 379:   
 380:              return sql;
 381:          }
 382:   
 383:          ////////////////////////////////////////////////////////////////////////////    
 384:   
 385:          /// <summary>
 386:          /// Return the Tuple range but with a reset of list so to get edge records
 387:          /// </summary>
 388:          public static Tuple<int, int> ServiceRequestIdStartEndRangeManagerWithReset(ref int index, out string result)
 389:          {
 390:              serviceRequestIdStartEndRangeBufferedListIndex = 0;
 391:   
 392:              return ServiceRequestIdStartEndRangeManager(ref index, out result);
 393:          }
 394:   
 395:          ////////////////////////////////////////////////////////////////////////////    
 396:   
 397:          /// <summary>
 398:          ///
 399:          /// </summary>
 400:          public static Tuple<int, int> ServiceRequestIdStartEndRangeManager(ref int index, out string result)
 401:          {
 402:              int count, edgeBufferRange, lastestRangeList;
 403:              Tuple<int, int> tuple;
 404:              List<int> serviceRequestIdList;
 405:   
 406:              count = 50;
 407:              edgeBufferRange = 5000;
 408:              lastestRangeList = 21;
 409:   
 410:              if (serviceRequestIdStartEndRangeBufferedTupleList == null || serviceRequestIdStartEndRangeBufferedListIndex == 0)
 411:              {
 412:                  serviceRequestIdStartEndRangeBufferedListIndex = index;
 413:   
 414:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 415:                  {
 416:                      serviceRequestIdList = (from sr in db.ServiceRequests orderby sr.Id ascending select sr.Id).ToList();
 417:                  }
 418:   
 419:                  serviceRequestIdStartEndRangeBufferedTupleList = Ia.Cl.Model.Default.OptimizedStartEndRangeBufferedList(serviceRequestIdList, count, edgeBufferRange);
 420:   
 421:                  serviceRequestIdStartEndRangeBufferedListIndex = 0;
 422:              }
 423:   
 424:              if (serviceRequestIdStartEndRangeBufferedTupleList.Count > 0)
 425:              {
 426:                  if (Ia.Ngn.Cl.Model.Business.Administration.NowIsOfficialWorkingTime)
 427:                  {
 428:                      // flip between last and latest range to favor reading fresh work orders quickly
 429:                      if (Ia.Cl.Model.Default.RandomBool)
 430:                      {
 431:                          serviceRequestIdLatestStartEndRangeBufferedListIndex = serviceRequestIdStartEndRangeBufferedTupleList.Count - Ia.Cl.Model.Default.Random(lastestRangeList) - 1;
 432:                          serviceRequestIdLatestStartEndRangeBufferedListIndex = (serviceRequestIdLatestStartEndRangeBufferedListIndex >= 0) ? serviceRequestIdLatestStartEndRangeBufferedListIndex : 0;
 433:                      }
 434:                      else serviceRequestIdLatestStartEndRangeBufferedListIndex = serviceRequestIdStartEndRangeBufferedTupleList.Count - 1;
 435:   
 436:                      tuple = serviceRequestIdStartEndRangeBufferedTupleList[serviceRequestIdLatestStartEndRangeBufferedListIndex];
 437:   
 438:                      if (serviceRequestIdLatestStartEndRangeBufferedListIndex == serviceRequestIdStartEndRangeBufferedTupleList.Count - 1)
 439:                      {
 440:                          tuple = new Tuple<int, int>(tuple.Item1, tuple.Item2 + edgeBufferRange);
 441:                      }
 442:   
 443:                      result = "(latest: " + tuple.Item1 + "-" + tuple.Item2 + " " + serviceRequestIdLatestStartEndRangeBufferedListIndex + "/" + serviceRequestIdStartEndRangeBufferedTupleList.Count + ")";
 444:                  }
 445:                  else
 446:                  {
 447:                      tuple = serviceRequestIdStartEndRangeBufferedTupleList[serviceRequestIdStartEndRangeBufferedListIndex];
 448:   
 449:                      result = "(historic: " + tuple.Item1 + "-" + tuple.Item2 + " " + serviceRequestIdStartEndRangeBufferedListIndex + "/" + serviceRequestIdStartEndRangeBufferedTupleList.Count + ")";
 450:   
 451:                      serviceRequestIdStartEndRangeBufferedListIndex = Ia.Cl.Model.Default.IncrementListIndexOrRestart(serviceRequestIdStartEndRangeBufferedTupleList, serviceRequestIdStartEndRangeBufferedListIndex);
 452:                  }
 453:              }
 454:              else
 455:              {
 456:                  result = "(0-0 0/0)";
 457:   
 458:                  tuple = null;
 459:              }
 460:   
 461:              index = serviceRequestIdStartEndRangeBufferedListIndex;
 462:   
 463:              return tuple;
 464:          }
 465:   
 466:          /*
 467:          ////////////////////////////////////////////////////////////////////////////    
 468:  
 469:          /// <summary>
 470:          ///
 471:          /// </summary>
 472:          private static ArrayList SqlCommandsForSingleDaysArrayList
 473:          {
 474:              get
 475:              {
 476:                  string sql;
 477:                  DateTime startDateTime, endDateTime;
 478:                  Ia.Ngn.Cl.Model.ServiceRequest serviceRequest;
 479:  
 480:                  if (sqlCommandsForSingleDaysArrayList == null || sqlCommandsForSingleDaysArrayList.Count == 0)
 481:                  {
 482:                      // below: start from the date with the oldest updated time
 483:                      using (var db = new Ia.Ngn.Cl.Model.Ngn())
 484:                      {
 485:                          serviceRequest = (from q in db.ServiceRequests orderby q.Updated select q).Take(1).SingleOrDefault();
 486:  
 487:                          if (serviceRequest != null)
 488:                          {
 489:                              startDateTime = serviceRequest.RequestDateTime;
 490:  
 491:                              endDateTime = DateTime.UtcNow.AddHours(3);
 492:  
 493:                              // below: the date format on each database to ensuer they are the same
 494:                              //sql = @"select * from nls_session_parameters";
 495:  
 496:                              // sql = @"SELECT * FROM SRV_REQ_FIPER WHERE ROWNUM = 1"; // @"DESCRIBE SRV_REQ_FIPER;";
 497:  
 498:                              sqlCommandsForSingleDaysArrayList = new ArrayList();
 499:  
 500:                              for (DateTime dateTime = startDateTime; dateTime < endDateTime; dateTime = dateTime.AddDays(1))
 501:                              {
 502:                                  sql = OracleSqlCommandForGivenDateTime(dateTime);
 503:  
 504:                                  sqlCommandsForSingleDaysArrayList.Add(sql);
 505:                              }
 506:                          }
 507:                      }
 508:                  }
 509:  
 510:                  return sqlCommandsForSingleDaysArrayList;
 511:              }
 512:          }*/
 513:   
 514:          ////////////////////////////////////////////////////////////////////////////    
 515:   
 516:          /// <summary>
 517:          ///
 518:          /// </summary>
 519:          public static string AlterSessionOfCustomerDepartmentOracleDatabase
 520:          {
 521:              get
 522:              {
 523:                  return @"alter session set nls_date_format = 'DD/MM/YYYY HH24:MI:SS'";
 524:              }
 525:          }
 526:   
 527:          ////////////////////////////////////////////////////////////////////////////
 528:   
 529:          /// <summary>
 530:          ///
 531:          /// </summary>
 532:          public static void UpdateWithServiceList(List<string> serviceList, List<Ia.Ngn.Cl.Model.ServiceRequest> newServiceRequestList, out string result)
 533:          {
 534:              int readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount, serviceRequestId;
 535:              string serviceRequestServiceId;
 536:              Ia.Ngn.Cl.Model.ServiceRequest serviceRequest;
 537:   
 538:              readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
 539:              result = "";
 540:   
 541:              readItemCount = newServiceRequestList.Count;
 542:   
 543:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 544:              {
 545:                  foreach (Ia.Ngn.Cl.Model.ServiceRequest newServiceRequest in newServiceRequestList)
 546:                  {
 547:                      serviceRequestId = newServiceRequest.Id;
 548:   
 549:                      serviceRequest = (from sr in db.ServiceRequests where sr.Id == serviceRequestId select sr).SingleOrDefault();
 550:   
 551:                      existingItemCount = (serviceRequest != null) ? 1 : 0;
 552:   
 553:                      if (newServiceRequest.ServiceRequestService != null)
 554:                      {
 555:                          serviceRequestServiceId = newServiceRequest.ServiceRequestService.Id;
 556:   
 557:                          newServiceRequest.ServiceRequestService = (from srs in db.ServiceRequestServices where srs.Id == serviceRequestServiceId select srs).SingleOrDefault();
 558:                      }
 559:   
 560:                      if (serviceRequest.Update(newServiceRequest))
 561:                      {
 562:                          db.ServiceRequests.Attach(serviceRequest);
 563:                          db.Entry(serviceRequest).State = System.Data.Entity.EntityState.Modified;
 564:   
 565:                          updatedItemCount++;
 566:                      }
 567:                  }
 568:   
 569:                  db.SaveChanges();
 570:   
 571:                  result = "(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ") ";
 572:              }
 573:          }
 574:   
 575:          ////////////////////////////////////////////////////////////////////////////
 576:   
 577:          /// <summary>
 578:          ///
 579:          /// </summary>
 580:          public static bool UpdateServiceRequestService(Ia.Ngn.Cl.Model.ServiceRequest serviceRequest, Ia.Ngn.Cl.Model.ServiceRequestService serviceRequestService, out string result)
 581:          {
 582:              bool b;
 583:   
 584:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 585:              {
 586:                  serviceRequest = (from sr in db.ServiceRequests where sr.Id == serviceRequest.Id select sr).SingleOrDefault();
 587:   
 588:                  if (serviceRequest.ServiceRequestService != serviceRequestService)
 589:                  {
 590:                      serviceRequest.ServiceRequestService = (from srs in db.ServiceRequestServices where srs.Id == serviceRequestService.Id select srs).SingleOrDefault();
 591:   
 592:                      db.ServiceRequests.Attach(serviceRequest);
 593:                      db.Entry(serviceRequest).Property(x => x.ServiceRequestService).IsModified = true;
 594:   
 595:                      db.SaveChanges();
 596:   
 597:                      result = "Success: ServiceRequests ServiceRequestService updated. ";
 598:                      b = true;
 599:                  }
 600:                  else
 601:                  {
 602:                      result = "Warning: ServiceRequests ServiceRequestService value was not updated because its the same. ";
 603:   
 604:                      b = false;
 605:                  }
 606:              }
 607:   
 608:              return b;
 609:          }
 610:   
 611:          ////////////////////////////////////////////////////////////////////////////
 612:   
 613:          /// <summary>
 614:          ///
 615:          /// </summary>
 616:          public static void UpdateForADateTimeRangeWithOutputDataTable(DataTable dataTable, DateTime dateTime, out string result)
 617:          {
 618:              // below: the SQL statement should be within the dataTable.TableName variable
 619:              int number, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
 620:              int serviceRequestId;
 621:              string sql, r, customerAddress;
 622:              ArrayList newServiceRequestIdArryList;
 623:              DateTime startDateTime, endDateTime;
 624:              Match match;
 625:              Ia.Ngn.Cl.Model.Business.ServiceAddress serviceAddress;
 626:              Ia.Ngn.Cl.Model.ServiceRequest serviceRequest, newServiceRequest;
 627:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
 628:              List<Ia.Ngn.Cl.Model.ServiceRequestType> serviceRequestTypeList;
 629:   
 630:              readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
 631:              result = r = "";
 632:   
 633:              startDateTime = DateTime.MinValue;
 634:   
 635:              if (dataTable != null)
 636:              {
 637:                  sql = dataTable.TableName;
 638:   
 639:                  // select * from SRV_REQ_FIPER where REQ_DATE >= 'dd/MM/yyyy' and REQ_DATE < 'dd/MM/yyyy' order by REQ_DATE ASC, SRV_REQ_ID ASC;
 640:                  // select * from SRV_REQ_FIPER where REQ_DATE >= '01-10-2006' and REQ_DATE < '02-10-2006' order by REQ_DATE asc, SRV_REQ_ID asc;
 641:   
 642:                  match = Regex.Match(sql, @".+'(\d{2})\/(\d{2})\/(\d{4})'.+'(\d{2})\/(\d{2})\/(\d{4})'.+", RegexOptions.Singleline);
 643:                  //                             1        2        3        4          5        6
 644:   
 645:                  if (match.Success)
 646:                  {
 647:                      using (var db = new Ia.Ngn.Cl.Model.Ngn())
 648:                      {
 649:                          readItemCount = dataTable.Rows.Count;
 650:   
 651:                          //if (dataTable.Rows.Count > 0)
 652:                          //{
 653:                          startDateTime = DateTime.Parse(match.Groups[3].Value + "-" + match.Groups[2].Value + "-" + match.Groups[1].Value);
 654:                          endDateTime = DateTime.Parse(match.Groups[6].Value + "-" + match.Groups[5].Value + "-" + match.Groups[4].Value);
 655:   
 656:                          serviceRequestList = Ia.Ngn.Cl.Model.Data.ServiceRequest.ReadListWithinDateTimeRange(startDateTime, endDateTime);
 657:                          existingItemCount = serviceRequestList.Count;
 658:   
 659:                          newServiceRequestIdArryList = new ArrayList(dataTable.Rows.Count + 1);
 660:   
 661:                          foreach (DataRow dataRow in dataTable.Rows)
 662:                          {
 663:                              number = int.Parse(dataRow["SRV_NO"].ToString());
 664:   
 665:                              if (Ia.Ngn.Cl.Model.Business.Service.NumberIsWithinAllowedDomainList(number))
 666:                              {
 667:                                  serviceRequestId = int.Parse(dataRow["SRV_REQ_ID"].ToString());
 668:   
 669:                                  customerAddress = dataRow["ADDRESS"].ToString();
 670:                                  serviceAddress = Ia.Ngn.Cl.Model.Business.ServiceRequest.StatisticalServiceAddress(number.ToString(), customerAddress, out string level);
 671:   
 672:                                  newServiceRequest = new Ia.Ngn.Cl.Model.ServiceRequest()
 673:                                  {
 674:                                      Id = serviceRequestId,
 675:                                      Number = number,
 676:   
 677:                                      CustomerAddress = customerAddress,
 678:                                      AreaId = serviceAddress.AreaId,
 679:   
 680:                                      CustomerCategoryId = int.Parse(dataRow["CUST_CAT_ID"].ToString()),
 681:                                      CustomerId = int.Parse(dataRow["ACCOUNT_NO"].ToString()),
 682:                                      CustomerName = Ia.Ngn.Cl.Model.Business.Default.CorrectCustomerName(dataRow["NAME"].ToString()),
 683:                                      RequestDateTime = DateTime.Parse(dataRow["REQ_DATE"].ToString()),
 684:                                      Serial = int.Parse(dataRow["SRV_SER_NO"].ToString()),
 685:                                      ServiceCategoryId = int.Parse(dataRow["SRV_CAT_ID"].ToString()),
 686:                                      ServiceId = int.Parse(dataRow["SRV_ID"].ToString()),
 687:                                      Balance = double.Parse(dataRow["BALANCE"].ToString()),
 688:                                      Status = int.Parse(dataRow["STATUS"].ToString())
 689:                                  };
 690:   
 691:                                  serviceRequest = (from sr in serviceRequestList where sr.Id == newServiceRequest.Id select sr).SingleOrDefault();
 692:   
 693:                                  if (serviceRequest == null)
 694:                                  {
 695:                                      newServiceRequest.Created = newServiceRequest.Updated = DateTime.UtcNow.AddHours(3);
 696:   
 697:                                      db.ServiceRequests.Add(newServiceRequest);
 698:   
 699:                                      insertedItemCount++;
 700:                                  }
 701:                                  else
 702:                                  {
 703:                                      // below: copy values from newServiceRequest to serviceRequest
 704:   
 705:                                      if (serviceRequest.Update(newServiceRequest))
 706:                                      {
 707:                                          db.ServiceRequests.Attach(serviceRequest);
 708:                                          db.Entry(serviceRequest).State = System.Data.Entity.EntityState.Modified;
 709:   
 710:                                          updatedItemCount++;
 711:                                      }
 712:                                  }
 713:   
 714:                                  newServiceRequestIdArryList.Add(serviceRequestId);
 715:                              }
 716:                              else
 717:                              {
 718:                                  r += "Number: " + number + " is not within allowed domain list, ";
 719:                              }
 720:                          }
 721:   
 722:                          // below: this function will remove values that were not present in the reading
 723:                          if (serviceRequestList.Count > 0)
 724:                          {
 725:                              foreach (Ia.Ngn.Cl.Model.ServiceRequest sr in serviceRequestList)
 726:                              {
 727:                                  if (!newServiceRequestIdArryList.Contains(sr.Id))
 728:                                  {
 729:                                      serviceRequest = (from sr2 in db.ServiceRequests where sr2.Id == sr.Id select sr2).SingleOrDefault();
 730:   
 731:                                      db.ServiceRequests.Remove(serviceRequest);
 732:   
 733:                                      // below: we will also remove SRT records referensing this SR
 734:                                      serviceRequestTypeList = (from srt in db.ServiceRequestTypes where srt.ServiceRequest.Id == sr.Id select srt).ToList();
 735:   
 736:                                      foreach (Ia.Ngn.Cl.Model.ServiceRequestType srt in serviceRequestTypeList) db.ServiceRequestTypes.Remove(srt);
 737:   
 738:                                      deletedItemCount++;
 739:                                  }
 740:                              }
 741:                          }
 742:   
 743:                          db.SaveChanges();
 744:   
 745:                          result = "(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ") " + r;
 746:                          //}
 747:                          //else
 748:                          //{
 749:                          //    result = "(" + readItemCount + "/?/?) ";
 750:                          //}
 751:                      }
 752:                  }
 753:                  else
 754:                  {
 755:                      result = "(?/?/?: SQL in TableName is unmatched) ";
 756:                  }
 757:              }
 758:              else
 759:              {
 760:                  result = "(dataTable == null/?/?) ";
 761:              }
 762:          }
 763:   
 764:          ////////////////////////////////////////////////////////////////////////////
 765:   
 766:          /// <summary>
 767:          ///
 768:          /// </summary>
 769:          public static void UpdateForServiceRequestIdRangeWithOutputDataTable(DataTable dataTable, Tuple<int, int> startEndRange, out bool isUpdated, out string result)
 770:          {
 771:              // below: the SQL statement should be within the dataTable.TableName variable
 772:              int serviceRequestId, start, end, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
 773:              string sql, r, customerAddress;
 774:              ArrayList newServiceRequestIdArrayList;
 775:              Match match;
 776:              Ia.Ngn.Cl.Model.Business.ServiceAddress serviceAddress;
 777:              Ia.Ngn.Cl.Model.ServiceRequest serviceRequest, newServiceRequest;
 778:              List<int> numbersNotWithinAllowedDomainList;
 779:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
 780:              List<Ia.Ngn.Cl.Model.ServiceRequestType> serviceRequestTypeList;
 781:   
 782:              isUpdated = false;
 783:              readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
 784:              result = r = "";
 785:              numbersNotWithinAllowedDomainList = new List<int>();
 786:   
 787:              if (dataTable != null)
 788:              {
 789:                  sql = dataTable.TableName;
 790:   
 791:                  // select * from SRV_REQ_FIPER where SRV_REQ_ID >= 110000 and SRV_REQ_ID <= 321203 order by REQ_DATE asc, SRV_REQ_ID asc
 792:                  match = Regex.Match(sql, @"SRV_REQ_ID >= (\d+) and SRV_REQ_ID <= (\d+) ", RegexOptions.Singleline);
 793:                  //                                       1                       2
 794:   
 795:                  if (match.Success)
 796:                  {
 797:                      using (var db = new Ia.Ngn.Cl.Model.Ngn())
 798:                      {
 799:                          readItemCount = dataTable.Rows.Count;
 800:   
 801:                          start = int.Parse(match.Groups[1].Value);
 802:                          end = int.Parse(match.Groups[2].Value);
 803:   
 804:                          serviceRequestList = Ia.Ngn.Cl.Model.Data.ServiceRequest.ReadListWithinIdRange(start, end);
 805:                          existingItemCount = serviceRequestList.Count;
 806:   
 807:                          newServiceRequestIdArrayList = new ArrayList(dataTable.Rows.Count + 1);
 808:   
 809:                          foreach (DataRow dataRow in dataTable.Rows)
 810:                          {
 811:                              if (int.TryParse(dataRow["SRV_NO"].ToString(), out int number))
 812:                              {
 813:                                  if (Ia.Ngn.Cl.Model.Business.Service.NumberIsWithinAllowedDomainList(number))
 814:                                  {
 815:                                      serviceRequestId = int.Parse(dataRow["SRV_REQ_ID"].ToString());
 816:                                      customerAddress = dataRow["ADDRESS"].ToString();
 817:   
 818:                                      serviceAddress = Ia.Ngn.Cl.Model.Business.ServiceRequest.StatisticalServiceAddress(number.ToString(), customerAddress, out string level);
 819:   
 820:                                      newServiceRequest = new Ia.Ngn.Cl.Model.ServiceRequest()
 821:                                      {
 822:                                          Id = serviceRequestId,
 823:                                          Number = number,
 824:   
 825:                                          CustomerAddress = customerAddress,
 826:                                          AreaId = serviceAddress.AreaId,
 827:   
 828:                                          CustomerCategoryId = int.TryParse(dataRow["CUST_CAT_ID"].ToString(), out int i) ? i : 0,
 829:                                          CustomerId = int.TryParse(dataRow["ACCOUNT_NO"].ToString(), out i) ? i : 0,
 830:                                          CustomerName = Ia.Ngn.Cl.Model.Business.Default.CorrectCustomerName(dataRow["NAME"].ToString()),
 831:                                          RequestDateTime = DateTime.Parse(dataRow["REQ_DATE"].ToString()),
 832:                                          Serial = int.TryParse(dataRow["SRV_SER_NO"].ToString(), out i) ? i : 0,
 833:                                          ServiceCategoryId = int.TryParse(dataRow["SRV_CAT_ID"].ToString(), out i) ? i : 0,
 834:                                          ServiceId = int.TryParse(dataRow["SRV_ID"].ToString(), out i) ? i : 0,
 835:                                          Balance = double.Parse(dataRow["BALANCE"].ToString()),
 836:   
 837:                                          Status = int.TryParse(dataRow["STATUS"].ToString(), out i) ? i : 0
 838:                                      };
 839:   
 840:                                      serviceRequest = (from sr in serviceRequestList where sr.Id == newServiceRequest.Id select sr).SingleOrDefault();
 841:   
 842:                                      if (serviceRequest == null)
 843:                                      {
 844:                                          newServiceRequest.Created = newServiceRequest.Updated = DateTime.UtcNow.AddHours(3);
 845:   
 846:                                          db.ServiceRequests.Add(newServiceRequest);
 847:   
 848:                                          insertedItemCount++;
 849:                                      }
 850:                                      else
 851:                                      {
 852:                                          // below: copy values from newServiceRequest to serviceRequest
 853:   
 854:                                          if (serviceRequest.UpdateSkipServiceRequestService(newServiceRequest))
 855:                                          {
 856:                                              db.ServiceRequests.Attach(serviceRequest);
 857:                                              db.Entry(serviceRequest).State = System.Data.Entity.EntityState.Modified;
 858:   
 859:                                              updatedItemCount++;
 860:                                          }
 861:                                      }
 862:   
 863:                                      newServiceRequestIdArrayList.Add(serviceRequestId); // keep at the end
 864:                                  }
 865:                                  else
 866:                                  {
 867:                                      numbersNotWithinAllowedDomainList.Add(number);
 868:                                  }
 869:                              }
 870:                              else
 871:                              {
 872:                                  //
 873:                              }
 874:                          }
 875:   
 876:                          /*
 877:                          if (numbersNotWithinAllowedDomainList.Count > 0)
 878:                          {
 879:                              r = "Numbers not within allowed domain list: ";
 880:  
 881:                              foreach (int n in numbersNotWithinAllowedDomainList) r += n + ",";
 882:  
 883:                              r = r.Trim(',');
 884:                          }
 885:                          */
 886:   
 887:                          // below: this function will remove values that were not present in the reading
 888:                          if (serviceRequestList.Count > 0)
 889:                          {
 890:                              foreach (Ia.Ngn.Cl.Model.ServiceRequest sr in serviceRequestList)
 891:                              {
 892:                                  if (!newServiceRequestIdArrayList.Contains(sr.Id))
 893:                                  {
 894:                                      serviceRequest = (from sr2 in db.ServiceRequests where sr2.Id == sr.Id select sr2).SingleOrDefault();
 895:   
 896:                                      db.ServiceRequests.Remove(serviceRequest);
 897:   
 898:                                      // below: we will also remove SRT records referensing this SR
 899:                                      serviceRequestTypeList = (from srt in db.ServiceRequestTypes where srt.ServiceRequest.Id == sr.Id select srt).ToList();
 900:   
 901:                                      foreach (Ia.Ngn.Cl.Model.ServiceRequestType srt in serviceRequestTypeList) db.ServiceRequestTypes.Remove(srt);
 902:   
 903:                                      deletedItemCount++;
 904:                                  }
 905:                              }
 906:                          }
 907:   
 908:                          db.SaveChanges();
 909:   
 910:                          if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
 911:                          else isUpdated = false;
 912:   
 913:                          result = "(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ") " + r;
 914:                      }
 915:                  }
 916:                  else
 917:                  {
 918:                      result = "(?/?/?: SQL in TableName is unmatched) ";
 919:                  }
 920:              }
 921:              else
 922:              {
 923:                  result = "(dataTable == null/?/?) ";
 924:              }
 925:          }
 926:   
 927:          ////////////////////////////////////////////////////////////////////////////
 928:   
 929:          /// <summary>
 930:          /// Return list of service requests
 931:          /// </summary>
 932:          public static List<Ia.Ngn.Cl.Model.ServiceRequest> List()
 933:          {
 934:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
 935:   
 936:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 937:              {
 938:                  serviceRequestList = (from sr in db.ServiceRequests select sr).ToList();
 939:              }
 940:   
 941:              return serviceRequestList;
 942:          }
 943:   
 944:          ////////////////////////////////////////////////////////////////////////////
 945:   
 946:          /// <summary>
 947:          /// Return a list of service requests that have numbers-serials within the passed list
 948:          /// </summary>
 949:          public static List<Ia.Ngn.Cl.Model.ServiceRequest> List(List<Ia.Ngn.Cl.Model.Business.ServiceRequest.NumberSerial> numberSerialList)
 950:          {
 951:              List<long> idList;
 952:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
 953:   
 954:              idList = numberSerialList.IdList();
 955:   
 956:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 957:              {
 958:                  serviceRequestList = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService)
 959:                                        where
 960:                                        //numberSerialList.Contains(q.Number, q.Serial) does not work
 961:                                        //((from r in numberSerialList where r.Number == q.Number && r.Serial == q.Serial select r) != null) does not work
 962:                                        //numberList.Any<int>(i=> i == q.Number)  does not work
 963:                                        idList.Contains((long)sr.Number * 100 + sr.Serial)
 964:                                        select sr).ToList();
 965:              }
 966:   
 967:              return serviceRequestList;
 968:          }
 969:   
 970:          ////////////////////////////////////////////////////////////////////////////
 971:   
 972:          /// <summary>
 973:          /// Return a list of service requests that have numbers (including changed-to numbers) within the passed list
 974:          /// </summary>
 975:          public static List<Ia.Ngn.Cl.Model.ServiceRequest> List(List<int> numberList)
 976:          {
 977:              List<string> serviceList;
 978:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
 979:   
 980:              serviceList = (from n in numberList select n.ToString()).ToList();
 981:   
 982:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 983:              {
 984:                  serviceRequestList = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService).Include(a => a.ServiceRequestTypes)
 985:                                        where numberList.Contains(sr.Number) || (sr.ServiceRequestTypes != null && sr.ServiceRequestTypes.Any(u => u.TypeId == 11 && serviceList.Contains(u.Value)))
 986:                                        select sr).ToList();
 987:              }
 988:   
 989:              return serviceRequestList;
 990:          }
 991:   
 992:          ////////////////////////////////////////////////////////////////////////////
 993:   
 994:          /// <summary>
 995:          /// Return a list of numbers, including changed-to numbers, that are related to the given service request list
 996:          /// </summary>
 997:          public static List<int> NumberListFromServiceRequestList(List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList)
 998:          {
 999:              List<int> list, changedList, numberList;
1000:   
1001:              if (serviceRequestList != null)
1002:              {
1003:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
1004:                  {
1005:                      list = (from sr in db.ServiceRequests where serviceRequestList.Contains(sr) select sr.Number).ToList();
1006:   
1007:                      changedList = (from srt in db.ServiceRequestTypes
1008:                                     where serviceRequestList.Contains(srt.ServiceRequest) || (srt.TypeId == 11 && serviceList.Contains(srt.Value))
1009:                                     select srt).ToList();
1010:   
1011:                      numberList = list.Union(changedList).Distinct().ToList();
1012:                  }
1013:              }
1014:              else numberList = new List<int>();
1015:   
1016:              return numberList;
1017:          }
1018:   
1019:          ////////////////////////////////////////////////////////////////////////////
1020:   
1021:          /// <summary>
1022:          /// Return list of service requests with number
1023:          /// </summary>
1024:          public static List<Ia.Ngn.Cl.Model.ServiceRequest> List(int number)
1025:          {
1026:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
1027:   
1028:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
1029:              {
1030:                  serviceRequestList = (from sr in db.ServiceRequests where sr.Number == number select sr).ToList();
1031:              }
1032:   
1033:              return serviceRequestList;
1034:          }
1035:   
1036:          ////////////////////////////////////////////////////////////////////////////
1037:   
1038:          /// <summary>
1039:          /// Return a list of service requests that have numbers within the passed domain list
1040:          /// </summary>
1041:          public static List<Ia.Ngn.Cl.Model.ServiceRequest> ListByDomain(List<int> domainList)
1042:          {
1043:              List<string> stringDomainList;
1044:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
1045:   
1046:              stringDomainList = new List<string>();
1047:   
1048:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
1049:              {
1050:                  if (domainList != null)
1051:                  {
1052:                      foreach (int i in domainList) stringDomainList.Add(i.ToString());
1053:   
1054:                      serviceRequestList = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService) where domainList.Any(u => sr.Number.ToString().StartsWith(u.ToString())) select sr).ToList();
1055:                  }
1056:                  else
1057:                  {
1058:                      serviceRequestList = new List<Ia.Ngn.Cl.Model.ServiceRequest>();
1059:                  }
1060:              }
1061:   
1062:              return serviceRequestList;
1063:          }
1064:   
1065:          ////////////////////////////////////////////////////////////////////////////
1066:   
1067:          /// <summary>
1068:          /// 
1069:          /// </summary>
1070:          public static Dictionary<string, Ia.Ngn.Cl.Model.ServiceRequest> ServiceToServiceRequestDictionary(List<int> domainList)
1071:          {
1072:              string key;
1073:              List<string> stringDomainList;
1074:              List<Ia.Ngn.Cl.Model.ServiceRequest> list;
1075:              Dictionary<string, Ia.Ngn.Cl.Model.ServiceRequest> dictionary;
1076:   
1077:              stringDomainList = new List<string>();
1078:   
1079:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
1080:              {
1081:                  if (domainList != null)
1082:                  {
1083:                      foreach (int i in domainList) stringDomainList.Add(i.ToString());
1084:   
1085:                      list = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService).Include(a => a.ServiceRequestTypes) where domainList.Any(u => sr.Number.ToString().StartsWith(u.ToString())) orderby sr.RequestDateTime ascending select sr).ToList();
1086:   
1087:                      dictionary = new Dictionary<string, Ia.Ngn.Cl.Model.ServiceRequest>(list.Count);
1088:   
1089:                      foreach (var sr in list)
1090:                      {
1091:                          key = sr.Number.ToString();
1092:   
1093:                          if (dictionary.ContainsKey(key))
1094:                          {
1095:                              dictionary[key] = sr;
1096:                          }
1097:                          else dictionary[key] = sr;
1098:                      }
1099:                  }
1100:                  else
1101:                  {
1102:                      dictionary = new Dictionary<string, Ia.Ngn.Cl.Model.ServiceRequest>();
1103:                  }
1104:              }
1105:   
1106:              return dictionary;
1107:          }
1108:   
1109:          ////////////////////////////////////////////////////////////////////////////
1110:   
1111:          /// <summary>
1112:          ///
1113:          /// </summary>
1114:          public static List<Ia.Ngn.Cl.Model.ServiceRequest> ReadListWithinDateTimeRange(DateTime startDateTime, DateTime endDateTime)
1115:          {
1116:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
1117:   
1118:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
1119:              {
1120:                  serviceRequestList = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService) where sr.RequestDateTime >= startDateTime && sr.RequestDateTime < endDateTime select sr).ToList();
1121:              }
1122:   
1123:              return serviceRequestList;
1124:          }
1125:   
1126:          ////////////////////////////////////////////////////////////////////////////
1127:   
1128:          /// <summary>
1129:          ///
1130:          /// </summary>
1131:          public static List<Ia.Ngn.Cl.Model.ServiceRequest> ReadListWithinIdRange(int start, int end)
1132:          {
1133:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
1134:   
1135:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
1136:              {
1137:                  serviceRequestList = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService) where sr.Id >= start && sr.Id <= end select sr).ToList();
1138:              }
1139:   
1140:              return serviceRequestList;
1141:          }
1142:   
1143:          ////////////////////////////////////////////////////////////////////////////
1144:   
1145:          /// <summary>
1146:          ///
1147:          /// </summary>
1148:          public static Hashtable NumberToCustomerAddressHashtable(List<int> domainList)
1149:          {
1150:              Hashtable ht;
1151:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
1152:   
1153:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
1154:              {
1155:                  if (domainList != null)
1156:                  {
1157:                      serviceRequestList = (from sr in db.ServiceRequests where domainList.Contains(sr.Number / 10000) || domainList.Contains(sr.Number / 1000) select sr).ToList();
1158:   
1159:                      ht = new Hashtable(serviceRequestList.Count);
1160:   
1161:                      foreach (Ia.Ngn.Cl.Model.ServiceRequest sr in serviceRequestList.OrderBy(u => u.Id)) ht[sr.Number.ToString()] = sr.CustomerAddress;
1162:                  }
1163:                  else
1164:                  {
1165:                      ht = new Hashtable();
1166:                  }
1167:              }
1168:   
1169:              return ht;
1170:          }
1171:   
1172:          ////////////////////////////////////////////////////////////////////////////
1173:   
1174:          /// <summary>
1175:          /// Return the latest RequestDateTime for number
1176:          /// </summary>
1177:          public static DateTime LatestRequestDateTime(int number)
1178:          {
1179:              DateTime dateTime;
1180:              Ia.Ngn.Cl.Model.ServiceRequest serviceRequest;
1181:              List<Ia.Ngn.Cl.Model.ServiceRequest> serviceRequestList;
1182:   
1183:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
1184:              {
1185:                  serviceRequestList = Ia.Ngn.Cl.Model.Data.ServiceRequest.List(number);
1186:   
1187:                  if (serviceRequestList.Count > 0)
1188:                  {
1189:                      serviceRequest = serviceRequestList.OrderByDescending(u => u.Id).FirstOrDefault();
1190:   
1191:                      dateTime = serviceRequest.RequestDateTime;
1192:                  }
1193:                  else
1194:                  {
1195:                      dateTime = DateTime.MinValue;
1196:                  }
1197:              }
1198:   
1199:              return dateTime;
1200:          }
1201:   
1202:          ////////////////////////////////////////////////////////////////////////////
1203:   
1204:          /// <summary>
1205:          ///
1206:          /// </summary>
1207:          public static string ToSimpleTextString(Ia.Ngn.Cl.Model.ServiceRequest serviceRequest)
1208:          {
1209:              StringBuilder sb;
1210:   
1211:              sb = new StringBuilder();
1212:   
1213:              // Id    Number    Serial    Status    RequestDateTime    Service    ServiceCategory    CustomerId    CustomerName    CustomerCategory    CustomerAddress    Balance
1214:              sb.AppendLine("Id: " + serviceRequest.Id);
1215:              sb.AppendLine("Number: " + serviceRequest.Number + "/" + serviceRequest.Serial);
1216:              //sb.AppendLine("Serial: " + serviceRequest.Serial);
1217:              sb.AppendLine("Status: " + Ia.Ngn.Cl.Model.Data.ServiceRequest.StatusSortedList[serviceRequest.Status].ToString());
1218:              sb.AppendLine("RequestDateTime: " + serviceRequest.RequestDateTime.ToString("yyyy-MM-dd HH:mm"));
1219:              sb.AppendLine("Service: " + Ia.Ngn.Cl.Model.Data.ServiceRequest.ServiceSortedList[serviceRequest.ServiceId].ToString());
1220:              //sb.AppendLine("ServiceCategory: " + Ia.Ngn.Cl.Model.Data.ServiceRequest.ServiceCategorySortedList[serviceRequest.ServiceCategoryId].ToString());
1221:              //sb.AppendLine("CustomerId: " + serviceRequest.CustomerId);
1222:              sb.AppendLine("CustomerName: " + serviceRequest.CustomerName);
1223:              //sb.AppendLine("CustomerCategory: " + Ia.Ngn.Cl.Model.Data.ServiceRequest.CustomerCategorySortedList[serviceRequest.CustomerCategoryId].ToString());
1224:   
1225:              sb.AppendLine("CustomerAddress: " + serviceRequest.CustomerAddress);
1226:              //sb.AppendLine("Balance: " + serviceRequest.Balance);
1227:   
1228:              return sb.ToString();
1229:          }
1230:   
1231:          ////////////////////////////////////////////////////////////////////////////
1232:   
1233:          /// <summary>
1234:          /// 
1235:          /// How to embed and access resources by using Visual C# http://support.microsoft.com/kb/319292/en-us
1236:          /// 
1237:          /// 1. Change the "Build Action" property of your XML file from "Content" to "Embedded Resource".
1238:          /// 2. Add "using System.Reflection".
1239:          /// 3. See sample below.
1240:          /// 
1241:          /// </summary>
1242:   
1243:          private static XDocument XDocument
1244:          {
1245:              get
1246:              {
1247:                  if (xDocument == null)
1248:                  {
1249:                      Assembly _assembly;
1250:                      StreamReader streamReader;
1251:   
1252:                      _assembly = Assembly.GetExecutingAssembly();
1253:                      streamReader = new StreamReader(_assembly.GetManifestResourceStream("Ia.Ngn.Cl.model.data.service-request.xml"));
1254:   
1255:                      try
1256:                      {
1257:                          if (streamReader.Peek() != -1)
1258:                          {
1259:                              xDocument = System.Xml.Linq.XDocument.Load(streamReader);
1260:                          }
1261:                      }
1262:                      catch (Exception)
1263:                      {
1264:                      }
1265:                      finally
1266:                      {
1267:                      }
1268:                  }
1269:   
1270:                  return xDocument;
1271:              }
1272:          }
1273:   
1274:          ////////////////////////////////////////////////////////////////////////////    
1275:          ////////////////////////////////////////////////////////////////////////////    
1276:      }
1277:   
1278:      ////////////////////////////////////////////////////////////////////////////
1279:      ////////////////////////////////////////////////////////////////////////////
1280:  }