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

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

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

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Data;
   5:  using System.Data.Entity;
   6:   
   7:  namespace Ia.Ngn.Cl.Model.Data.Huawei
   8:  {
   9:      ////////////////////////////////////////////////////////////////////////////
  10:   
  11:      /// <summary publish="true">
  12:      /// Huawei's Board 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 Board
  29:      {
  30:          /// <summary/>
  31:          public Board() { }
  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, queryFn, querySn, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
  42:              string id, queryCommand, queryDev;
  43:              DataColumnCollection columns;
  44:              DataRow columnDataRow;
  45:              Ia.Ngn.Cl.Model.Huawei.EmsBoard board, newBoard;
  46:              Ia.Ngn.Cl.Model.Huawei.EmsDev dev;
  47:              List<string> itemIdList;
  48:              List<Ia.Ngn.Cl.Model.Huawei.EmsBoard> boardList;
  49:   
  50:              isUpdated = false;
  51:              readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
  52:              result = new Ia.Cl.Model.Result();
  53:   
  54:              if (response.QueryDataTable != null)
  55:              {
  56:                  queryCommand = response.CommandFromCorrelationTagDictionaryByCtag;
  57:   
  58:                  // LST-BOARD::DEV=MDU-FNT-806-002,FN=0:{ctag}::;
  59:                  // LST-BOARD::DEV=MDU-FNT-806-002,FN=0,SN=3:{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:                  dev = Ia.Ngn.Cl.Model.Data.Huawei.Dev.ReadByDev(queryDev);
  68:   
  69:                  if (dev != null)
  70:                  {
  71:                      itemIdList = new List<string>(response.QueryDataTable.Rows.Count + 1);
  72:   
  73:                      using (var db = new Ia.Ngn.Cl.Model.Ngn())
  74:                      {
  75:                          boardList = (from eb in db.EmsBoards where eb.EmsDev.DEV == queryDev select eb).ToList();
  76:   
  77:                          existingItemCount = boardList.Count;
  78:   
  79:                          if (response.QueryDataTable.Rows.Count >= 1)
  80:                          {
  81:                              columnDataRow = response.QueryDataTable.Rows[0];
  82:   
  83:                              foreach (DataRow dataRow in response.QueryDataTable.Rows)
  84:                              {
  85:                                  did = int.Parse(dataRow[columns.IndexOf("DID")].ToString());
  86:                                  fn = int.Parse(dataRow[columns.IndexOf("FN")].ToString());
  87:                                  sn = int.Parse(dataRow[columns.IndexOf("SN")].ToString());
  88:   
  89:                                  id = Ia.Ngn.Cl.Model.Business.Huawei.Board.BoardId(dev.Id, fn, sn);
  90:   
  91:                                  board = (from b in boardList where b.Id == id select b).SingleOrDefault();
  92:   
  93:                                  newBoard = new Ia.Ngn.Cl.Model.Huawei.EmsBoard()
  94:                                  {
  95:                                      Id = id,
  96:                                      DID = did,
  97:                                      FN = fn,
  98:                                      SN = sn,
  99:                                      BT = columns.Contains("BT") ? dataRow[columns.IndexOf("BT")].ToString() : string.Empty,
 100:                                      BVER = columns.Contains("BVER") ? dataRow[columns.IndexOf("BVER")].ToString() : string.Empty,
 101:                                      BSTAT = columns.Contains("BSTAT") ? dataRow[columns.IndexOf("BSTAT")].ToString() : string.Empty,
 102:                                      DEV = columns.Contains("DEV") ? dataRow[columns.IndexOf("DEV")].ToString() : string.Empty,
 103:                                      BNAME = columns.Contains("BNAME") ? dataRow[columns.IndexOf("BNAME")].ToString() : string.Empty,
 104:                                      NAME = columns.Contains("NAME") ? dataRow[columns.IndexOf("NAME")].ToString() : string.Empty,
 105:                                      ALIAS = columns.Contains("ALIAS") ? dataRow[columns.IndexOf("ALIAS")].ToString() : string.Empty,
 106:                                      ELABEL = columns.Contains("ELABEL") ? dataRow[columns.IndexOf("ELABEL")].ToString() : string.Empty,
 107:                                      PCBVER = columns.Contains("PCBVER") ? dataRow[columns.IndexOf("PCBVER")].ToString() : string.Empty,
 108:                                      SWVER = columns.Contains("SWVER") ? dataRow[columns.IndexOf("SWVER")].ToString() : string.Empty,
 109:                                      ROLE = columns.Contains("ROLE") ? dataRow[columns.IndexOf("ROLE")].ToString() : string.Empty,
 110:                                      ALARM = columns.Contains("ALARM") ? dataRow[columns.IndexOf("ALARM")].ToString() : string.Empty,
 111:                                      ST = columns.Contains("ST") ? dataRow[columns.IndexOf("ST")].ToString() : string.Empty,
 112:                                      PROTECTMODE = columns.Contains("PROTECTMODE") ? dataRow[columns.IndexOf("PROTECTMODE")].ToString() : string.Empty,
 113:                                      PORTCOUNT = columns.Contains("PORTCOUNT") ? dataRow[columns.IndexOf("PORTCOUNT")].ToString() : string.Empty,
 114:                                      PORTTYPE = columns.Contains("PORTTYPE") ? dataRow[columns.IndexOf("PORTTYPE")].ToString() : string.Empty,
 115:                                      WORKMODE = columns.Contains("WORKMODE") ? dataRow[columns.IndexOf("WORKMODE")].ToString() : string.Empty,
 116:                                      REPLACE = columns.Contains("REPLACE") ? dataRow[columns.IndexOf("REPLACE")].ToString() : string.Empty,
 117:                                      EmsDev = (from ed in db.EmsDevs where ed.Id == dev.Id select ed).SingleOrDefault()
 118:                                  };
 119:   
 120:                                  if (board == null)
 121:                                  {
 122:                                      newBoard.Created = newBoard.Updated = DateTime.UtcNow.AddHours(3);
 123:   
 124:                                      db.EmsBoards.Add(newBoard);
 125:   
 126:                                      insertedItemCount++;
 127:                                  }
 128:                                  else // update
 129:                                  {
 130:                                      // below: copy values from newBoard to board
 131:   
 132:                                      if (board.Update(newBoard))
 133:                                      {
 134:                                          db.EmsBoards.Attach(board);
 135:                                          db.Entry(board).State = System.Data.Entity.EntityState.Modified;
 136:   
 137:                                          updatedItemCount++;
 138:                                      }
 139:                                  }
 140:   
 141:                                  itemIdList.Add(id); // keep at the end
 142:                              }
 143:   
 144:                              // below: this function will remove values that were not present in the reading
 145:                              if (boardList.Count > 0)
 146:                              {
 147:                                  foreach (Ia.Ngn.Cl.Model.Huawei.EmsBoard bo in boardList)
 148:                                  {
 149:                                      if (!itemIdList.Contains(bo.Id))
 150:                                      {
 151:                                          board = (from eb in db.EmsBoards where eb.Id == bo.Id select eb).SingleOrDefault();
 152:   
 153:                                          db.EmsBoards.Remove(board);
 154:   
 155:                                          deletedItemCount++;
 156:                                      }
 157:                                  }
 158:                              }
 159:                          }
 160:                          else
 161:                          {
 162:                              // below: remove all values since reading was empty
 163:   
 164:                              if (boardList.Count > 0)
 165:                              {
 166:                                  foreach (Ia.Ngn.Cl.Model.Huawei.EmsBoard bo in boardList)
 167:                                  {
 168:                                      board = (from eb in db.EmsBoards where eb.Id == bo.Id select eb).SingleOrDefault();
 169:   
 170:                                      db.EmsBoards.Remove(board);
 171:   
 172:                                      deletedItemCount++;
 173:                                  }
 174:                              }
 175:                          }
 176:   
 177:                          db.SaveChanges();
 178:                      }
 179:   
 180:                      if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
 181:                      else isUpdated = false;
 182:   
 183:                      result.AddSuccess("(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")");
 184:                  }
 185:                  else
 186:                  {
 187:                      result.AddError("Dev is null for dev: " + queryDev);
 188:                  }
 189:              }
 190:              else
 191:              {
 192:                  result.AddWarning("response.QueryDataTable is null");
 193:              }
 194:   
 195:              return isUpdated;
 196:          }
 197:   
 198:          ////////////////////////////////////////////////////////////////////////////
 199:   
 200:          /// <summary>
 201:          ///
 202:          /// </summary>
 203:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsBoard> List()
 204:          {
 205:              List<Ia.Ngn.Cl.Model.Huawei.EmsBoard> list;
 206:   
 207:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 208:              {
 209:                  list = (from eb in db.EmsBoards select eb).Include(x => x.EmsDev).ToList();
 210:              }
 211:   
 212:              return list.ToList();
 213:          }
 214:   
 215:          ////////////////////////////////////////////////////////////////////////////
 216:   
 217:          /// <summary>
 218:          ///
 219:          /// </summary>
 220:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsBoard> List(int devId)
 221:          {
 222:              List<Ia.Ngn.Cl.Model.Huawei.EmsBoard> list;
 223:   
 224:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 225:              {
 226:                  list = (from b in db.EmsBoards where b.EmsDev.Id == devId select b).ToList();
 227:              }
 228:   
 229:              return list;
 230:          }
 231:   
 232:          ////////////////////////////////////////////////////////////////////////////
 233:   
 234:          /// <summary>
 235:          ///
 236:          /// </summary>
 237:          public static Ia.Ngn.Cl.Model.Huawei.EmsBoard ReadByEmsNameFnSn(string emsName, int fn, int sn)
 238:          {
 239:              Ia.Ngn.Cl.Model.Huawei.EmsBoard item;
 240:   
 241:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 242:              {
 243:                  item = (from b in db.EmsBoards where b.DEV == emsName && b.FN == fn && b.SN == sn select b).SingleOrDefault();
 244:              }
 245:   
 246:              return item;
 247:          }
 248:   
 249:          ////////////////////////////////////////////////////////////////////////////
 250:   
 251:          /// <summary>
 252:          ///
 253:          /// </summary>
 254:          public static Ia.Ngn.Cl.Model.Huawei.EmsBoard ReadByDidFnSn(int did, int fn, int sn)
 255:          {
 256:              Ia.Ngn.Cl.Model.Huawei.EmsBoard item;
 257:   
 258:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 259:              {
 260:                  item = (from b in db.EmsBoards where b.DID == did && b.FN == fn && b.SN == sn select b).SingleOrDefault();
 261:              }
 262:   
 263:              return item;
 264:          }
 265:   
 266:          ////////////////////////////////////////////////////////////////////////////
 267:          ////////////////////////////////////////////////////////////////////////////
 268:      }
 269:   
 270:      ////////////////////////////////////////////////////////////////////////////
 271:      ////////////////////////////////////////////////////////////////////////////
 272:  }