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

ImportPricesFromCSVStrings() public method

Imports the prices from a list of csv-strings
public ImportPricesFromCSVStrings ( String CSV_Strings, enImportBehaviour importBehaviour, enDataSource dataSource ) : List
CSV_Strings String data to import
importBehaviour enImportBehaviour filter, which prices to import
dataSource enDataSource if data has no information about the datasource, this setting will count
return List
        public List<EDStation> ImportPricesFromCSVStrings(String[] CSV_Strings, enImportBehaviour importBehaviour, enDataSource dataSource)
        {
            Boolean MissingSystem   = false;
            Boolean MissingStation  = false;
            String currentLanguage;
            DataTable newData;
            List<EDStation> StationData;
            List<EDSystem> SystemData = null;
            List<CsvRow> csvRowList = new List<CsvRow>();
            ProgressEventArgs eva;

            Int32 counter = 0;
            Dictionary<String, String> foundNames = new Dictionary<string,string>();            // quick cache for finding commodity names

            try
            {
                // *****************************************************************
                // START :section for automatically add unknown commodities

                currentLanguage     = Program.DBCon.getIniValue(IBESettingsView.DB_GROUPNAME, "Language", Program.BASE_LANGUAGE, false);
                newData             = new DataTable();
                newData.TableName   = "Names";
                newData.Columns.Add(Program.BASE_LANGUAGE, typeof(String));
                if(currentLanguage != Program.BASE_LANGUAGE)
                    newData.Columns.Add(currentLanguage, typeof(String));

                eva = new ProgressEventArgs() { Info="analysing data...", AddSeparator = true};
                sendProgressEvent(eva);

                for (int i = 0; i < CSV_Strings.Length; i++)
                {
                    String currentName;
                    List<dsEliteDB.tbcommoditylocalizationRow> currentCommodity;
                    if (CSV_Strings[i].Trim().Length > 0)
                    {
                        currentName = new CsvRow(CSV_Strings[i]).CommodityName;
                        if (!String.IsNullOrEmpty(currentName))
                        {
                            // check if we need to remap this name
                            Datasets.dsEliteDB.tbdnmap_commodityRow mappedName = (Datasets.dsEliteDB.tbdnmap_commodityRow)BaseData.tbdnmap_commodity.Rows.Find(new object[] {currentName, ""});
                            if (mappedName != null)
                            {
                                CSV_Strings[i] = CSV_Strings[i].Replace(mappedName.CompanionName, mappedName.GameName);
                                currentName = mappedName.GameName;
                            }

                            if (!foundNames.ContainsKey(currentName))
                            {
                                currentCommodity = Program.Data.BaseData.tbcommoditylocalization.Where(x => x.locname.Equals(currentName, StringComparison.InvariantCultureIgnoreCase)).ToList();
                                if (currentCommodity.Count == 0)
                                {
                                    if (currentLanguage == Program.BASE_LANGUAGE)
                                        newData.Rows.Add(currentName);
                                    else
                                        newData.Rows.Add(currentName, currentName);
                                }
                                foundNames.Add(currentName, "");
                            }
                        }
                    }
                    counter++;

                    eva = new ProgressEventArgs() { Info="analysing data...", CurrentValue=counter, TotalValue=CSV_Strings.GetUpperBound(0) + 1 };
                    sendProgressEvent(eva);
                    if(eva.Cancelled)
                        break;
                }

                eva = new ProgressEventArgs() { Info="analysing data...", CurrentValue=counter, TotalValue=counter, ForceRefresh=true };
                sendProgressEvent(eva);

                if (!eva.Cancelled)
                    if(newData.Rows.Count > 0)
                    {
                        // add found unknown commodities
                        var ds = new DataSet();
                        ds.Tables.Add(newData);
                        ImportCommodityLocalizations(ds);

                        // refresh translation columns
                        Program.Data.updateTranslation();

                        // refresh working tables 
                        Program.Data.PrepareBaseTables(Program.Data.BaseData.tbcommoditylocalization.TableName);
                        Program.Data.PrepareBaseTables(Program.Data.BaseData.tbcommodity.TableName);
                    }
                    
                // END : section for automatically add unknown commodities
                // *****************************************************************

                // convert csv-strings to EDStation-objects
                StationData = fromCSV(CSV_Strings, ref SystemData, ref csvRowList);

                // check if we've unknown systems or stations
                if(!eva.Cancelled)
                    foreach (EDStation Station in StationData)
                    {
                        if (Station.SystemId == 0)
                            MissingSystem = true;
                        else if(Station.Id == 0)
                            MissingStation = true;
                    }


                if ((!eva.Cancelled) && MissingSystem)
                {
                    // add unknown systems
                    ImportSystems_Own(ref SystemData, true);
                }

                if (!eva.Cancelled && (MissingSystem || MissingStation))
                {
                    // add unknown stations
                    foreach (EDStation Station in StationData)
                    {
                        // first get all missing system ids
                        if (Station.SystemId == 0)
                        {
                            EDSystem thisSystem = SystemData.FirstOrDefault(x => x.Name == Station.SystemName);

                            if(thisSystem != null)
                            {
                                // got it - set the id
                                Station.SystemId = thisSystem.Id;
                            }
                        }

                    }

                    ImportStations_Own(StationData, new Dictionary<Int32, Int32>(), true);
                }

                // now import the prices
                ImportPrices(StationData, importBehaviour, dataSource);

                if (MissingSystem)
                {
                    // reloading of base tables
                    Program.Data.PrepareBaseTables(Program.Data.BaseData.tbsystems.TableName);
                }

                if (MissingSystem || MissingStation)
                {
                    // reloading of base tables
                    Program.Data.PrepareBaseTables(Program.Data.BaseData.tbstations.TableName);

                    Program.Data.PrepareBaseTables(Program.Data.BaseData.visystemsandstations.TableName);
                }

                return StationData;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while importing self collected price data", ex);
            }
        }