protected virtual string CalculateDependent(DependentColumn col, Dictionary <string, object> cache, object val) { string t; if (col.Calculator != null) { t = col.Calculator(val); } else { t = col.Map[val]; } cache.Add(col.Name, t); return(t); }
public override void BuildAndSaveFile() { base.BuildAndSaveFile(); var csvDef = definition as CSVImportDefinition; var results = new List <ValidationResult>() as ICollection <ValidationResult>; if (!validation.IsModelValid(csvDef, out results)) { var errors = string.Join(Environment.NewLine, results); throw new Exception(errors); } var file = new StringBuilder(); var rand = new Random(); var cols = csvDef.Columns.OrderBy(x => x.ColumnOrder).ToArray(); var colLen = cols.Count(); //header row var hdr = ""; for (var i = 0; i < colLen; i++) { hdr += cols[i].Name; if (i < colLen - 1) { hdr += csvDef.Delimiter; } } file.AppendLine(hdr); //data rows for (var i = 0; i < csvDef.RowCount; i++) { var row = ""; var rowCache = new Dictionary <string, object>(); for (var j = 0; j < colLen; j++) { var c = cols[j]; string temp; switch (c.Type) { case ColumnType.AutoIncremented: temp = CalculateIncremented(c as AutoIncrementedColumn, rowCache); break; case ColumnType.Randomized: temp = CalculateRandomized(c as RandomizedColumn, rand, rowCache); break; case ColumnType.Dependent: DependentColumn cDep = c as DependentColumn; temp = CalculateDependent(cDep, rowCache, rowCache[cDep.DependsOn]); break; case ColumnType.Static: temp = CalculateStatic(c as StaticColumn, rowCache); break; default: throw new KeyNotFoundException(string.Format("Column Type {0} not supported", c.Type)); } //wrap in quotes if contains set delimiter if (temp.Contains(csvDef.Delimiter.Value)) { temp = csvDef.QuoteCharacter + temp + csvDef.QuoteCharacter; } row += temp; if (j < colLen - 1) { row += csvDef.Delimiter; } } file.AppendLine(row); } //write to file writer.Write(file.ToString()); }