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.Mdaa.Cl.Model.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: ///
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 class DatabaseInformation
29: {
30: private string filePath = "C:\\Users\\Jasem\\Documents\\Visual Studio 2022\\Projects\\Next Generation Network\\cl\\model\\data\\mdaa\\database-information.json";
31:
32: private readonly object objectLock = new object();
33:
34: /// <summary/>
35: public List<TableInformation> TableInformationList { get; set; }
36:
37: ////////////////////////////////////////////////////////////////////////////
38:
39: /// <summary>
40: ///
41: /// </summary>
42: public DatabaseInformation()
43: {
44: this.TableInformationList = new List<TableInformation>();
45: }
46:
47: ////////////////////////////////////////////////////////////////////////////
48:
49: /// <summary>
50: ///
51: /// </summary>
52: public void Write()
53: {
54: lock (objectLock)
55: {
56: var json = Serialize();
57:
58: File.WriteAllText(filePath, json);
59: }
60: }
61:
62: ////////////////////////////////////////////////////////////////////////////
63:
64: /// <summary>
65: ///
66: /// </summary>
67: public void Read()
68: {
69: lock (objectLock)
70: {
71: if (File.Exists(filePath))
72: {
73: var json = File.ReadAllText(filePath);
74:
75: var databaseInformation = Deserialize(json);
76:
77: this.TableInformationList = databaseInformation.TableInformationList;
78: }
79: else
80: {
81: var tableList = Ia.Ngn.Cl.Model.Data.MinistryDatabase.TableList;
82:
83: foreach (var table in tableList)
84: {
85: var tableInformation = new TableInformation();
86:
87: tableInformation.Name = table.Name;
88: tableInformation.Schema = table.Schema;
89:
90: this.TableInformationList.Add(tableInformation);
91: }
92:
93: Write();
94: }
95: }
96: }
97:
98: ////////////////////////////////////////////////////////////////////////////
99:
100: /// <summary>
101: ///
102: /// </summary>
103: public void Update(Ia.Mdaa.Cl.Model.Business.TableInformation tableInformation)
104: {
105: if (tableInformation != null)
106: {
107: if (!string.IsNullOrEmpty(tableInformation.Name))
108: {
109: var currentTableInformation = (from ti in this.TableInformationList
110: where ti.Name == tableInformation.Name && ti.Schema == tableInformation.Schema
111: select ti).SingleOrDefault();
112:
113: if (currentTableInformation != null)
114: {
115: currentTableInformation.Name = tableInformation.Name;
116: currentTableInformation.Schema = tableInformation.Schema;
117: currentTableInformation.Count = tableInformation.Count;
118: currentTableInformation.Desc = tableInformation.Desc;
119: currentTableInformation.TopRecordText = tableInformation.TopRecordText;
120:
121: Write();
122:
123: Read();
124: }
125: }
126: }
127: }
128:
129: ////////////////////////////////////////////////////////////////////////////
130:
131: /// <summary>
132: ///
133: /// </summary>
134: private DatabaseInformation Deserialize(string json)
135: {
136: DatabaseInformation databaseInformation;
137:
138: if (!string.IsNullOrEmpty(json))
139: {
140: databaseInformation = JsonSerializer.Deserialize<DatabaseInformation>(json);
141: }
142: else databaseInformation = default(DatabaseInformation);
143:
144: return databaseInformation;
145: }
146:
147: ////////////////////////////////////////////////////////////////////////////
148:
149: /// <summary>
150: ///
151: /// </summary>
152: private string Serialize()
153: {
154: var jsonSerializerOptions = new JsonSerializerOptions
155: {
156: WriteIndented = true
157: };
158:
159: return JsonSerializer.Serialize(this, jsonSerializerOptions);
160: }
161:
162: ////////////////////////////////////////////////////////////////////////////
163: ////////////////////////////////////////////////////////////////////////////
164: }
165:
166: ////////////////////////////////////////////////////////////////////////////
167: ////////////////////////////////////////////////////////////////////////////
168: }