-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
267 lines (255 loc) · 6.81 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using WiktionaryCrawler.Data;
using WiktionaryCrawler.Models;
using WiktionaryCrawler.Serializers;
namespace WiktionaryCrawler
{
/// <summary>
/// The console app to create and retrieve a copy of the wiktionary.
/// </summary>
class Program
{
/// <summary>
/// The entry point of the application.
/// </summary>
public static void Main()
{
Console.WriteLine("Please wait; initializing...");
IWiktionarySerializer iws;
string userInput;
int menuOption;
if(ChooseMenuOption(out menuOption) || menuOption == 3)
{
return;
}
if(GetIWS(menuOption, out iws))
{
return;
}
if (iws.CheckExistence())
{
Console.WriteLine("The file or table already exists. Delete it?");
if (GetUserInput(out userInput))
{
return;
}
if (userInput.ToUpper()[0] == 'N')
{
return;
}
iws.DeleteExisting();
}
iws.CreateNew();
Console.WriteLine("Creating and serializing dictionary...");
WiktionaryParser wc = new WiktionaryParser();
if (wc.GetDictionary())
{
Console.WriteLine("Creation and serialization complete. Press any key to exit.");
}
else
{
Console.WriteLine("Creation and serialization failed. Try again?");
string tryAgainUI;
if (GetUserInput(out tryAgainUI))
{
return;
}
if (tryAgainUI.ToUpper()[0] == 'Y')
{
Main();
}
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
/// <summary>
/// A Ui method to show the main menu.
/// </summary>
/// <param name="menuChoice">An out parameter that indicates what menu option was chosen.</param>
/// <returns>True if the user chose to quit, false otherwise.</returns>
public static bool ChooseMenuOption(out int menuChoice)
{
string userInput;
menuChoice = 0;
Console.WriteLine("Welcome to Doug's Wikitionary scraper!");
Console.WriteLine("Q Quits at any time.");
Console.WriteLine("1.) Create the wiktionary and write it to file.");
Console.WriteLine("2.) Create the wiktionary and write it to a SqlServer instance.");
Console.WriteLine("3.) Quit.");
if(GetUserInput(out userInput) || userInput == "3")
{
return true;
}
if (!int.TryParse(userInput, out menuChoice) || menuChoice < 0 || menuChoice > 4)
{
Console.WriteLine("Please enter a valid int between 1 and 3.");
return ChooseMenuOption(out menuChoice);
}
return false;
}
/// <summary>
/// A Ui method that aids creation of the wikitionary serializer the user chose.
/// </summary>
/// <param name="option">The menu option for the wiktionary serializer chosen.</param>
/// <param name="iws">An out parameter that returns the user-chosen class that implements IWiktionarySerializer.</param>
/// <returns>True if the user chose to quit, false otherwise.</returns>
public static bool GetIWS(int option, out IWiktionarySerializer iws)
{
string name;
string path;
iws = null;
bool isSqlServer = option == 2;
if (isSqlServer)
{
Console.WriteLine("What table would you like to use? ");
if (GetUserInput(out name) || GetSqlConnectionString(out path))
{
return true;
}
iws = new SqlManager(name, path);
return false;
}
else
{
Console.WriteLine("Please enter a file name: ");
if (GetUserInput(out name))
{
return true;
}
iws = string.IsNullOrWhiteSpace(name) ? new FileManager() : new FileManager(name);
return false;
}
}
/// <summary>
/// A Ui method that aids creation of the Sql connection string for a SqlManager.
/// </summary>
/// <param name="sqlConnectionString">An out string of the final Sql connection.</param>
/// <returns>True if the user chose to quit, false otherwise.</returns>
public static bool GetSqlConnectionString(out string sqlConnectionString)
{
Boolean connectionMade = false;
sqlConnectionString = null;
string userInput;
while(!connectionMade)
{
Console.WriteLine("Enter a Sql connection string directly? ");
if (Console.ReadLine().ToUpper()[0] == 'Y')
{
Console.WriteLine("Enter: ");
if (GetUserInput(out sqlConnectionString))
{
return true;
}
}
else
{
if(BuildSqlConnectionString(out sqlConnectionString))
{
return true;
}
}
SqlConnection sqlconn = new SqlConnection(sqlConnectionString);
try
{
sqlconn.Open();
Console.WriteLine("Testing Connection...");
Console.WriteLine(sqlconn.ServerVersion);
Console.WriteLine("Success!");
connectionMade = true;
}
catch
{
Console.WriteLine("Connection failed...");
connectionMade = false;
}
finally
{
sqlconn.Close();
}
if (connectionMade)
{
return false;
}
Console.WriteLine("Try Again?");
if (GetUserInput(out userInput))
{
return true;
}
if (userInput.ToUpper()[0] == 'N')
{
return true;
}
}
return false;
}
/// <summary>
/// A helper Ui method that gets string input from the user.
/// </summary>
/// <param name="userInput">An out string that contains the string the user input.</param>
/// <returns>True if the user chose to quit, false otherwise.</returns>
public static bool GetUserInput(out string userInput)
{
userInput = Console.ReadLine();
if (string.IsNullOrWhiteSpace(userInput))
{
Console.WriteLine("Please try again.");
GetUserInput(out userInput);
}
else if (userInput.ToUpper()[0] == 'Q')
{
return true;
}
return false;
}
/// <summary>
/// A helper Ui method that helps a user build their own Sql connection string.
/// </summary>
/// <param name="sqlConnStr">An out string that contains the final Sql connection.</param>
/// <returns>True if the user chose to quit, false otherwise.</returns>
public static bool BuildSqlConnectionString(out string sqlConnStr)
{
string userInput = "";
sqlConnStr = "";
SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
Console.WriteLine("Server name: ");
if (GetUserInput(out userInput))
{
return true;
}
sb.DataSource = userInput;
Console.WriteLine("Database: ");
if (GetUserInput(out userInput))
{
return true;
}
sb.InitialCatalog = userInput;
Console.WriteLine("Use Windows integrated security?");
if (GetUserInput(out userInput))
{
return true;
}
if (userInput.ToUpper()[0] == 'Y')
{
sb.IntegratedSecurity = true;
sqlConnStr = sb.ToString();
return false;
}
Console.WriteLine("User name: ");
if (GetUserInput(out userInput))
{
return true;
}
sb.UserID = userInput;
Console.WriteLine("Password: ");
if (GetUserInput(out userInput))
{
return true;
}
sb.Password = userInput;
sqlConnStr = sb.ConnectionString;
return false;
}
}
}