-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
516 lines (440 loc) · 18.2 KB
/
Form1.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
using System;
using System.IO;
using Newtonsoft.Json;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
// Self
using N11Category.Models;
namespace N11Category
{
public partial class Form1 : Form
{
public Form1(bool hasToken)
{
if (!hasToken)
{
MessageBox.Show("Please login!");
this.Visible = false;
}
else
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false; // to avoid cross thread
}
}
// category
public static List<Category> categoryList = new List<Category>();
// attributes
public static List<Attribute> attributeList = new List<Attribute>();
public static List<AttributeValue> attributesWithValueList = new List<AttributeValue>();
// together
public static List<AttributeAndCategory> atrAndCatList = new List<AttributeAndCategory>();
public static int counter = 1; // file counter
public static string excelPath = ""; // excel path
ConnectionModelN11 conModel = null; // connection info
private void btnShowDialog_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Browse Text Files";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "txt";
//openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.Filter = "Excel Worksheets (*.xlsx)|*.xlsx";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string file = openFileDialog1.FileName;
lblExcelFileName.Text = file;
excelPath = file;
}
}
private Thread threadReadData = null;
private void btnReadData_Click(object sender, EventArgs e)
{
if (excelPath == "")
{
MessageBox.Show("Error no: 5 - Please Select Excel File!");
}
else
{
threadReadData = new Thread(new ThreadStart(GetDataFromExcel));
threadReadData.Start();
}
}
private Thread threadParseData = null;
private Thread threadGetAttributeCategory = null;
private void btnParseData_Click(object sender, EventArgs e)
{
if (categoryList.Count <= 0 || attributeList.Count <= 0)
{
MessageBox.Show("Error no: 6 - Please Read Data!");
}
else
{
threadParseData = new Thread(new ThreadStart(SetValues));
threadParseData.Start();
threadGetAttributeCategory = new Thread(new ThreadStart(GetAttributeAndCategoryId));
threadGetAttributeCategory.Start();
}
}
private void GetDataFromExcel()
{
//Create COM Objects. Create a COM object for everything that is referenced
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(excelPath);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[2];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
lblRowCount.Text = xlRange.Rows.Count.ToString(); // set label
//iterate over the rows and columns and print to the console as it appears in the file
//excel is not zero based!!
for (int i = 2; i <= rowCount; i++)
{
Category c = new Category();
c.name = xlRange.Cells[i, 1].Value2.ToString();
string catId = xlRange.Cells[i, 2].Value2.ToString();
c.category_id = int.Parse(catId);
c.attributeNames = new List<string>();
// kategorileri topla
if (!categoryList.Exists(cat => cat.category_id == c.category_id))
{
categoryList.Add(c);
}
Attribute at = new Attribute();
at.category_id = c.category_id;
string attrId = xlRange.Cells[i, 3].Value2.ToString();
at.attribute_id = int.Parse(attrId);
at.attribute_name = xlRange.Cells[i, 4].Value2.ToString();
at.attribute_value = xlRange.Cells[i, 5].Value2.ToString();
if (!categoryList.Find(cc => cc.category_id == c.category_id).attributeNames.Contains(at.attribute_name))
{
categoryList.Find(cc => cc.category_id == c.category_id).attributeNames.Add(at.attribute_name);
}
at.multipleselect = 0;
string mand = xlRange.Cells[i, 6].Value2.ToString();
if (mand == "Evet")
{
at.mandatory = 1;
}
else
{
at.mandatory = 0;
}
// özellikleri topla
if (!attributeList.Exists(atrk => atrk.attribute_id == at.attribute_id && atrk.attribute_value == at.attribute_value && atrk.category_id == at.category_id))
{
attributeList.Add(at);
}
UpdateCurrentRowText(i);
UpdateLeftRowText(rowCount, i);
UpdateAttrivuteValueText(at.attribute_value);
}
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
//rule of thumb for releasing com objects:
// never use two dots, all COM objects must be referenced and released individually
// ex: [somthing].[something].[something] is bad
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
lblReadDataStatus.Text = "Completed";
}
// get for oc_n11category_to_attr
private void GetAttributeAndCategoryId()
{
int counter2 = 1;
foreach (Category item in categoryList)
{
foreach (string item2 in item.attributeNames)
{
Attribute atari = attributeList.Find(iii => iii.attribute_name == item2);
AttributeAndCategory atr = new AttributeAndCategory();
atr.id = counter2;
atr.category_id = item.category_id.ToString();
atr.attribute_id = atari.attribute_id.ToString();
atrAndCatList.Add(atr);
counter2++;
// Update UI
UpdateCurrentParseCategoryText(item.name + " " + atari.attribute_name);
}
}
// Update Status
lblParseCategoryStatus.Text = "Completed";
}
// do it for n11category_attributes
private void SetValues()
{
foreach (Category item in categoryList)
{
List<Attribute> atrs = attributeList.FindAll(i => i.category_id == item.category_id);
foreach (string item2 in item.attributeNames)
{
AttributeValue atrValue = new AttributeValue();
List<Attribute> subAtr = atrs.FindAll(str => str.attribute_name == item2);
if (subAtr.Count > 0)
{
atrValue.mandatory = subAtr[0].mandatory;
atrValue.attribute_id = subAtr[0].attribute_id.ToString();
atrValue.multipleselect = subAtr[0].multipleselect;
atrValue.attribute_name = subAtr[0].attribute_name;
}
// begin to array
string begin = "[";
string value = "";
string end = "]";
foreach (Attribute item3 in subAtr)
{
value += '"' + item3.attribute_value + '"' + ',';
// Update UI
UpdateCurrentParseAttributeValueText(item2 + " " + item3.attribute_value);
}
value = value.Remove(value.LastIndexOf(','));
atrValue.valuelist = begin + value + end;
atrValue.id = counter;
if (!attributesWithValueList.Exists(akar => akar.attribute_id == atrValue.attribute_id && akar.attribute_name == atrValue.attribute_name))
{
attributesWithValueList.Add(atrValue);
}
counter++;
}
}
// Update Status
lblParseAttributeStatus.Text = "Completed";
}
// do it for n11category_attributes
private void InsertToc_n11category_attributes(DBConnection con)
{
try
{
foreach (AttributeValue item in attributesWithValueList)
{
string query = "INSERT INTO " + conModel.tableAttributes + "(attribute_id,attribute_name,mandatory,multipleselect,valuelist) VALUES(@attribute_id, @attribute_name, @mandatory, @multipleselect, @valuelist)";
var cmd = new MySqlCommand(query, con.Connection);
cmd.Parameters.AddWithValue("@attribute_id", item.attribute_id);
cmd.Parameters.AddWithValue("@attribute_name", item.attribute_name);
cmd.Parameters.AddWithValue("@mandatory", item.mandatory);
cmd.Parameters.AddWithValue("@multipleselect", item.multipleselect);
cmd.Parameters.AddWithValue("@valuelist", item.valuelist);
cmd.ExecuteNonQuery();
Console.WriteLine("executed item: " + item.id);
}
// Update status
lblInsertAttributesStatus.Text = "Completed";
}
catch (Exception ex)
{
MessageBox.Show("Error no: 3 - Insert into " + conModel.tableAttributes + " failed!");
}
}
// do it for oc_n11category_to_attr
private void InsertToc_n11category_to_attr(DBConnection con)
{
try
{
foreach (AttributeAndCategory item in atrAndCatList)
{
string query = "INSERT INTO " + conModel.tableCategories + "(category_id,attribute_id) VALUES(@category_id, @attribute_id)";
var cmd = new MySqlCommand(query, con.Connection);
cmd.Parameters.AddWithValue("@category_id", item.category_id);
cmd.Parameters.AddWithValue("@attribute_id", item.attribute_id);
cmd.ExecuteNonQuery();
Console.WriteLine("executed item: " + item.id);
}
// Update status
lblInsertCategoriesStatus.Text = "Completed";
}
catch (Exception ex)
{
MessageBox.Show("Error no: 4 - Insert into " + conModel.tableCategories + " failed!");
}
}
// Begin Update UI
private void UpdateCurrentRowText(int row)
{
lblCurrentRow.Invoke((MethodInvoker)delegate
{
lblCurrentRow.Text = row.ToString();
});
}
private void UpdateLeftRowText(int totalRow, int currentRow)
{
int leftRow = totalRow - currentRow;
lblLeftRow.Invoke((MethodInvoker)delegate
{
lblLeftRow.Text = leftRow.ToString();
});
}
private void UpdateAttrivuteValueText(string value)
{
lblAttributeValue.Invoke((MethodInvoker)delegate
{
lblAttributeValue.Text = value;
});
}
private void UpdateCurrentParseAttributeValueText(string value)
{
lblParseCurrentValue.Invoke((MethodInvoker)delegate
{
lblParseCurrentValue.Text = value;
});
}
private void UpdateCurrentParseCategoryText(string value)
{
lblParseCurrentCategory.Invoke((MethodInvoker)delegate
{
lblParseCurrentCategory.Text = value;
});
}
// End Update UI
// open copyright url
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string url = "https://doxasoftware.co.uk";
Process.Start(url);
}
private void btnTestConnection_Click(object sender, EventArgs e)
{
if (conModel == null && txtDbName.Text.Trim() == "")
{
MessageBox.Show("Error no: 2 - Please enter connection info OR Import database config!");
}
else
{
if (conModel == null)
{
conModel = new ConnectionModelN11();
conModel.port = txtPort.Text.Trim();
conModel.server = txtServer.Text.Trim();
conModel.username = txtUsername.Text.Trim();
conModel.password = txtPassword.Text.Trim();
conModel.databaseName = txtDbName.Text.Trim();
conModel.tableCategories = txtTableCategory.Text.Trim();
conModel.tableAttributes = txtTableAttribute.Text.Trim();
}
var con = DBConnection.Instance();
con.DatabaseName = conModel.databaseName;
if (con.IsConnect(conModel))
{
txtPort.Text = conModel.port;
txtServer.Text = conModel.server;
txtUsername.Text = conModel.username;
txtDbName.Text = conModel.databaseName;
txtTableAttribute.Text = conModel.tableAttributes;
txtTableCategory.Text = conModel.tableCategories;
MessageBox.Show("Test is successful.");
}
con.Close();
}
}
private void btnImportConfig_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Browse Text Files";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "txt";
openFileDialog1.Filter = "Db Config File (*.json)|*.json";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string file = openFileDialog1.FileName;
string tmp = File.ReadAllText(file);
conModel = JsonConvert.DeserializeObject<ConnectionModelN11>(tmp);
}
}
private void btnInsertAttributeValues_Click(object sender, EventArgs e)
{
if (conModel == null)
{
MessageBox.Show("Error no: 2 - Please import database config!");
}
else
{
var con = DBConnection.Instance();
con.DatabaseName = con.DatabaseName;
if (con.IsConnect(conModel))
{
InsertToc_n11category_attributes(con); // insert to n11category_attributes
}
con.Close();
}
}
private void btnInsertCategories_Click(object sender, EventArgs e)
{
if (conModel == null)
{
MessageBox.Show("Error no: 2 - Please import database config!");
}
else
{
var con = DBConnection.Instance();
con.DatabaseName = "n11";
if (con.IsConnect(conModel))
{
InsertToc_n11category_to_attr(con); // insert to oc_n11category_to_attr
}
con.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void BtnChangeLog_Click(object sender, EventArgs e)
{
var changeLogForm = new ChangeLogForm();
changeLogForm.Show();
}
}
}
public class Category
{
public string name { get; set; }
public int category_id { get; set; }
public List<string> attributeNames { get; set; }
}
public class Attribute
{
public int category_id { get; set; }
public int attribute_id { get; set; }
public string attribute_name { get; set; }
public string attribute_value { get; set; }
public int mandatory { get; set; }
public int multipleselect { get; set; }
}
public class AttributeValue
{
public int id { get; set; }
public string attribute_id { get; set; }
public string attribute_name { get; set; }
public int mandatory { get; set; }
public int multipleselect { get; set; }
public string valuelist { get; set; }
}
public class AttributeAndCategory
{
public int id { get; set; }
public string category_id { get; set; }
public string attribute_id { get; set; }
}