DynamicImportForm.xaml.cs 12 KB

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