forked from izavyalov-dev/TreeBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeBuilder.cs
387 lines (347 loc) · 14.1 KB
/
TreeBuilder.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;
using System.Web.Script.Serialization;
namespace TreeBuilder
{
public partial class TreeBuilder : Form
{
List<Color> ColorIndex = new List<Color>();
private List<Button> ListItems = new List<Button>();
List<string> HistoryNode = new List<string>();
private readonly List<string> ListOfAllCategory = new List<string>();
private readonly List<string> ListOfAllParentCategory = new List<string>();
public TreeBuilder()
{
InitializeComponent();
btnBack.Enabled = HistoryNode.Count > 0;
ColorIndex.Add(Color.Brown);
ColorIndex.Add(Color.BlueViolet);
ColorIndex.Add(Color.CadetBlue);
ColorIndex.Add(Color.Chartreuse);
ColorIndex.Add(Color.Coral);
ColorIndex.Add(Color.Cyan);
}
private dynamic Deserialise(string _json)
{
try
{
JavaScriptSerializer var = new JavaScriptSerializer();
var.MaxJsonLength = _json.Length + 50;
return var.DeserializeObject(_json);
}
catch (Exception exception)
{
Console.WriteLine(exception);
return _json;
}
}
private string Serialise(dynamic _value)
{
return new JavaScriptSerializer().Serialize(_value);
}
private HttpWebRequest CreateHttp(string createOption)
{
return WebRequest.Create(createOption) as HttpWebRequest;
}
private dynamic SendRequset(string options)
{
dynamic ev = new EventData();
string result = "";
HttpWebRequest httpWeb = CreateHttp(options);
if (httpWeb == null)
{
return new Exception("error");
}
httpWeb.Timeout = 40000;
httpWeb.ReadWriteTimeout = 40000;
httpWeb.Method = "GET";
httpWeb.ContentType = "application/x-www-form-urlencoded; boundary=----WebKitFormBoundarySkAQdHysJKel8YBM";
httpWeb.UseDefaultCredentials = true;
httpWeb.ServicePoint.Expect100Continue = false;
httpWeb.Headers.Add(HttpRequestHeader.Authorization,
"Basic YWthZG9tQGdtYWlsLmNvbTpZVGcxWmpBMU1tUTFNelZoTW1JeFpESTVabUZqTmpZNVpUazBPVFUyTWprNk1tUQ");
try
{
HttpWebResponse httpResponse = httpWeb.GetResponse() as HttpWebResponse;
if (httpResponse == null)
{
return new Exception("error");
}
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
{
result = streamReader.ReadToEnd();
}
Dictionary<string, string> headers = new Dictionary<string, string>();
for (int i = 0; i < httpResponse.Headers.Count; ++i)
headers.Add(httpResponse.Headers.Keys[i], httpResponse.Headers[i]);
ev.headers = headers;
ev.successful = true;
ev.items = Deserialise(result);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
return ev;
}
private void btnSendRequest_Click_1(object sender, EventArgs e)
{
HistoryNode.Add(tbValue.Text);
btnBack.Enabled = HistoryNode.Count > 1;
MainPanel.Controls.Clear();
ListItems.Clear();
int StepW = 1;
int StepH = 1;
ListOfAllCategory.Clear();
//string reqest = GenerateRequest(tbValue.Text);
CreateTreeNodesEx(tbValue.Text, "", new Point(0, 0));
}
private string GenerateRequest(string id)
{
string reqest = tbAsk.Text;
reqest += "?expand=categoriesDescription&advancedFilter=[{";
reqest += tbParent.Text + ": ";
reqest += id;
reqest += "},{\"categories_status\": 1}]";
return reqest;
}
private void AddCategory(string categoryNumebr, string categoryDescription, int x)
{
string category = "";
category += '\r';
category += '\n';
for (int i = 0; i < x * 2; i++)
category += ' ';
category += categoryNumebr;
category += " - ";
category += categoryDescription;
ListOfAllCategory.Add(category);
if (categoryDescription != String.Empty)
ListOfAllParentCategory.Add(categoryDescription);
}
private Point CreateTreeNodesEx(string rootNumber, string rootDescription, Point point)
{
dynamic data = SendRequset(GenerateRequest(rootNumber));
if (rootDescription == String.Empty)
rootDescription = ParseFromDynamicData(data.items, 0, "categoriesDescription", "categories_name").ToString();
if (rootDescription == String.Empty)
rootDescription = ParseFromDynamicData(data.items, 0, "categoriesDescription", "categories_description").ToString();
if (rootDescription == String.Empty)
rootDescription = ParseFromDynamicData(data.items, 0, "categoriesDescription", "categories_heading_title").ToString();
if (rootDescription == String.Empty)
rootDescription = "нет описание";
AddCategory(rootNumber, rootDescription, point.X);
CreateAndPushNode(rootNumber, rootDescription, point);
Point tmpPoint = new Point(point.X + 1, point.Y);
foreach (var item in data.items)
{
string description = ParseFromDynamicData(item, "categoriesDescription", "categories_name").ToString();
if (description == String.Empty)
description = ParseFromDynamicData(item, "categoriesDescription", "categories_description").ToString();
if (description == String.Empty)
description = ParseFromDynamicData(item, "categoriesDescription", "categories_heading_title").ToString();
if (description == String.Empty)
description = "нет описание";
Console.WriteLine("parse dd " + description);
string number = item[tbKey.Text].ToString();
tmpPoint = new Point(tmpPoint.X, tmpPoint.Y + 1);
tmpPoint = CreateTreeNodesEx(number, description, tmpPoint);
Control[] ww = MainPanel.Controls.Find(number, true);
if (ww.Count() == 0)
{
AddCategory(rootNumber, rootDescription, point.X);
CreateAndPushNode(number, description, tmpPoint);
}
}
point = new Point(point.X, tmpPoint.Y);
return point;
}
private void CreateAndPushNode(string id, string text, Point point)
{
string complexName = id + "\n" + text;
NodeItem node = new NodeItem(id, complexName, new Point(10 + point.X * 200, 70 * point.Y));
node.MouseUp += ClickToNode;
node.BackColor = ColorIndex[point.X];
MainPanel.Controls.Add(node);
}
private void ClickToNode(object o, MouseEventArgs e)
{
/*
if (e.Button == MouseButtons.Right)
{
// Clipboard.SetText(NameButton);
}
else
{
NodeItem item = o as NodeItem;
if (item == null) return;
tbValue.Text = item.Text;
btnSendRequest_Click_1(null, null);
}
*/
}
private void MainPanel_Paint(object sender, PaintEventArgs e)
{
OnPaint(e);
/*
using (Graphics g = e.Graphics)
{
for (int i = 0; i < 300; i += 8)
{
var p = new Pen(Color.Red, 2);
var point1 = new Point(234 + i, 118);
var point2 = new Point(293 - i, 228);
g.DrawLine(p, point1, point2);
}
}
*/
}
private void button2_Click(object sender, EventArgs e)
{
if (HistoryNode.Count > 1)
{
tbValue.Text = HistoryNode[HistoryNode.Count - 2];
HistoryNode.RemoveAt(HistoryNode.Count - 2);
HistoryNode.RemoveAt(HistoryNode.Count - 1);
btnSendRequest_Click_1(null, null);
}
btnBack.Enabled = HistoryNode.Count > 1;
}
private void tbValue_KeyUp(object sender, KeyEventArgs e)
{
button2_Click(null, null);
}
public dynamic ParseFromDynamicData(dynamic data, params object[] keys)
{
if (keys.Length == 0) return String.Empty;
foreach (object key in keys)
{
try
{
if ((key is string) && !data.ContainsKey(key.ToString()))
{
//MessageBox.Show("Не корректные данные от сервера для ключа " + KeyHistory + "/" + key);
return String.Empty;
}
}
catch
{
return String.Empty;
}
try
{
if ((key is int) && Convert.ToInt32(key.ToString()) >= data.Length)
{
return String.Empty;
}
}
catch
{
return "";
}
if (key is string)
data = data[key.ToString()];
else
data = data[Convert.ToInt32(key)];
}
return data ?? String.Empty;
}
private void btnToText_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = "category.txt";
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
Console.WriteLine(saveFileDialog1.FileName);
using (StreamWriter outfile = new StreamWriter(saveFileDialog1.FileName, true))
{
for (int i = 0; i < ListOfAllCategory.Count; i++)
{
outfile.WriteLine(ListOfAllCategory[i]);
Console.WriteLine(ListOfAllCategory[i] + (i >= ListOfAllParentCategory.Count ? " " : ListOfAllParentCategory[i]));
}
}
}
}
}
public class EventData : DynamicObject, ICloneable
{
public bool successful { set; get; }
public int sessionId { set; get; }
public object sender { set; get; }
public string image { set; get; }
// путь к Api
public string PathApi { set; get; }
// метод "GET" "PUT" и тд
public string Method { set; get; }
// другое поля не обязательное
//public dynamic Data { set; get; }
public Dictionary<string, object> BodyParam { set; get; }
public Dictionary<string, object> UrlParam { set; get; }
public Dictionary<string, string> DictionaryData;
public EventData()
{
image = "";
sender = new object();
sessionId = -1;
}
// The inner dictionary.
public Dictionary<string, object> dictionary
= new Dictionary<string, object>();
// This property returns the number of elements
// in the inner dictionary.
public int Count
{
get { return dictionary.Count; }
}
// If you try to get a value of a property
// not defined in the class, this method is called.
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
string name = binder.Name.ToLower();
// If the property name is found in a dictionary,
// set the result parameter to the property value and return true.
// Otherwise, return false.
return dictionary.TryGetValue(name, out result);
}
// If you try to set a value of a property that is
// not defined in the class, this method is called.
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
dictionary[binder.Name.ToLower()] = value;
// You can always add a value to a dictionary,
// so this method always returns true.
return true;
}
public object Clone()
{
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
stream.Seek(0, SeekOrigin.Begin);
return formatter.Deserialize(stream);
}
}
}
}