/
ConversionHandler.xaml.cs
377 lines (309 loc) · 13 KB
/
ConversionHandler.xaml.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace MultiTermTBXMapper.Menu
{
/// <summary>
/// Interaction logic for VariantHandler.xaml
/// </summary>
public partial class ConversionHandler : UserControl, ISwitchable
{
private Mapping fullMapping = new Mapping();
private MappingDict mappingDict;
private Dictionary<string, Dictionary<string, string>> tbxInfo = TBXDatabase.getDCInfo();
private Dictionary<string, List<string[]>> tbx_picklists = TBXDatabase.getPicklists();
private class InvalidLevelException : Exception
{
public InvalidLevelException() : base("Only levels 'concept', 'language', and 'term' are allowed as group values.") { }
}
#region ISwitchable members
public void UtilizeState<T>(T state)
{
ConverterApp mt2tbx = new ConverterApp();
var (outputFile, errorFile) = mt2tbx.deserializeFile(state as string, Singleton.Instance.getPath(), Singleton.Instance.getDialect(), false);
FinishedConversion(outputFile, errorFile);
}
public void UtilizeState<T>(ref T r)
{
throw new NotImplementedException();
}
public void UtilizeState<T1, T2>(ref T1 r, T2 state)
{
mappingDict = r as MappingDict;
map();
fullMapping.queueDrainOrders = state as QueueDrainOrders;
//Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
//string json = Newtonsoft.Json.JsonConvert.SerializeObject(fullMapping, settings);
//json = fixJSON(json);
string json = fullMapping.Serialize();
string mappingFile = Path.GetTempFileName();
int saveOption = Singleton.Instance.getSaveOption();
if (saveOption == 1) // Mapping and Conversion
{
File.WriteAllText(mappingFile, json);
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.DefaultExt = ".json";
dlg.Filter = "JSON Files (*.json)|*.json|All Files (*.*)|*.*";
bool? result = dlg.ShowDialog();
if (result == true)
{
if (File.Exists(dlg.FileName))
{
File.Delete(dlg.FileName);
}
//File.WriteAllText(dlg.FileName, tbxOutput);
//File.Move(tbxOutput.Replace("/", "\\"), dlg.FileName);
File.Move(mappingFile, dlg.FileName);
}
ConverterApp mt2tbx = new ConverterApp();
var (outputFile, errorFile) = mt2tbx.deserializeFile(json, Singleton.Instance.getPath(), Singleton.Instance.getDialect(), true);
FinishedConversion(outputFile, errorFile);
}
else if (saveOption == 2) // Will be handled in a different Dialogue
{
throw new Exception("Mapping not handled!!!");
}
else if (saveOption == 3) // Just Mapping File
{
File.WriteAllText(mappingFile, json);
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.DefaultExt = ".json";
dlg.Filter = "JSON Files (*.json)|*.json|All files (*.*)|*.*";
bool? result = dlg.ShowDialog();
if (result == true)
{
if (File.Exists(dlg.FileName))
{
File.Delete(dlg.FileName);
}
//File.WriteAllText(dlg.FileName, tbxOutput);
//File.Move(tbxOutput.Replace("/", "\\"), dlg.FileName);
File.Move(mappingFile, dlg.FileName);
}
}
}
#endregion
public ConversionHandler()
{
InitializeComponent();
}
private void FinishedConversion(string outputFile, string errorFile)
{
textblock_conversionStatus.Text = $"Your mapping file has been created and can be found:\n {outputFile}\n\nExceptions to the mapping can be found:\n {errorFile}";
}
private void map()
{
foreach (string dc in mappingDict.Keys)
{
if (isConceptGrp(dc))
{
handleGrp(dc, "concept");
}
if (isLanguageGrp(dc))
{
handleGrp(dc, "language");
}
if (isTermGrp(dc))
{
handleGrp(dc, "term");
}
}
}
private static string fixJSON(string json)
{
//json = Methods.rgxReplace("^\\{", "[", json);
//json = Methods.rgxReplace("\\}(?!.+?)", "]", json);
//json = Methods.rgxReplace("\\\"(dialect|xcs|catMap|empty)\\\":", "", json);
//json = Methods.rgxReplace("\\{\\\"teasp\\\":", "", json);
//json = Methods.rgxReplace("\\]\\}\\]\\]", "]]]", json);
return json;
}
/// <summary>
/// Handles a level.
/// </summary>
/// <param name="dc">User data category name</param>
/// <param name="group">concept|language|term</param>
private void handleGrp(string dc, string group)
{
if (group != "concept" && group != "language" && group != "term")
{
throw (new InvalidLevelException());
}
TemplateSet ts;
//Handle data category with split contents and picklists
if (mappingDict.hasPicklist(dc) && mappingDict.hasSplitContents(dc))
{
ts = createTemplateSet(dc, true);
(List<ValueGroup> vgs, List<Teasp> teasps) = handleSplitContents(dc, true);
ts.addValueGroups(vgs);
ts.addSpecialTeasps(teasps);
}
//Handle single tbx datcat with picklists
else if (mappingDict.hasPicklist(dc) && !mappingDict.hasSplitContents(dc))
{
string tbx_dc = getTBXdc(dc);
ts = createTemplateSet(dc, tbx_dc);
}
//Handle data category with split contents (no picklists)
else if (!mappingDict.hasPicklist(dc) && mappingDict.hasSplitContents(dc))
{
ts = createTemplateSet(dc, true);
(List<ValueGroup> vgs, List<Teasp> teasps) = handleSplitContents(dc);
ts.addValueGroups(vgs);
ts.addSpecialTeasps(teasps);
}
//Handle all simple cases: no multiple tbx datcats, no picklists
else
{
ts = createTemplateSet(dc);
}
fullMapping.catMap[group].Add(dc, ts);
}
/// <summary>
/// Method for creating of list of value groups and their corresponding special teasps.
/// </summary>
/// <param name="dc">user data category</param>
/// <returns>List of Value Groups</returns>
private (List<ValueGroup>, List<Teasp>) handleSplitContents(string dc, bool hasPicklist = false)
{
TBXContentMap cm = mappingDict.getTBXContentMap(dc);
string[] tbx_keys = Methods.getKeyArray(tbx_picklists.Keys);
List<ValueGroup> vgs = new List<ValueGroup>();
List<Teasp> teasps = new List<Teasp>();
foreach (string tbx_dc in mappingDict.getTBXMappingList(dc))
{
if (Methods.inArray(ref tbx_keys, tbx_dc))
{
Teasp teasp = new Teasp();
(string target, string eltAtt) = getEltTgtEltAtt(tbx_dc);
if (hasPicklist)
{
teasp.setAll(target, eltAtt, mappingDict.getPicklistMap(dc));
}
else
{
teasp.setAll(target, eltAtt);
}
ValueGroup vg = new ValueGroup();
foreach (string content in cm.Keys)
{
if (cm[content] == tbx_dc)
{
vg.Add(content);
}
}
vgs.Add(vg);
teasps.Add(teasp);
}
else
{
Teasp teasp = new Teasp();
(string target, string eltAtt) = getEltTgtEltAtt(tbx_dc);
teasp.setAll(target, eltAtt);
ValueGroup vg = new ValueGroup();
foreach(string content in cm.Keys)
{
if (cm[content] == tbx_dc)
{
vg.Add(content);
}
}
vgs.Add(vg);
teasps.Add(teasp);
}
}
return (vgs, teasps);
}
/// <summary>
/// Gets the mapped TBX data category for a specific content value if provided, or the first (and should be only, if there is no content mapping) TBX data category for the user data category.
/// </summary>
/// <param name="dc">Name of the User Data Category</param>
/// <param name="content_key">Optional Content of Data Category (default = null)</param>
/// <returns>TBX data category name</returns>
private string getTBXdc(string dc, string content_key = null)
{
string tbx_dc = null;
if (content_key == null)
{
tbx_dc = mappingDict.getTBXMappingList(dc)?[0];
}
else
{
tbx_dc = mappingDict.getTBXContentMap(dc)?[content_key];
}
return tbx_dc;
}
/// <summary>
/// Create a default Template Set, no Picklist values. Unhandled is used for both actual unhandled cases and split content mappings.
/// </summary>
/// <param name="user_dc">User data category name</param>
/// <param name="tbx_dc">TBX data category name</param>
/// <param name="unhandled">Is this an unhandled data category</param>
/// <returns></returns>
private TemplateSet createTemplateSet(string user_dc, bool unhandled = false)
{
(string target, string eltAtt, TemplateSet ts) = ("", "", new TemplateSet());
string tbx_dc = (unhandled) ? "unhandled" : getTBXdc(user_dc);
(target, eltAtt) = getEltTgtEltAtt(tbx_dc);
((ts[0] as KeyList)[0] as Teasp).setAll(target, eltAtt);
return ts;
}
/// <summary>
/// Create a Template Set with Picklist Values
/// </summary>
/// <param name="user_dc">User data category name</param>
/// <param name="tbx_dc">User data category name</param>
/// <returns>Template Set with Picklist values</returns>
private TemplateSet createTemplateSet(string user_dc, string tbx_dc)
{
(string target, string eltAtt) = getEltTgtEltAtt(tbx_dc);
TemplateSet ts = new TemplateSet();
Dictionary<string, string> sub = new Dictionary<string, string>();
TBXContentMap cm = mappingDict.getTBXContentMap(user_dc);
PicklistMap pm = mappingDict.getPicklistMap(user_dc);
foreach (string content in cm?.Keys)
{
if (cm[content] == tbx_dc)
{
sub.Add(content, pm[content]);
}
}
((ts[0] as KeyList)[0] as Teasp).setAll(target, eltAtt, sub);
return ts;
}
private (string, string) getEltTgtEltAtt(string tbx_dc)
{
string elt = "";
if (tbx_dc == "unhandled" || tbx_dc == "None: Do Not Map")
{
elt = "<unhandled>";
}
else
{
elt = tbxInfo[tbx_dc]?["element"];
}
string target = fullMapping.getTarget(elt);
string eltAtt = fullMapping.getEltAtt(elt, tbx_dc);
return (target, eltAtt);
}
private bool isConceptGrp(string dc)
{
List<string> grp = mappingDict.levelMap["conceptGrp"];
return (Methods.inList(ref grp, dc)) ? true : false;
}
private bool isLanguageGrp(string dc)
{
List<string> grp = mappingDict.levelMap["languageGrp"];
return (Methods.inList(ref grp, dc)) ? true : false;
}
private bool isTermGrp(string dc)
{
List<string> grp = mappingDict.levelMap["termGrp"];
return (Methods.inList(ref grp, dc)) ? true : false;
}
}
}