ImportFactory.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace InABox.Core
  5. {
  6. public enum ImportMappingType
  7. {
  8. Import,
  9. Export
  10. }
  11. public static class ImportFactory
  12. {
  13. private static readonly List<ImportDefinition> _definitions = new List<ImportDefinition>();
  14. public static ImportDefinition[] Definitions => _definitions.ToArray();
  15. public static IEnumerable<ImportMapping> ExtractMappings(Type type, ImportMappingType Import)
  16. {
  17. var results = new List<ImportMapping>();
  18. var props = DatabaseSchema.Properties(type).OrderBy(x=>x.Sequence);
  19. //var keys = CoreUtils.PropertyList(type, x => true, true).Keys.ToArray();
  20. foreach (var prop in props)
  21. {
  22. if (prop.Editor != null && prop.Editor is NullEditor == false && (!prop.IsCalculated || Import == ImportMappingType.Export))
  23. {
  24. var bOK = true;
  25. var bKey = prop.Editor is UniqueCodeEditor;
  26. var lookup = ImportLookupType.None;
  27. if (Import == ImportMappingType.Import)
  28. {
  29. var comps = prop.Name.Split('.');
  30. if (comps.Length > 1)
  31. {
  32. if ((comps.Length == 2) && (prop.Editor is CodeEditor))
  33. lookup = ImportLookupType.Restrict;
  34. else
  35. bOK = false;
  36. }
  37. }
  38. if (bOK)
  39. results.Add(new ImportMapping { Property = prop.Name, Field = "", Constant = "", Lookup = lookup, Key = bKey });
  40. }
  41. }
  42. return results;
  43. }
  44. private static bool IsNestedEntityLink(Type type, string property)
  45. {
  46. if (String.IsNullOrWhiteSpace(property))
  47. return false;
  48. var parent = string.Join(".", property.Split('.').Reverse().Skip(1).Reverse());
  49. if (!string.IsNullOrEmpty(parent))
  50. {
  51. var parentprop = CoreUtils.GetProperty(type, parent);
  52. if (parentprop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
  53. return true;
  54. return IsNestedEntityLink(type,parent);
  55. }
  56. return false;
  57. }
  58. public static void Register(Type type, string description, string filter)
  59. {
  60. if (!_definitions.Any(x => x.Type.Equals(type)))
  61. _definitions.Add(new ImportDefinition(type, description, filter));
  62. }
  63. public static IImporter Create(ImportDefinition definition, Type type)
  64. {
  65. var importer = definition.Type.MakeGenericType(type);
  66. var result = Activator.CreateInstance(importer) as IImporter;
  67. return result;
  68. }
  69. //public static IImporter<T> Create<T>(ImportDefinition definition) where T : new()
  70. //{
  71. // var importer = definition.Type.MakeGenericType(typeof(T));
  72. // var result = Activator.CreateInstance(importer) as IImporter<T>;
  73. // return result;
  74. //}
  75. }
  76. }