DynamicImportForm.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Forms.Design;
  8. using InABox.Core;
  9. using InABox.Wpf;
  10. using Microsoft.Win32;
  11. namespace InABox.DynamicGrid
  12. {
  13. /// <summary>
  14. /// Interaction logic for DynamicImportForm.xaml
  15. /// </summary>
  16. public partial class DynamicImportForm : ThemableWindow
  17. {
  18. private Type _entitytype;
  19. private ImportDefinition _importdefinition;
  20. private readonly Importer _importer;
  21. private bool bLoading;
  22. public DynamicImportForm(Importer importer)
  23. {
  24. _importer = importer;
  25. InitializeComponent();
  26. LoadData();
  27. CheckOKButton();
  28. }
  29. private void LoadData()
  30. {
  31. bLoading = true;
  32. _entitytype = CoreUtils.GetEntity(_importer.EntityName);
  33. _importdefinition = ImportFactory.Definitions.FirstOrDefault(x => x.Description.Equals(_importer.ImporterDescription));
  34. var _mappings = string.IsNullOrWhiteSpace(_importer.Definition)
  35. ? new List<ImportMapping>()
  36. : Serialization.Deserialize<List<ImportMapping>>(_importer.Definition);
  37. Name.Text = _importer.Description;
  38. Type.ItemsSource = ImportFactory.Definitions;
  39. Type.SelectedValue = _importdefinition;
  40. HasHeader.IsChecked = _importer.HasHeader;
  41. HeaderRow.Value = Math.Max(1, _importer.HeaderRows);
  42. ColumnWidths.Text = _importer.ColumnWidths;
  43. FileName.Text = _importer.FileName;
  44. var mappings = ImportFactory.ExtractMappings(_entitytype, ImportMappingType.Import);
  45. Mappings.Items.AddRange(mappings);
  46. var key = mappings.FirstOrDefault(x => x.Key.Equals(true));
  47. if (key != null)
  48. {
  49. key.Key = false;
  50. Mappings.UniqueCode = key.Property;
  51. }
  52. else
  53. {
  54. Mappings.UniqueCode = "";
  55. }
  56. foreach (var custom in _mappings)
  57. {
  58. var mapping = Mappings.Items.FirstOrDefault(x => string.Equals(x.Property, custom.Property));
  59. if (mapping != null)
  60. {
  61. mapping.Key = custom.Key;
  62. mapping.Field = custom.Field;
  63. mapping.Constant = custom.Constant;
  64. mapping.Lookup = custom.Lookup;
  65. }
  66. }
  67. ImportFieldGenerator.Fields = Array.Empty<string>();
  68. if (File.Exists(FileName.Text) && Type.SelectedValue as ImportDefinition != null)
  69. OpenFile(FileName.Text);
  70. else
  71. Mappings.Refresh(true, true);
  72. bLoading = false;
  73. }
  74. private void OpenFile(string filename)
  75. {
  76. var definition = Type.SelectedValue as ImportDefinition;
  77. var importer = ImportFactory.Create(definition, _entitytype);
  78. if (importer is IFixedWidthImporter)
  79. ((IFixedWidthImporter)importer).ColumnWidths = ExtractColumnWidths();
  80. importer.HeaderRow = _importer.HeaderRows;
  81. try
  82. {
  83. using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
  84. {
  85. if (importer.Open(stream) && importer.ReadHeader())
  86. {
  87. FileName.Text = filename;
  88. ImportFieldGenerator.Fields = importer.Fields;
  89. Mappings.Refresh(true, true);
  90. }
  91. else
  92. {
  93. MessageBox.Show(string.Format("{0} is not a valid file!", filename));
  94. }
  95. }
  96. }
  97. catch (Exception e)
  98. {
  99. MessageBox.Show(string.Format("Unable to open {0}!\n\n{1}", filename, e.Message));
  100. }
  101. }
  102. private void Open_Click(object sender, RoutedEventArgs e)
  103. {
  104. var type = Type.SelectedValue as ImportDefinition;
  105. if (type == null)
  106. {
  107. MessageBox.Show(string.Format("Type is unexpected: {0}", type));
  108. return;
  109. }
  110. var dlg = new OpenFileDialog();
  111. dlg.Filter = type.Filter;
  112. var sFile = FileName.Text;
  113. if (!string.IsNullOrWhiteSpace(sFile))
  114. {
  115. dlg.FileName = sFile;
  116. var sFolder = string.IsNullOrWhiteSpace(sFile) ? Path.GetDirectoryName(sFile) : "";
  117. if (!string.IsNullOrWhiteSpace(sFolder) && Directory.Exists(sFolder))
  118. dlg.InitialDirectory = sFolder;
  119. }
  120. if (dlg.ShowDialog() == true)
  121. if (File.Exists(dlg.FileName) && Type.SelectedValue as ImportDefinition != null)
  122. OpenFile(dlg.FileName);
  123. CheckOKButton();
  124. }
  125. private void HeaderRow_Checked(object sender, RoutedEventArgs e)
  126. {
  127. HeaderRowLabel.Visibility = HasHeader.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
  128. HeaderRow.Visibility = HasHeader.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
  129. }
  130. private void HeaderRow_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  131. {
  132. if (!bLoading)
  133. _importer.HeaderRows = HeaderRow.Value.HasValue ? (int)HeaderRow.Value : 1;
  134. if (File.Exists(FileName.Text) && Type.SelectedValue as ImportDefinition != null)
  135. OpenFile(FileName.Text);
  136. CheckOKButton();
  137. }
  138. private void Type_SelectionChanged(object sender, SelectionChangedEventArgs e)
  139. {
  140. if (!bLoading)
  141. _importdefinition = e.AddedItems.Count > 0 ? e.AddedItems[0] as ImportDefinition : null;
  142. Open.IsEnabled = _importdefinition != null;
  143. ColumnWidthsLabel.Visibility = _importdefinition != null && _importdefinition.Type == typeof(FixedWidthImporter<>)
  144. ? Visibility.Visible
  145. : Visibility.Hidden;
  146. ColumnWidths.Visibility = _importdefinition != null && _importdefinition.Type == typeof(FixedWidthImporter<>)
  147. ? Visibility.Visible
  148. : Visibility.Hidden;
  149. if (File.Exists(FileName.Text) && Type.SelectedValue as ImportDefinition != null)
  150. OpenFile(FileName.Text);
  151. CheckOKButton();
  152. }
  153. private void HideBlank_Click(object sender, RoutedEventArgs e)
  154. {
  155. Mappings.HideBlank = !Mappings.HideBlank;
  156. HideBlank.Content = Mappings.HideBlank ? "Show All" : "Hide Blank";
  157. Mappings.Refresh(false, true);
  158. }
  159. private void Match_Click(object sender, RoutedEventArgs e)
  160. {
  161. Mappings.MatchFields();
  162. }
  163. private void Reset_Click(object sender, RoutedEventArgs e)
  164. {
  165. Mappings.Reset();
  166. }
  167. private void Cancel_Click(object sender, RoutedEventArgs e)
  168. {
  169. DialogResult = false;
  170. }
  171. private void OK_Click(object sender, RoutedEventArgs e)
  172. {
  173. var bOK = true;
  174. if (!Mappings.Items.Any(x => x.Key))
  175. bOK = MessageBox.Show(
  176. "There are no keys defined - this import will ALWAYS create new items\ninstead of updating existing items.\n\nAre you sure you wish to do this?",
  177. "No Keys Defined", MessageBoxButton.YesNo) == MessageBoxResult.Yes;
  178. if (bOK)
  179. {
  180. UnloadData();
  181. DialogResult = true;
  182. }
  183. }
  184. private void UnloadData()
  185. {
  186. _importer.EntityName = _entitytype.EntityName();
  187. _importer.ImporterDescription = _importdefinition != null ? _importdefinition.Description : "";
  188. _importer.Definition =
  189. Serialization.Serialize(Mappings.Items.Where(x => !string.IsNullOrWhiteSpace(x.Field) || !string.IsNullOrWhiteSpace(x.Constant)),
  190. true);
  191. _importer.Description = Name.Text;
  192. _importer.FileName = FileName.Text;
  193. }
  194. private void Name_TextChanged(object sender, TextChangedEventArgs e)
  195. {
  196. CheckOKButton();
  197. }
  198. private void CheckOKButton()
  199. {
  200. OK.IsEnabled = !string.IsNullOrWhiteSpace(Name.Text) && _importdefinition != null && !string.IsNullOrWhiteSpace(FileName.Text);
  201. }
  202. private int[] ExtractColumnWidths()
  203. {
  204. if (string.IsNullOrWhiteSpace(ColumnWidths.Text))
  205. return new[] { 1024 };
  206. try
  207. {
  208. return ColumnWidths.Text.Split(',').Select(x => int.Parse(x)).ToArray();
  209. }
  210. catch
  211. {
  212. return new[] { 1024 };
  213. }
  214. }
  215. private void ColumnWidths_TextChanged(object sender, TextChangedEventArgs e)
  216. {
  217. if (!bLoading)
  218. _importer.ColumnWidths = ColumnWidths.Text;
  219. if (File.Exists(FileName.Text) && Type.SelectedValue as ImportDefinition != null)
  220. OpenFile(FileName.Text);
  221. }
  222. private void EditScript_Click(object sender, RoutedEventArgs e)
  223. {
  224. if (string.IsNullOrWhiteSpace(_importer.Script))
  225. _importer.Script =
  226. "using System;\r\n" +
  227. "using System.Collections.Generic;\r\n" +
  228. "using System.Linq;\r\n" +
  229. "using System.Runtime;\r\n" +
  230. "using System.Windows;\r\n" +
  231. "using System.Windows.Media;\r\n" +
  232. "using InABox.Core;\r\n" +
  233. "using Comal.Classes;\r\n" +
  234. "\r\n" +
  235. "public class Module\r\n" +
  236. "{\r\n" +
  237. "\r\n" +
  238. " public bool BeforeProcess(Dictionary<String,String> values)\r\n" +
  239. " {\r\n" +
  240. " return true;\r\n" +
  241. " }\r\n" +
  242. "\r\n" +
  243. " public void AfterProcess(" + _entitytype.Name + " item, Dictionary<String,String> values)\r\n" +
  244. " {\r\n" +
  245. " return true;\r\n" +
  246. " }\r\n" +
  247. "\r\n" +
  248. "}";
  249. var editor = new ScriptEditorWindow(_importer.Script);
  250. if (editor.ShowDialog() == true) _importer.Script = editor.Script;
  251. }
  252. private void Save_Click(object sender, RoutedEventArgs e)
  253. {
  254. var dlg = new SaveFileDialog();
  255. dlg.Filter = string.Format("{0} Import Files (*.{1}import)|*.{1}import", _entitytype.Name, _entitytype.Name.ToLower());
  256. dlg.FileName = string.Format("{0}.{1}import", Name.Text, _entitytype.Name.ToLower());
  257. if (dlg.ShowDialog() == true)
  258. try
  259. {
  260. UnloadData();
  261. var json = Serialization.Serialize(_importer, true);
  262. File.WriteAllText(dlg.FileName, json);
  263. }
  264. catch (Exception e2)
  265. {
  266. MessageBox.Show("Error saving import definition!\n\n" + e2.Message);
  267. }
  268. }
  269. private void Load_Click(object sender, RoutedEventArgs e)
  270. {
  271. var dlg = new OpenFileDialog();
  272. dlg.Filter = string.Format("{0} Import Files (*.{1}import)|*.{1}import", _entitytype.Name, _entitytype.Name.ToLower());
  273. if (dlg.ShowDialog() == true)
  274. {
  275. Mappings.Items.Clear();
  276. var json = File.ReadAllText(dlg.FileName);
  277. var id = _importer.ID;
  278. var entityid = _importer.EntityID;
  279. Serialization.DeserializeInto(json, _importer);
  280. _importer.ID = id;
  281. _importer.EntityID = entityid;
  282. LoadData();
  283. }
  284. CheckOKButton();
  285. }
  286. }
  287. }