/// <summary> /// Corresponds to CLI "run" keyword. Executes handler specified by run options. /// </summary> public void Execute() { Type recordType = ActivatorUtil.ResolveType(Type, ActivatorSettings.Assemblies) ?? throw new ArgumentException($"Type '{Type}' not found"); // Register type in BsonClassMap if not yet registered. // This part is critical for derived types, since collection creation registers only base type. if (!BsonClassMap.IsClassMapRegistered(recordType)) { MethodInfo registerMap = typeof(BsonClassMap) .GetMethod(nameof(BsonClassMap.RegisterClassMap), new Type[] { }) .MakeGenericMethod(recordType); registerMap.Invoke(null, new object[] { }); } (Type keyType, Type baseRecordType) = GetRecordTypeArguments(recordType); MethodInfo createHandlerMethod = typeof(RunCommand) .GetMethod(nameof(CreateHandler), BindingFlags.Static | BindingFlags.NonPublic) .MakeGenericMethod(keyType, baseRecordType, recordType); // TODO: changes to db naming should be synchronized with client, afterwards connection should be parsed from source string connectionLiteral = "ConnectionString="; // Extract ConnectionString from source string string[] sourceParams = Source.Split(','); string connectionString = sourceParams.First(t => t.StartsWith(connectionLiteral)).Substring(connectionLiteral.Length); // Convert connection string to db name and hosts MongoUrl url = MongoUrl.Create(connectionString); // Use case insensitive parsing for the environment type var dataSource = new TemporalMongoDataSource { EnvType = Enum.Parse <EnvType>(EnvType, true), EnvGroup = EnvGroup, EnvName = EnvName, MongoServer = new MongoServerKey { MongoServerUri = $"mongodb://{url.Server}" } }; var context = new Context(); context.DataSource = dataSource; context.DataSet = TemporalId.Empty; object record = createHandlerMethod.Invoke(null, new object[] { context, this }); MethodInfo handlerMethod = record.GetType().GetMethod(Handler) ?? throw new ArgumentException($"Method '{Handler}' not found"); // Check that method has [HandlerMethod] attribute before calling it. if (handlerMethod.GetCustomAttribute <HandlerMethodAttribute>() == null) { throw new Exception($"Cannot run {Handler} method, missing [HandlerMethod] attribute."); } handlerMethod.Invoke(record, ActivatorUtil.CreateParameterValues(handlerMethod, Arguments)); }
private static void ProcessDirectory(Context context, string path, TemporalId parentDataset) { var dirName = Path.GetFileName(path); // Do not create dataset for Common var currentDataset = dirName != "Common" ? context.CreateDataSet(dirName) : parentDataset; foreach (var csvFile in Directory.GetFiles(path, "*.csv")) { var type = Path.GetFileNameWithoutExtension(csvFile); Type recordType = ActivatorUtil.ResolveType(type, ActivatorSettings.Assemblies) ?? throw new ArgumentException($"Type '{type}' not found"); MethodInfo convertToMongo = typeof(CsvConvertCommand) .GetMethod(nameof(ConvertCsvToMongo), BindingFlags.Static | BindingFlags.NonPublic) ?.MakeGenericMethod(recordType); convertToMongo?.Invoke(null, new object[] { context, currentDataset, csvFile }); } var directories = Directory.GetDirectories(path); foreach (string directory in directories) { ProcessDirectory(context, directory, currentDataset); } }