public static void GenerateFile(List <CsvDataSource> csvDataSource, string outputFilePath) { var stgMetadata = new StgMetadata(); //get unique source models var sourceModels = csvDataSource.Select(e => e.SourceModel).Distinct().ToList(); //remove empty spaces if any sourceModels = sourceModels.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList(); if (sourceModels == null || !sourceModels.Any()) { Logger.LogInfo("Could not find any valid Source Model in the csv"); return; } outputFilePath += "stage"; Utility.CreateDirectoryIfDoesNotExists(outputFilePath); foreach (var sourceModel in sourceModels) { try { //get the records related to the source model List <CsvDataSource> tableRecords = csvDataSource.Where(e => e.SourceModel.Equals(sourceModel, StringComparison.OrdinalIgnoreCase)).ToList(); if (tableRecords == null || !tableRecords.Any()) { Logger.LogInfo("Could not find records for Source Model " + sourceModel + " in the csv"); break; } Logger.LogInfo("Generating stage file for " + sourceModel); if (sourceModel.Contains(".")) { stgMetadata.SourceModelLabel = sourceModel.Substring(0, sourceModel.IndexOf(".")); stgMetadata.SourceModelValue = sourceModel.Substring(sourceModel.LastIndexOf(".") + 1); } else { stgMetadata.SourceModelLabel = Constants.NotFoundString; stgMetadata.SourceModelValue = Constants.NotFoundString; } stgMetadata.HashDiff = false; stgMetadata.IsFIleTypeBR = false; foreach (var tableRecord in tableRecords) { if (tableRecord.ColumnName.Equals(Constants.SrcHashDiff, StringComparison.OrdinalIgnoreCase)) { stgMetadata.HashDiff = true; } } stgMetadata.HashedColumns = GetHashedColumns(tableRecords); stgMetadata.DerivedColumns = GetDerivedColumns(tableRecords); stgMetadata.Columns = GetHashDiffColumns(tableRecords); stgMetadata.Tags = GetTags(tableRecords, sourceModel).Select(i => i.ToString()).ToArray(); // getting the stage file name var fileName = sourceModel; if (fileName.Contains(".")) { var index = fileName.LastIndexOf("."); index = fileName.Substring(0, index - 1).LastIndexOf("."); if (index != -1) { fileName = fileName.Substring(index + 1).Replace(".", "_").ToLower(); } } var stgTemplate = new StgTemplate(stgMetadata); var content = stgTemplate.TransformText(); var pathStr = $"{outputFilePath}\\stg_{fileName}.sql"; File.WriteAllText(pathStr, content); Logger.LogInfo("Generated stage file for Soure Model: " + sourceModel); } catch (Exception e) { Logger.LogError(e, Utility.ErrorGeneratingFileForSourceModel("STG", sourceModel, e.Message), "{@StgMetadata}", stgMetadata); } } }
public StgTemplate(StgMetadata stgMetadata) { StgMetadata = stgMetadata; }