|
@@ -69,6 +69,88 @@ namespace InABox.Core
|
|
|
public event ImportLogEvent OnLog;
|
|
|
public event ImportLogEvent OnError;
|
|
|
|
|
|
+ private void ImportValues(T item, ImportMapping mapping, string value)
|
|
|
+ {
|
|
|
+ var p2 = DatabaseSchema.Property(typeof(T), mapping.Property);
|
|
|
+ //var prop = CoreUtils.GetProperty(typeof(T), property);
|
|
|
+ var type = p2.PropertyType;
|
|
|
+
|
|
|
+ item.OriginalValues[mapping.Property] = CoreUtils.GetPropertyValue(item, mapping.Property);
|
|
|
+
|
|
|
+ if (type == typeof(string))
|
|
|
+ {
|
|
|
+ if (p2.Editor is UniqueCodeEditor || p2.Editor is CodeEditor)
|
|
|
+ CoreUtils.SetPropertyValue(item, mapping.Property, value?.ToUpper());
|
|
|
+ else
|
|
|
+ CoreUtils.SetPropertyValue(item, mapping.Property, value);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(value))
|
|
|
+ {
|
|
|
+ var def = type.GetDefault();
|
|
|
+ CoreUtils.SetPropertyValue(item, mapping.Property, def);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var converter = TypeDescriptor.GetConverter(type);
|
|
|
+ var converted = converter.ConvertFrom(value);
|
|
|
+ CoreUtils.SetPropertyValue(item, mapping.Property, converted);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void DoLookup(T item, ImportMapping mapping, List<ImportLookup> lookups)
|
|
|
+ {
|
|
|
+ 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 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))
|
|
|
+ {
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+ throw new Exception(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]);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public int Import()
|
|
|
{
|
|
|
_log.Clear();
|
|
@@ -176,60 +258,64 @@ namespace InABox.Core
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- var bUpdatesOK = BeforeProcess != null ? BeforeProcess.Invoke(this, values) : true;
|
|
|
- try
|
|
|
+ var ok = BeforeProcess == null || BeforeProcess.Invoke(this, values);
|
|
|
+ if (!ok)
|
|
|
{
|
|
|
- foreach (var property in values.Keys)
|
|
|
- {
|
|
|
- var value = values[property];
|
|
|
- var p2 = DatabaseSchema.Property(typeof(T), property);
|
|
|
- //var prop = CoreUtils.GetProperty(typeof(T), property);
|
|
|
- var type = p2.PropertyType;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- item.OriginalValues[property] = CoreUtils.GetPropertyValue(item, property);
|
|
|
+ // First import lookups
|
|
|
+ foreach(var mapping in Mappings.Where(x => x.Lookup != ImportLookupType.None))
|
|
|
+ {
|
|
|
+ var value = values.GetValueOrDefault(mapping.Property) ?? "";
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ImportValues(item, mapping, value);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ WriteError(iRow, string.Format("Unable to Set Value: {0} [{1}] {2}", mapping.Property, value, e.Message));
|
|
|
+ ok = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
- if (type == typeof(string))
|
|
|
- {
|
|
|
- if (p2.Editor is UniqueCodeEditor || p2.Editor is CodeEditor)
|
|
|
- CoreUtils.SetPropertyValue(item, property, value?.ToUpper());
|
|
|
- else
|
|
|
- CoreUtils.SetPropertyValue(item, property, value);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (string.IsNullOrWhiteSpace(value))
|
|
|
- {
|
|
|
- var def = type.GetDefault();
|
|
|
- CoreUtils.SetPropertyValue(item, property, def);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- var converter = TypeDescriptor.GetConverter(type);
|
|
|
- try
|
|
|
- {
|
|
|
- var converted = converter.ConvertFrom(value);
|
|
|
- CoreUtils.SetPropertyValue(item, property, converted);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- WriteError(iRow, string.Format("Unable to Set Value: {0} [{1}] {2}", property, value, e.Message));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ DoLookup(item, mapping, lookups);
|
|
|
}
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ ok = false;
|
|
|
+ WriteError(iRow, string.Format("Exception setting lookup values: {0}", e.Message));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!ok)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
}
|
|
|
- catch (Exception e)
|
|
|
+
|
|
|
+ // Then do non-lookups
|
|
|
+ foreach (var mapping in Mappings.Where(x => x.Lookup == ImportLookupType.None))
|
|
|
{
|
|
|
- bUpdatesOK = false;
|
|
|
- WriteError(iRow, string.Format("Unable to Update Values: {0}", e.Message));
|
|
|
+ var value = values.GetValueOrDefault(mapping.Property) ?? "";
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ImportValues(item, mapping, value);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ WriteError(iRow, string.Format("Unable to Set Value: {0} [{1}] {2}", mapping.Property, value, e.Message));
|
|
|
+ ok = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
- if (!bUpdatesOK)
|
|
|
+ if (!ok)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- var bLookupsOK = true;
|
|
|
-
|
|
|
+ // Do Primary key lookup
|
|
|
if (keylookup != null)
|
|
|
{
|
|
|
try
|
|
@@ -247,7 +333,7 @@ namespace InABox.Core
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
- bLookupsOK = false;
|
|
|
+ ok = false;
|
|
|
WriteError(iRow, string.Format("Unable to set Primary Key: {0}", e.Message));
|
|
|
}
|
|
|
}
|
|
@@ -255,99 +341,42 @@ namespace InABox.Core
|
|
|
{
|
|
|
CoreUtils.SetPropertyValue(item, "ID", Guid.Empty);
|
|
|
}
|
|
|
-
|
|
|
- try
|
|
|
+ if (!ok)
|
|
|
{
|
|
|
- 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 lookuptype = bt.GetGenericArguments().FirstOrDefault();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- var lookup = lookups.FirstOrDefault(x => x.Type.Equals(lookuptype));
|
|
|
+ var bOK = AfterProcess == null || AfterProcess.Invoke(this, item, values);
|
|
|
|
|
|
- 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)
|
|
|
- {
|
|
|
- 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]);
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- 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)
|
|
|
+ if (bOK && item.IsChanged())
|
|
|
{
|
|
|
- bLookupsOK = false;
|
|
|
- WriteError(iRow, string.Format("Exception setting lookup values: {0}", e.Message));
|
|
|
- }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var bNewKey = keylookup != null && item.ID == Guid.Empty;
|
|
|
|
|
|
- if (bLookupsOK)
|
|
|
- {
|
|
|
- var bOK = AfterProcess != null ? AfterProcess.Invoke(this, item, values) : true;
|
|
|
+ if (OnSave != null)
|
|
|
+ OnSave?.Invoke(this, item);
|
|
|
+ else
|
|
|
+ new Client<T>().Save(item, "");
|
|
|
|
|
|
- if (bOK && item.IsChanged())
|
|
|
- try
|
|
|
+ if (bNewKey)
|
|
|
{
|
|
|
- var bNewKey = keylookup != null && item.ID == Guid.Empty;
|
|
|
-
|
|
|
- if (OnSave != null)
|
|
|
- OnSave?.Invoke(this, item);
|
|
|
- else
|
|
|
- new Client<T>().Save(item, "");
|
|
|
-
|
|
|
- if (bNewKey)
|
|
|
- {
|
|
|
- var row = keylookup!.Results.NewRow();
|
|
|
- keylookup.Results.LoadRow(row, item);
|
|
|
- keylookup.Results.Rows.Add(row);
|
|
|
- }
|
|
|
+ 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)));
|
|
|
+ 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));
|
|
|
- }
|
|
|
+ iResult++;
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ WriteError(iRow, string.Format("Unable to Save Item: {0}", e.Message));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|