// Copyright (c) 2017 schick Informatik // Description: Alle Lookup-Tabellen für das Display statisch aus der SQLite Datenbank initialisieren // using System.Data; using System.Data.SQLite; using System.Collections.Generic; using bsmd.database; using ENI2.Locode; namespace ENI2 { internal static class LocalizedLookup { private static readonly SQLiteConnection _con; private const string _locode_DB_NAME = "db.sqlite"; private static Dictionary _nationalities = null; private static Dictionary> _portAreaInfos = null; static LocalizedLookup() { _con = new SQLiteConnection(string.Format("data source={0}; Version=3;", _locode_DB_NAME)); _con.Open(); } public static Dictionary getLADGCargoHandlingStrings(string languageCode) { Dictionary result = new Dictionary(); string query = string.Format("SELECT key, text FROM LADG_CargoHandlingCodes WHERE langKey = '{0}'", languageCode); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); int key; string text; while (reader.Read()) { key = reader.GetInt32(0); text = reader.GetString(1); result[key] = text; } reader.Close(); return result; } public static Dictionary getPortAreasForLocode(string locode) { Dictionary result = new Dictionary(); string query = string.Format("SELECT Code, PortArea FROM INFO_PortArea WHERE Locode = '{0}' ORDER BY PortArea", locode); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string code = null; string portarea = null; if (!reader.IsDBNull(0)) code = reader.GetString(0); if (!reader.IsDBNull(1)) portarea = reader.GetString(1); if ((code != null) && (portarea != null)) { result[code] = string.Format("{0}-{1}", code, portarea); } } reader.Close(); return result; } public static bool PortAreaExists(string locode, string portArea) { if (locode.IsNullOrEmpty() || portArea.IsNullOrEmpty()) return false; string query = string.Format("SELECT COUNT(*) FROM INFO_PortArea WHERE Locode = '{0}' AND Code = '{1}'", locode, portArea); SQLiteCommand cmd = new SQLiteCommand(query, _con); long numResults = (long)cmd.ExecuteScalar(); return numResults > 0; } public static bool CheckNationality(string nationality) { if (nationality.IsNullOrEmpty()) return false; if (_nationalities == null) _nationalities = getNationalities(); return _nationalities.ContainsKey(nationality); } public static Dictionary getNationalities() { Dictionary result = new Dictionary(); string query = string.Format("SELECT Code, Name FROM Nationality ORDER BY Code"); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string code = null; string name = null; if (!reader.IsDBNull(0)) code = reader.GetString(0); if (!reader.IsDBNull(1)) name = reader.GetString(1); if ((code != null) && (name != null)) result[code] = string.Format("{0} {1}", code, name); } reader.Close(); return result; } public static Dictionary getVesselTypes() { Dictionary result = new Dictionary(); string query = string.Format("SELECT Code, Name FROM VesselType ORDER BY Code"); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string code = null; string name = null; if (!reader.IsDBNull(0)) code = reader.GetString(0); if (!reader.IsDBNull(1)) name = reader.GetString(1); if ((code != null) && (name != null)) result[code] = string.Format("{0} {1}", code, name); } reader.Close(); return result; } public static Dictionary getPackageTypes() { Dictionary result = new Dictionary(); string query = string.Format("SELECT Code, Text FROM PackageTypes ORDER BY Code"); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string code = null; string name = null; if (!reader.IsDBNull(0)) code = reader.GetString(0); if (!reader.IsDBNull(1)) name = reader.GetString(1); if ((code != null) && (name != null)) result[code] = string.Format("{0} {1}", code, name); } reader.Close(); return result; } public static string GetPortAreaFromCode(string code) { string result = null; string query = string.Format("SELECT PortArea from INFO_PortArea WHERE Code = '{0}'", code); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { if (!reader.IsDBNull(0)) result = reader.GetString(0); } reader.Close(); return result; } public static Dictionary getTransportModes() { Dictionary result = new Dictionary(); string query = string.Format("SELECT Code, Name FROM TransportMode ORDER BY Code"); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string code = null; string name = null; if (!reader.IsDBNull(0)) code = reader.GetString(0); if (!reader.IsDBNull(1)) name = reader.GetString(1); if ((code != null) && (name != null)) result[code] = string.Format("{0} {1}", code, name); } reader.Close(); return result; } public static Dictionary getLACodes() { Dictionary result = new Dictionary(); string query = string.Format("SELECT Code, Description FROM LATypes ORDER BY Code"); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { int? code = null; string name = null; if (!reader.IsDBNull(0)) code = reader.GetInt32(0); if (!reader.IsDBNull(1)) name = reader.GetString(1); if ((code != null) && (name != null)) result[(int)code] = string.Format("{0} {1}", code, name); } reader.Close(); return result; } public static Dictionary getCargoCodesNST() { Dictionary result = new Dictionary(); string query = string.Format("SELECT Code, Description FROM CargoTypesNST ORDER BY Code"); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string code = null; string description = null; if (!reader.IsDBNull(0)) code = reader.GetString(0); if (!reader.IsDBNull(1)) description = reader.GetString(1); if (code?.Length == 1) code = string.Format("0{0}", code); // bei einstelligen Werten 0 voranstellen if ((code != null) && (description != null)) result[code] = string.Format("{0} {1}", code, description); } reader.Close(); return result; } public static List> getCargoCodesNST3() { List> result = new List>(); string query = string.Format("SELECT Code, Description, codeGrp3 FROM CargoTypesNST3 ORDER BY Code"); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string code = null; string description = null; string codeGrp3 = null; if (!reader.IsDBNull(0)) code = reader.GetString(0); if (!reader.IsDBNull(1)) description = reader.GetString(1); if (!reader.IsDBNull(2)) codeGrp3 = reader.GetString(2); if ((code != null) && (description != null) && (codeGrp3 != null)) { KeyValuePair kvp = new KeyValuePair(codeGrp3, string.Format("{0} {1} {2}", code, description, codeGrp3)); result.Add(kvp); } } reader.Close(); return result; } public static List SSNAllLocodesForCityNameAsEntries(string city) { List results = new List(); const string query = "SELECT LocationCode, LocationName FROM SSN_LOCODES WHERE LocationName like $PAR"; SQLiteCommand cmd = new SQLiteCommand(query, _con); cmd.Parameters.AddWithValue("$PAR", city); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { if (!reader.IsDBNull(0) && !reader.IsDBNull(1)) { LocodeDB.LocodeEntry entry = new LocodeDB.LocodeEntry(); entry.Locode = reader.GetString(0); entry.Name = reader.GetString(1); results.Add(entry); } } reader.Close(); return results; } public static List LoadHAZTemplates() { SQLiteCommand cmd = new SQLiteCommand(HAZPosTemplate.GetQuery(), _con); IDataReader reader = cmd.ExecuteReader(); List result = HAZPosTemplate.LoadList(reader); reader.Close(); return result; } public static List> GetNST2007List() { List> result = new List>(); string query = string.Format("SELECT Description, NST2007 FROM LADG_NST2007 ORDER BY Description"); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { if (reader.IsDBNull(0)) continue; string desc = reader.GetString(0); string code = reader.GetString(1); KeyValuePair kvp = new KeyValuePair(desc, code); result.Add(kvp); } reader.Close(); return result; } public static List getMVSHLocodes() { List result = new List(); string query = string.Format("SELECT locode from MVSH_ports"); SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { if (reader.IsDBNull(0)) continue; result.Add(reader.GetString(0)); } reader.Close(); return result; } public static Dictionary> getPortAreaInfos() { int cnt = 0; if (_portAreaInfos == null) { _portAreaInfos = new Dictionary>(); string query = @"SELECT Locode, Agentur, Schiffe, Liegeplatz, Hafengebiet, `Hafengebiet-Code`, Bemerkungen from INFO_PortArea_Helper"; SQLiteCommand cmd = new SQLiteCommand(query, _con); IDataReader reader = cmd.ExecuteReader(); while(reader.Read()) { PortAreaInfo pai = new PortAreaInfo(); if (reader.IsDBNull(0)) continue; pai.Locode = reader.GetString(0); if (!reader.IsDBNull(1)) pai.Agency = reader.GetString(1); if (!reader.IsDBNull(2)) pai.Ships = reader.GetString(2); if (!reader.IsDBNull(3)) pai.Berth = reader.GetString(3); if (!reader.IsDBNull(4)) pai.PortArea = reader.GetString(4); if (!reader.IsDBNull(5)) pai.PortAreaCode = reader.GetString(5); if (!reader.IsDBNull(6)) pai.Remark = reader.GetString(6); if (!_portAreaInfos.ContainsKey(pai.Locode)) { _portAreaInfos[pai.Locode] = new List(); } _portAreaInfos[pai.Locode].Add(pai); cnt++; } } return _portAreaInfos; } } }