Browse Source

Changed import results to not worry about mappings, reducing duplicate code.

Kenric Nugteren 1 year ago
parent
commit
389cc2b70a

+ 0 - 1
prs.desktop/Panels/Staging/StagingPanel.xaml.cs

@@ -1136,7 +1136,6 @@ public class Module
             {
                 var importer = new Importer
                 {
-                    Description = "Default",
                     EntityName = entityName,
                     FileName = componentFileName
                 };

+ 21 - 15
prs.desktop/Utils/CustomImporter.cs

@@ -11,6 +11,16 @@ using System.Text;
 
 namespace PRSDesktop
 {
+    public class CreateTableArgs
+    {
+        public Stream Stream { get; set; }
+
+        public CreateTableArgs(Stream stream)
+        {
+            Stream = stream;
+        }
+    }
+
     public class LoadDataArgs
     {
         public Stream Stream { get; set; }
@@ -50,7 +60,7 @@ using PRSDesktop;
 public class Module
 {
     // Initialise a table with the necessary columns.
-    public CoreTable CreateTable()
+    public CoreTable CreateTable(CreateTableArgs args)
     {
         var table = new CoreTable();
         table.Columns.Add(new CoreColumn { ColumnName = ..., DataType = typeof(string) });
@@ -78,6 +88,7 @@ public class Module
         #region Script
 
         private ScriptDocument? _script;
+
         private ScriptDocument? Script
         {
             get
@@ -173,7 +184,12 @@ public class Module
         [MemberNotNullWhen(true, nameof(_table))]
         public override bool ReadHeader()
         {
-            _table = CreateTableMethod.Invoke(ScriptObject, Array.Empty<object?>()) as CoreTable;
+            if (_stream is null)
+            {
+                throw new Exception("Cannot read header before Open() is called.");
+            }
+            var args = new CreateTableArgs(_stream);
+            _table = CreateTableMethod.Invoke(ScriptObject, new object[] { args }) as CoreTable;
             if(_table is not null)
             {
                 Fields = _table.Columns.Select(x => x.ColumnName).ToArray();
@@ -211,19 +227,9 @@ public class Module
         {
             EnsureData();
             var row = _table.Rows[rowIdx];
-            return Mappings.ToDictionary(
-                x => x.Property,
-                x =>
-                {
-                    if (!string.IsNullOrWhiteSpace(x.Field))
-                    {
-                        return row[x.Field]?.ToString() ?? "";
-                    }
-                    else
-                    {
-                        return x.Constant;
-                    }
-                });
+            return _table.Columns.ToDictionary(
+                x => x.ColumnName,
+                x => row[x.ColumnName]?.ToString() ?? "");
         }
     }
 }

+ 2 - 7
prs.desktop/Utils/ExcelImporter.cs

@@ -67,9 +67,8 @@ namespace PRSDesktop
         {
             var results = new Dictionary<string, string>();
             var row = _row.Current as IRow;
-            foreach (var mapping in Mappings)
+            foreach (var field in Fields)
             {
-                var field = mapping.Field;
                 if (!string.IsNullOrWhiteSpace(field) && _columns.ContainsKey(field))
                 {
                     var cell = row.GetCell(_columns[field]);
@@ -99,11 +98,7 @@ namespace PRSDesktop
                     }
 
 
-                    results[mapping.Property] = value;
-                }
-                else
-                {
-                    results[mapping.Property] = mapping.Constant;
+                    results[field] = value;
                 }
             }