public Dictionary<Int32, Int32> ImportSystems_Own(ref List<EDSystem> Systems, Boolean OnlyAddUnknown = false, Boolean setVisitedFlag = false)
{
DBConnector lDBCon = null;
String sqlString;
dsEliteDB.tbsystemsRow[] FoundRows;
DateTime Timestamp_new, Timestamp_old;
Int32 ImportCounter = 0;
Dictionary<Int32, Int32> changedSystemIDs = new Dictionary<Int32, Int32>();
Int32 currentSelfCreatedIndex = -1;
dsEliteDB Data;
Int32 Counter = 0;
try
{
lDBCon = new DBConnector(Program.DBCon.ConfigData, true);
Data = new dsEliteDB();
// gettin' some freaky performance
lDBCon.Execute("set global innodb_flush_log_at_trx_commit=2");
lDBCon.TransBegin();
sqlString = "select * from tbSystems lock in share mode";
lDBCon.TableRead(sqlString, Data.tbsystems);
sqlString = "select * from tbSystems_org lock in share mode";
lDBCon.TableRead(sqlString, Data.tbsystems_org);
currentSelfCreatedIndex = getNextOwnSystemIndex();
sendProgressEvent(new ProgressEventArgs() { Info="import self-added systems", CurrentValue=Counter, TotalValue=Systems.Count });
foreach (EDSystem System in Systems)
{
if (!String.IsNullOrEmpty(System.Name.ToString().Trim()))
{
// self-created systems don't have the correct id so it must be identified by name
FoundRows = (dsEliteDB.tbsystemsRow[])Data.tbsystems.Select("systemname=" + DBConnector.SQLAString(DBConnector.DTEscape(System.Name.ToString())));
if ((FoundRows != null) && (FoundRows.Count() > 0))
{
if (!OnlyAddUnknown)
{
// system is existing
// memorize the changed system ids for importing user changed stations in the (recommend) second step
changedSystemIDs.Add(System.Id, (Int32)FoundRows[0]["id"]);
System.Id = (Int32)FoundRows[0]["id"];
if ((bool)(FoundRows[0]["is_changed"]))
{
// old data is changed by user and the new data is also a user changed data
// keep the newer version in the main table
Timestamp_old = (DateTime)(FoundRows[0]["updated_at"]);
Timestamp_new = DateTimeOffset.FromUnixTimeSeconds(System.UpdatedAt).DateTime;
if (Timestamp_new > Timestamp_old)
{
// data from file is newer -> take it but hold the old id
CopyEDSystemToDataRow(System, (DataRow)FoundRows[0], true, null, true);
ImportCounter += 1;
}
}
else
{
// new data is user changed data, old data is original data
// copy the original data ("tbSystems") to the saving data table ("tbSystems_org")
// and get the correct system ID
Data.tbsystems_org.LoadDataRow(FoundRows[0].ItemArray, false);
CopyEDSystemToDataRow(System, (DataRow)FoundRows[0], true, null, true);
ImportCounter += 1;
}
}
else
{
System.Id = (Int32)FoundRows[0]["id"];
}
}
else
{
// add a new system
// memorize the changed system ids for importing user changed stations in the (recommend) second step
if (!OnlyAddUnknown)
changedSystemIDs.Add(System.Id, currentSelfCreatedIndex);
System.Id = currentSelfCreatedIndex;
dsEliteDB.tbsystemsRow newRow = (dsEliteDB.tbsystemsRow)Data.tbsystems.NewRow();
CopyEDSystemToDataRow(System, (DataRow)newRow, true, null, true);
newRow.visited = setVisitedFlag;
newRow.updated_at = DateTime.UtcNow;
Data.tbsystems.Rows.Add(newRow);
currentSelfCreatedIndex -= 1;
ImportCounter += 1;
}
if ((ImportCounter > 0) && ((ImportCounter % 100) == 0))
{
// save changes
Debug.Print("added Systems : " + ImportCounter.ToString());
lDBCon.TableUpdate(Data.tbsystems);
lDBCon.TableUpdate(Data.tbsystems_org);
}
}
Counter++;
sendProgressEvent(new ProgressEventArgs() { Info="import self-added systems", CurrentValue=Counter, TotalValue=Systems.Count });
}
// save changes
lDBCon.TableUpdate(Data.tbsystems);
lDBCon.TableUpdate(Data.tbsystems_org);
lDBCon.TransCommit();
// reset freaky performance
lDBCon.Execute("set global innodb_flush_log_at_trx_commit=1");
lDBCon.Dispose();
// return all changed ids
return changedSystemIDs;
}
catch (Exception ex)
{
if(lDBCon != null)
{
if (lDBCon.TransActive())
lDBCon.TransRollback();
try
{
// reset freaky performance
lDBCon.Execute("set global innodb_flush_log_at_trx_commit=1");
lDBCon.Dispose();
}
catch (Exception)
{ }
}
throw new Exception("Error while importing system data", ex);
}
}