|
@@ -1,13 +1,16 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Diagnostics;
|
|
|
+using System.Diagnostics.CodeAnalysis;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Windows;
|
|
|
+using System.Windows.Forms.Design;
|
|
|
using System.Windows.Media.Imaging;
|
|
|
using InABox.Core;
|
|
|
using InABox.Scripting;
|
|
|
using InABox.WPF;
|
|
|
+using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
using Microsoft.Win32;
|
|
|
|
|
|
namespace InABox.DynamicGrid
|
|
@@ -25,10 +28,29 @@ namespace InABox.DynamicGrid
|
|
|
{
|
|
|
private static readonly BitmapImage run = Wpf.Resources.rightarrow.AsBitmapImage();
|
|
|
|
|
|
+ private DynamicImageColumn ImportColumn;
|
|
|
+
|
|
|
+ private bool _canImport = true;
|
|
|
+ public bool CanImport
|
|
|
+ {
|
|
|
+ get => _canImport;
|
|
|
+ set
|
|
|
+ {
|
|
|
+ if(_canImport != value)
|
|
|
+ {
|
|
|
+ _canImport = value;
|
|
|
+ ImportColumn.Position = _canImport ? DynamicActionColumnPosition.End : DynamicActionColumnPosition.Hidden;
|
|
|
+ Refresh(true, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
protected override void Init()
|
|
|
{
|
|
|
base.Init();
|
|
|
- ActionColumns.Add(new DynamicImageColumn(ImportImage, ImportAction));
|
|
|
+ ImportColumn = new DynamicImageColumn(ImportImage, ImportAction);
|
|
|
+ ActionColumns.Add(ImportColumn);
|
|
|
+
|
|
|
HiddenColumns.Add(x => x.EntityName);
|
|
|
HiddenColumns.Add(x => x.ImporterDescription);
|
|
|
HiddenColumns.Add(x => x.FileName);
|
|
@@ -68,14 +90,14 @@ namespace InABox.DynamicGrid
|
|
|
return arg != null ? run : null;
|
|
|
}
|
|
|
|
|
|
- private int[] ExtractColumnWidths(string widths)
|
|
|
+ private static int[] ExtractColumnWidths(string widths)
|
|
|
{
|
|
|
if (string.IsNullOrWhiteSpace(widths))
|
|
|
return new[] { 1024 };
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- return widths.Split(',').Select(x => int.Parse(x)).ToArray();
|
|
|
+ return widths.Split(',').Select(int.Parse).ToArray();
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
@@ -83,135 +105,165 @@ namespace InABox.DynamicGrid
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private bool ImportAction(CoreRow? arg)
|
|
|
+ public static bool CreateImporter(Importer importer,
|
|
|
+ [NotNullWhen(true)]
|
|
|
+ ref string? filename,
|
|
|
+ [NotNullWhen(true)]
|
|
|
+ out IImporter iimporter,
|
|
|
+ Action<CustomiseImportArgs>? customiseImport = null,
|
|
|
+ Func<object, bool>? onImport = null)
|
|
|
{
|
|
|
- if (arg != null)
|
|
|
+ iimporter = null;
|
|
|
+ var definition =
|
|
|
+ ImportFactory.Definitions.FirstOrDefault(x => x.Description.Equals(importer.ImporterDescription));
|
|
|
+ var entityType = CoreUtils.GetEntity(importer.EntityName);
|
|
|
+ if (definition != null)
|
|
|
{
|
|
|
- var definition =
|
|
|
- ImportFactory.Definitions.FirstOrDefault(x => x.Description.Equals(arg.Get<Importer, string>(c => c.ImporterDescription)));
|
|
|
- if (definition != null)
|
|
|
+ ScriptDocument? helper = null;
|
|
|
+ try
|
|
|
{
|
|
|
- ScriptDocument? helper = null;
|
|
|
- try
|
|
|
+ if (!importer.Script.IsNullOrWhiteSpace())
|
|
|
{
|
|
|
- var script = arg.Get<Importer, string>(x => x.Script);
|
|
|
- if (!string.IsNullOrWhiteSpace(script))
|
|
|
+ helper = new ScriptDocument(importer.Script);
|
|
|
+ if (!helper.Compile())
|
|
|
{
|
|
|
- helper = new ScriptDocument(script);
|
|
|
- if (!helper.Compile())
|
|
|
- {
|
|
|
- MessageBox.Show("Unable to Compile Import Helper Script!");
|
|
|
- return false;
|
|
|
- }
|
|
|
+ MessageBox.Show("Unable to Compile Import Helper Script!");
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
|
|
|
- }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
|
|
|
+ }
|
|
|
|
|
|
- var fullfilename = arg.Get<Importer, string>(x => x.FileName);
|
|
|
- var path = Path.GetDirectoryName(fullfilename);
|
|
|
- var extension = Path.GetExtension(fullfilename);
|
|
|
- var args = new CustomiseImportArgs { FileName = Path.GetFileNameWithoutExtension(fullfilename) };
|
|
|
- OnCustomiseImport?.Invoke(this, args);
|
|
|
- var filename = CoreUtils.SanitiseFileName(args.FileName);
|
|
|
- var dlg = new OpenFileDialog();
|
|
|
- dlg.Filter = definition.Filter;
|
|
|
- dlg.FileName = Path.Combine(path, filename) + (extension.StartsWith(".") ? "" : ".") + extension;
|
|
|
+ if(filename is null)
|
|
|
+ {
|
|
|
+ var fullFileName = importer.FileName;
|
|
|
+ var path = Path.GetDirectoryName(fullFileName);
|
|
|
+ var extension = Path.GetExtension(fullFileName);
|
|
|
+ var args = new CustomiseImportArgs { FileName = Path.GetFileNameWithoutExtension(fullFileName) };
|
|
|
+ customiseImport?.Invoke(args);
|
|
|
+
|
|
|
+ var filename2 = CoreUtils.SanitiseFileName(args.FileName);
|
|
|
+ var dlg = new OpenFileDialog
|
|
|
+ {
|
|
|
+ Filter = definition.Filter,
|
|
|
+ FileName = Path.Combine(path, filename2) + (extension.StartsWith(".") ? "" : ".") + extension
|
|
|
+ };
|
|
|
if (!string.IsNullOrWhiteSpace(dlg.FileName) && Directory.Exists(Path.GetDirectoryName(dlg.FileName)))
|
|
|
dlg.InitialDirectory = Path.GetDirectoryName(dlg.FileName);
|
|
|
- if (dlg.ShowDialog() == true)
|
|
|
+ if(dlg.ShowDialog() == true)
|
|
|
{
|
|
|
- Progress.Show("Importing Data");
|
|
|
- var importer = ImportFactory.Create(definition, EntityType);
|
|
|
- if (importer is IFixedWidthImporter)
|
|
|
- ((IFixedWidthImporter)importer).ColumnWidths = ExtractColumnWidths(arg.Get<Importer, string>(x => x.ColumnWidths));
|
|
|
+ filename = dlg.FileName;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var path = Path.GetDirectoryName(filename);
|
|
|
+ var extension = Path.GetExtension(filename);
|
|
|
+ var args = new CustomiseImportArgs { FileName = Path.GetFileNameWithoutExtension(filename) };
|
|
|
+ customiseImport?.Invoke(args);
|
|
|
|
|
|
- try
|
|
|
- {
|
|
|
- importer.HasHeader = arg.Get<Importer, bool>(x => x.HasHeader);
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- importer.HasHeader = true;
|
|
|
- }
|
|
|
+ var filename2 = CoreUtils.SanitiseFileName(args.FileName);
|
|
|
+ filename = Path.ChangeExtension(Path.Combine(path, filename2), extension);
|
|
|
+ }
|
|
|
|
|
|
- try
|
|
|
- {
|
|
|
- importer.HeaderRow = Math.Max(1, arg.Get<Importer, int>(x => x.HeaderRows));
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- importer.HeaderRow = 1;
|
|
|
- }
|
|
|
+ iimporter = ImportFactory.Create(definition, entityType);
|
|
|
+ if (iimporter is IFixedWidthImporter fixedImporter)
|
|
|
+ fixedImporter.ColumnWidths = ExtractColumnWidths(importer.ColumnWidths);
|
|
|
|
|
|
- importer.BeforeProcess += (sender, values) =>
|
|
|
- {
|
|
|
- if (helper != null)
|
|
|
- return helper.Execute("Module", "BeforeProcess", new object[] { values });
|
|
|
- return true;
|
|
|
- };
|
|
|
+ iimporter.HasHeader = importer.HasHeader;
|
|
|
+ iimporter.HeaderRow = Math.Max(1, importer.HeaderRows);
|
|
|
|
|
|
- importer.AfterProcess += (sender, item, values) =>
|
|
|
- {
|
|
|
- var bOK = true;
|
|
|
- if (helper != null)
|
|
|
- bOK = helper.Execute("Module", "AfterProcess", new[] { item, values });
|
|
|
- bOK = !bOK || OnImportItem == null || OnImportItem.Invoke(item);
|
|
|
- return bOK;
|
|
|
- };
|
|
|
-
|
|
|
- importer.OnSave += (sender, entity) => OnSave?.Invoke(sender, entity);
|
|
|
- importer.OnLoad += (sender, type, fields, id) => OnLoad(sender, type, fields, id);
|
|
|
-
|
|
|
- importer.OnNotify += (o, m) => { Progress.SetMessage(m); };
|
|
|
-
|
|
|
- var settings = arg.Get<Importer, string>(c => c.Definition);
|
|
|
- var mappings = Serialization.Deserialize<List<ImportMapping>>(settings);
|
|
|
- importer.Mappings.AddRange(mappings);
|
|
|
- using (var stream = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read))
|
|
|
+ iimporter.BeforeProcess += (sender, values) =>
|
|
|
+ {
|
|
|
+ if (helper != null)
|
|
|
+ return helper.Execute("Module", "BeforeProcess", new object[] { values });
|
|
|
+ return true;
|
|
|
+ };
|
|
|
+
|
|
|
+ iimporter.AfterProcess += (sender, item, values) =>
|
|
|
+ {
|
|
|
+ var bOK = true;
|
|
|
+ if (helper != null)
|
|
|
+ bOK = helper.Execute("Module", "AfterProcess", new[] { item, values });
|
|
|
+ bOK = !bOK || onImport is null || onImport.Invoke(item);
|
|
|
+ return bOK;
|
|
|
+ };
|
|
|
+
|
|
|
+ var settings = importer.Definition;
|
|
|
+ var mappings = Serialization.Deserialize<List<ImportMapping>>(settings);
|
|
|
+ iimporter.Mappings.AddRange(mappings);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool ImportAction(CoreRow? arg)
|
|
|
+ {
|
|
|
+ if (arg != null)
|
|
|
+ {
|
|
|
+ var importer = arg.ToObject<Importer>();
|
|
|
+ string? filename = null;
|
|
|
+
|
|
|
+ if (CreateImporter(importer,
|
|
|
+ ref filename,
|
|
|
+ out var iimporter,
|
|
|
+ (args) => OnCustomiseImport?.Invoke(this, args),
|
|
|
+ (o) => OnImportItem?.Invoke(o) != false))
|
|
|
+ {
|
|
|
+ iimporter.OnLoad += OnLoad;
|
|
|
+ iimporter.OnSave += OnSave;
|
|
|
+
|
|
|
+ Progress.Show("Importing Data");
|
|
|
+
|
|
|
+ using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
|
|
|
+ Progress.SetMessage("Opening File");
|
|
|
+ if (iimporter.Open(stream))
|
|
|
+ {
|
|
|
+ if (iimporter.ReadHeader())
|
|
|
{
|
|
|
- Progress.SetMessage("Opening File");
|
|
|
- if (importer.Open(stream))
|
|
|
+ var mismatches = iimporter.Mappings.Where(x =>
|
|
|
+ !string.IsNullOrWhiteSpace(x.Field) &&
|
|
|
+ !iimporter.Fields.Contains(x.Field)
|
|
|
+ ).Select(x => x.Field).ToArray();
|
|
|
+ if (!mismatches.Any())
|
|
|
{
|
|
|
- if (importer.ReadHeader())
|
|
|
- {
|
|
|
- var mismatches = mappings.Where(x =>
|
|
|
- !string.IsNullOrWhiteSpace(x.Field) &&
|
|
|
- !importer.Fields.Contains(x.Field)
|
|
|
- ).Select(x => x.Field).ToArray();
|
|
|
- if (!mismatches.Any())
|
|
|
- {
|
|
|
- var imported = importer.Import();
|
|
|
- Progress.Close();
|
|
|
- MessageBox.Show(string.Format("Imported {0} records!", imported));
|
|
|
- var LogFile = Path.ChangeExtension(dlg.FileName, ".log");
|
|
|
- File.AppendAllLines(LogFile, importer.Log.ToArray());
|
|
|
- Process.Start(new ProcessStartInfo(LogFile) { UseShellExecute = true });
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Progress.Close();
|
|
|
- MessageBox.Show("Import Mappings do not match file headers!\n\n- " + string.Join("\n- ", mismatches),
|
|
|
- "Import Failed");
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Progress.Close();
|
|
|
- MessageBox.Show("Unable to Read Headers from {0}", Path.GetFileName(dlg.FileName));
|
|
|
- }
|
|
|
+ var imported = iimporter.Import();
|
|
|
+ Progress.Close();
|
|
|
+ MessageBox.Show(string.Format("Imported {0} records!", imported));
|
|
|
+ var LogFile = Path.ChangeExtension(filename, ".log");
|
|
|
+ File.AppendAllLines(LogFile, iimporter.Log.ToArray());
|
|
|
+ Process.Start(new ProcessStartInfo(LogFile) { UseShellExecute = true });
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Progress.Close();
|
|
|
- MessageBox.Show("Unable to Open {0}", Path.GetFileName(dlg.FileName));
|
|
|
+ MessageBox.Show("Import Mappings do not match file headers!\n\n- " + string.Join("\n- ", mismatches),
|
|
|
+ "Import Failed");
|
|
|
}
|
|
|
-
|
|
|
- importer.Close();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Progress.Close();
|
|
|
+ MessageBox.Show("Unable to Read Headers from {0}", Path.GetFileName(filename));
|
|
|
}
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Progress.Close();
|
|
|
+ MessageBox.Show("Unable to Open {0}", Path.GetFileName(filename));
|
|
|
+ }
|
|
|
+
|
|
|
+ iimporter.Close();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -243,7 +295,7 @@ namespace InABox.DynamicGrid
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public override bool EditItems(Importer[] items, Func<Type, CoreTable>? PageDataHandler = null, bool PreloadPages = false)
|
|
|
+ public override bool EditItems(Importer[] items, Func<Type, CoreTable?>? PageDataHandler = null, bool PreloadPages = false)
|
|
|
{
|
|
|
if (items.Length != 1)
|
|
|
{
|