123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- namespace InABox.Core
- {
- public enum ImportMappingType
- {
- Import,
- Export
- }
- public static class ImportFactory
- {
- private static readonly List<ImportDefinition> _definitions = new List<ImportDefinition>();
- public static ImportDefinition[] Definitions => _definitions.ToArray();
- private static Dictionary<Type, Type>? _generators;
-
- private static IImportMappingGenerator? GetGenerator(Type type)
- {
- _generators ??= CoreUtils.TypeList(
- AppDomain.CurrentDomain.GetAssemblies(),
- myType =>
- myType.IsClass
- && !myType.IsAbstract
- && !myType.IsGenericType
- && myType.HasInterface(typeof(IImportMappingGenerator<>))
- ).ToDictionary(x => x.GetInterfaceDefinition(typeof(IImportMappingGenerator<>))!.GenericTypeArguments[0], x => x);
- if(_generators.TryGetValue(type, out var generatorType))
- {
- return Activator.CreateInstance(generatorType) as IImportMappingGenerator;
- }
- else
- {
- return null;
- }
- }
- public static List<ImportMapping> GenerateMappings(Type t, List<ImportMapping> mappings, ImportMappingType importType)
- {
- var generator = GetGenerator(t);
- if(generator is null)
- {
- return mappings;
- }
- else
- {
- return generator.GenerateMappings(mappings, importType);
- }
- }
- public static IEnumerable<ImportMapping> ExtractMappings(Type type, ImportMappingType Import)
- {
- var results = new List<ImportMapping>();
-
- var props = DatabaseSchema.Properties(type).OrderBy(x=>x.Sequence);
-
- //var keys = CoreUtils.PropertyList(type, x => true, true).Keys.ToArray();
- foreach (var prop in props)
- {
- if (prop.Editor != null && !(prop.Editor is NullEditor) && (!prop.IsCalculated || Import == ImportMappingType.Export))
- {
- var bOK = true;
- var bKey = prop.Editor is UniqueCodeEditor;
- var lookup = ImportLookupType.None;
- if (Import == ImportMappingType.Import)
- {
- var comps = prop.Name.Split('.');
- if (comps.Length > 1)
- {
- if (prop.HasParentEntityLink())
- {
- bOK = false;
- if (prop.Editor is CodeEditor && prop.Parent!.IsEntityLink && !prop.Parent.HasParentEntityLink())
- {
- lookup = ImportLookupType.Restrict;
- bOK = true;
- }
- }
- }
- }
- if (bOK)
- results.Add(new ImportMapping { Property = prop.Name, Field = "", Constant = "", Lookup = lookup, Key = bKey });
- }
- }
- return GenerateMappings(type, results, Import);
- }
- private static bool IsNestedEntityLink(Type type, string property)
- {
- if (String.IsNullOrWhiteSpace(property))
- return false;
- var parent = string.Join(".", property.Split('.').Reverse().Skip(1).Reverse());
- if (!string.IsNullOrEmpty(parent))
- {
- var parentprop = CoreUtils.GetProperty(type, parent);
- if (parentprop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
- return true;
- return IsNestedEntityLink(type,parent);
- }
- return false;
- }
- public static void Register(Type type, string description, string filter)
- {
- if (!_definitions.Any(x => x.Type.Equals(type)))
- _definitions.Add(new ImportDefinition(type, description, filter));
- }
- public static IImporter Create(ImportDefinition definition, Type type)
- {
- var importer = definition.Type.MakeGenericType(type);
- var result = Activator.CreateInstance(importer) as IImporter;
- return result;
- }
- //public static IImporter<T> Create<T>(ImportDefinition definition) where T : new()
- //{
- // var importer = definition.Type.MakeGenericType(typeof(T));
- // var result = Activator.CreateInstance(importer) as IImporter<T>;
- // return result;
- //}
- }
- }
|