public bool CheckBuildVariableTable(string tableName)
{
try
{
using (var conn = DbClient.GetConnection())
{
if (DataClient.HasTable(conn, DbClient, tableName))
{
if (!ReusePreviousJobTable)
{
DbClient.GetCommand(string.Format("DROP TABLE IF EXISTS \"{0}\";", tableName), conn).ExecuteNonQuery();
}
else
{
_log.DebugFormat("Table {0} was already built", tableName);
return true;
}
}
//gets a list of the LRUs we want
HashSet<string> requestedLRNs = GetFilteredLRUs(conn);
//gets all the variable mapping information we need
DataTable reqVariablesDT = GetRequestedVariables(conn);
if ((reqVariablesDT == null) || (reqVariablesDT.Rows.Count == 0))
{
_log.Info("Zero variables found: I couldn't understand those variables, can you check them and try again?");
//_log.Warn("I didn't understand those variables, can you check them and try again?");
return false;
}
DataTable newTable = new DataTable();
newTable.Columns.Add("LOGRECNO", typeof(string));
Dictionary<string, DataRow> rowsByLRN = new Dictionary<string, DataRow>(requestedLRNs.Count);
foreach (var id in requestedLRNs)
{
var row = newTable.NewRow();
row[0] = id;
newTable.Rows.Add(row);
rowsByLRN[id] = row;
}
bool shouldNotParseErrorValues = PreserveJam;
_log.Debug("Importing Columns");
int varNum = 0;
foreach (DataRow variableRow in reqVariablesDT.Rows)
{
if (this.IsCancelled()) { return false; }
varNum++;
var sequenceNo = Utilities.GetAs<int>(variableRow["SEQNO"] as string, -1);
var seqFile = Directory.GetFiles(this.GetAggregateDataPath(), "e*" + sequenceNo.ToString("0000") + "000.txt"); //0001000
if ((seqFile == null) || (seqFile.Length == 0))
{
_log.DebugFormat("Couldn't find sequence file {0}", sequenceNo);
continue;
}
var errorFile = Directory.GetFiles(this.GetAggregateDataPath(), "m*" + sequenceNo.ToString("0000") + "000.txt"); //0001000
if ((errorFile == null) || (errorFile.Length == 0))
{
_log.DebugFormat("Couldn't find error margin file {0}", sequenceNo);
}
//These next two if statement checks should probably be removed once
//#19869 is resolved
//Until that case is resolved, we can't guarantee rows in reqVariablesDT will be
//unique so they should stay.
//TODO: alternate column naming?
string newColumnName = variableRow["COLNAME"] as string;
if (newTable.Columns.Contains(newColumnName))
{
newColumnName = newColumnName + varNum;
}
newTable.Columns.Add(newColumnName, typeof(double));
//this really ought to be unique.
string newErrorMarginColumnName = "m" + newColumnName;
if (newTable.Columns.Contains(newErrorMarginColumnName))
{
newErrorMarginColumnName = newErrorMarginColumnName + varNum;
}
if (shouldNotParseErrorValues)
{
newTable.Columns.Add(newErrorMarginColumnName, typeof(string));
}
else
{
newTable.Columns.Add(newErrorMarginColumnName, typeof(double));
}
_log.DebugFormat("Importing {0}...", newColumnName);
int columnIDX = Utilities.GetAs<int>(variableRow["COLNO"] as string, -1);
CommaSeparatedValueReader reader = new CommaSeparatedValueReader(seqFile[0], false);
foreach (List<string> values in reader)
{
string lrn = values[5];
if (!requestedLRNs.Contains(lrn))
continue;
if (columnIDX < values.Count)
{
double val = Utilities.GetAs<double>(values[columnIDX], double.NaN);
if (!double.IsNaN(val))
{
rowsByLRN[lrn][newColumnName] = val;
}
}
if (this.IsCancelled()) { break; }
}
reader.Close();
//these error files better have the exact same format!
reader = new CommaSeparatedValueReader(errorFile[0], false);
foreach (List<string> values in reader)
{
string lrn = values[5];
if (!requestedLRNs.Contains(lrn))
continue;
if (columnIDX < values.Count)
{
if (shouldNotParseErrorValues)
{
rowsByLRN[lrn][newErrorMarginColumnName] = values[columnIDX];
}
else
{
double val = Utilities.GetAs<double>(values[columnIDX], double.NaN);
if (!double.IsNaN(val))
{
rowsByLRN[lrn][newErrorMarginColumnName] = val;
}
rowsByLRN[lrn][newErrorMarginColumnName] = val;
}
}
if (this.IsCancelled()) { break; }
}
reader.Close();
}
if (this.IsCancelled()) { return false; }
_log.DebugFormat("Creating Table {0}", tableName);
string createTableSQL = SqliteDataClient.GenerateTableSQLFromTable(tableName, newTable, "LOGRECNO");
DbClient.GetCommand(createTableSQL, conn).ExecuteNonQuery();
if (this.IsCancelled()) { return false; }
_log.DebugFormat("Saving Table {0}...", tableName);
var dba = DataClient.GetMagicAdapter(conn, DbClient, string.Format("SELECT * FROM \"{0}\"", tableName));
dba.Update(newTable);
_log.Debug("Done!");
_log.Debug("Import complete!");
//TODO: save the table to the database
}
return true;
}
catch (Exception ex)
{
_log.Error("Error while building table", ex);
}
return false;
}