IBE.SQL.EliteDBIO.ImportStations_Own C# (CSharp) Method

ImportStations_Own() public method

imports the "own" station data into the database
public ImportStations_Own ( List Stations, Int32>.Dictionary changedSystemIDs, System.Boolean OnlyAddUnknown = false, System.Boolean setVisitedFlag = false ) : void
Stations List
changedSystemIDs Int32>.Dictionary
OnlyAddUnknown System.Boolean
setVisitedFlag System.Boolean
return void
        public void ImportStations_Own(List<EDStation> Stations, Dictionary<Int32, Int32> changedSystemIDs, Boolean OnlyAddUnknown = false, Boolean setVisitedFlag = false)
        {
            DBConnector               lDBCon = null;
            String sqlString;
            dsEliteDB.tbstationsRow[] FoundRows;
            dsEliteDB.tbsystemsRow[] FoundSysRows;
            DateTime Timestamp_new, Timestamp_old;
            Int32 ImportCounter = 0;
            Int32 currentSelfCreatedIndex = -1;
            dsEliteDB Data = new dsEliteDB();
            Int32 Counter = 0;

            try
            {
                lDBCon = new DBConnector(Program.DBCon.ConfigData, true);

                // gettin' some freaky performance
                lDBCon.Execute("set global innodb_flush_log_at_trx_commit=2");

                lDBCon.TransBegin();

                sqlString = "select * from tbStations lock in share mode";
                lDBCon.TableRead(sqlString, Data.tbstations);
                sqlString = "select * from tbStations_org lock in share mode";
                lDBCon.TableRead(sqlString, Data.tbstations_org);
                sqlString = "select * from tbStationEconomy lock in share mode";
                lDBCon.TableRead(sqlString, Data.tbstationeconomy);

                // get the smallest ID for self added stations
                sqlString = "select min(id) As min_id from tbStations";
                lDBCon.Execute(sqlString, "minID", Data);

                if (Convert.IsDBNull(Data.Tables["minID"].Rows[0]["min_id"]))
                    currentSelfCreatedIndex = -1;
                else
                {
                    currentSelfCreatedIndex = ((Int32)Data.Tables["minID"].Rows[0]["min_id"]) - 1;
                    if (currentSelfCreatedIndex >= 0)
                        currentSelfCreatedIndex = -1;
                }

                sendProgressEvent(new ProgressEventArgs() { Info="import self-added stations", CurrentValue=Counter,  TotalValue=Stations.Count });

                foreach (EDStation Station in Stations)
                {
                    Int32 SystemID;

                    if(Station.Name == "Glass City")
                        Debug.Print("stop");

                    // is the system id changed ? --> get the new system id, otherwise the original
                    if (changedSystemIDs.TryGetValue(Station.SystemId, out SystemID))
                        Station.SystemId = SystemID;

                    // if there are missing system ids, try to get them
                    if ((Station.SystemId == 0) && (!String.IsNullOrEmpty(Station.SystemName)))
                    {
                        FoundSysRows = (dsEliteDB.tbsystemsRow[])Data.tbsystems.Select("systemname=" + DBConnector.SQLAString(DBConnector.DTEscape(Station.SystemName)));

                        if((FoundSysRows != null) && (FoundSysRows.Count() > 0))
                        {
                            // got it - set the id
                            Station.SystemId = FoundSysRows[0].id;
                        }
                    }

                    if (!String.IsNullOrEmpty(Station.Name.Trim()) && (Station.SystemId != 0))
                    {
                        // self-created stations don't have the correct id so they must be identified by name    
                        FoundRows = (dsEliteDB.tbstationsRow[])Data.tbstations.Select("stationname=" + DBConnector.SQLAString(DBConnector.DTEscape(Station.Name)) + " and " +
                                                                     "system_id =  " + Station.SystemId);

                        if ((FoundRows != null) && (FoundRows.Count() > 0))
                        {
                            // Location is existing, get the same Id
                            Station.Id = (Int32)FoundRows[0]["id"];

                            if (!OnlyAddUnknown)
                            {

                                if ((bool)(FoundRows[0]["is_changed"]))
                                {
                                    // existing data data is also changed by user - keep the newer version 
                                    Timestamp_old = (DateTime)(FoundRows[0]["updated_at"]);
                                    Timestamp_new = DateTimeOffset.FromUnixTimeSeconds(Station.UpdatedAt).DateTime;

                                    if (Timestamp_new > Timestamp_old)
                                    {
                                        // data from file is newer
                                        CopyEDStationToDataRow(Station, (DataRow)FoundRows[0], true, null, true);

                                        CopyEDStationEconomiesToDataRows(Station, Data.tbstationeconomy);
                                        // commodities are not considered because there was no possibility for input in the old RN

                                        ImportCounter += 1;
                                    }
                                }
                                else
                                {
                                    // new data is user changed data, old data is original data
                                    // copy the original data ("tbStations") to the saving data table ("tbStations_org")
                                    // and get the correct system ID
                                    Data.tbstations_org.LoadDataRow(FoundRows[0].ItemArray, false);
                                    CopyEDStationToDataRow(Station, (DataRow)FoundRows[0], true, null, true);

                                    CopyEDStationEconomiesToDataRows(Station, Data.tbstationeconomy);
                                    // commodities are not considered because there was no possibility for input in the old RN

                                    ImportCounter += 1;
                                }
                            }
                        }
                        else
                        {
                            // add a new station
                            Station.Id = currentSelfCreatedIndex;

                            dsEliteDB.tbstationsRow newRow = (dsEliteDB.tbstationsRow)Data.tbstations.NewRow();

                            CopyEDStationToDataRow(Station, (DataRow)newRow, true, null, true);
                            newRow.visited      = setVisitedFlag;
                            newRow.updated_at   = DateTime.UtcNow;

                            Data.tbstations.Rows.Add(newRow);

                            currentSelfCreatedIndex -= 1;
                            ImportCounter += 1;

                            CopyEDStationEconomiesToDataRows(Station, Data.tbstationeconomy);
                            // commodities are not considered because there was no possibility for input in the old RN

                            ImportCounter += 1;
                        }

                        if ((ImportCounter > 0) && ((ImportCounter % 100) == 0))
                        {
                            // save changes
                            Debug.Print("added Stations : " + ImportCounter.ToString());

                            lDBCon.TableUpdate(Data.tbstations);
                            lDBCon.TableUpdate(Data.tbstations_org);
                            lDBCon.TableUpdate(Data.tbstationeconomy);
                        }
                    }
                    else
                        Debug.Print("why");


                    Counter++;
                    sendProgressEvent(new ProgressEventArgs() { Info="import self-added stations", CurrentValue=Counter, TotalValue=Stations.Count });

                }

                // save changes
                lDBCon.TableUpdate(Data.tbstations);
                lDBCon.TableUpdate(Data.tbstations_org);
                lDBCon.TableUpdate(Data.tbstationeconomy);

                lDBCon.TransCommit();

                // reset freaky performance
                lDBCon.Execute("set global innodb_flush_log_at_trx_commit=1");

                lDBCon.Dispose();
            }
            catch (Exception ex)
            {
                if(lDBCon != null)
                {
                    if (lDBCon.TransActive())
                        lDBCon.TransRollback();

                    try
                    {

	                    lDBCon.Dispose();
                        // reset freaky performance
                        lDBCon.Execute("set global innodb_flush_log_at_trx_commit=1");

                        lDBCon.Dispose();
                    }
                    catch (Exception) { }
                }

                throw new Exception("Error while importing Station data", ex);
            }
        }

Same methods

EliteDBIO::ImportStations_Own ( EDStation Station, System.Boolean OnlyAddUnknown = false, System.Boolean setVisitedFlag = false ) : void
EliteDBIO::ImportStations_Own ( String Filename, System.Boolean OnlyAddUnknown = false ) : void
EliteDBIO::ImportStations_Own ( String Filename, Int32>.Dictionary changedSystemIDs, System.Boolean OnlyAddUnknown = false ) : void