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

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Port

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

Huawei's Port support class of Next Generation Network'a (NGN's) data model.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Data;
   4:  using System.Linq;
   5:  using System.Text;
   6:   
   7:  namespace Ia.Ngn.Cl.Model.Data.Huawei
   8:  {
   9:      ////////////////////////////////////////////////////////////////////////////
  10:   
  11:      /// <summary publish="true">
  12:      /// Huawei's Port support class of Next Generation Network'a (NGN's) data model.
  13:      /// </summary>
  14:      /// 
  15:      /// <remarks> 
  16:      /// Copyright © 2016-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet 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 Port
  29:      {
  30:          /// <summary/>
  31:          public Port() { }
  32:   
  33:          ////////////////////////////////////////////////////////////////////////////
  34:   
  35:          /// <summary>
  36:          ///
  37:          /// </summary>
  38:          public static bool Update(Ia.Ngn.Cl.Model.Business.Huawei.Ems.Response response, out Ia.Cl.Model.Result result)
  39:          {
  40:              bool isUpdated;
  41:              int did, fn, sn, pn, queryFn, querySn, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
  42:              string id, queryCommand, queryDev;
  43:              DataColumnCollection columns;
  44:              DataRow columnDataRow;
  45:              Dictionary<string, int> devToDidDictionary;
  46:              Ia.Ngn.Cl.Model.Huawei.EmsPort port, newPort;
  47:              Ia.Ngn.Cl.Model.Huawei.EmsBoard board;
  48:              List<string> itemIdList;
  49:              List<Ia.Ngn.Cl.Model.Huawei.EmsPort> portList;
  50:   
  51:              isUpdated = false;
  52:              readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
  53:              result = new Ia.Cl.Model.Result();
  54:   
  55:              if (response.QueryDataTable != null)
  56:              {
  57:                  queryCommand = response.CommandFromCorrelationTagDictionaryByCtag;
  58:   
  59:                  // LST-PORT::DEV=MDU-SAA-838-001,FN=0,SN=1:{ctag}::;
  60:                  queryDev = Ia.Cl.Model.Default.Match(queryCommand, @"DEV=(.+?),");
  61:                  queryFn = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"FN=(\d+)"));
  62:                  querySn = int.TryParse(Ia.Cl.Model.Default.Match(queryCommand, @"SN=(\d+)"), out int i) ? i : 0;
  63:   
  64:                  columns = response.QueryDataTable.Columns;
  65:                  readItemCount = response.QueryDataTable.Rows.Count;
  66:   
  67:                  devToDidDictionary = Ia.Ngn.Cl.Model.Data.Huawei.Dev.DevToDidDictionary;
  68:   
  69:                  if (devToDidDictionary.ContainsKey(queryDev))
  70:                  {
  71:                      did = devToDidDictionary[queryDev];
  72:   
  73:                      board = Ia.Ngn.Cl.Model.Data.Huawei.Board.ReadByDidFnSn(did, queryFn, querySn);
  74:   
  75:                      if (board != null)
  76:                      {
  77:                          itemIdList = new List<string>(response.QueryDataTable.Rows.Count + 1);
  78:   
  79:                          using (var db = new Ia.Ngn.Cl.Model.Ngn())
  80:                          {
  81:                              portList = (from ep in db.EmsPorts where ep.EmsBoard.Id == board.Id select ep).ToList();
  82:                              // or portList = Ia.Ngn.Cl.Model.Data.Huawei.Port.List(board.Id);
  83:   
  84:                              existingItemCount = portList.Count;
  85:   
  86:                              if (response.QueryDataTable.Rows.Count >= 1)
  87:                              {
  88:                                  columnDataRow = response.QueryDataTable.Rows[0];
  89:   
  90:                                  foreach (DataRow dataRow in response.QueryDataTable.Rows)
  91:                                  {
  92:                                      did = int.Parse(dataRow[columns.IndexOf("DID")].ToString());
  93:                                      fn = int.Parse(dataRow[columns.IndexOf("FN")].ToString());
  94:                                      sn = int.Parse(dataRow[columns.IndexOf("SN")].ToString());
  95:                                      pn = int.Parse(dataRow[columns.IndexOf("PN")].ToString());
  96:   
  97:                                      id = Ia.Ngn.Cl.Model.Business.Huawei.Port.PortId(board.Id, pn);
  98:   
  99:                                      port = (from p in portList where p.Id == id select p).SingleOrDefault();
 100:   
 101:                                      newPort = new Ia.Ngn.Cl.Model.Huawei.EmsPort()
 102:                                      {
 103:                                          Id = id,
 104:                                          DID = did,
 105:                                          FN = fn,
 106:                                          SN = sn,
 107:                                          PN = pn,
 108:                                          ALIAS = columns.Contains("ALIAS") ? dataRow[columns.IndexOf("ALIAS")].ToString() : string.Empty,
 109:                                          PT = columns.Contains("PT") ? dataRow[columns.IndexOf("PT")].ToString() : string.Empty,
 110:                                          PSTAT = columns.Contains("PSTAT") ? dataRow[columns.IndexOf("PSTAT")].ToString() : string.Empty,
 111:                                          NAME = columns.Contains("NAME") ? dataRow[columns.IndexOf("NAME")].ToString() : string.Empty,
 112:                                          PSPEED = columns.Contains("PSPEED") ? dataRow[columns.IndexOf("PSPEED")].ToString() : string.Empty,
 113:                                          PDIRECT = columns.Contains("PDIRECT") ? dataRow[columns.IndexOf("PDIRECT")].ToString() : string.Empty,
 114:                                          LOOPSTAT = columns.Contains("LOOPSTAT") ? dataRow[columns.IndexOf("LOOPSTAT")].ToString() : string.Empty,
 115:                                          TESTSTAT = columns.Contains("TESTSTAT") ? dataRow[columns.IndexOf("TESTSTAT")].ToString() : string.Empty,
 116:                                          FEC = columns.Contains("FEC") ? dataRow[columns.IndexOf("FEC")].ToString() : string.Empty,
 117:                                          RN = columns.Contains("RN") ? dataRow[columns.IndexOf("RN")].ToString() : string.Empty,
 118:                                          AState = columns.Contains("AState") ? dataRow[columns.IndexOf("AState")].ToString() : string.Empty,
 119:                                          EmsBoard = (from eb in db.EmsBoards where eb.Id == board.Id select eb).SingleOrDefault()
 120:                                      };
 121:   
 122:                                      if (port == null)
 123:                                      {
 124:                                          newPort.Created = newPort.Updated = DateTime.UtcNow.AddHours(3);
 125:   
 126:                                          db.EmsPorts.Add(newPort);
 127:   
 128:                                          insertedItemCount++;
 129:                                      }
 130:                                      else // update
 131:                                      {
 132:                                          // below: copy values from newPort to port
 133:   
 134:                                          if (port.Update(newPort))
 135:                                          {
 136:                                              db.EmsPorts.Attach(port);
 137:                                              db.Entry(port).State = System.Data.Entity.EntityState.Modified;
 138:   
 139:                                              updatedItemCount++;
 140:                                          }
 141:                                      }
 142:   
 143:                                      itemIdList.Add(id); // keep at the end
 144:                                  }
 145:   
 146:                                  // below: this function will remove values that were not present in the reading
 147:                                  if (portList.Count > 0)
 148:                                  {
 149:                                      foreach (Ia.Ngn.Cl.Model.Huawei.EmsPort po in portList)
 150:                                      {
 151:                                          if (!itemIdList.Contains(po.Id))
 152:                                          {
 153:                                              port = (from ep in db.EmsPorts where ep.Id == po.Id select ep).SingleOrDefault();
 154:   
 155:                                              db.EmsPorts.Remove(port);
 156:   
 157:                                              deletedItemCount++;
 158:                                          }
 159:                                      }
 160:                                  }
 161:                              }
 162:                              else
 163:                              {
 164:                                  // below: remove all values since reading was empty
 165:   
 166:                                  if (portList.Count > 0)
 167:                                  {
 168:                                      foreach (Ia.Ngn.Cl.Model.Huawei.EmsPort po in portList)
 169:                                      {
 170:                                          port = (from ep in db.EmsPorts where ep.Id == po.Id select ep).SingleOrDefault();
 171:   
 172:                                          db.EmsPorts.Remove(port);
 173:   
 174:                                          deletedItemCount++;
 175:                                      }
 176:                                  }
 177:                              }
 178:   
 179:                              db.SaveChanges();
 180:                          }
 181:   
 182:                          if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
 183:                          else isUpdated = false;
 184:   
 185:                          result.AddSuccess("(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")");
 186:                      }
 187:                      else
 188:                      {
 189:                          result.AddWarning("board is null");
 190:                      }
 191:                  }
 192:                  else
 193:                  {
 194:                      result.AddError("devToDidDictionary does not contain key: " + queryDev);
 195:                  }
 196:              }
 197:              else
 198:              {
 199:                  result.AddWarning("dataTable is null");
 200:              }
 201:   
 202:              return isUpdated;
 203:          }
 204:   
 205:          ////////////////////////////////////////////////////////////////////////////
 206:   
 207:          /// <summary>
 208:          ///
 209:          /// </summary>
 210:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsPort> List()
 211:          {
 212:              List<Ia.Ngn.Cl.Model.Huawei.EmsPort> list;
 213:   
 214:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 215:              {
 216:                  list = (from p in db.EmsPorts select p).ToList();
 217:              }
 218:   
 219:              return list;
 220:          }
 221:   
 222:          ////////////////////////////////////////////////////////////////////////////
 223:   
 224:          /// <summary>
 225:          ///
 226:          /// </summary>
 227:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsPort> List(string boardId)
 228:          {
 229:              List<Ia.Ngn.Cl.Model.Huawei.EmsPort> list;
 230:   
 231:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 232:              {
 233:                  list = (from p in db.EmsPorts where p.EmsBoard.Id == boardId select p).ToList();
 234:              }
 235:   
 236:              return list;
 237:          }
 238:   
 239:          ////////////////////////////////////////////////////////////////////////////
 240:   
 241:          /// <summary>
 242:          ///
 243:          /// </summary>
 244:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsPort> ListByDevId(int devId)
 245:          {
 246:              List<Ia.Ngn.Cl.Model.Huawei.EmsPort> list;
 247:   
 248:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 249:              {
 250:                  list = (from p in db.EmsPorts where p.EmsBoard.EmsDev.Id == devId select p).ToList();
 251:              }
 252:   
 253:              return list;
 254:          }
 255:   
 256:          ////////////////////////////////////////////////////////////////////////////
 257:   
 258:          /// <summary>
 259:          ///
 260:          /// </summary>
 261:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsPort> ListByDid(int did)
 262:          {
 263:              List<Ia.Ngn.Cl.Model.Huawei.EmsPort> list;
 264:   
 265:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 266:              {
 267:                  list = (from p in db.EmsPorts where p.DID == did select p).ToList();
 268:              }
 269:   
 270:              return list;
 271:          }
 272:   
 273:          ////////////////////////////////////////////////////////////////////////////
 274:   
 275:          /// <summary>
 276:          ///
 277:          /// </summary>
 278:          public static Ia.Ngn.Cl.Model.Huawei.EmsPort ReadByDidFnSnPn(int did, int fn, int sn, int pn)
 279:          {
 280:              Ia.Ngn.Cl.Model.Huawei.EmsPort item;
 281:   
 282:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 283:              {
 284:                  item = (from p in db.EmsPorts where p.DID == did && p.FN == fn && p.SN == sn && p.PN == pn select p).SingleOrDefault();
 285:              }
 286:   
 287:              return item;
 288:          }
 289:   
 290:          ////////////////////////////////////////////////////////////////////////////
 291:          ////////////////////////////////////////////////////////////////////////////
 292:   
 293:          /// <summary>
 294:          ///
 295:          /// </summary>
 296:          public static string ToSimpleTextString(Ia.Ngn.Cl.Model.Huawei.EmsPort emdPort)
 297:          {
 298:              StringBuilder sb;
 299:   
 300:              sb = new StringBuilder();
 301:   
 302:              sb.AppendLine("ALIAS: " + emdPort.ALIAS);
 303:              sb.AppendLine("DID: " + emdPort.DID);
 304:              sb.AppendLine("FN: " + emdPort.FN);
 305:              sb.AppendLine("SN: " + emdPort.SN);
 306:              sb.AppendLine("PN: " + emdPort.PN);
 307:              sb.AppendLine("PT: " + emdPort.PT);
 308:              sb.AppendLine("PSTAT: " + emdPort.PSTAT);
 309:   
 310:              return sb.ToString();
 311:          }
 312:   
 313:          ////////////////////////////////////////////////////////////////////////////
 314:          ////////////////////////////////////////////////////////////////////////////
 315:      }
 316:   
 317:      ////////////////////////////////////////////////////////////////////////////
 318:      ////////////////////////////////////////////////////////////////////////////
 319:  }