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

Integrated Applications Programming Company

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.Data;
   4:  using System.Data.Entity;
   5:  using System.Linq;
   6:  using System.Text;
   7:   
   8:  namespace Ia.Ngn.Cl.Model.Data.Huawei
   9:  {
  10:      ////////////////////////////////////////////////////////////////////////////
  11:   
  12:      /// <summary publish="true">
  13:      /// Huawei's Board support class of Next Generation Network'a (NGN's) data model.
  14:      /// </summary>
  15:      /// 
  16:      /// <remarks> 
  17:      /// Copyright © 2016-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  18:      ///
  19:      /// 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
  20:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  21:      ///
  22:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  23:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  24:      /// 
  25:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  26:      /// 
  27:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  28:      /// </remarks> 
  29:      public partial class Board
  30:      {
  31:          /// <summary/>
  32:          public Board() { }
  33:   
  34:          ////////////////////////////////////////////////////////////////////////////
  35:   
  36:          /// <summary>
  37:          ///
  38:          /// </summary>
  39:          public static bool Update(Ia.Ngn.Cl.Model.Business.Huawei.Ems.Response response, out Ia.Cl.Model.Result result)
  40:          {
  41:              bool isUpdated;
  42:              int did, fn, sn, queryFn, querySn, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
  43:              string id, queryCommand, queryDev;
  44:              DataColumnCollection columns;
  45:              DataRow columnDataRow;
  46:              Ia.Ngn.Cl.Model.Huawei.EmsBoard board, newBoard;
  47:              Ia.Ngn.Cl.Model.Huawei.EmsDev dev;
  48:              List<string> itemIdList;
  49:              List<Ia.Ngn.Cl.Model.Huawei.EmsBoard> boardList;
  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:                  //queryCommand = "LST-BOARD::DEV=MDU-SMC-1126-002,FN=0:{ctag}::;"; // debug
  59:   
  60:                  // LST-BOARD::DEV=MDU-FNT-806-002,FN=0:{ctag}::;
  61:                  // LST-BOARD::DEV=MDU-FNT-806-002,FN=0,SN=3:{ctag}::;
  62:                  queryDev = Ia.Cl.Model.Default.Match(queryCommand, @"DEV=(.+?),");
  63:                  queryFn = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"FN=(\d+)"));
  64:                  querySn = int.TryParse(Ia.Cl.Model.Default.Match(queryCommand, @"SN=(\d+)"), out int i) ? i : 0;
  65:   
  66:                  columns = response.QueryDataTable.Columns;
  67:                  readItemCount = response.QueryDataTable.Rows.Count;
  68:   
  69:                  dev = Ia.Ngn.Cl.Model.Data.Huawei.Dev.ReadByDev(queryDev);
  70:   
  71:                  if (dev != null)
  72:                  {
  73:                      itemIdList = new List<string>(response.QueryDataTable.Rows.Count + 1);
  74:   
  75:                      using (var db = new Ia.Ngn.Cl.Model.Ngn())
  76:                      {
  77:                          boardList = (from eb in db.EmsBoards where eb.EmsDev.DEV == queryDev select eb).ToList();
  78:   
  79:                          existingItemCount = boardList.Count;
  80:   
  81:                          if (response.QueryDataTable.Rows.Count >= 1)
  82:                          {
  83:                              columnDataRow = response.QueryDataTable.Rows[0];
  84:   
  85:                              foreach (DataRow dataRow in response.QueryDataTable.Rows)
  86:                              {
  87:                                  did = int.Parse(dataRow[columns.IndexOf("DID")].ToString());
  88:                                  fn = int.Parse(dataRow[columns.IndexOf("FN")].ToString());
  89:                                  sn = int.Parse(dataRow[columns.IndexOf("SN")].ToString());
  90:   
  91:                                  id = Ia.Ngn.Cl.Model.Business.Huawei.Board.BoardId(dev.Id, fn, sn);
  92:   
  93:                                  board = (from b in boardList where b.Id == id select b).SingleOrDefault();
  94:   
  95:                                  newBoard = new Ia.Ngn.Cl.Model.Huawei.EmsBoard()
  96:                                  {
  97:                                      Id = id,
  98:                                      DID = did,
  99:                                      FN = fn,
 100:                                      SN = sn,
 101:                                      BT = columns.Contains("BT") ? dataRow[columns.IndexOf("BT")].ToString() : string.Empty,
 102:                                      BVER = columns.Contains("BVER") ? dataRow[columns.IndexOf("BVER")].ToString() : string.Empty,
 103:                                      BSTAT = columns.Contains("BSTAT") ? dataRow[columns.IndexOf("BSTAT")].ToString() : string.Empty,
 104:                                      DEV = columns.Contains("DEV") ? dataRow[columns.IndexOf("DEV")].ToString() : string.Empty,
 105:                                      BNAME = columns.Contains("BNAME") ? dataRow[columns.IndexOf("BNAME")].ToString() : string.Empty,
 106:                                      NAME = columns.Contains("NAME") ? dataRow[columns.IndexOf("NAME")].ToString() : string.Empty,
 107:                                      ALIAS = columns.Contains("ALIAS") ? dataRow[columns.IndexOf("ALIAS")].ToString() : string.Empty,
 108:                                      ELABEL = columns.Contains("ELABEL") ? dataRow[columns.IndexOf("ELABEL")].ToString() : string.Empty,
 109:                                      PCBVER = columns.Contains("PCBVER") ? dataRow[columns.IndexOf("PCBVER")].ToString() : string.Empty,
 110:                                      SWVER = columns.Contains("SWVER") ? dataRow[columns.IndexOf("SWVER")].ToString() : string.Empty,
 111:                                      ROLE = columns.Contains("ROLE") ? dataRow[columns.IndexOf("ROLE")].ToString() : string.Empty,
 112:                                      ALARM = columns.Contains("ALARM") ? dataRow[columns.IndexOf("ALARM")].ToString() : string.Empty,
 113:                                      ST = columns.Contains("ST") ? dataRow[columns.IndexOf("ST")].ToString() : string.Empty,
 114:                                      PROTECTMODE = columns.Contains("PROTECTMODE") ? dataRow[columns.IndexOf("PROTECTMODE")].ToString() : string.Empty,
 115:                                      PORTCOUNT = columns.Contains("PORTCOUNT") ? dataRow[columns.IndexOf("PORTCOUNT")].ToString() : string.Empty,
 116:                                      PORTTYPE = columns.Contains("PORTTYPE") ? dataRow[columns.IndexOf("PORTTYPE")].ToString() : string.Empty,
 117:                                      WORKMODE = columns.Contains("WORKMODE") ? dataRow[columns.IndexOf("WORKMODE")].ToString() : string.Empty,
 118:                                      REPLACE = columns.Contains("REPLACE") ? dataRow[columns.IndexOf("REPLACE")].ToString() : string.Empty,
 119:                                      EmsDev = (from ed in db.EmsDevs where ed.Id == dev.Id select ed).SingleOrDefault()
 120:                                  };
 121:   
 122:                                  if (board == null)
 123:                                  {
 124:                                      newBoard.Created = newBoard.Updated = DateTime.UtcNow.AddHours(3);
 125:   
 126:                                      db.EmsBoards.Add(newBoard);
 127:   
 128:                                      insertedItemCount++;
 129:                                  }
 130:                                  else // update
 131:                                  {
 132:                                      // below: copy values from newBoard to board
 133:   
 134:                                      if (board.Update(newBoard))
 135:                                      {
 136:                                          db.EmsBoards.Attach(board);
 137:                                          db.Entry(board).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 (boardList.Count > 0)
 148:                              {
 149:                                  foreach (Ia.Ngn.Cl.Model.Huawei.EmsBoard bo in boardList)
 150:                                  {
 151:                                      if (!itemIdList.Contains(bo.Id))
 152:                                      {
 153:                                          board = (from eb in db.EmsBoards where eb.Id == bo.Id select eb).SingleOrDefault();
 154:   
 155:                                          db.EmsBoards.Remove(board);
 156:   
 157:                                          deletedItemCount++;
 158:                                      }
 159:                                  }
 160:                              }
 161:                          }
 162:                          else
 163:                          {
 164:                              // below: remove all values since reading was empty
 165:   
 166:                              if (boardList.Count > 0)
 167:                              {
 168:                                  foreach (Ia.Ngn.Cl.Model.Huawei.EmsBoard bo in boardList)
 169:                                  {
 170:                                      board = (from eb in db.EmsBoards where eb.Id == bo.Id select eb).SingleOrDefault();
 171:   
 172:                                      db.EmsBoards.Remove(board);
 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.AddError("Dev is null for dev: " + queryDev);
 190:                  }
 191:              }
 192:              else
 193:              {
 194:                  result.AddWarning("response.QueryDataTable is null");
 195:              }
 196:   
 197:              return isUpdated;
 198:          }
 199:   
 200:          ////////////////////////////////////////////////////////////////////////////
 201:   
 202:          /// <summary>
 203:          ///
 204:          /// </summary>
 205:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsBoard> List()
 206:          {
 207:              List<Ia.Ngn.Cl.Model.Huawei.EmsBoard> list;
 208:   
 209:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 210:              {
 211:                  list = (from eb in db.EmsBoards select eb).Include(x => x.EmsDev).ToList();
 212:              }
 213:   
 214:              return list.ToList();
 215:          }
 216:   
 217:          ////////////////////////////////////////////////////////////////////////////
 218:   
 219:          /// <summary>
 220:          ///
 221:          /// </summary>
 222:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsBoard> List(int devId)
 223:          {
 224:              List<Ia.Ngn.Cl.Model.Huawei.EmsBoard> list;
 225:   
 226:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 227:              {
 228:                  list = (from b in db.EmsBoards where b.EmsDev.Id == devId select b).ToList();
 229:              }
 230:   
 231:              return list;
 232:          }
 233:   
 234:          ////////////////////////////////////////////////////////////////////////////
 235:   
 236:          /// <summary>
 237:          ///
 238:          /// </summary>
 239:          public static Ia.Ngn.Cl.Model.Huawei.EmsBoard ReadByEmsNameFnSn(string emsName, int fn, int sn)
 240:          {
 241:              Ia.Ngn.Cl.Model.Huawei.EmsBoard item;
 242:   
 243:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 244:              {
 245:                  item = (from b in db.EmsBoards where b.DEV == emsName && b.FN == fn && b.SN == sn select b).SingleOrDefault();
 246:              }
 247:   
 248:              return item;
 249:          }
 250:   
 251:          ////////////////////////////////////////////////////////////////////////////
 252:   
 253:          /// <summary>
 254:          ///
 255:          /// </summary>
 256:          public static Ia.Ngn.Cl.Model.Huawei.EmsBoard ReadByDidFnSn(int did, int fn, int sn)
 257:          {
 258:              Ia.Ngn.Cl.Model.Huawei.EmsBoard item;
 259:   
 260:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 261:              {
 262:                  item = (from b in db.EmsBoards where b.DID == did && b.FN == fn && b.SN == sn select b).SingleOrDefault();
 263:              }
 264:   
 265:              return item;
 266:          }
 267:   
 268:          ////////////////////////////////////////////////////////////////////////////
 269:          ////////////////////////////////////////////////////////////////////////////
 270:   
 271:          /// <summary>
 272:          ///
 273:          /// </summary>
 274:          public static string ToSimpleTextString(Ia.Ngn.Cl.Model.Huawei.EmsBoard emdBoard)
 275:          {
 276:              StringBuilder sb;
 277:   
 278:              sb = new StringBuilder();
 279:   
 280:              sb.AppendLine("DID: " + emdBoard.DID);
 281:              sb.AppendLine("FN: " + emdBoard.FN);
 282:              sb.AppendLine("SN: " + emdBoard.SN);
 283:              sb.AppendLine("BT: " + emdBoard.BT);
 284:              sb.AppendLine("BVER: " + emdBoard.BVER);
 285:              sb.AppendLine("BSTAT: " + emdBoard.BSTAT);
 286:   
 287:              return sb.ToString();
 288:          }
 289:   
 290:          ////////////////////////////////////////////////////////////////////////////
 291:          ////////////////////////////////////////////////////////////////////////////
 292:      }
 293:   
 294:      ////////////////////////////////////////////////////////////////////////////
 295:      ////////////////////////////////////////////////////////////////////////////
 296:  }