ImportFactory.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 keys = CoreUtils.PropertyList(type, x => true, true).Keys.ToArray();
  19. foreach (var key in keys)
  20. {
  21. var prop = CoreUtils.GetProperty(type, key);
  22. var editor = prop.GetEditor();
  23. if (editor != null && editor is NullEditor == false && (!prop.IsCalculated() || Import == ImportMappingType.Export))
  24. {
  25. var bOK = true;
  26. var bKey = editor is UniqueCodeEditor;
  27. var lookup = ImportLookupType.None;
  28. if (Import == ImportMappingType.Import)
  29. {
  30. var parentkey = string.Join(".", key.Split('.').Reverse().Skip(1).Reverse());
  31. var childkey = key.Split('.').Last();
  32. if (!string.IsNullOrEmpty(parentkey))
  33. {
  34. var parentprop = CoreUtils.GetProperty(type, parentkey);
  35. if (parentprop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
  36. {
  37. if (!IsNestedEntityLink(type, parentkey))
  38. {
  39. bOK = editor is CodeEditor;
  40. lookup = ImportLookupType.Restrict;
  41. }
  42. else
  43. {
  44. bOK = false;
  45. lookup = ImportLookupType.None;
  46. }
  47. }
  48. }
  49. }
  50. if (bOK)
  51. results.Add(new ImportMapping { Property = key, Field = "", Constant = "", Lookup = lookup, Key = bKey });
  52. }
  53. }
  54. return results;
  55. }
  56. private static bool IsNestedEntityLink(Type type, string property)
  57. {
  58. if (String.IsNullOrWhiteSpace(property))
  59. return false;
  60. var parent = string.Join(".", property.Split('.').Reverse().Skip(1).Reverse());
  61. if (!string.IsNullOrEmpty(parent))
  62. {
  63. var parentprop = CoreUtils.GetProperty(type, parent);
  64. if (parentprop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
  65. return true;
  66. return IsNestedEntityLink(type,parent);
  67. }
  68. return false;
  69. }
  70. public static void Register(Type type, string description, string filter)
  71. {
  72. if (!_definitions.Any(x => x.Type.Equals(type)))
  73. _definitions.Add(new ImportDefinition(type, description, filter));
  74. }
  75. public static IImporter Create(ImportDefinition definition, Type type)
  76. {
  77. var importer = definition.Type.MakeGenericType(type);
  78. var result = Activator.CreateInstance(importer) as IImporter;
  79. return result;
  80. }
  81. //public static IImporter<T> Create<T>(ImportDefinition definition) where T : new()
  82. //{
  83. // var importer = definition.Type.MakeGenericType(typeof(T));
  84. // var result = Activator.CreateInstance(importer) as IImporter<T>;
  85. // return result;
  86. //}
  87. }
  88. }