ImportFactory.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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)
  52. .OrderBy(x => x.Page, Comparer<string>.Create((x, y) =>
  53. {
  54. var result = x.CompareTo(y);
  55. if(result == 0)
  56. {
  57. return 0;
  58. }
  59. else if(x.Equals(""))
  60. {
  61. return -1;
  62. }
  63. else if(y.Equals(""))
  64. {
  65. return 1;
  66. }
  67. return result;
  68. }))
  69. .ThenBy(x => x.PropertySequence());
  70. //var keys = CoreUtils.PropertyList(type, x => true, true).Keys.ToArray();
  71. foreach (var prop in props)
  72. {
  73. if (prop.Editor != null && !(prop.Editor is NullEditor) && (!prop.IsCalculated || Import == ImportMappingType.Export))
  74. {
  75. var bOK = true;
  76. var bKey = prop.Editor is UniqueCodeEditor;
  77. var lookup = ImportLookupType.None;
  78. if (Import == ImportMappingType.Import)
  79. {
  80. var comps = prop.Name.Split('.');
  81. if (comps.Length > 1)
  82. {
  83. if (prop.HasParentEntityLink())
  84. {
  85. bOK = false;
  86. if (prop.Editor is CodeEditor && prop.Parent!.IsEntityLink && !prop.Parent.HasParentEntityLink())
  87. {
  88. lookup = ImportLookupType.Restrict;
  89. bOK = true;
  90. }
  91. }
  92. }
  93. }
  94. if (bOK)
  95. results.Add(new ImportMapping { Property = prop.Name, Field = "", Constant = "", Lookup = lookup, Key = bKey });
  96. }
  97. }
  98. return GenerateMappings(type, results, Import);
  99. }
  100. private static bool IsNestedEntityLink(Type type, string property)
  101. {
  102. if (String.IsNullOrWhiteSpace(property))
  103. return false;
  104. var parent = string.Join(".", property.Split('.').Reverse().Skip(1).Reverse());
  105. if (!string.IsNullOrEmpty(parent))
  106. {
  107. var parentprop = CoreUtils.GetProperty(type, parent);
  108. if (parentprop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
  109. return true;
  110. return IsNestedEntityLink(type,parent);
  111. }
  112. return false;
  113. }
  114. public static void Register(Type type, string description, string filter)
  115. {
  116. if (!_definitions.Any(x => x.Type.Equals(type)))
  117. _definitions.Add(new ImportDefinition(type, description, filter));
  118. }
  119. private static int[] ExtractColumnWidths(string widths)
  120. {
  121. if (string.IsNullOrWhiteSpace(widths))
  122. return new[] { 1024 };
  123. try
  124. {
  125. return widths.Split(',').Select(int.Parse).ToArray();
  126. }
  127. catch
  128. {
  129. return new[] { 1024 };
  130. }
  131. }
  132. public static IImporter Create(ImportDefinition definition, Type type, Importer importer)
  133. {
  134. var iimporter = definition.Type.MakeGenericType(type);
  135. var result = (Activator.CreateInstance(iimporter) as IImporter)!;
  136. if (result is IFixedWidthImporter fixedImporter)
  137. fixedImporter.ColumnWidths = ExtractColumnWidths(importer.ColumnWidths);
  138. if (result is ISettingsImporter settingsImporter)
  139. {
  140. if (!importer.Settings.IsNullOrWhiteSpace())
  141. {
  142. var settings = Serialization.Deserialize(settingsImporter.SettingsType, importer.Settings);
  143. if (settings != null)
  144. {
  145. settingsImporter.SetSettings(settings);
  146. }
  147. }
  148. }
  149. result.HasHeader = importer.HasHeader;
  150. result.HeaderRow = Math.Max(1, importer.HeaderRows);
  151. return result;
  152. }
  153. //public static IImporter<T> Create<T>(ImportDefinition definition) where T : new()
  154. //{
  155. // var importer = definition.Type.MakeGenericType(typeof(T));
  156. // var result = Activator.CreateInstance(importer) as IImporter<T>;
  157. // return result;
  158. //}
  159. }
  160. }