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

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