-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
191 lines (161 loc) · 9.35 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.IO;
using TBLKoWizard.DataAccess.Connections;
using TBLKoWizard.DataAccess.Factories;
using TBLKoWizard.Utlis;
namespace TBLKoWizard
{
class Program
{
static void Main(string[] args) {
DateTime currentTime = DateTime.Now;
string logFileName = $"{currentTime:yyyy-MM-dd_HH-mm-ss}.log";
EventLogger _logger = new EventLogger("logs", logFileName);
_logger.LogEvent($"Application started: {currentTime:yyyy-MM-dd_HH-mm-ss}", LogLevel.Info);
try
{
string configFile = "config.conf";
if (!File.Exists(configFile))
{
_logger.LogEvent("Config file does not exist. Creating new one.", LogLevel.Info);
ConfigurationHelper.CreateDefaultConfigFile(configFile);
var userSettings = ConfigurationHelper.ReadConnectionSettingsFromConsole();
ConfigurationHelper.SaveConnectionSettingsToFile(configFile, userSettings);
_logger.LogEvent("Settings saved to config file.", LogLevel.Info);
}
var settings = ConfigurationHelper.LoadConnectionSettings(configFile);
IDatabaseConnectionFactory connectionFactory = new DatabaseConnectionFactory(_logger);
IDatabaseConnection databaseConnection = connectionFactory.GetDatabaseConnection(settings.ConnectionMethod);
databaseConnection.Connect(settings.Server, settings.DbName, settings.Username, settings.Password);
if (!databaseConnection.DatabaseExists(settings.DbName))
{
_logger.LogEvent("Database does not exist. Creating new one.", LogLevel.Info);
databaseConnection.CreateDatabase(settings.DbName);
}
else
{
Console.WriteLine();
if (!databaseConnection.TableVersionExists())
{
_logger.LogEvent("'_VERSION' table does not exist. Creating new one.", LogLevel.Info);
databaseConnection.CreateVersionTable();
}
var versionInfo = databaseConnection.GetVersionEntry();
string? questionAction = null;
if (versionInfo != null && versionInfo.Rows.Count > 0)
{
// Menu import, export
while (true)
{
Console.WriteLine("What would you like to do? Type 'import' or 'i' for Import and 'export' or 'e' for Export.");
questionAction = Console.ReadLine();
_logger.LogEvent("What would you like to do? Type 'import' or 'i' for Import and 'export' or 'e' for Export.", LogLevel.Info);
if (questionAction != null && (questionAction.ToLower() == "import" || questionAction.ToLower() == "i" || questionAction.ToLower() == "export" || questionAction.ToLower() == "e"))
{
break;
}
else
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Invalid response. Please type 'import' or 'i' for Import and 'export' or 'e' for Export.");
Console.ResetColor();
}
}
if(questionAction != null && questionAction.ToLower() == "import" || questionAction.ToLower() == "i") {
int versionID = (int)versionInfo.Rows[0]["VersionID"];
DateTime createdAt = (DateTime)versionInfo.Rows[0]["CreatedAt"];
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Data entries from tbl files already exist in the database, for version {versionID}, created on {createdAt}.");
Console.ResetColor();
_logger.LogEvent($"Data entries from tbl files already exist in the database, for version {versionID}, created on {createdAt}.", LogLevel.Warning);
while (true)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Are you sure you want to delete them? Please confirm by typing 'yes' ('y') or 'no' ('n').");
Console.ResetColor();
string? question = Console.ReadLine();
if (question != null && (question.ToLower() == "yes" || question.ToLower() == "y" || question.ToLower() == "no" || question.ToLower() == "n"))
{
if (question.ToLower() == "yes" || question.ToLower() == "y")
{
Console.WriteLine();
databaseConnection.DropAllTables();
}
break;
}
else
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Invalid response. Please type 'yes' ('y') or 'no' ('n').");
Console.ResetColor();
}
}
}
}
//Enter Client TBL Version
int clientVersion;
while (true)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Cyan;
if (questionAction != null && questionAction.ToLower() == "import" || questionAction.ToLower() == "i")
{
Console.WriteLine("Please enter the client version number from which the tbl files originate e.g. 1298");
} else
{
Console.WriteLine("Please enter the client version number to which the tables from the database to .tbl files are to be exported");
}
Console.ResetColor();
string? input = Console.ReadLine();
if (int.TryParse(input, out clientVersion) && clientVersion > 0)
{
if (questionAction != null && questionAction.ToLower() == "import" || questionAction.ToLower() == "i")
{
if (!databaseConnection.TableVersionExists())
{
databaseConnection.CreateVersionTable();
}
databaseConnection.CreateVersionEntry(clientVersion);
//Enter Client Data Path and LoadData
DataImporter dataImporter = new DataImporter(clientVersion, databaseConnection, _logger);
dataImporter.ImportDataFromDirectory();
}
else if (questionAction != null && questionAction.ToLower() == "export" || questionAction.ToLower() == "e")
{
DataExporter dataEmporter = new DataExporter(clientVersion, databaseConnection, _logger);
dataEmporter.ExportDataFromDatabase();
}
databaseConnection.Disconnect();
break;
}
else
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Invalid input. Please enter a valid version.");
Console.ResetColor();
}
}
}
}
catch (System.NotImplementedException ex)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"Error connecting to the database: {ex.Message}");
Console.ResetColor();
_logger.LogEvent($"Error connecting to the database: {ex.Message}", LogLevel.Error);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Unexpected error: {ex.Message}");
Console.ResetColor();
_logger.LogEvent($"Unexpected error: {ex.Message}", LogLevel.Error);
}
}
}
}