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

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Access

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

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

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Data.Entity;
   4:  using System.Linq;
   5:  using System.Text;
   6:   
   7:  namespace Ia.Ngn.Cl.Model.Data
   8:  {
   9:      ////////////////////////////////////////////////////////////////////////////
  10:   
  11:      /// <summary publish="true">
  12:      /// Access support class for Next Generation Network (NGN) data model.
  13:      /// </summary>
  14:      /// 
  15:      /// <remarks> 
  16:      /// Copyright © 2006-2017 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 partial class Access
  29:      {
  30:          /// <summary/>
  31:          public Access() { }
  32:   
  33:          ////////////////////////////////////////////////////////////////////////////
  34:   
  35:          /// <summary>
  36:          ///
  37:          /// </summary>
  38:          public static void Create(Ia.Ngn.Cl.Model.Access newAccess, out Ia.Cl.Model.Result result)
  39:          {
  40:              Ia.Ngn.Cl.Model.Access access;
  41:   
  42:              result = new Ia.Cl.Model.Result();
  43:   
  44:              try
  45:              {
  46:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
  47:                  {
  48:                      // check if it exists already
  49:                      access = (from a in db.Accesses where a.Id == newAccess.Id select a).SingleOrDefault();
  50:   
  51:                      if (access == null)
  52:                      {
  53:                          newAccess.Created = newAccess.Updated = DateTime.UtcNow.AddHours(3);
  54:   
  55:                          db.Accesses.Add(newAccess);
  56:                          db.SaveChanges();
  57:   
  58:                          result.AddSuccess("Access record " + newAccess.Name + " created. ");
  59:                      }
  60:                      else
  61:                      {
  62:                          result.AddError("Access record " + newAccess.Name + " already exists. ");
  63:                      }
  64:                  }
  65:              }
  66:              catch (Exception ex)
  67:              {
  68:                  result.AddError("Exception: Access record was not created: " + ex.Message);
  69:              }
  70:          }
  71:   
  72:          ////////////////////////////////////////////////////////////////////////////
  73:   
  74:          /// <summary>
  75:          ///
  76:          /// </summary>
  77:          public static Ia.Ngn.Cl.Model.Access Read(Ia.Ngn.Cl.Model.Ngn db, string id)
  78:          {
  79:              Ia.Ngn.Cl.Model.Access access;
  80:   
  81:              access = (from a in db.Accesses where a.Id == id select a).SingleOrDefault();
  82:   
  83:              return access;
  84:          }
  85:   
  86:          ////////////////////////////////////////////////////////////////////////////
  87:   
  88:          /// <summary>
  89:          ///
  90:          /// </summary>
  91:          public static Ia.Ngn.Cl.Model.Access Read(string id)
  92:          {
  93:              Ia.Ngn.Cl.Model.Access item;
  94:   
  95:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
  96:              {
  97:                  item = (from a in db.Accesses where a.Id == id select a).SingleOrDefault();
  98:              }
  99:   
 100:              return item;
 101:          }
 102:   
 103:          ////////////////////////////////////////////////////////////////////////////
 104:   
 105:          /// <summary>
 106:          ///
 107:          /// </summary>
 108:          public static Ia.Ngn.Cl.Model.Access Read(int oltId, int ponNumber, int ontNumber)
 109:          {
 110:              Ia.Ngn.Cl.Model.Access item;
 111:   
 112:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 113:              {
 114:                  item = (from a in db.Accesses where a.Olt == oltId && a.Pon == ponNumber && a.Ont == ontNumber select a).SingleOrDefault();
 115:              }
 116:   
 117:              return item;
 118:          }
 119:   
 120:          ////////////////////////////////////////////////////////////////////////////
 121:   
 122:          /// <summary>
 123:          ///
 124:          /// </summary>
 125:          public static Ia.Ngn.Cl.Model.Access Read(Ia.Ngn.Cl.Model.Ngn db, int oltId, int ponNumber, int ontNumber)
 126:          {
 127:              return (from a in db.Accesses where a.Olt == oltId && a.Pon == ponNumber && a.Ont == ontNumber select a).SingleOrDefault();
 128:          }
 129:   
 130:          ////////////////////////////////////////////////////////////////////////////
 131:   
 132:          /// <summary>
 133:          ///
 134:          /// </summary>
 135:          public static Ia.Ngn.Cl.Model.Access ReadBySerialInNote(string serial)
 136:          {
 137:              List<Ia.Ngn.Cl.Model.Access> list;
 138:   
 139:              if (!string.IsNullOrEmpty(serial))
 140:              {
 141:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 142:                  {
 143:                      list = (from a in db.Accesses where a.Note.Contains(serial) select a).ToList();
 144:                  }
 145:              }
 146:              else list = new List<Ia.Ngn.Cl.Model.Access>();
 147:   
 148:              return list.FirstOrDefault();
 149:          }
 150:   
 151:          ////////////////////////////////////////////////////////////////////////////
 152:   
 153:          /// <summary>
 154:          ///
 155:          /// </summary>
 156:          public static List<Ia.Ngn.Cl.Model.Access> ReadListByPaci(string paci)
 157:          {
 158:              List<Ia.Ngn.Cl.Model.Access> list;
 159:   
 160:              if (!string.IsNullOrEmpty(paci))
 161:              {
 162:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 163:                  {
 164:                      list = (from a in db.Accesses where a.Paci == paci select a).ToList();
 165:                  }
 166:              }
 167:              else list = new List<Ia.Ngn.Cl.Model.Access>();
 168:   
 169:              return list;
 170:          }
 171:   
 172:          ////////////////////////////////////////////////////////////////////////////
 173:   
 174:          /// <summary>
 175:          ///
 176:          /// </summary>
 177:          public static bool PaciExists(string paci)
 178:          {
 179:              bool paciExists;
 180:   
 181:              if (!string.IsNullOrEmpty(paci))
 182:              {
 183:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 184:                  {
 185:                      paciExists = (from a in db.Accesses where a.Paci == paci select a).Any();
 186:                  }
 187:              }
 188:              else paciExists = false;
 189:   
 190:              return paciExists;
 191:          }
 192:   
 193:          ////////////////////////////////////////////////////////////////////////////
 194:   
 195:          /// <summary>
 196:          ///
 197:          /// </summary>
 198:          public static List<Ia.Ngn.Cl.Model.Access> ReadListByAreaId(int areaId)
 199:          {
 200:              List<Ia.Ngn.Cl.Model.Access> itemList;
 201:   
 202:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 203:              {
 204:                  itemList = (from a in db.Accesses where a.AreaId == areaId select a).ToList();
 205:              }
 206:   
 207:              return itemList;
 208:          }
 209:   
 210:          ////////////////////////////////////////////////////////////////////////////
 211:   
 212:          /// <summary>
 213:          ///
 214:          /// </summary>
 215:          public static int AreaIdFromOltIdAndPon(int oltId, int ponNumber)
 216:          {
 217:              int areaId;
 218:   
 219:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 220:              {
 221:                  areaId = (from a in db.Accesses where a.Olt == oltId && a.Pon == ponNumber select a.AreaId).FirstOrDefault();
 222:   
 223:                  if (areaId == 0)
 224:                  {
 225:                      areaId = (from a in db.Accesses where a.Olt == oltId select a.AreaId).FirstOrDefault();
 226:                  }
 227:              }
 228:   
 229:              return areaId;
 230:          }
 231:   
 232:          ////////////////////////////////////////////////////////////////////////////
 233:   
 234:          /// <summary>
 235:          ///
 236:          /// </summary>
 237:          public static List<Ia.Ngn.Cl.Model.Access> List()
 238:          {
 239:              List<Ia.Ngn.Cl.Model.Access> list;
 240:   
 241:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 242:              {
 243:                  list = (from a in db.Accesses select a).ToList();
 244:              }
 245:   
 246:              return list;
 247:          }
 248:   
 249:          ////////////////////////////////////////////////////////////////////////////
 250:   
 251:          /// <summary>
 252:          ///
 253:          /// </summary>
 254:          public static List<Ia.Ngn.Cl.Model.Access> List(Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Olt olt)
 255:          {
 256:              List<Ia.Ngn.Cl.Model.Access> list;
 257:   
 258:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 259:              {
 260:                  list = (from a in db.Accesses where a.Olt == olt.Id select a).ToList();
 261:              }
 262:   
 263:              return list;
 264:          }
 265:   
 266:          ////////////////////////////////////////////////////////////////////////////
 267:   
 268:          /// <summary>
 269:          ///
 270:          /// </summary>
 271:          public static List<Ia.Ngn.Cl.Model.Access> List(int oltId, int ponNumber)
 272:          {
 273:              List<Ia.Ngn.Cl.Model.Access> itemList;
 274:   
 275:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 276:              {
 277:                  itemList = (from a in db.Accesses where a.Olt == oltId && a.Pon == ponNumber select a).ToList();
 278:              }
 279:   
 280:              return itemList;
 281:          }
 282:   
 283:          ////////////////////////////////////////////////////////////////////////////
 284:   
 285:          /// <summary>
 286:          ///
 287:          /// </summary>
 288:          public static bool Update(Ia.Ngn.Cl.Model.Access updatedAccess, out string result)
 289:          {
 290:              bool b;
 291:              Ia.Ngn.Cl.Model.Access access;
 292:   
 293:              b = false;
 294:              result = string.Empty;
 295:   
 296:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 297:              {
 298:                  access = (from a in db.Accesses where a.Id == updatedAccess.Id select a).SingleOrDefault();
 299:   
 300:                  if (access.Update(updatedAccess))
 301:                  {
 302:                      db.Accesses.Attach(access);
 303:                      db.Entry(access).State = System.Data.Entity.EntityState.Modified;
 304:                  }
 305:   
 306:                  db.SaveChanges();
 307:   
 308:                  b = true;
 309:              }
 310:   
 311:              return b;
 312:          }
 313:   
 314:          ////////////////////////////////////////////////////////////////////////////
 315:   
 316:          /// <summary>
 317:          ///
 318:          /// </summary>
 319:          public static void Delete(string id, out Ia.Cl.Model.Result result)
 320:          {
 321:              result = new Ia.Cl.Model.Result();
 322:   
 323:              try
 324:              {
 325:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 326:                  {
 327:                      var v = (from a in db.Accesses where a.Id == id select a).FirstOrDefault();
 328:   
 329:                      if (v != null)
 330:                      {
 331:                          db.Accesses.Remove(v);
 332:                          db.SaveChanges();
 333:   
 334:                          result.AddSuccess("Access record deleted. ");
 335:                      }
 336:                      else
 337:                      {
 338:                          result.AddWarning("Access record does not exist. ");
 339:                      }
 340:   
 341:                  }
 342:              }
 343:              catch (Exception ex)
 344:              {
 345:                  result.AddError("Exception: Access record was not deleted: " + ex.Message);
 346:              }
 347:          }
 348:   
 349:          ////////////////////////////////////////////////////////////////////////////
 350:          ////////////////////////////////////////////////////////////////////////////
 351:   
 352:          /// <summary>
 353:          ///
 354:          /// </summary>
 355:          public static List<int> OntsNotInAccessList(List<Ia.Ngn.Cl.Model.Access> accessList)
 356:          {
 357:              // below: produce a list of ONTs between 1 and 32 that are not in ontList
 358:              List<int> ontNotInListArrayList = new List<int>(32);
 359:   
 360:              for (int i = 1; i <= 32; i++)
 361:              {
 362:                  if (accessList != null)
 363:                  {
 364:                      if ((from a in accessList where a.Ont == i select a).SingleOrDefault() == null)
 365:                      {
 366:                          ontNotInListArrayList.Add(i);
 367:                      }
 368:                      else
 369:                      {
 370:                      }
 371:                  }
 372:                  else
 373:                  {
 374:                      ontNotInListArrayList.Add(i);
 375:                  }
 376:              }
 377:   
 378:              return ontNotInListArrayList;
 379:          }
 380:   
 381:          ////////////////////////////////////////////////////////////////////////////
 382:   
 383:          /// <summary>
 384:          ///
 385:          /// </summary>
 386:          public static List<int> OltIdDistinctList
 387:          {
 388:              get
 389:              {
 390:                  List<int> list;
 391:   
 392:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 393:                  {
 394:                      list = (from o in db.Accesses select o.Olt).Distinct().OrderBy(u => u).ToList();
 395:                  }
 396:   
 397:                  return list;
 398:              }
 399:          }
 400:   
 401:          ////////////////////////////////////////////////////////////////////////////
 402:   
 403:          /// <summary>
 404:          ///
 405:          /// </summary>
 406:          public static Ia.Ngn.Cl.Model.Access ReadByAccessName(string accessName)
 407:          {
 408:              int oltId, pon, ont;
 409:              Ia.Ngn.Cl.Model.Access access;
 410:   
 411:              // below: this expects accessName in exact format like SUR.12.3
 412:   
 413:              Ia.Ngn.Cl.Model.Business.Access.ExtractOltIdAndPonNumberAndOntNumberFromOntName(accessName, out oltId, out pon, out ont);
 414:   
 415:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 416:              {
 417:                  access = (from a in db.Accesses where a.Olt == oltId && a.Pon == pon && a.Ont == ont select a).SingleOrDefault();
 418:              }
 419:   
 420:              return access;
 421:          }
 422:   
 423:          ////////////////////////////////////////////////////////////////////////////
 424:   
 425:          /// <summary>
 426:          ///
 427:          /// </summary>
 428:          public static List<Ia.Ngn.Cl.Model.Access> ListUsingOntFamilyType(int familyTypeId)
 429:          {
 430:              // below: return access list for family type
 431:              List<Ia.Ngn.Cl.Model.Access> noList, huList, list;
 432:              List<string> equipmentTypeStringList;
 433:   
 434:              list = new List<Ia.Ngn.Cl.Model.Access>();
 435:   
 436:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 437:              {
 438:                  noList = (from a in db.Accesses join o in db.Onts on a equals o.Access where o.FamilyTypeId == familyTypeId select a).ToList();
 439:   
 440:                  equipmentTypeStringList = (from e in Ia.Ngn.Cl.Model.Business.Huawei.Ont.EquipmentTypeList
 441:                                             where e.FamilyType == (Ia.Ngn.Cl.Model.Business.Huawei.Ont.FamilyType)familyTypeId
 442:                                             select e.Name).ToList();
 443:   
 444:                  huList = (from a in db.Accesses join o in db.EmsOnts on a equals o.Access where equipmentTypeStringList.Contains(o.EQUIPMENTID) select a).ToList();
 445:              }
 446:   
 447:              list.AddRange(noList);
 448:              list.AddRange(huList);
 449:   
 450:              return list.ToList();
 451:          }
 452:   
 453:          ////////////////////////////////////////////////////////////////////////////
 454:   
 455:          /// <summary>
 456:          ///
 457:          /// </summary>
 458:          public static List<string> ReadAccessNameListUsingKuwaitNgnAreaIdAndBlock(int kuwaitAreaId, string block)
 459:          {
 460:              Dictionary<string, string> di;
 461:              List<string> accessIdList, accessNameList;
 462:   
 463:              if (kuwaitAreaId > 0 && !string.IsNullOrEmpty(block))
 464:              {
 465:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 466:                  {
 467:                      accessIdList = (from a in db.Accesses where a.AreaId == kuwaitAreaId && a.Block == block select a.Id).ToList();
 468:                      di = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.OntAccessIdToOntAccessNameDictionary;
 469:   
 470:                      if (accessIdList != null && accessIdList.Count > 0)
 471:                      {
 472:                          accessNameList = new List<string>(accessIdList.Count);
 473:   
 474:                          foreach (string accessId in accessIdList)
 475:                          {
 476:                              if (di.ContainsKey(accessId)) accessNameList.Add(di[accessId]);
 477:                          }
 478:                      }
 479:                      else
 480:                      {
 481:                          accessNameList = null;
 482:                      }
 483:                  }
 484:              }
 485:              else
 486:              {
 487:                  accessNameList = null;
 488:              }
 489:   
 490:              return accessNameList;
 491:          }
 492:   
 493:          ////////////////////////////////////////////////////////////////////////////
 494:   
 495:          /// <summary>
 496:          ///
 497:          /// </summary>
 498:          public static List<Ia.Ngn.Cl.Model.Access> ListByKuwaitNgnAreaId(int kuwaitAreaId)
 499:          {
 500:              List<Ia.Ngn.Cl.Model.Access> list;
 501:   
 502:              if (kuwaitAreaId > 0)
 503:              {
 504:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 505:                  {
 506:                      list = (from a in db.Accesses where a.AreaId == kuwaitAreaId select a).ToList();
 507:                  }
 508:              }
 509:              else
 510:              {
 511:                  list = null;
 512:              }
 513:   
 514:              return list;
 515:          }
 516:   
 517:          ////////////////////////////////////////////////////////////////////////////
 518:   
 519:          /// <summary>
 520:          ///
 521:          /// </summary>
 522:          public static Dictionary<string, string> DistinctAccessKuwaitNgnAreaIdAndBlockDictionary
 523:          {
 524:              get
 525:              {
 526:                  int kuwaitNgnAreaId, block;
 527:                  string kuwaitNgnAreaIdString, blockString, kuwaitNgnAreaBlockValue, kuwaitNgnAreaNameArabicName;
 528:                  Dictionary<string, string> tempDictionary;
 529:                  Dictionary<string, string> dictionary;
 530:   
 531:                  dictionary = new Dictionary<string, string>();
 532:                  tempDictionary = new Dictionary<string, string>();
 533:   
 534:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 535:                  {
 536:                      tempDictionary = (from a in db.Accesses select new { a.AreaId, a.Block }).Distinct().OrderBy(u => u.AreaId).ThenBy(u => u.Block).ToDictionary(m => m.AreaId + "," + m.Block, m => m.AreaId + "," + m.Block);
 537:   
 538:                      if (tempDictionary != null && tempDictionary.Count > 0)
 539:                      {
 540:                          foreach (KeyValuePair<string, string> kvp in tempDictionary)
 541:                          {
 542:                              kuwaitNgnAreaIdString = kvp.Key.Split(',')[0].ToString();
 543:                              blockString = kvp.Key.Split(',')[1].ToString();
 544:   
 545:                              if (int.TryParse(kuwaitNgnAreaIdString, out kuwaitNgnAreaId) && int.TryParse(blockString, out block))
 546:                              {
 547:                                  kuwaitNgnAreaNameArabicName = (from kna in Ia.Ngn.Cl.Model.Data.Service.KuwaitNgnAreaList where kna.Id == kuwaitNgnAreaId select kna.NameArabicName).SingleOrDefault();
 548:   
 549:                                  kuwaitNgnAreaBlockValue = kuwaitNgnAreaNameArabicName + ", block " + block;
 550:   
 551:                                  dictionary.Add(kvp.Key, kuwaitNgnAreaBlockValue);
 552:                              }
 553:                              else
 554:                              {
 555:                              }
 556:                          }
 557:                      }
 558:                      else
 559:                      {
 560:                      }
 561:                  }
 562:   
 563:                  return dictionary;
 564:              }
 565:          }
 566:   
 567:          ////////////////////////////////////////////////////////////////////////////
 568:   
 569:          /// <summary>
 570:          ///
 571:          /// </summary>
 572:          public static Dictionary<int, string> DistinctAccessKuwaitNgnAreaIdToNameArabicNameDictionary
 573:          {
 574:              get
 575:              {
 576:                  string kuwaitNgnAreaNameArabicName;
 577:                  List<int> list;
 578:                  Dictionary<int, string> dictionary;
 579:   
 580:                  dictionary = new Dictionary<int, string>();
 581:                  list = new List<int>();
 582:   
 583:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 584:                  {
 585:                      list = (from a in db.Accesses select a.AreaId).Distinct().ToList();
 586:   
 587:                      if (list.Count > 0)
 588:                      {
 589:                          foreach (int i in list)
 590:                          {
 591:                              kuwaitNgnAreaNameArabicName = (from kna in Ia.Ngn.Cl.Model.Data.Service.KuwaitNgnAreaList where kna.Id == i select kna.NameArabicName).SingleOrDefault();
 592:   
 593:                              dictionary.Add(i, kuwaitNgnAreaNameArabicName);
 594:                          }
 595:                      }
 596:                      else
 597:                      {
 598:                      }
 599:                  }
 600:   
 601:                  return dictionary;
 602:              }
 603:          }
 604:   
 605:          ////////////////////////////////////////////////////////////////////////////
 606:   
 607:          /// <summary>
 608:          ///
 609:          /// </summary>
 610:          public static Dictionary<string, int> IdToKuwaitNgnAreaIdDictionary
 611:          {
 612:              get
 613:              {
 614:                  Dictionary<string, int> dictionary;
 615:   
 616:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 617:                  {
 618:                      dictionary = (from a in db.Accesses select new { a.Id, a.AreaId }).ToDictionary(u => u.Id, u => u.AreaId);
 619:                  }
 620:   
 621:                  return dictionary;
 622:              }
 623:          }
 624:   
 625:          ////////////////////////////////////////////////////////////////////////////
 626:   
 627:          /// <summary>
 628:          ///
 629:          /// </summary>
 630:          public static Dictionary<string, string> IdToKuwaitNgnAreaSymbolDictionary
 631:          {
 632:              get
 633:              {
 634:                  Dictionary<string, int> accessIdToKuwaitNgnAreaIdDictionary;
 635:                  Dictionary<int, string> areaIdToSymbolDictionary;
 636:                  Dictionary<string, string> dictionary;
 637:   
 638:                  accessIdToKuwaitNgnAreaIdDictionary = Ia.Ngn.Cl.Model.Data.Access.IdToKuwaitNgnAreaIdDictionary;
 639:                  areaIdToSymbolDictionary = Ia.Ngn.Cl.Model.Data.Service.AreaIdToSymbolDictionary;
 640:   
 641:                  dictionary = new Dictionary<string, string>(accessIdToKuwaitNgnAreaIdDictionary.Count);
 642:   
 643:                  foreach (KeyValuePair<string, int> kvp in accessIdToKuwaitNgnAreaIdDictionary)
 644:                  {
 645:                      if (areaIdToSymbolDictionary.ContainsKey(kvp.Value)) dictionary[kvp.Key] = areaIdToSymbolDictionary[kvp.Value];
 646:                  }
 647:   
 648:                  return dictionary;
 649:              }
 650:          }
 651:   
 652:          ////////////////////////////////////////////////////////////////////////////
 653:   
 654:          /// <summary>
 655:          ///
 656:          /// </summary>
 657:          public static Dictionary<string, int> IdToOltIdDictionary
 658:          {
 659:              get
 660:              {
 661:                  Dictionary<string, int> dictionary;
 662:   
 663:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 664:                  {
 665:                      dictionary = (from a in db.Accesses select new { a.Id, a.Olt }).ToDictionary(u => u.Id, u => u.Olt);
 666:                  }
 667:   
 668:                  return dictionary;
 669:              }
 670:          }
 671:   
 672:          ////////////////////////////////////////////////////////////////////////////
 673:   
 674:          /// <summary>
 675:          ///
 676:          /// </summary>
 677:          public static Dictionary<string, Ia.Ngn.Cl.Model.Access> IdToAccessDictionary
 678:          {
 679:              get
 680:              {
 681:                  Dictionary<string, Ia.Ngn.Cl.Model.Access> dictionary;
 682:   
 683:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 684:                  {
 685:                      dictionary = (from a in db.Accesses select new { AccessId = a.Id, Access = a }).ToDictionary(u => u.AccessId, u => u.Access);
 686:                  }
 687:   
 688:                  return dictionary;
 689:              }
 690:          }
 691:   
 692:          ////////////////////////////////////////////////////////////////////////////
 693:   
 694:          /// <summary>
 695:          ///
 696:          /// </summary>
 697:          public static Dictionary<string, Ia.Ngn.Cl.Model.Access> IdToHuaweiAccessDictionary
 698:          {
 699:              get
 700:              {
 701:                  List<int> oltIdList;
 702:                  Dictionary<string, Ia.Ngn.Cl.Model.Access> dictionary;
 703:   
 704:                  oltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.HuaweiOltIdList();
 705:   
 706:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 707:                  {
 708:                      dictionary = (from a in db.Accesses where oltIdList.Contains(a.Olt) select new { AccessId = a.Id, Access = a }).ToDictionary(u => u.AccessId, u => u.Access);
 709:                  }
 710:   
 711:                  return dictionary;
 712:              }
 713:          }
 714:   
 715:          ////////////////////////////////////////////////////////////////////////////
 716:   
 717:          /// <summary>
 718:          ///
 719:          /// </summary>
 720:          public static List<string> IdList
 721:          {
 722:              get
 723:              {
 724:                  List<string> list;
 725:   
 726:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 727:                  {
 728:                      list = (from a in db.Accesses select a.Id).ToList();
 729:                  }
 730:   
 731:                  return list;
 732:              }
 733:          }
 734:   
 735:          ////////////////////////////////////////////////////////////////////////////
 736:   
 737:          /// <summary>
 738:          ///
 739:          /// </summary>
 740:          public static List<Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock> FamilyStatisticInKuwaitNgnAreaAndBlockTable
 741:          {
 742:              // FamilyType { Undefined = 0, Sfu = 1, Soho = 2, Mdu = 3 };
 743:   
 744:              get
 745:              {
 746:                  Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock item;
 747:                  List<Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock> list, sfu, soho, mdu;
 748:   
 749:                  list = new List<Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock>();
 750:   
 751:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 752:                  {
 753:                      sfu = (from a in db.Accesses join o in db.Onts on a.Id equals o.Access.Id where o.FamilyTypeId == 1 group a by new { a.AreaId, a.Block } into g select new Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock() { AreaId = g.Key.AreaId, Block = g.Key.Block, Sfu = g.Count() }).ToList();
 754:                      soho = (from a in db.Accesses join o in db.Onts on a.Id equals o.Access.Id where o.FamilyTypeId == 2 group a by new { a.AreaId, a.Block } into g select new Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock() { AreaId = g.Key.AreaId, Block = g.Key.Block, Soho = g.Count() }).ToList();
 755:                      mdu = (from a in db.Accesses join o in db.Onts on a.Id equals o.Access.Id where o.FamilyTypeId == 3 group a by new { a.AreaId, a.Block } into g select new Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock() { AreaId = g.Key.AreaId, Block = g.Key.Block, Mdu = g.Count() }).ToList();
 756:   
 757:                      foreach (Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock a in sfu) list.Add(new Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock { AreaId = a.AreaId, Block = a.Block, Sfu = a.Sfu });
 758:   
 759:                      foreach (Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock a in soho)
 760:                      {
 761:                          item = (from b in list where b.AreaId == a.AreaId && b.Block == a.Block select b).SingleOrDefault();
 762:   
 763:                          if (item != null) item.Soho = a.Soho;
 764:                          else list.Add(new Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock { AreaId = a.AreaId, Block = a.Block, Soho = a.Soho });
 765:                      }
 766:   
 767:                      foreach (Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock a in mdu)
 768:                      {
 769:                          item = (from b in list where b.AreaId == a.AreaId && b.Block == a.Block select b).SingleOrDefault();
 770:   
 771:                          if (item != null) item.Mdu = a.Mdu;
 772:                          else list.Add(new Ia.Ngn.Cl.Model.Ui.Maintenance.AccessFamilyTypeAreaBlock { AreaId = a.AreaId, Block = a.Block, Mdu = a.Mdu });
 773:                      }
 774:   
 775:                      list = list.OrderBy(u => u.AreaId).ThenBy(u => u.Block).ToList();
 776:   
 777:                      return list;
 778:                  }
 779:              }
 780:          }
 781:   
 782:          ////////////////////////////////////////////////////////////////////////////
 783:   
 784:          /// <summary>
 785:          ///
 786:          /// </summary>
 787:          public static List<Ia.Ngn.Cl.Model.Access> ListOfAccessesWithProvisionedAndReadyOntsAndEmsOntsButDoNotExistInCustomerDepartmentDatabase()
 788:          {
 789:              return ListOfAccessesWithProvisionedAndReadyOntsButDoNotExistInCustomerDepartmentDatabaseBySiteId(0);
 790:          }
 791:   
 792:          ////////////////////////////////////////////////////////////////////////////
 793:   
 794:          /// <summary>
 795:          ///
 796:          /// </summary>
 797:          public static List<Ia.Ngn.Cl.Model.Access> ListOfAccessesWithProvisionedAndReadyOntsButDoNotExistInCustomerDepartmentDatabaseBySiteId(int siteId)
 798:          {
 799:              Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Site site;
 800:              List<int> nokiaOltIdList, huaweiOltIdList;
 801:              List<Ia.Ngn.Cl.Model.Access> list, nokiaList, huaweiOntList;
 802:   
 803:              // this is related to AccessesWithProvisionedAndReadyOntsIdToKuwaitNgnAreaIdDictionary
 804:   
 805:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 806:              {
 807:                  if (siteId == 0)
 808:                  {
 809:                      // if siteId is 0 we will return results for all sites
 810:   
 811:                      //nokiaOltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.NokiaOltIdList();
 812:   
 813:                      // Nokia
 814:                      nokiaList = (from a in db.Accesses
 815:                                       // join oid in nokiaOltIdList on a.Olt equals oid
 816:                                   join o in db.Onts on a equals o.Access
 817:                                   join osv in db.OntServiceVoips on o equals osv.Ont
 818:                                   join sro in db.ServiceRequestOnts on a equals sro.Access into gj
 819:                                   from subsro in gj.DefaultIfEmpty()
 820:                                   where o.StateId == (int)Ia.Ngn.Cl.Model.Business.Nokia.Ams.BellcoreState.IsNr
 821:                                   && o.Serial != null
 822:                                   && o.Serial != "ALCL00000000"
 823:                                   && o.FamilyTypeId != 0
 824:                                   && o.ActiveSoftware != null
 825:                                   //&& o.ActiveSoftware == o.PlannedSoftware
 826:                                   && osv.Ip != null
 827:                                   && subsro.Access.Id == null
 828:                                   select a).Distinct().Include(c => c.Onts).Include(c => c.EmsOnts).ToList();
 829:   
 830:                      // Huawei
 831:                      huaweiOltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.HuaweiOltIdList();
 832:   
 833:                      huaweiOntList = (from a in db.Accesses
 834:                                       join o in db.EmsOnts on a equals o.Access
 835:                                       join sro in db.ServiceRequestOnts on a equals sro.Access into gj
 836:                                       from subsro in gj.DefaultIfEmpty()
 837:                                       where o.EQUIPMENTID != string.Empty //.FamilyTypeId != 0
 838:                                       && huaweiOltIdList.Contains(a.Olt)
 839:                                       && subsro.Access.Id == null
 840:                                       select a).Distinct().Include(c => c.Onts).Include(c => c.EmsOnts).ToList();
 841:   
 842:                      list = nokiaList.Union(huaweiOntList).ToList();
 843:                  }
 844:                  else
 845:                  {
 846:                      site = (from a in Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.SiteList where a.Id == siteId select a).SingleOrDefault();
 847:   
 848:                      nokiaOltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.NokiaOltIdList(siteId);
 849:   
 850:                      // Nokia
 851:                      nokiaList = (from a in db.Accesses
 852:                                   join oid in nokiaOltIdList on a.Olt equals oid
 853:                                   join o in db.Onts on a equals o.Access
 854:                                   join osv in db.OntServiceVoips on o equals osv.Ont
 855:                                   join sro in db.ServiceRequestOnts on a equals sro.Access into gj
 856:                                   from subsro in gj.DefaultIfEmpty()
 857:                                   where o.StateId == (int)Ia.Ngn.Cl.Model.Business.Nokia.Ams.BellcoreState.IsNr
 858:                                   && o.Serial != null
 859:                                   && o.Serial != "ALCL00000000"
 860:                                   && o.FamilyTypeId != 0
 861:                                   && o.ActiveSoftware != null
 862:                                   //&& o.ActiveSoftware == o.PlannedSoftware
 863:                                   && osv.Ip != null
 864:                                   && subsro.Access.Id == null
 865:                                   select a).Distinct().Include(c => c.Onts).Include(c => c.EmsOnts).ToList();
 866:   
 867:                      // Huawei
 868:                      huaweiOltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.HuaweiOltIdList(siteId);
 869:   
 870:                      huaweiOntList = (from a in db.Accesses
 871:                                       join o in db.EmsOnts on a equals o.Access
 872:                                       join sro in db.ServiceRequestOnts on a equals sro.Access into gj
 873:                                       from subsro in gj.DefaultIfEmpty()
 874:                                       where o.EQUIPMENTID != string.Empty //.FamilyTypeId != 0
 875:                                       && huaweiOltIdList.Contains(a.Olt)
 876:                                       && subsro.Access.Id == null
 877:                                       select a).Distinct().Include(c => c.Onts).Include(c => c.EmsOnts).ToList();
 878:   
 879:                      list = nokiaList.Union(huaweiOntList).ToList();
 880:                  }
 881:              }
 882:   
 883:              return list.OrderBy(u => u.Id).ToList();
 884:          }
 885:   
 886:          ////////////////////////////////////////////////////////////////////////////
 887:   
 888:          /// <summary>
 889:          ///
 890:          /// </summary>
 891:          public static List<Ia.Ngn.Cl.Model.Access> AccessesWithMismatchedInfomationInServiceRequestOntList()
 892:          {
 893:              List<Ia.Ngn.Cl.Model.Access> list;
 894:              //Dictionary<int, string> areaIdToSymbolDictionary;
 895:   
 896:              //areaIdToSymbolDictionary = Ia.Ngn.Cl.Model.Data.Service.AreaIdToSymbolDictionary;
 897:   
 898:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 899:              {
 900:                  // I will ignore area because its fixed with access id, which is shared by both Access and ServiceRequestOnt
 901:   
 902:                  list = (from a in db.Accesses
 903:                          join o in db.Onts on a equals o.Access
 904:                          join osv in db.OntServiceVoips on o equals osv.Ont
 905:                          join sro in db.ServiceRequestOnts on a equals sro.Access
 906:                          //join areaSymbol in areaIdToSymbolDictionary on a.AreaId equals areaSymbol.Key
 907:                          //where sro.AreaSymbol != areaSymbol.Value
 908:                          where (sro.Block != a.Block && !string.IsNullOrEmpty(a.Block))
 909:                          || (sro.Street != a.Street && !string.IsNullOrEmpty(a.Street))
 910:                          || (sro.PremisesOld != a.PremisesOld && !string.IsNullOrEmpty(a.PremisesOld))
 911:                          || (sro.PremisesNew != a.PremisesNew && !string.IsNullOrEmpty(a.PremisesNew))
 912:                          || (sro.Paci != a.Paci && !string.IsNullOrEmpty(a.Paci))
 913:                          //|| (sro.PossibleNumberOfTd != a.PossibleNumberOfTd)
 914:                          //|| (sro.PossibleNumberOfEthernet != a.PossibleNumberOfEthernet)
 915:                          select a).Distinct().Include(c => c.Onts).Include(c => c.EmsOnts).ToList();
 916:              }
 917:   
 918:              return list.OrderBy(u => u.Id).ToList();
 919:          }
 920:   
 921:          ////////////////////////////////////////////////////////////////////////////
 922:   
 923:          /// <summary>
 924:          ///
 925:          /// </summary>
 926:          public static Dictionary<string, int> AccessesWithProvisionedAndReadyOntsIdToKuwaitNgnAreaIdDictionary
 927:          {
 928:              get
 929:              {
 930:                  Dictionary<string, int> dictionary, nokiaDictionary, huaweiDictionary;
 931:                  List<int> huaweiOltIdList;//, nokiaOltIdList;
 932:   
 933:                  // this is related to ListOfAccessesWithProvisionedAndReadyOntsButDoNotExistInCustomerDepartmentDatabaseBySiteId()
 934:   
 935:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 936:                  {
 937:                      //nokiaOltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.NokiaOltIdList();
 938:   
 939:                      // Nokia
 940:                      nokiaDictionary = (from a in db.Accesses
 941:                                         join o in db.Onts on a equals o.Access
 942:                                         join osv in db.OntServiceVoips on o equals osv.Ont
 943:                                         join sro in db.ServiceRequestOnts on a equals sro.Access
 944:                                         where o.StateId == (int)Ia.Ngn.Cl.Model.Business.Nokia.Ams.BellcoreState.IsNr // ?
 945:                                         && o.Serial != null
 946:                                         && o.Serial != "ALCL00000000"
 947:                                         && o.FamilyTypeId != 0 // ?
 948:                                         && o.ActiveSoftware != null
 949:                                         //&& o.ActiveSoftware == o.PlannedSoftware
 950:                                         && osv.Ip != null
 951:                                         select new { a.Id, a.AreaId }).Distinct().ToDictionary(u => u.Id, u => u.AreaId);
 952:   
 953:                      // Huawei
 954:                      huaweiOltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.HuaweiOltIdList();
 955:   
 956:                      huaweiDictionary = (from a in db.Accesses
 957:                                          join o in db.EmsOnts on a equals o.Access
 958:                                          join sro in db.ServiceRequestOnts on a equals sro.Access
 959:                                          //where o.FamilyTypeId != 0
 960:                                          where o.EQUIPMENTID != "--"
 961:                                          && huaweiOltIdList.Contains(a.Olt)
 962:                                          select new { a.Id, a.AreaId }).Distinct().ToDictionary(u => u.Id, u => u.AreaId);
 963:   
 964:                      dictionary = nokiaDictionary.Union(huaweiDictionary).ToDictionary(k => k.Key, v => v.Value);
 965:                  }
 966:   
 967:                  return dictionary;
 968:              }
 969:          }
 970:   
 971:          ////////////////////////////////////////////////////////////////////////////
 972:   
 973:          /// <summary>
 974:          ///
 975:          /// </summary>
 976:          public static List<Ia.Ngn.Cl.Model.Access> NokiaAccessList
 977:          {
 978:              get
 979:              {
 980:                  List<int> oltIdList;
 981:                  List<Ia.Ngn.Cl.Model.Access> list;
 982:   
 983:                  oltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.NokiaOltIdList();
 984:   
 985:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 986:                  {
 987:                      list = (from a in db.Accesses where oltIdList.Contains(a.Olt) select a).ToList();
 988:                  }
 989:   
 990:                  return list;
 991:              }
 992:          }
 993:   
 994:          ////////////////////////////////////////////////////////////////////////////
 995:   
 996:          /// <summary>
 997:          ///
 998:          /// </summary>
 999:          public static List<Ia.Ngn.Cl.Model.Access> HuaweiAccessList
1000:          {
1001:              get
1002:              {
1003:                  List<int> oltIdList;
1004:                  List<Ia.Ngn.Cl.Model.Access> list;
1005:   
1006:                  oltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.HuaweiOltIdList();
1007:   
1008:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
1009:                  {
1010:                      list = (from a in db.Accesses where oltIdList.Contains(a.Olt) select a).ToList();
1011:                  }
1012:   
1013:                  return list;
1014:              }
1015:          }
1016:   
1017:          ////////////////////////////////////////////////////////////////////////////
1018:   
1019:          /// <summary>
1020:          ///
1021:          /// </summary>
1022:          public static List<Ia.Ngn.Cl.Model.Access> HuaweiAccessesWithNoEmsOntList
1023:          {
1024:              get
1025:              {
1026:                  List<int> huaweiOltIdList;
1027:                  List<Ia.Ngn.Cl.Model.Access> list;
1028:   
1029:                  huaweiOltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.HuaweiOltIdList();
1030:   
1031:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
1032:                  {
1033:                      list = (from a in db.Accesses
1034:                              join o in db.EmsOnts on a equals o.Access into gj
1035:                              from subsro in gj.DefaultIfEmpty()
1036:                              where huaweiOltIdList.Contains(a.Olt) && subsro.Access.Id == null
1037:                              select a).Distinct().ToList();
1038:                  }
1039:   
1040:                  return list;
1041:              }
1042:          }
1043:   
1044:          ////////////////////////////////////////////////////////////////////////////
1045:   
1046:          /// <summary>
1047:          ///
1048:          /// </summary>
1049:          public static Ia.Ngn.Cl.Model.Access ReadByIp(string ip)
1050:          {
1051:              // below: return the Access object of this ip
1052:   
1053:              Ia.Ngn.Cl.Model.Access item;
1054:   
1055:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
1056:              {
1057:                  item = (from a in db.Accesses join o in db.Onts on a equals o.Access join ov in db.OntServiceVoips on o equals ov.Ont where ov.Ip == ip select a).SingleOrDefault();
1058:              }
1059:   
1060:              return item;
1061:          }
1062:   
1063:          ////////////////////////////////////////////////////////////////////////////
1064:   
1065:          /// <summary>
1066:          ///
1067:          /// </summary>
1068:          public static Ia.Ngn.Cl.Model.Access ReadByIp(Ia.Ngn.Cl.Model.Ngn db, string ip)
1069:          {
1070:              // below: return the Access object of this ip
1071:   
1072:              return (from a in db.Accesses join o in db.Onts on a equals o.Access join ov in db.OntServiceVoips on o equals ov.Ont where ov.Ip == ip select a).SingleOrDefault();
1073:          }
1074:   
1075:          ////////////////////////////////////////////////////////////////////////////
1076:   
1077:          /// <summary>
1078:          ///
1079:          /// </summary>
1080:          public static Ia.Ngn.Cl.Model.Ui.Access StatisticalAccess(Ia.Ngn.Cl.Model.Business.ServiceAddress serviceAddress, ref List<Ia.Ngn.Cl.Model.Ui.Access> accessList, out string note)
1081:          {
1082:              int numberOfTrials;
1083:              Ia.Ngn.Cl.Model.Ui.Access statisticalAccess;
1084:              List<Ia.Ngn.Cl.Model.Ui.Access> _accessList;
1085:   
1086:              numberOfTrials = 0;
1087:   
1088:              if (serviceAddress.AreaId != 0)
1089:              {
1090:                  numberOfTrials++;
1091:   
1092:                  _accessList = accessList.Where(u => u.AreaId == serviceAddress.AreaId).ToList();
1093:   
1094:                  if (!string.IsNullOrEmpty(serviceAddress.Block) && _accessList.Any(u => u.Block == serviceAddress.Block))
1095:                  {
1096:                      numberOfTrials++;
1097:   
1098:                      note = string.Empty;
1099:   
1100:                      _accessList = accessList.Where(u => u.AreaId == serviceAddress.AreaId && u.Block == serviceAddress.Block).ToList();
1101:   
1102:                      if (!string.IsNullOrEmpty(serviceAddress.Street))
1103:                      {
1104:                          numberOfTrials++;
1105:   
1106:                          statisticalAccess = StatisticalAccessBlockAndStreet(serviceAddress, serviceAddress.Street, ref accessList, ref note, ref numberOfTrials);
1107:                      }
1108:                      else
1109:                      {
1110:                          statisticalAccess = StatisticalAccessBlockAndNoStreet(serviceAddress, ref accessList, ref note, ref numberOfTrials);
1111:                      }
1112:                  }
1113:                  else
1114:                  {
1115:                      numberOfTrials++;
1116:   
1117:                      statisticalAccess = null;
1118:   
1119:                      note = "serviceAddress.Block is empty or null";
1120:                  }
1121:              }
1122:              else
1123:              {
1124:                  numberOfTrials++;
1125:   
1126:                  statisticalAccess = null;
1127:   
1128:                  note = "serviceAddress.AreaId is 0";
1129:              }
1130:   
1131:              if (statisticalAccess != null) statisticalAccess.NumberOfTrials = numberOfTrials;
1132:   
1133:              return statisticalAccess;
1134:          }
1135:   
1136:          //
1137:          ////////////////////////////////////////////////////////////////////////////
1138:   
1139:          /// <summary>
1140:          ///
1141:          /// </summary>
1142:          private static Ia.Ngn.Cl.Model.Ui.Access LevenshteinDistance(Ia.Ngn.Cl.Model.Business.ServiceAddress serviceAddress, ref List<Ia.Ngn.Cl.Model.Ui.Access> accessList)
1143:          {
1144:              string accessLevenshteinString, serviceAddressLevenshteinString, idOfKeyWithMinValue;
1145:              Dictionary<string, int> dictionary;
1146:              Ia.Ngn.Cl.Model.Ui.Access statisticalAccess;
1147:   
1148:              dictionary = new Dictionary<string, int>();
1149:   
1150:              serviceAddressLevenshteinString = serviceAddress.Block + ":" + serviceAddress.Street + ":" + serviceAddress.PremisesOld + ":" + serviceAddress.PremisesNew;
1151:   
1152:              foreach (var a in accessList)
1153:              {
1154:                  accessLevenshteinString = a.Block + ":" + a.Street + ":" + a.PremisesOld + ":" + a.PremisesNew;
1155:   
1156:                  dictionary[a.Id] = Ia.Cl.Model.Default.LevenshteinDistance(serviceAddressLevenshteinString, accessLevenshteinString);
1157:              }
1158:   
1159:              var minValue = dictionary.Values.Min();
1160:   
1161:              idOfKeyWithMinValue = dictionary.FirstOrDefault(u => u.Value == minValue).Key;
1162:   
1163:              statisticalAccess = (from a in accessList where a.Id == idOfKeyWithMinValue select a).SingleOrDefault();
1164:   
1165:              return statisticalAccess;
1166:          }
1167:   
1168:          ////////////////////////////////////////////////////////////////////////////
1169:   
1170:          /// <summary>
1171:          ///
1172:          /// </summary>
1173:          private static Ia.Ngn.Cl.Model.Ui.Access StatisticalAccessBlockAndStreet(Ia.Ngn.Cl.Model.Business.ServiceAddress serviceAddress, string street, ref List<Ia.Ngn.Cl.Model.Ui.Access> accessList, ref string note, ref int numberOfTrials)
1174:          {
1175:              string po, pn;
1176:              Ia.Ngn.Cl.Model.Ui.Access statisticalAccess;
1177:              List<Ia.Ngn.Cl.Model.Ui.Access> _accessList;
1178:   
1179:              numberOfTrials++;
1180:   
1181:              _accessList = accessList.Where(u => u.AreaId == serviceAddress.AreaId && u.Block == serviceAddress.Block && u.Street == street).ToList();
1182:   
1183:              note = string.Empty;
1184:   
1185:              EvaluateAndCorrectPremisesOldAndPremisesNew(serviceAddress.PremisesOld, serviceAddress.PremisesNew, out po, out pn);
1186:              serviceAddress.PremisesOld = po;
1187:              serviceAddress.PremisesNew = pn;
1188:   
1189:              statisticalAccess = (from a in _accessList
1190:                                   where a.AreaId == serviceAddress.AreaId
1191:                                       && a.Block == serviceAddress.Block
1192:                                       && a.Street == street
1193:                                       && a.PremisesOld == serviceAddress.PremisesOld && !string.IsNullOrEmpty(serviceAddress.PremisesOld)
1194:                                       && a.PremisesNew == serviceAddress.PremisesNew && !string.IsNullOrEmpty(serviceAddress.PremisesNew)
1195:                                   select a).FirstOrDefault();
1196:   
1197:              if (statisticalAccess == null)
1198:              {
1199:                  numberOfTrials++;
1200:   
1201:                  statisticalAccess = (from a in _accessList
1202:                                       where a.AreaId == serviceAddress.AreaId
1203:                                           && a.Block == serviceAddress.Block
1204:                                           && a.Street == street
1205:                                           && a.PremisesOld == serviceAddress.PremisesOld && !string.IsNullOrEmpty(serviceAddress.PremisesOld)
1206:                                       select a).FirstOrDefault();
1207:   
1208:                  if (statisticalAccess == null)
1209:                  {
1210:                      numberOfTrials++;
1211:   
1212:                      statisticalAccess = (from a in _accessList
1213:                                           where a.AreaId == serviceAddress.AreaId
1214:                                               && a.Block == serviceAddress.Block
1215:                                               && a.Street == street
1216:                                               && a.PremisesNew == serviceAddress.PremisesNew && !string.IsNullOrEmpty(serviceAddress.PremisesNew)
1217:                                           select a).FirstOrDefault();
1218:   
1219:                      if (statisticalAccess == null)
1220:                      {
1221:                          numberOfTrials++;
1222:   
1223:                          statisticalAccess = (from a in _accessList
1224:                                               where a.AreaId == serviceAddress.AreaId
1225:                                                   && a.Block == serviceAddress.Block
1226:                                                   && a.Street == street
1227:                                                   && a.PremisesOld == serviceAddress.PremisesNew && !string.IsNullOrEmpty(serviceAddress.PremisesNew)
1228:                                               select a).FirstOrDefault();
1229:   
1230:                          if (statisticalAccess == null)
1231:                          {
1232:                              numberOfTrials++;
1233:   
1234:                              statisticalAccess = (from a in _accessList
1235:                                                   where a.AreaId == serviceAddress.AreaId
1236:                                                       && a.Block == serviceAddress.Block
1237:                                                       && a.Street == street
1238:                                                       && a.PremisesNew == serviceAddress.PremisesOld && !string.IsNullOrEmpty(serviceAddress.PremisesOld)
1239:                                                   select a).FirstOrDefault();
1240:   
1241:                              if (statisticalAccess == null)
1242:                              {
1243:                                  numberOfTrials++;
1244:   
1245:                                  statisticalAccess = (from a in _accessList
1246:                                                       where a.AreaId == serviceAddress.AreaId
1247:                                                           && a.Block == serviceAddress.Block
1248:                                                           && a.Street == street
1249:                                                           && a.PremisesNew == serviceAddress.Boulevard && !string.IsNullOrEmpty(serviceAddress.Boulevard)
1250:                                                       select a).FirstOrDefault();
1251:   
1252:                                  if (statisticalAccess == null)
1253:                                  {
1254:                                      numberOfTrials++;
1255:   
1256:                                      statisticalAccess = (from a in _accessList
1257:                                                           where a.AreaId == serviceAddress.AreaId
1258:                                                               && a.Block == serviceAddress.Block
1259:                                                               && a.Street == street
1260:                                                               && a.PremisesOld == serviceAddress.Boulevard && !string.IsNullOrEmpty(serviceAddress.Boulevard)
1261:                                                           select a).FirstOrDefault();
1262:   
1263:                                      if (statisticalAccess == null)
1264:                                      {
1265:                                          numberOfTrials++;
1266:   
1267:                                          statisticalAccess = (from a in _accessList
1268:                                                               where a.AreaId == serviceAddress.AreaId
1269:                                                                   && a.Block == serviceAddress.Block
1270:                                                                   && a.Street == street
1271:                                                                   && !string.IsNullOrEmpty(serviceAddress.PremisesOld) && !string.IsNullOrEmpty(a.PremisesOld) && a.PremisesOld.Contains(serviceAddress.PremisesOld)
1272:                                                                   && a.PremisesNew == serviceAddress.PremisesNew && !string.IsNullOrEmpty(serviceAddress.PremisesNew)
1273:                                                               select a).FirstOrDefault();
1274:   
1275:                                          if (statisticalAccess == null)
1276:                                          {
1277:                                              numberOfTrials++;
1278:   
1279:                                              statisticalAccess = (from a in _accessList
1280:                                                                   where a.AreaId == serviceAddress.AreaId
1281:                                                                       && a.Block == serviceAddress.Block
1282:                                                                       && a.Street == street
1283:                                                                       && a.PremisesOld == serviceAddress.PremisesOld && !string.IsNullOrEmpty(serviceAddress.PremisesOld)
1284:                                                                       && !string.IsNullOrEmpty(serviceAddress.PremisesNew) && !string.IsNullOrEmpty(a.PremisesNew) && a.PremisesNew.Contains(serviceAddress.PremisesNew)
1285:                                                                   select a).FirstOrDefault();
1286:   
1287:                                              if (statisticalAccess == null)
1288:                                              {
1289:                                                  numberOfTrials++;
1290:   
1291:                                                  statisticalAccess = (from a in _accessList
1292:                                                                       where a.AreaId == serviceAddress.AreaId
1293:                                                                           && a.Block == serviceAddress.Block
1294:                                                                           && a.Street == street
1295:                                                                           && !string.IsNullOrEmpty(serviceAddress.PremisesOld) && !string.IsNullOrEmpty(a.PremisesOld) && a.PremisesOld.Contains(serviceAddress.PremisesOld)
1296:                                                                       select a).FirstOrDefault();
1297:   
1298:                                                  if (statisticalAccess == null)
1299:                                                  {
1300:                                                      numberOfTrials++;
1301:   
1302:                                                      statisticalAccess = (from a in _accessList
1303:                                                                           where a.AreaId == serviceAddress.AreaId
1304:                                                                               && a.Block == serviceAddress.Block
1305:                                                                               && a.Street == street
1306:                                                                               && !string.IsNullOrEmpty(serviceAddress.PremisesNew) && !string.IsNullOrEmpty(a.PremisesNew) && a.PremisesNew.Contains(serviceAddress.PremisesNew)
1307:                                                                           select a).FirstOrDefault();
1308:   
1309:                                                      if (statisticalAccess == null)
1310:                                                      {
1311:                                                          numberOfTrials++;
1312:   
1313:   
1314:                                                      }
1315:                                                  }
1316:                                              }
1317:                                          }
1318:                                      }
1319:                                  }
1320:                              }
1321:                          }
1322:                      }
1323:                  }
1324:              }
1325:   
1326:              return statisticalAccess;
1327:          }
1328:   
1329:          ////////////////////////////////////////////////////////////////////////////
1330:   
1331:          /// <summary>
1332:          ///
1333:          /// </summary>
1334:          private static Ia.Ngn.Cl.Model.Ui.Access StatisticalAccessBlockAndNoStreet(Ia.Ngn.Cl.Model.Business.ServiceAddress serviceAddress, ref List<Ia.Ngn.Cl.Model.Ui.Access> accessList, ref string note, ref int numberOfTrials)
1335:          {
1336:              string po, pn;
1337:              Ia.Ngn.Cl.Model.Ui.Access statisticalAccess;
1338:              List<Ia.Ngn.Cl.Model.Ui.Access> _accessList;
1339:   
1340:              numberOfTrials++;
1341:   
1342:              _accessList = accessList.Where(u => u.AreaId == serviceAddress.AreaId && u.Block == serviceAddress.Block).ToList();
1343:   
1344:              note = string.Empty;
1345:   
1346:              EvaluateAndCorrectPremisesOldAndPremisesNew(serviceAddress.PremisesOld, serviceAddress.PremisesNew, out po, out pn);
1347:              serviceAddress.PremisesOld = po;
1348:              serviceAddress.PremisesNew = pn;
1349:   
1350:              statisticalAccess = (from a in _accessList
1351:                                   where a.AreaId == serviceAddress.AreaId
1352:                                       && a.Block == serviceAddress.Block
1353:                                       && a.PremisesOld == serviceAddress.PremisesOld && !string.IsNullOrEmpty(serviceAddress.PremisesOld)
1354:                                       && a.PremisesNew == serviceAddress.PremisesNew && !string.IsNullOrEmpty(serviceAddress.PremisesNew)
1355:                                   select a).FirstOrDefault();
1356:   
1357:              if (statisticalAccess == null)
1358:              {
1359:                  statisticalAccess = (from a in _accessList
1360:                                       where a.AreaId == serviceAddress.AreaId
1361:                                           && a.Block == serviceAddress.Block
1362:                                           && a.PremisesOld == serviceAddress.PremisesOld && !string.IsNullOrEmpty(serviceAddress.PremisesOld)
1363:                                       select a).FirstOrDefault();
1364:   
1365:                  if (statisticalAccess == null)
1366:                  {
1367:                      numberOfTrials++;
1368:   
1369:                      statisticalAccess = (from a in _accessList
1370:                                           where a.AreaId == serviceAddress.AreaId
1371:                                               && a.Block == serviceAddress.Block
1372:                                               && a.PremisesNew == serviceAddress.PremisesNew && !string.IsNullOrEmpty(serviceAddress.PremisesNew)
1373:                                           select a).FirstOrDefault();
1374:   
1375:                      if (statisticalAccess == null)
1376:                      {
1377:                          numberOfTrials++;
1378:   
1379:                          statisticalAccess = (from a in _accessList
1380:                                               where a.AreaId == serviceAddress.AreaId
1381:                                                   && a.Block == serviceAddress.Block
1382:                                                   && a.PremisesOld == serviceAddress.PremisesNew && !string.IsNullOrEmpty(serviceAddress.PremisesNew)
1383:                                               select a).FirstOrDefault();
1384:   
1385:                          if (statisticalAccess == null)
1386:                          {
1387:                              numberOfTrials++;
1388:   
1389:                              statisticalAccess = (from a in _accessList
1390:                                                   where a.AreaId == serviceAddress.AreaId
1391:                                                       && a.Block == serviceAddress.Block
1392:                                                       && a.PremisesNew == serviceAddress.PremisesOld && !string.IsNullOrEmpty(serviceAddress.PremisesOld)
1393:                                                   select a).FirstOrDefault();
1394:   
1395:                              if (statisticalAccess == null)
1396:                              {
1397:                                  numberOfTrials++;
1398:   
1399:                                  statisticalAccess = (from a in _accessList
1400:                                                       where a.AreaId == serviceAddress.AreaId
1401:                                                           && a.Block == serviceAddress.Block
1402:                                                           && !string.IsNullOrEmpty(serviceAddress.PremisesOld) && !string.IsNullOrEmpty(a.PremisesOld) && a.PremisesOld.Contains(serviceAddress.PremisesOld)
1403:                                                           && a.PremisesNew == serviceAddress.PremisesNew && !string.IsNullOrEmpty(serviceAddress.PremisesNew)
1404:                                                       select a).FirstOrDefault();
1405:   
1406:                                  if (statisticalAccess == null)
1407:                                  {
1408:                                      numberOfTrials++;
1409:   
1410:                                      statisticalAccess = (from a in _accessList
1411:                                                           where a.AreaId == serviceAddress.AreaId
1412:                                                               && a.Block == serviceAddress.Block
1413:                                                               && a.PremisesOld == serviceAddress.PremisesOld && !string.IsNullOrEmpty(serviceAddress.PremisesOld)
1414:                                                               && !string.IsNullOrEmpty(serviceAddress.PremisesNew) && !string.IsNullOrEmpty(a.PremisesNew) && a.PremisesNew.Contains(serviceAddress.PremisesNew)
1415:                                                           select a).FirstOrDefault();
1416:   
1417:                                      if (statisticalAccess == null)
1418:                                      {
1419:                                          numberOfTrials++;
1420:   
1421:                                          statisticalAccess = (from a in _accessList
1422:                                                               where a.AreaId == serviceAddress.AreaId
1423:                                                                   && a.Block == serviceAddress.Block
1424:                                                                   && !string.IsNullOrEmpty(serviceAddress.PremisesOld) && !string.IsNullOrEmpty(a.PremisesOld) && a.PremisesOld.Contains(serviceAddress.PremisesOld)
1425:                                                               select a).FirstOrDefault();
1426:   
1427:                                          if (statisticalAccess == null)
1428:                                          {
1429:                                              numberOfTrials++;
1430:   
1431:                                              statisticalAccess = (from a in _accessList
1432:                                                                   where a.AreaId == serviceAddress.AreaId
1433:                                                                       && a.Block == serviceAddress.Block
1434:                                                                       && !string.IsNullOrEmpty(serviceAddress.PremisesNew) && !string.IsNullOrEmpty(a.PremisesNew) && a.PremisesNew.Contains(serviceAddress.PremisesNew)
1435:                                                                   select a).FirstOrDefault();
1436:   
1437:                                              if (statisticalAccess == null)
1438:                                              {
1439:                                                  numberOfTrials++;
1440:   
1441:                                                  statisticalAccess = null;
1442:   
1443:                                                  note = "serviceAddress.Street is empty or null";
1444:                                              }
1445:                                          }
1446:                                      }
1447:                                  }
1448:                              }
1449:                          }
1450:                      }
1451:                  }
1452:              }
1453:   
1454:              return statisticalAccess;
1455:          }
1456:   
1457:          ////////////////////////////////////////////////////////////////////////////
1458:   
1459:          /// <summary>
1460:          ///
1461:          /// </summary>
1462:          public static void EvaluateAndCorrectPremisesOldAndPremisesNew(string premisesOld, string premisesNew, out string newPremisesOld, out string newPremisesNew)
1463:          {
1464:              int po, pn;
1465:   
1466:              if (!string.IsNullOrEmpty(premisesOld) && !string.IsNullOrEmpty(premisesNew))
1467:              {
1468:                  if (int.TryParse(premisesOld, out po) && int.TryParse(premisesNew, out pn))
1469:                  {
1470:                      // if equal then one is invalid
1471:                      if (po == pn)
1472:                      {
1473:                          if (po > 100)
1474:                          {
1475:                              newPremisesOld = po.ToString();
1476:                              newPremisesNew = string.Empty;
1477:                          }
1478:                          else
1479:                          {
1480:                              newPremisesOld = string.Empty;
1481:                              newPremisesNew = po.ToString();
1482:                          }
1483:                      }
1484:                      else
1485:                      {
1486:                          if (pn > po)
1487:                          {
1488:                              newPremisesOld = pn.ToString();
1489:                              newPremisesNew = po.ToString();
1490:                          }
1491:                          else
1492:                          {
1493:                              newPremisesOld = po.ToString();
1494:                              newPremisesNew = pn.ToString();
1495:                          }
1496:                      }
1497:                  }
1498:                  else if (int.TryParse(premisesOld, out po) && !int.TryParse(premisesNew, out pn))
1499:                  {
1500:                      // if equal then one is invalid
1501:                      if (po.ToString() == premisesNew)
1502:                      {
1503:                          if (po > 100)
1504:                          {
1505:                              newPremisesOld = po.ToString();
1506:                              newPremisesNew = string.Empty;
1507:                          }
1508:                          else
1509:                          {
1510:                              newPremisesOld = string.Empty;
1511:                              newPremisesNew = po.ToString();
1512:                          }
1513:                      }
1514:                      else
1515:                      {
1516:                          if (premisesNew.Length > po.ToString().Length)
1517:                          {
1518:                              newPremisesOld = premisesNew;
1519:                              newPremisesNew = po.ToString();
1520:                          }
1521:                          else
1522:                          {
1523:                              newPremisesOld = po.ToString();
1524:                              newPremisesNew = premisesNew;
1525:                          }
1526:                      }
1527:                  }
1528:                  else if (!int.TryParse(premisesOld, out po) && int.TryParse(premisesNew, out pn))
1529:                  {
1530:                      // if equal then one is invalid
1531:                      if (premisesOld == pn.ToString())
1532:                      {
1533:                          if (premisesOld.Length >= 3)
1534:                          {
1535:                              newPremisesOld = premisesOld.ToString();
1536:                              newPremisesNew = string.Empty;
1537:                          }
1538:                          else
1539:                          {
1540:                              newPremisesOld = string.Empty;
1541:                              newPremisesNew = premisesOld;
1542:                          }
1543:                      }
1544:                      else
1545:                      {
1546:                          if (pn.ToString().Length > premisesOld.Length)
1547:                          {
1548:                              newPremisesOld = pn.ToString();
1549:                              newPremisesNew = premisesOld;
1550:                          }
1551:                          else
1552:                          {
1553:                              newPremisesOld = premisesOld;
1554:                              newPremisesNew = pn.ToString();
1555:                          }
1556:                      }
1557:                  }
1558:                  else //if (!int.TryParse(premisesOld, out po) && !int.TryParse(premisesNew, out pn))
1559:                  {
1560:                      // if equal then one is invalid
1561:                      if (premisesOld == premisesNew)
1562:                      {
1563:                          if (premisesOld.Length >= 3)
1564:                          {
1565:                              newPremisesOld = premisesOld;
1566:                              newPremisesNew = string.Empty;
1567:                          }
1568:                          else
1569:                          {
1570:                              newPremisesOld = string.Empty;
1571:                              newPremisesNew = premisesOld;
1572:                          }
1573:                      }
1574:                      else
1575:                      {
1576:                          if (premisesNew.Length > premisesOld.Length)
1577:                          {
1578:                              newPremisesOld = premisesNew;
1579:                              newPremisesNew = premisesOld;
1580:                          }
1581:                          else
1582:                          {
1583:                              newPremisesOld = premisesOld;
1584:                              newPremisesNew = premisesNew;
1585:                          }
1586:                      }
1587:                  }
1588:              }
1589:              else if (!string.IsNullOrEmpty(premisesOld) && string.IsNullOrEmpty(premisesNew))
1590:              {
1591:                  if (premisesOld.Length >= 3)
1592:                  {
1593:                      newPremisesOld = premisesOld;
1594:                      newPremisesNew = string.Empty;
1595:                  }
1596:                  else
1597:                  {
1598:                      newPremisesOld = string.Empty;
1599:                      newPremisesNew = premisesOld;
1600:                  }
1601:              }
1602:              else if (string.IsNullOrEmpty(premisesOld) && !string.IsNullOrEmpty(premisesNew))
1603:              {
1604:                  if (premisesNew.Length < 3)
1605:                  {
1606:                      newPremisesOld = string.Empty;
1607:                      newPremisesNew = premisesNew;
1608:                  }
1609:                  else
1610:                  {
1611:                      newPremisesOld = premisesNew;
1612:                      newPremisesNew = string.Empty;
1613:                  }
1614:              }
1615:              else //if (string.IsNullOrEmpty(premisesOld) && string.IsNullOrEmpty(premisesNew))
1616:              {
1617:                  newPremisesOld = string.Empty;
1618:                  newPremisesNew = string.Empty;
1619:              }
1620:          }
1621:   
1622:          ////////////////////////////////////////////////////////////////////////////
1623:          ////////////////////////////////////////////////////////////////////////////
1624:   
1625:          /// <summary>
1626:          ///
1627:          /// </summary>
1628:          public static string ToSimpleTextString(Ia.Ngn.Cl.Model.Access access)
1629:          {
1630:              StringBuilder sb;
1631:              Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor vendor;
1632:   
1633:              sb = new StringBuilder();
1634:   
1635:              vendor = (from o in Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.OntList where o.Access.Id == access.Id select o.Pon.PonGroup.Olt.Odf.Vendor).SingleOrDefault();
1636:   
1637:              sb.AppendLine("Vendor: " + vendor.Name);
1638:              sb.AppendLine("Name: " + access.Name);
1639:              sb.AppendLine("Address: " + access.Address);
1640:   
1641:              if (!string.IsNullOrEmpty(access.Paci)) sb.AppendLine("Paci: " + access.Paci);
1642:              if (!string.IsNullOrEmpty(access.Note)) sb.AppendLine("Note: " + access.Note);
1643:   
1644:              return sb.ToString();
1645:          }
1646:   
1647:          ////////////////////////////////////////////////////////////////////////////
1648:          ////////////////////////////////////////////////////////////////////////////
1649:      }
1650:   
1651:      ////////////////////////////////////////////////////////////////////////////
1652:      ////////////////////////////////////////////////////////////////////////////
1653:  }