)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » DatabaseInformation (Ia.Ftn.Mdaa.Cl.Models.Business)

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

DatabaseInformation support class for Ministry Database Analysis Application business model.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.IO;
    4: using System.Linq;
    5: using System.Text.Json;
    6:  
    7: namespace Ia.Ftn.Mdaa.Cl.Models.Business
    8: {
    9:     ////////////////////////////////////////////////////////////////////////////
   10:  
   11:     /// <summary publish="true">
   12:     /// DatabaseInformation support class for Ministry Database Analysis Application business model.
   13:     /// </summary>
   14:     /// 
   15:     /// <remarks> 
   16:     /// Copyright © 2022-2023 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   17:     /// </remarks> 
   18:     public class DatabaseInformation
   19:     {
   20:         private string filePath = "C:\\Users\\Jasem\\Documents\\Visual Studio 2022\\Projects\\Fixed Telecommunications Network\\cl\\model\\data\\mdaa\\database-information.json";
   21:  
   22:         private readonly object objectLock = new object();
   23:  
   24:         /// <summary/>
   25:         public List<TableInformation> TableInformationList { get; set; }
   26:  
   27:         ////////////////////////////////////////////////////////////////////////////
   28:  
   29:         /// <summary>
   30:         ///
   31:         /// </summary>
   32:         public DatabaseInformation()
   33:         {
   34:             this.TableInformationList = new List<TableInformation>();
   35:         }
   36:  
   37:         ////////////////////////////////////////////////////////////////////////////
   38:  
   39:         /// <summary>
   40:         ///
   41:         /// </summary>
   42:         public void Write()
   43:         {
   44:             lock (objectLock)
   45:             {
   46:                 var json = Serialize();
   47:  
   48:                 File.WriteAllText(filePath, json);
   49:             }
   50:         }
   51:  
   52:         ////////////////////////////////////////////////////////////////////////////
   53:  
   54:         /// <summary>
   55:         ///
   56:         /// </summary>
   57:         public void Read()
   58:         {
   59:             lock (objectLock)
   60:             {
   61:                 if (File.Exists(filePath))
   62:                 {
   63:                     var json = File.ReadAllText(filePath);
   64:  
   65:                     var databaseInformation = Deserialize(json);
   66:  
   67:                     this.TableInformationList = databaseInformation.TableInformationList;
   68:                 }
   69:                 else
   70:                 {
   71:                     var tableList = Ia.Ftn.Cl.Models.Data.MinistryDatabase.TableList;
   72:  
   73:                     foreach (var table in tableList)
   74:                     {
   75:                         var tableInformation = new TableInformation();
   76:  
   77:                         tableInformation.Name = table.Name;
   78:                         tableInformation.Schema = table.Schema;
   79:  
   80:                         this.TableInformationList.Add(tableInformation);
   81:                     }
   82:  
   83:                     Write();
   84:                 }
   85:             }
   86:         }
   87:  
   88:         ////////////////////////////////////////////////////////////////////////////
   89:  
   90:         /// <summary>
   91:         ///
   92:         /// </summary>
   93:         public void Update(Ia.Ftn.Mdaa.Cl.Models.Business.TableInformation tableInformation)
   94:         {
   95:             if (tableInformation != null)
   96:             {
   97:                 if (!string.IsNullOrEmpty(tableInformation.Name))
   98:                 {
   99:                     var currentTableInformation = (from ti in this.TableInformationList
  100:                                                    where ti.Name == tableInformation.Name && ti.Schema == tableInformation.Schema
  101:                                                    select ti).SingleOrDefault();
  102:  
  103:                     if (currentTableInformation != null)
  104:                     {
  105:                         currentTableInformation.Name = tableInformation.Name;
  106:                         currentTableInformation.Schema = tableInformation.Schema;
  107:                         currentTableInformation.Count = tableInformation.Count;
  108:                         currentTableInformation.Desc = tableInformation.Desc;
  109:                         currentTableInformation.TopRecordText = tableInformation.TopRecordText;
  110:  
  111:                         Write();
  112:  
  113:                         Read();
  114:                     }
  115:                 }
  116:             }
  117:         }
  118:  
  119:         ////////////////////////////////////////////////////////////////////////////
  120:  
  121:         /// <summary>
  122:         ///
  123:         /// </summary>
  124:         private DatabaseInformation Deserialize(string json)
  125:         {
  126:             DatabaseInformation databaseInformation;
  127:  
  128:             if (!string.IsNullOrEmpty(json))
  129:             {
  130:                 databaseInformation = JsonSerializer.Deserialize<DatabaseInformation>(json);
  131:             }
  132:             else databaseInformation = default(DatabaseInformation);
  133:  
  134:             return databaseInformation;
  135:         }
  136:  
  137:         ////////////////////////////////////////////////////////////////////////////
  138:  
  139:         /// <summary>
  140:         ///
  141:         /// </summary>
  142:         private string Serialize()
  143:         {
  144:             var jsonSerializerOptions = new JsonSerializerOptions
  145:             {
  146:                 WriteIndented = true
  147:             };
  148:  
  149:             return JsonSerializer.Serialize(this, jsonSerializerOptions);
  150:         }
  151:  
  152:         ////////////////////////////////////////////////////////////////////////////
  153:         ////////////////////////////////////////////////////////////////////////////
  154:     }
  155:  
  156:     ////////////////////////////////////////////////////////////////////////////
  157:     ////////////////////////////////////////////////////////////////////////////
  158: }