ImportFactory.cs 6.2 KB

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