|
@@ -4,6 +4,7 @@ using System.ComponentModel;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using InABox.Clients;
|
|
|
+using Newtonsoft.Json.Linq;
|
|
|
|
|
|
namespace InABox.Core
|
|
|
{
|
|
@@ -145,7 +146,16 @@ namespace InABox.Core
|
|
|
|
|
|
var item = new T();
|
|
|
Notify(string.Format("Parsing Row {0}", iRow));
|
|
|
- var values = ReadLine();
|
|
|
+ Dictionary<string, string> values;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ values = ReadLine();
|
|
|
+ }
|
|
|
+ catch(Exception e)
|
|
|
+ {
|
|
|
+ WriteError(iRow, $"Error reading line: {e.Message}");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
var bUpdatesOK = BeforeProcess != null ? BeforeProcess.Invoke(this, values) : true;
|
|
|
try
|
|
@@ -194,130 +204,131 @@ namespace InABox.Core
|
|
|
bUpdatesOK = false;
|
|
|
WriteError(iRow, string.Format("Unable to Update Values: {0}", e.Message));
|
|
|
}
|
|
|
-
|
|
|
- if (bUpdatesOK)
|
|
|
+ if (!bUpdatesOK)
|
|
|
{
|
|
|
- var bLookupsOK = true;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- if (keylookup != null)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- var keyrows = keylookup.Results.Rows.ToList();
|
|
|
- foreach (var mapping in Mappings.Where(x => x.Key))
|
|
|
- {
|
|
|
- var keyvalue = CoreUtils.GetPropertyValue(item, mapping.Property);
|
|
|
- keyrows = keyrows.Where(r => string.Equals(r.Get<string>(mapping.Property)?.Trim(), keyvalue?.ToString().Trim()))
|
|
|
- .ToList();
|
|
|
- }
|
|
|
+ var bLookupsOK = true;
|
|
|
|
|
|
- var keyid = keyrows.Any() ? keyrows.First().Get<Guid>("ID") : Guid.Empty;
|
|
|
- CoreUtils.SetPropertyValue(item, "ID", keyid);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
+ if (keylookup != null)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var keyrows = keylookup.Results.Rows.ToList();
|
|
|
+ foreach (var mapping in Mappings.Where(x => x.Key))
|
|
|
{
|
|
|
- bLookupsOK = false;
|
|
|
- WriteError(iRow, string.Format("Unable to set Primary Key: {0}", e.Message));
|
|
|
+ var keyvalue = CoreUtils.GetPropertyValue(item, mapping.Property);
|
|
|
+ keyrows = keyrows.Where(r => string.Equals(r.Get<string>(mapping.Property)?.Trim(), keyvalue?.ToString().Trim()))
|
|
|
+ .ToList();
|
|
|
}
|
|
|
+
|
|
|
+ var keyid = keyrows.Any() ? keyrows.First().Get<Guid>("ID") : Guid.Empty;
|
|
|
+ CoreUtils.SetPropertyValue(item, "ID", keyid);
|
|
|
}
|
|
|
- else
|
|
|
+ catch (Exception e)
|
|
|
{
|
|
|
- CoreUtils.SetPropertyValue(item, "ID", Guid.Empty);
|
|
|
+ bLookupsOK = false;
|
|
|
+ WriteError(iRow, string.Format("Unable to set Primary Key: {0}", e.Message));
|
|
|
}
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ CoreUtils.SetPropertyValue(item, "ID", Guid.Empty);
|
|
|
+ }
|
|
|
|
|
|
- try
|
|
|
+ try
|
|
|
+ {
|
|
|
+ foreach (var mapping in Mappings.Where(x => x.Lookup != ImportLookupType.None))
|
|
|
{
|
|
|
- foreach (var mapping in Mappings.Where(x => x.Lookup != ImportLookupType.None))
|
|
|
+ var parentprop = string.Join(".", mapping.Property.Split('.').Reverse().Skip(1).Reverse());
|
|
|
+ var parent = CoreUtils.GetProperty(typeof(T), parentprop);
|
|
|
+ var childprop = mapping.Property.Split('.').Last();
|
|
|
+ var bt = parent.PropertyType.BaseType;
|
|
|
+ if (bt != null)
|
|
|
{
|
|
|
- var parentprop = string.Join(".", mapping.Property.Split('.').Reverse().Skip(1).Reverse());
|
|
|
- var parent = CoreUtils.GetProperty(typeof(T), parentprop);
|
|
|
- var childprop = mapping.Property.Split('.').Last();
|
|
|
- var bt = parent.PropertyType.BaseType;
|
|
|
- if (bt != null)
|
|
|
- {
|
|
|
- var lookuptype = bt.GetGenericArguments().FirstOrDefault();
|
|
|
+ var lookuptype = bt.GetGenericArguments().FirstOrDefault();
|
|
|
|
|
|
- var lookup = lookups.FirstOrDefault(x => x.Type.Equals(lookuptype));
|
|
|
+ var lookup = lookups.FirstOrDefault(x => x.Type.Equals(lookuptype));
|
|
|
|
|
|
- IEnumerable<CoreRow> lookuprows = lookup.Results.Rows;
|
|
|
- var lookupvalue = CoreUtils.GetPropertyValue(item, mapping.Property) as string;
|
|
|
- if (!string.IsNullOrWhiteSpace(lookupvalue))
|
|
|
+ IEnumerable<CoreRow> lookuprows = lookup.Results.Rows;
|
|
|
+ var lookupvalue = CoreUtils.GetPropertyValue(item, mapping.Property) as string;
|
|
|
+ if (!string.IsNullOrWhiteSpace(lookupvalue))
|
|
|
+ {
|
|
|
+ lookuprows = lookuprows.Where(r => r.Get<string>(childprop).Equals(lookupvalue));
|
|
|
+ var lookupid = lookuprows.Any() ? lookuprows.First().Get<Guid>("ID") : Guid.Empty;
|
|
|
+ if (lookupid == Guid.Empty)
|
|
|
{
|
|
|
- lookuprows = lookuprows.Where(r => r.Get<string>(childprop).Equals(lookupvalue));
|
|
|
- var lookupid = lookuprows.Any() ? lookuprows.First().Get<Guid>("ID") : Guid.Empty;
|
|
|
- if (lookupid == Guid.Empty)
|
|
|
+ if (mapping.Lookup == ImportLookupType.Restrict)
|
|
|
{
|
|
|
- if (mapping.Lookup == ImportLookupType.Restrict)
|
|
|
- {
|
|
|
- bLookupsOK = false;
|
|
|
- WriteError(iRow, string.Format("Lookup Value [{0}] not found", lookupvalue));
|
|
|
- }
|
|
|
- else if (mapping.Lookup == ImportLookupType.Create)
|
|
|
- {
|
|
|
- var newlookup = Activator.CreateInstance(lookuptype);
|
|
|
- CoreUtils.SetPropertyValue(newlookup, childprop, lookupvalue);
|
|
|
- ClientFactory.CreateClient(lookuptype).Save(newlookup, "Created by Import");
|
|
|
- lookupid = (Guid?)CoreUtils.GetPropertyValue(newlookup, "ID") ?? Guid.Empty;
|
|
|
- var newrow = lookup.Results.NewRow();
|
|
|
- lookup.Results.LoadRow(newrow, newlookup);
|
|
|
- lookup.Results.Rows.Add(newrow);
|
|
|
- CoreUtils.SetPropertyValue(item, lookup.ID, lookupid);
|
|
|
- var prefix = String.Join(".", lookup.ID.Split('.').Reverse().Skip(1).Reverse());
|
|
|
- foreach (var field in lookup.Fields)
|
|
|
- CoreUtils.SetPropertyValue(item, String.Join(".", new String[] { prefix, field }), newrow[field]);
|
|
|
-
|
|
|
- }
|
|
|
+ bLookupsOK = false;
|
|
|
+ WriteError(iRow, string.Format("Lookup Value [{0}] not found", lookupvalue));
|
|
|
}
|
|
|
- else
|
|
|
+ else if (mapping.Lookup == ImportLookupType.Create)
|
|
|
{
|
|
|
+ var newlookup = Activator.CreateInstance(lookuptype);
|
|
|
+ CoreUtils.SetPropertyValue(newlookup, childprop, lookupvalue);
|
|
|
+ ClientFactory.CreateClient(lookuptype).Save(newlookup, "Created by Import");
|
|
|
+ lookupid = (Guid?)CoreUtils.GetPropertyValue(newlookup, "ID") ?? Guid.Empty;
|
|
|
+ var newrow = lookup.Results.NewRow();
|
|
|
+ lookup.Results.LoadRow(newrow, newlookup);
|
|
|
+ lookup.Results.Rows.Add(newrow);
|
|
|
CoreUtils.SetPropertyValue(item, lookup.ID, lookupid);
|
|
|
var prefix = String.Join(".", lookup.ID.Split('.').Reverse().Skip(1).Reverse());
|
|
|
foreach (var field in lookup.Fields)
|
|
|
- CoreUtils.SetPropertyValue(item, String.Join(".", new String[] { prefix, field }), lookuprows.First()[field]);
|
|
|
+ CoreUtils.SetPropertyValue(item, String.Join(".", new String[] { prefix, field }), newrow[field]);
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ CoreUtils.SetPropertyValue(item, lookup.ID, lookupid);
|
|
|
+ var prefix = String.Join(".", lookup.ID.Split('.').Reverse().Skip(1).Reverse());
|
|
|
+ foreach (var field in lookup.Fields)
|
|
|
+ CoreUtils.SetPropertyValue(item, String.Join(".", new String[] { prefix, field }), lookuprows.First()[field]);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- bLookupsOK = false;
|
|
|
- WriteError(iRow, string.Format("Exception setting lookup values: {0}", e.Message));
|
|
|
- }
|
|
|
-
|
|
|
- if (bLookupsOK)
|
|
|
- {
|
|
|
- var bOK = AfterProcess != null ? AfterProcess.Invoke(this, item, values) : true;
|
|
|
-
|
|
|
- if (bOK && item.IsChanged())
|
|
|
- try
|
|
|
- {
|
|
|
- var bNewKey = keylookup != null && item.ID == Guid.Empty;
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ bLookupsOK = false;
|
|
|
+ WriteError(iRow, string.Format("Exception setting lookup values: {0}", e.Message));
|
|
|
+ }
|
|
|
|
|
|
- if (OnSave != null)
|
|
|
- OnSave?.Invoke(this, item);
|
|
|
- else
|
|
|
- new Client<T>().Save(item, "");
|
|
|
+ if (bLookupsOK)
|
|
|
+ {
|
|
|
+ var bOK = AfterProcess != null ? AfterProcess.Invoke(this, item, values) : true;
|
|
|
|
|
|
- if (bNewKey)
|
|
|
- {
|
|
|
- var row = keylookup!.Results.NewRow();
|
|
|
- keylookup.Results.LoadRow(row, item);
|
|
|
- keylookup.Results.Rows.Add(row);
|
|
|
- }
|
|
|
+ if (bOK && item.IsChanged())
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var bNewKey = keylookup != null && item.ID == Guid.Empty;
|
|
|
|
|
|
- var key = new List<object?>();
|
|
|
- foreach (var mapping in Mappings.Where(x => x.Key))
|
|
|
- key.Add(CoreUtils.GetPropertyValue(item, mapping.Property));
|
|
|
- WriteLog(iRow, string.Format("Successfully Imported [{0}]", string.Join(" + ", key)));
|
|
|
+ if (OnSave != null)
|
|
|
+ OnSave?.Invoke(this, item);
|
|
|
+ else
|
|
|
+ new Client<T>().Save(item, "");
|
|
|
|
|
|
- iResult++;
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
+ if (bNewKey)
|
|
|
{
|
|
|
- WriteError(iRow, string.Format("Unable to Save Item: {0}", e.Message));
|
|
|
+ var row = keylookup!.Results.NewRow();
|
|
|
+ keylookup.Results.LoadRow(row, item);
|
|
|
+ keylookup.Results.Rows.Add(row);
|
|
|
}
|
|
|
- }
|
|
|
+
|
|
|
+ var key = new List<object?>();
|
|
|
+ foreach (var mapping in Mappings.Where(x => x.Key))
|
|
|
+ key.Add(CoreUtils.GetPropertyValue(item, mapping.Property));
|
|
|
+ WriteLog(iRow, string.Format("Successfully Imported [{0}]", string.Join(" + ", key)));
|
|
|
+
|
|
|
+ iResult++;
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ WriteError(iRow, string.Format("Unable to Save Item: {0}", e.Message));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|