ImportFactory.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. namespace InABox.Core
  6. {
  7. public enum ImportMappingType
  8. {
  9. Import,
  10. Export
  11. }
  12. public static class ImportFactory
  13. {
  14. private static readonly List<ImportDefinition> _definitions = new List<ImportDefinition>();
  15. public static ImportDefinition[] Definitions => _definitions.ToArray();
  16. private static Dictionary<Type, Type>? _generators;
  17. private static IImportMappingGenerator? GetGenerator(Type type)
  18. {
  19. _generators ??= CoreUtils.TypeList(
  20. AppDomain.CurrentDomain.GetAssemblies(),
  21. myType =>
  22. myType.IsClass
  23. && !myType.IsAbstract
  24. && !myType.IsGenericType
  25. && myType.HasInterface(typeof(IImportMappingGenerator<>))
  26. ).ToDictionary(x => x.GetInterfaceDefinition(typeof(IImportMappingGenerator<>))!.GenericTypeArguments[0], x => x);
  27. if(_generators.TryGetValue(type, out var generatorType))
  28. {
  29. return Activator.CreateInstance(generatorType) as IImportMappingGenerator;
  30. }
  31. else
  32. {
  33. return null;
  34. }
  35. }
  36. public static List<ImportMapping> GenerateMappings(Type t, List<ImportMapping> mappings, ImportMappingType importType)
  37. {
  38. var generator = GetGenerator(t);
  39. if(generator is null)
  40. {
  41. return mappings;
  42. }
  43. else
  44. {
  45. return generator.GenerateMappings(mappings, importType);
  46. }
  47. }
  48. public static IEnumerable<ImportMapping> ExtractMappings(Type type, ImportMappingType Import)
  49. {
  50. var results = new List<ImportMapping>();
  51. var props = DatabaseSchema.Properties(type).OrderBy(x=>x.Sequence);
  52. //var keys = CoreUtils.PropertyList(type, x => true, true).Keys.ToArray();
  53. foreach (var prop in props)
  54. {
  55. if (prop.Editor != null && !(prop.Editor is NullEditor) && (!prop.IsCalculated || Import == ImportMappingType.Export))
  56. {
  57. var bOK = true;
  58. var bKey = prop.Editor is UniqueCodeEditor;
  59. var lookup = ImportLookupType.None;
  60. if (Import == ImportMappingType.Import)
  61. {
  62. var comps = prop.Name.Split('.');
  63. if (comps.Length > 1)
  64. {
  65. if (prop.HasParentEntityLink())
  66. {
  67. bOK = false;
  68. if (prop.Editor is CodeEditor && prop.Parent!.IsEntityLink && !prop.Parent.HasParentEntityLink())
  69. {
  70. lookup = ImportLookupType.Restrict;
  71. bOK = true;
  72. }
  73. }
  74. }
  75. }
  76. if (bOK)
  77. results.Add(new ImportMapping { Property = prop.Name, Field = "", Constant = "", Lookup = lookup, Key = bKey });
  78. }
  79. }
  80. return GenerateMappings(type, results, Import);
  81. }
  82. private static bool IsNestedEntityLink(Type type, string property)
  83. {
  84. if (String.IsNullOrWhiteSpace(property))
  85. return false;
  86. var parent = string.Join(".", property.Split('.').Reverse().Skip(1).Reverse());
  87. if (!string.IsNullOrEmpty(parent))
  88. {
  89. var parentprop = CoreUtils.GetProperty(type, parent);
  90. if (parentprop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
  91. return true;
  92. return IsNestedEntityLink(type,parent);
  93. }
  94. return false;
  95. }
  96. public static void Register(Type type, string description, string filter)
  97. {
  98. if (!_definitions.Any(x => x.Type.Equals(type)))
  99. _definitions.Add(new ImportDefinition(type, description, filter));
  100. }
  101. public static IImporter Create(ImportDefinition definition, Type type)
  102. {
  103. var importer = definition.Type.MakeGenericType(type);
  104. var result = Activator.CreateInstance(importer) as IImporter;
  105. return result;
  106. }
  107. //public static IImporter<T> Create<T>(ImportDefinition definition) where T : new()
  108. //{
  109. // var importer = definition.Type.MakeGenericType(typeof(T));
  110. // var result = Activator.CreateInstance(importer) as IImporter<T>;
  111. // return result;
  112. //}
  113. }
  114. }