// // Class: DBManager // Current CLR: 4.0.30319.34209 // System: Microsoft Visual Studio 10.0 // Author: dani // Created: 3/10/2015 7:25:55 AM // // Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved. using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Diagnostics; using log4net; namespace bsmd.database { public class DBManager { #region Fields private SqlConnection _con; private static DBManager _instance; private static ILog _log = LogManager.GetLogger(typeof(DBManager)); private static Dictionary allReportingParties; private static Dictionary allPortAreas; private object _lock = new object(); private bool _closeConnectionAfterUse = false; #endregion #region Enums public enum MessageLoad { ALL, EXCEL, HE, ENI } #endregion #region Properties public static DBManager Instance { get { if (_instance == null) _instance = new DBManager(); return _instance; } } public static DBManager GetSingleCon(string dbConnectionString) { DBManager aDBManager = new DBManager(); if (aDBManager.Connect(dbConnectionString)) { aDBManager._closeConnectionAfterUse = true; return aDBManager; } return null; } public string ConnectionString { get; set; } #endregion #region public DB funcs public bool Connect(string dbConnectionString) { try { _con = new SqlConnection(dbConnectionString); _con.Open(); return true; } catch (Exception ex) { _log.ErrorFormat("DBManager cannot connect:{0}", ex.Message); return false; } } public void Disconnect() { if ((this._con != null) && (this._con.State == ConnectionState.Open)) this._con.Close(); } public bool IsConnected { get { return (this._con != null) && (this._con.State == ConnectionState.Open); } } #endregion #region public helper funcs public List GetMessageCoresForExcelCreate() { MessageCore aMessageCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.CREATE_EXCEL); SqlDataReader reader = this.PerformCommand(cmd); List cores = aMessageCore.LoadList(reader); List result = new List(); foreach (MessageCore core in cores) { this.LoadCustomer(core); result.Add(core); } if (this._closeConnectionAfterUse) this.Disconnect(); return result; } public List GetMessageCoresByStatus(MessageCore.BSMDStatus status) { MessageCore aMessageCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.BSMDSTATUS, status); SqlDataReader reader = this.PerformCommand(cmd); List cores = aMessageCore.LoadList(reader); List result = new List(); foreach (MessageCore core in cores) { this.LoadCustomer(core); result.Add(core); } if (this._closeConnectionAfterUse) this.Disconnect(); return result; } public List GetMessageCoresByReportStatus(MessageCore.ReportStatusEnum reportStatus) { MessageCore aMessageCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.REPORTSTATUS, reportStatus); SqlDataReader reader = this.PerformCommand(cmd); List cores = aMessageCore.LoadList(reader); List result = new List(); foreach (MessageCore core in cores) { this.LoadCustomer(core); result.Add(core); } if (this._closeConnectionAfterUse) this.Disconnect(); return result; } internal List GetImportHeader(Guid messageCoreId) { ImportHeader ih = new ImportHeader(); SqlCommand cmd = new SqlCommand(); ih.PrepareLoadCommand(cmd, Message.LoadFilter.BY_CORE, messageCoreId); SqlDataReader reader = this.PerformCommand(cmd); List result = new List(); foreach (ImportHeader existingHeader in ih.LoadList(reader)) result.Add(existingHeader); if (this._closeConnectionAfterUse) this.Disconnect(); return result; } internal List GetValuesForImportHeader(ImportHeader importHeader) { ImportValue iv = new ImportValue(); SqlCommand cmd = new SqlCommand(); iv.PrepareLoadCommand(cmd, Message.LoadFilter.IMPORTHEADER_ID, importHeader.Id.Value); SqlDataReader reader = this.PerformCommand(cmd); List result = new List(); foreach (ImportValue existingValue in iv.LoadList(reader)) result.Add(existingValue); if (this._closeConnectionAfterUse) this.Disconnect(); return result; } public List GetMessageCoresWithNSWStatusFlag() { MessageCore aMessageCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.QUERY_NSW_STATUS); SqlDataReader reader = this.PerformCommand(cmd); List cores = aMessageCore.LoadList(reader); List result = new List(); foreach (MessageCore core in cores) { this.LoadCustomer(core); result.Add(core); } if (this._closeConnectionAfterUse) this.Disconnect(); return result; } public int? GetNumCoresWithFilters(Dictionary filters) { MessageCore aMessageCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); aMessageCore.PrepareCountCmd(cmd, Message.LoadFilter.SEARCH_CORE_FILTERS, filters); int? result = this.PerformReadIntQuery(cmd); if (this._closeConnectionAfterUse) this.Disconnect(); return result; } public List GetMessageCoresWithFilters(Dictionary filters, int? limit = null) { MessageCore aMessageCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); if (limit.HasValue && (limit.Value > 0)) aMessageCore.ResultLimit = limit; aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.SEARCH_CORE_FILTERS, filters); SqlDataReader reader = this.PerformCommand(cmd); List cores = aMessageCore.LoadList(reader); List result = new List(); foreach (MessageCore core in cores) { this.LoadCustomer(core); this.LoadSTATShipName(core); result.Add(core); } if (this._closeConnectionAfterUse) this.Disconnect(); return result; } /// /// Lädt MessageCore (=Schiffsanlauf) einer EU-NOAD Nachricht vom Fleettracker / Herberg /// /// /// public MessageCore GetHerbergFormMessage(Guid herbergFormGuid) { MessageCore result = null; MessageCore aMessageCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); aMessageCore.PrepareLoadCommand(cmd, Message.LoadFilter.HERBERG_FORMGUID, herbergFormGuid); SqlDataReader reader = this.PerformCommand(cmd); List cores = aMessageCore.LoadList(reader); if (cores.Count > 1) _log.WarnFormat("Herberg form message: {0} cores found for guid {1}", cores.Count, herbergFormGuid); if (cores.Count > 0) { result = (MessageCore)cores[0]; this.LoadCustomer(result); } if (this._closeConnectionAfterUse) this.Disconnect(); return result; } public List GetMessagesForCore(MessageCore core, MessageLoad loadType) { Message aMessage = new Message(); SqlCommand cmd = new SqlCommand(); Message.LoadFilter loadFilter = Message.LoadFilter.BY_CORE; switch(loadType) { case MessageLoad.ENI: loadFilter = Message.LoadFilter.BY_CORE_ENI; break; case MessageLoad.EXCEL: loadFilter = Message.LoadFilter.BY_CORE_EXCEL; break; case MessageLoad.HE: loadFilter = Message.LoadFilter.BY_CORE_HE; break; } aMessage.PrepareLoadCommand(cmd, loadFilter, core.Id); IDataReader reader = this.PerformCommand(cmd); List messages = aMessage.LoadList(reader); List messageList = new List(); foreach (Message message in messages) { message.MessageCore = core; messageList.Add(message); } this.LoadMessageDependencies(messageList); if (this._closeConnectionAfterUse) this.Disconnect(); return messageList; } public Message GetMessage(MessageCore core, Message.NotificationClass notificationClass) { Message aMessage = new Message(); SqlCommand cmd = new SqlCommand(); aMessage.PrepareLoadCommand(cmd, Message.LoadFilter.BY_CORE_AND_CLASS, core.Id, notificationClass); IDataReader reader = this.PerformCommand(cmd); List messages = aMessage.LoadList(reader); Message result = null; if(!messages.IsNullOrEmpty()) { result = (Message) messages[0]; // es kann nur eine sein result.MessageCore = core; // TODO: abhängige Listen laden? } if (this._closeConnectionAfterUse) this.Disconnect(); return result; } public string GetShipNameFromCore(MessageCore core) { foreach(Message message in this.GetMessagesForCore(core, MessageLoad.ALL)) { if (message.MessageNotificationClass == Message.NotificationClass.STAT) { if (message.Elements.Count > 0) { STAT stat = message.Elements[0] as STAT; if (stat != null) return stat.ShipName; } break; } } return null; } public Dictionary GetReportingPartyDict() { if (DBManager.allReportingParties == null) { ReportingParty aRep = new ReportingParty(); SqlCommand cmd = new SqlCommand(); aRep.PrepareLoadCommand(cmd, Message.LoadFilter.ALL); SqlDataReader reader = this.PerformCommand(cmd); List reportingParties = aRep.LoadList(reader); DBManager.allReportingParties = new Dictionary(); foreach (ReportingParty rp in reportingParties) DBManager.allReportingParties.Add(rp.Id.Value, rp); } return DBManager.allReportingParties; } public List GetValidationRules() { ValidationRule vr = new ValidationRule(); SqlCommand cmd = new SqlCommand(); vr.PrepareLoadCommand(cmd, Message.LoadFilter.ALL); SqlDataReader reader = this.PerformCommand(cmd); List allRules = vr.LoadList(reader); List result = new List(); foreach (ValidationRule aVR in allRules) result.Add(aVR); return result; } public Dictionary GetPortAreaDict() { if(DBManager.allPortAreas == null) { PortArea pa = new PortArea(); SqlCommand cmd = new SqlCommand(); pa.PrepareLoadCommand(cmd, Message.LoadFilter.ALL); SqlDataReader reader = this.PerformCommand(cmd); List portAreas = pa.LoadList(reader); DBManager.allPortAreas = new Dictionary(); foreach (PortArea aPa in portAreas) DBManager.allPortAreas.Add(aPa.Locode + aPa.Code, aPa); } return DBManager.allPortAreas; } public DatabaseEntity GetMessageById(Guid id) { Message aMessage = new Message(); SqlCommand cmd = new SqlCommand(); Message.LoadFilter filter = Message.LoadFilter.BY_ID; aMessage.PrepareLoadCommand(cmd, filter, id); SqlDataReader reader = this.PerformCommand(cmd); List messages = aMessage.LoadList(reader); List messageList = new List(); foreach (Message message in messages) messageList.Add(message); if (messageList.Count == 0) return null; this.LoadMessageDependencies(messageList); if(messageList[0].MessageCoreId.HasValue) messageList[0].MessageCore = this.GetMessageCoreById(messageList[0].MessageCoreId.Value); return messageList[0]; } public MessageCore GetMessageCoreById(Guid id) { MessageCore aCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); Message.LoadFilter filter = Message.LoadFilter.BY_ID; aCore.PrepareLoadCommand(cmd, filter, id); SqlDataReader reader = this.PerformCommand(cmd); List cores = aCore.LoadList(reader); MessageCore result = null; if (cores.Count > 0) { result = cores[0] as MessageCore; if (result.CustomerId.HasValue) { Customer c = new Customer(); SqlCommand cCmd = new SqlCommand(); c.PrepareLoadCommand(cCmd, Message.LoadFilter.BY_ID, result.CustomerId.Value); reader = this.PerformCommand(cCmd); List customers = c.LoadList(reader); if (customers.Count > 0) result.Customer = customers[0] as Customer; } } if (this._closeConnectionAfterUse) this.Disconnect(); return result; } public MessageCore GetMessageCoreByVisitId(string visitId) { MessageCore aCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); Message.LoadFilter filter = Message.LoadFilter.BY_VISITID; aCore.PrepareLoadCommand(cmd, filter, visitId); SqlDataReader reader = this.PerformCommand(cmd); List cores = aCore.LoadList(reader); MessageCore result = null; if (cores.Count > 0) { if (cores.Count > 1) _log.WarnFormat("more than one core in DB for VISIT ID {0}", visitId); result = cores[0] as MessageCore; if (result.CustomerId.HasValue) { Customer c = new Customer(); SqlCommand cCmd = new SqlCommand(); c.PrepareLoadCommand(cCmd, Message.LoadFilter.BY_ID, result.CustomerId.Value); reader = this.PerformCommand(cCmd); List customers = c.LoadList(reader); if (customers.Count > 0) result.Customer = customers[0] as Customer; } } return result; } public MessageCore GetMessageCoreByTransitId(string transitId) { MessageCore aCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); Message.LoadFilter filter = Message.LoadFilter.BY_TRANSITID; aCore.PrepareLoadCommand(cmd, filter, transitId); SqlDataReader reader = this.PerformCommand(cmd); List cores = aCore.LoadList(reader); MessageCore result = null; if (cores.Count > 0) { if (cores.Count > 1) _log.WarnFormat("more than one core in DB for TRANSIT ID {0}", transitId); result = cores[0] as MessageCore; if (result.CustomerId.HasValue) { Customer c = new Customer(); SqlCommand cCmd = new SqlCommand(); c.PrepareLoadCommand(cCmd, Message.LoadFilter.BY_ID, result.CustomerId.Value); reader = this.PerformCommand(cCmd); List customers = c.LoadList(reader); if (customers.Count > 0) result.Customer = customers[0] as Customer; } } return result; } public MessageCore GetMessageCoreByShipInfos(string imo, DateTime eta, string poc) { MessageCore aCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); Message.LoadFilter filter = Message.LoadFilter.IMO_ETA_POC; aCore.PrepareLoadCommand(cmd, filter, imo, eta, poc); SqlDataReader reader = this.PerformCommand(cmd); List cores = aCore.LoadList(reader); MessageCore result = null; if (cores.Count > 0) { if (cores.Count > 1) _log.WarnFormat("Multiple ({3}) cores found for IMO {0}, ETA {1}, PoC {2}", imo, eta, poc, cores.Count); result = cores[0] as MessageCore; } return result; } public void DeleteCore(MessageCore messageCore) { if(messageCore == null) return; List coreMessages = this.GetMessagesForCore(messageCore, MessageLoad.ALL); foreach (Message message in coreMessages) { if (message is ISublistContainer) { ((ISublistContainer)message).DeleteElements(); } this.Delete(message); } this.Delete(messageCore); } public void Save(DatabaseEntity entity) { List truncatedFields = new List(); entity.TruncateFields(truncatedFields); foreach(string truncatedField in truncatedFields) { _log.WarnFormat("Entity {0} save: Field truncated: {1}", entity.GetType(), truncatedField); } SqlCommand cmd = new SqlCommand(); entity.PrepareSave(cmd); int queryResult = this.PerformNonQuery(cmd); this.LogNonQueryResult(cmd.CommandText, queryResult); if (this._closeConnectionAfterUse) this.Disconnect(); } public void Delete(DatabaseEntity entity) { if (!entity.IsNew) { SqlCommand cmd = new SqlCommand(); entity.PrepareDelete(cmd); int queryResult = this.PerformNonQuery(cmd); this.LogNonQueryResult(cmd.CommandText, queryResult); } if (this._closeConnectionAfterUse) this.Disconnect(); } public void DeleteMessageErrors(Message message) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = string.Format("UPDATE Error SET Deleted = 1 WHERE MessageHeaderId='{0}'", message.Id); int queryResult = this.PerformNonQuery(cmd); this.LogNonQueryResult(cmd.CommandText, queryResult); } public void DeleteMessageViolations(Message message) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = string.Format("UPDATE Violation SET Deleted = 1 WHERE MessageHeaderId='{0}'", message.Id); int queryResult = this.PerformNonQuery(cmd); this.LogNonQueryResult(cmd.CommandText, queryResult); } public void DeleteSystemErrors(Message message) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = string.Format("UPDATE SystemError SET Deleted = 1 WHERE MessageHeaderId='{0}'", message.Id); int queryResult = this.PerformNonQuery(cmd); this.LogNonQueryResult(cmd.CommandText, queryResult); } public void Save(MessageCore core) { if (core.Customer != null) { SqlCommand cCmd = new SqlCommand(); core.Customer.PrepareSave(cCmd); _log.DebugFormat("Saved Customer to Core: {0}", this.PerformNonQuery(cCmd)); core.CustomerId = core.Customer.Id; } SqlCommand cmd = new SqlCommand(); core.PrepareSave(cmd); int queryResult = this.PerformNonQuery(cmd); this.LogNonQueryResult(cmd.CommandText, queryResult); if (this._closeConnectionAfterUse) this.Disconnect(); } public bool? GetMessageCoreQueryStatusFlag(Guid messageCoreId) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = string.Format("SELECT QueryNSWStatus FROM MessageCore WHERE Id = '{0}'", messageCoreId); bool? result = this.PerformReadFlagQuery(cmd); if (this._closeConnectionAfterUse) this.Disconnect(); return result; } #endregion #region internal/private funcs private void CheckConnection() { if ((this._con == null) || (this._con.State == ConnectionState.Closed)) this.Connect(this.ConnectionString); } private void LogNonQueryResult(string query, int queryResult) { switch (queryResult) { case 0: _log.DebugFormat("Query {0} affected no rows", query); break; case -1: _log.DebugFormat("Query {0} error occurred", query); break; default: _log.DebugFormat("Query {0} affected {1} rows", query, queryResult); break; } } internal void LoadMessageDependencies(List messageList) { // Dictionary messageCoreDict = this.GetToSendMessageCoreList(); Dictionary reportingPartyDict = this.GetReportingPartyDict(); // Zuordnung MessageCore,Zuordnung Reporting party Message.AssignReportingParties(messageList, reportingPartyDict); // Message.AssignMessageCores(messageList, messageCoreDict); foreach (Message message in messageList) { this.LoadErrorList(message); this.LoadViolationList(message); this.LoadSystemErrorList(message); SqlCommand cmd = new SqlCommand(); DatabaseEntity msgClass = DBManager.CreateMessage(message.MessageNotificationClass); if (msgClass != null) { msgClass.PrepareLoadCommand(cmd, Message.LoadFilter.MESSAGEHEADER, message.Id); SqlDataReader reader = this.PerformCommand(cmd); if (reader != null) { List statList = msgClass.LoadList(reader); foreach (DatabaseEntity derivedMessage in statList) { if(message.MessageNotificationClass == Message.NotificationClass.HAZD) { if(derivedMessage is HAZ) ((HAZ)derivedMessage).IsDeparture = true; } message.Elements.Add(derivedMessage); derivedMessage.MessageHeader = message; this.LoadDependingLists(derivedMessage); } } } /* Meldung ist überflüssig else { _log.DebugFormat("cannot create a message class for notification type {0}", message.MessageNotificationClass); } */ } } #region CreateMessage() /// /// factory method for messages by type /// internal static DatabaseEntity CreateMessage(Message.NotificationClass notificationClass) { DatabaseEntity result = null; switch (notificationClass) { case Message.NotificationClass.NOA_NOD: result = new NOA_NOD(); break; case Message.NotificationClass.ATA: result = new ATA(); break; case Message.NotificationClass.ATD: result = new ATD(); break; case Message.NotificationClass.SEC: result = new SEC(); break; case Message.NotificationClass.POBA: result = new POBA(); break; case Message.NotificationClass.POBD: result = new POBD(); break; case Message.NotificationClass.NAME: result = new NAME(); break; case Message.NotificationClass.TIEFA: result = new TIEFA(); break; case Message.NotificationClass.TIEFD: result = new TIEFD(); break; case Message.NotificationClass.BKRA: result = new BRKA(); break; case Message.NotificationClass.BKRD: result = new BRKD(); break; case Message.NotificationClass.STAT: result = new STAT(); break; case Message.NotificationClass.LADG: result = new LADG(); break; case Message.NotificationClass.INFO: result = new INFO(); break; case Message.NotificationClass.SERV: result = new SERV(); break; case Message.NotificationClass.PRE72H: result = new PRE72H(); break; case Message.NotificationClass.MDH: result = new MDH(); break; case Message.NotificationClass.WAS: result = new WAS(); break; case Message.NotificationClass.CREW: result = new CREW(); break; case Message.NotificationClass.PAS: result = new PAS(); break; case Message.NotificationClass.BPOL: result = new BPOL(); break; case Message.NotificationClass.TOWA: result = new TOWA(); break; case Message.NotificationClass.TOWD: result = new TOWD(); break; case Message.NotificationClass.HAZA: result = new HAZ(); break; case Message.NotificationClass.HAZD: result = new HAZ(); ((HAZ)result).IsDeparture = true; break; case Message.NotificationClass.AGNT: result = new AGNT(); break; case Message.NotificationClass.STO: result = new STO(); break; case Message.NotificationClass.CREWD: result = new CREWD(); break; case Message.NotificationClass.PASD: result = new PASD(); break; default: break; // VISIT, TRANSIT } return result; } #endregion /// /// Loads inner lists / collections /// internal void LoadDependingLists(DatabaseEntity databaseEntity) { SqlCommand cmd = new SqlCommand(); #region BPOL if (databaseEntity.GetType().IsAssignableFrom(typeof(BPOL))) { BPOL bpol = databaseEntity as BPOL; PortOfItinerary poi = new PortOfItinerary(); poi.PrepareLoadCommand(cmd, Message.LoadFilter.BPOL_ID, bpol.Id); SqlDataReader reader = this.PerformCommand(cmd); List pois = poi.LoadList(reader); foreach (PortOfItinerary aPoi in pois) { bpol.PortOfItineraries.Add(aPoi); aPoi.BPOL = bpol; } } #endregion #region HAZ if(databaseEntity.GetType().IsAssignableFrom(typeof(HAZ))) { HAZ haz = databaseEntity as HAZ; // load all 5 position sublists IMDGPosition imdg = new IMDGPosition(); imdg.PrepareLoadCommand(cmd, Message.LoadFilter.HAZ_ID, haz.Id); SqlDataReader reader = this.PerformCommand(cmd); List imdgs = imdg.LoadList(reader); foreach (IMDGPosition imdgPosition in imdgs) { haz.IMDGPositions.Add(imdgPosition); imdgPosition.HAZ = haz; // load SubsidiaryRisk sub-table SqlCommand srCmd = new SqlCommand(); SubsidiaryRisks subsidiaryRisks = new SubsidiaryRisks(); subsidiaryRisks.PrepareLoadCommand(srCmd, Message.LoadFilter.IMDG_ID, imdgPosition.Id); SqlDataReader srReader = this.PerformCommand(srCmd); List sRisks = subsidiaryRisks.LoadList(srReader); foreach (SubsidiaryRisks subsidiaryRisk in sRisks) { imdgPosition.SubsidiaryRiskList.Add(subsidiaryRisk); subsidiaryRisk.IMDGPosition = imdgPosition; } } cmd = new SqlCommand(); IBCPosition ibc = new IBCPosition(); ibc.PrepareLoadCommand(cmd, Message.LoadFilter.HAZ_ID, haz.Id); reader = this.PerformCommand(cmd); List ibcs = ibc.LoadList(reader); foreach (IBCPosition ibcPosition in ibcs) { haz.IBCPositions.Add(ibcPosition); ibcPosition.HAZ = haz; } cmd = new SqlCommand(); IGCPosition igc = new IGCPosition(); igc.PrepareLoadCommand(cmd, Message.LoadFilter.HAZ_ID, haz.Id); reader = this.PerformCommand(cmd); List igcs = igc.LoadList(reader); foreach (IGCPosition igcPosition in igcs) { haz.IGCPositions.Add(igcPosition); igcPosition.HAZ = haz; } cmd = new SqlCommand(); IMSBCPosition imsbc = new IMSBCPosition(); imsbc.PrepareLoadCommand(cmd, Message.LoadFilter.HAZ_ID, haz.Id); reader = this.PerformCommand(cmd); List imsbcs = imsbc.LoadList(reader); foreach (IMSBCPosition imsbcPosition in imsbcs) { haz.IMSBCPositions.Add(imsbcPosition); imsbcPosition.HAZ = haz; } cmd = new SqlCommand(); MARPOL_Annex_I_Position marpol = new MARPOL_Annex_I_Position(); marpol.PrepareLoadCommand(cmd, Message.LoadFilter.HAZ_ID, haz.Id); reader = this.PerformCommand(cmd); List marpols = marpol.LoadList(reader); foreach (MARPOL_Annex_I_Position marpolPosition in marpols) { haz.MARPOLPositions.Add(marpolPosition); marpolPosition.HAZ = haz; } } #endregion #region MDH if (databaseEntity.GetType().IsAssignableFrom(typeof(MDH))) { MDH mdh = databaseEntity as MDH; PortOfCallLast30Days poc30 = new PortOfCallLast30Days(); poc30.PrepareLoadCommand(cmd, Message.LoadFilter.MDH_ID, mdh.Id); SqlDataReader reader = this.PerformCommand(cmd); List poc30s = poc30.LoadList(reader); foreach (PortOfCallLast30Days apoc30 in poc30s) { mdh.PortOfCallLast30Days.Add(apoc30); apoc30.MDH = mdh; this.LoadDependingLists(apoc30); } SanitaryMeasuresDetail smd = new SanitaryMeasuresDetail(); smd.PrepareLoadCommand(cmd, Message.LoadFilter.MDH_ID, mdh.Id); reader = this.PerformCommand(cmd); List smds = smd.LoadList(reader); foreach(SanitaryMeasuresDetail aSmd in smds) { mdh.SanitaryMeasuresDetails.Add(aSmd); aSmd.MDH = mdh; } StowawaysJoiningLocation sjl = new StowawaysJoiningLocation(); sjl.PrepareLoadCommand(cmd, Message.LoadFilter.MDH_ID, mdh.Id); reader = this.PerformCommand(cmd); List sjls = sjl.LoadList(reader); foreach(StowawaysJoiningLocation aSjl in sjls) { mdh.StowawaysJoiningLocations.Add(aSjl); aSjl.MDH = mdh; } InfectedArea ia = new InfectedArea(); ia.PrepareLoadCommand(cmd, Message.LoadFilter.MDH_ID, mdh.Id); reader = this.PerformCommand(cmd); List ias = ia.LoadList(reader); foreach(InfectedArea aIa in ias) { mdh.InfectedAreas.Add(aIa); aIa.MDH = mdh; } } #endregion #region PortOfCallLast30Days if (databaseEntity.GetType().IsAssignableFrom(typeof(PortOfCallLast30Days))) { PortOfCallLast30Days poc30 = databaseEntity as PortOfCallLast30Days; PortOfCallLast30DaysCrewJoinedShip poc30s = new PortOfCallLast30DaysCrewJoinedShip(); poc30s.PrepareLoadCommand(cmd, Message.LoadFilter.POC30_ID, poc30.Id); SqlDataReader reader = this.PerformCommand(cmd); List poc30Names = poc30s.LoadList(reader); foreach (PortOfCallLast30DaysCrewJoinedShip poc30Name in poc30Names) { poc30.CrewJoinedShip.Add(poc30Name); poc30Name.PortOfCallLast30Days = poc30; } } #endregion #region SEC if (databaseEntity.GetType().IsAssignableFrom(typeof(SEC))) { SEC sec = databaseEntity as SEC; LastTenPortFacilitiesCalled ltp = new LastTenPortFacilitiesCalled(); ltp.PrepareLoadCommand(cmd, Message.LoadFilter.SEC_ID, sec.Id); SqlDataReader reader = this.PerformCommand(cmd); List ltps = ltp.LoadList(reader); foreach (LastTenPortFacilitiesCalled altp in ltps) { sec.LastTenPortFacilitesCalled.Add(altp); altp.SEC = sec; } cmd = new SqlCommand(); ShipToShipActivitiesDuringLastTenPortFacilitiesCalled sts = new ShipToShipActivitiesDuringLastTenPortFacilitiesCalled(); sts.PrepareLoadCommand(cmd, Message.LoadFilter.SEC_ID, sec.Id); reader = this.PerformCommand(cmd); List stss = sts.LoadList(reader); foreach (ShipToShipActivitiesDuringLastTenPortFacilitiesCalled asts in stss) { sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Add(asts); asts.SEC = sec; } } #endregion #region WAS if(databaseEntity.GetType().IsAssignableFrom(typeof(WAS))) { WAS was = databaseEntity as WAS; WasteDisposalServiceProvider wdsp = new WasteDisposalServiceProvider(); wdsp.PrepareLoadCommand(cmd, Message.LoadFilter.WAS_ID, was.Id); SqlDataReader reader = this.PerformCommand(cmd); List wdsps = wdsp.LoadList(reader); foreach (WasteDisposalServiceProvider awdsp in wdsps) { was.WasteDisposalServiceProvider.Add(awdsp); awdsp.WAS = was; this.LoadDependingLists(awdsp); } Waste waste = new Waste(); waste.PrepareLoadCommand(cmd, Message.LoadFilter.WAS_ID, was.Id); SqlDataReader reader2 = this.PerformCommand(cmd); List wastes = waste.LoadList(reader2); foreach (Waste aWaste in wastes) { was.Waste.Add(aWaste); aWaste.WAS = was; } } #endregion #region NOA_NOD if (databaseEntity.GetType().IsAssignableFrom(typeof(NOA_NOD))) { NOA_NOD noa_nod = databaseEntity as NOA_NOD; CallPurpose cp = new CallPurpose(); cp.PrepareLoadCommand(cmd, Message.LoadFilter.NOA_NODID, noa_nod.Id); SqlDataReader reader = this.PerformCommand(cmd); List cps = cp.LoadList(reader); foreach (CallPurpose callPurpose in cps) { noa_nod.CallPurposes.Add(callPurpose); callPurpose.NOA_NOD = noa_nod; } } #endregion #region WasteDisposalServiceProvider if (databaseEntity.GetType().IsAssignableFrom(typeof(WasteDisposalServiceProvider))) { WasteDisposalServiceProvider wdsp = databaseEntity as WasteDisposalServiceProvider; } #endregion } internal void LoadErrorList(Message message) { MessageError aMessageError = new MessageError(); SqlCommand cmd = new SqlCommand(); aMessageError.PrepareLoadCommand(cmd, Message.LoadFilter.MESSAGEHEADER, message.Id); SqlDataReader reader = this.PerformCommand(cmd); List errorList = aMessageError.LoadList(reader); foreach (MessageError error in errorList) message.ErrorList.Add(error); } internal void LoadViolationList(Message message) { MessageViolation aMessageViolation = new MessageViolation(); SqlCommand cmd = new SqlCommand(); aMessageViolation.PrepareLoadCommand(cmd, Message.LoadFilter.MESSAGEHEADER, message.Id); SqlDataReader reader = this.PerformCommand(cmd); List violationList = aMessageViolation.LoadList(reader); foreach (MessageViolation violation in violationList) message.ViolationList.Add(violation); } internal void LoadSystemErrorList(Message message) { SystemError aSystemError = new SystemError(); SqlCommand cmd = new SqlCommand(); aSystemError.PrepareLoadCommand(cmd, Message.LoadFilter.MESSAGEHEADER, message.Id); SqlDataReader reader = this.PerformCommand(cmd); List systemErrorList = aSystemError.LoadList(reader); foreach (SystemError sError in systemErrorList) message.SystemErrorList.Add(sError); } internal void LoadCustomer(MessageCore core) { if (core.CustomerId.HasValue) { Customer c = new Customer(); SqlCommand cCmd = new SqlCommand(); c.PrepareLoadCommand(cCmd, Message.LoadFilter.BY_ID, core.CustomerId.Value); SqlDataReader reader = this.PerformCommand(cCmd); List customers = c.LoadList(reader); if (customers.Count > 0) core.Customer = customers[0] as Customer; } } internal void LoadSTATShipName(MessageCore core) { if (core == null) return; SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT STAT.ShipName FROM STAT JOIN MessageHeader ON STAT.MessageHeaderId = MessageHeader.Id WHERE MessageHeader.MessageCoreId = @ID"; cmd.Parameters.AddWithValue("@ID", core.Id); SqlDataReader reader = this.PerformCommand(cmd); if(reader.Read()) { if (reader.IsDBNull(0)) core.Shipname = ""; else core.Shipname = reader.GetString(0); } reader.Close(); } internal Dictionary LoadErrorTexts() { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT ErrorCode, ErrorText FROM ErrorText"; SqlDataReader reader = this.PerformCommand(cmd); Dictionary result = new Dictionary(); while (reader.Read()) { int errorCode = reader.GetInt32(0); string errorText = reader.GetString(1); result[errorCode] = errorText; } reader.Close(); return result; } internal Dictionary LoadViolationTexts() { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT ViolationCode, ViolationText FROM ViolationText"; SqlDataReader reader = this.PerformCommand(cmd); Dictionary result = new Dictionary(); while (reader.Read()) { int violationCode = reader.GetInt32(0); string violationText = reader.GetString(1); result[violationCode] = violationText; } reader.Close(); return result; } #region DB access methods internal SqlDataReader PerformCommand(SqlCommand cmd) { SqlDataReader reader = null; lock (this._lock) { try { this.CheckConnection(); cmd.Connection = this._con; // Stopwatch sw = new Stopwatch(); // sw.Start(); reader = cmd.ExecuteReader(); // sw.Stop(); // _log.DebugFormat("{1}ms: {0}", cmd.CommandText, sw.ElapsedMilliseconds); } catch (SqlException ex) { Trace.WriteLine("SQL Exception:" + ex.Message); _log.Error("Error performing command", ex); _log.DebugFormat("Query: {0}", cmd.CommandText); _log.Debug("Parameters:"); for (int i = 0; i < cmd.Parameters.Count; i++) { _log.DebugFormat("{0}:{1}", cmd.Parameters[i].ParameterName, cmd.Parameters[i].Value); } } } return reader; } internal int PerformNonQuery(SqlCommand cmd) { int result = -1; lock (this._lock) { try { this.CheckConnection(); cmd.Connection = this._con; result = cmd.ExecuteNonQuery(); } catch (SqlException ex) { System.Diagnostics.Trace.WriteLine("SQL Exception:" + ex.Message); _log.Error("Error performing command", ex); _log.DebugFormat("Query: {0}", cmd.CommandText); _log.Debug("Parameters:"); for (int i = 0; i < cmd.Parameters.Count; i++) { _log.DebugFormat("{0}:{1}", cmd.Parameters[i].ParameterName, cmd.Parameters[i].Value); } } } return result; } internal bool? PerformReadFlagQuery(SqlCommand cmd) { bool? result = null; lock (this._lock) { try { this.CheckConnection(); cmd.Connection = this._con; result = (bool?)cmd.ExecuteScalar(); } catch (SqlException ex) { _log.Error("Error performing command", ex); _log.DebugFormat("Query: {0}", cmd.CommandText); _log.Debug("Parameters:"); for (int i = 0; i < cmd.Parameters.Count; i++) { _log.DebugFormat("{0}:{1}", cmd.Parameters[i].ParameterName, cmd.Parameters[i].Value); } } } return result; } internal int? PerformReadIntQuery(SqlCommand cmd) { int? result = null; lock (this._lock) { try { this.CheckConnection(); cmd.Connection = this._con; result = (int?)cmd.ExecuteScalar(); } catch (SqlException ex) { _log.Error("Error performing command", ex); _log.DebugFormat("Query: {0}", cmd.CommandText); _log.Debug("Parameters:"); for (int i = 0; i < cmd.Parameters.Count; i++) { _log.DebugFormat("{0}:{1}", cmd.Parameters[i].ParameterName, cmd.Parameters[i].Value); } } } return result; } public void PerformBulkInsert(DataTable table) { lock (this._lock) { this.CheckConnection(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(this._con, SqlBulkCopyOptions.UseInternalTransaction | SqlBulkCopyOptions.TableLock, null)) { bulkCopy.BulkCopyTimeout = 180; foreach (DataColumn dataColumn in table.Columns) { bulkCopy.ColumnMappings.Add(dataColumn.ColumnName, dataColumn.ColumnName); } bulkCopy.BatchSize = table.Rows.Count; bulkCopy.DestinationTableName = table.TableName; bulkCopy.WriteToServer(table); } } } #endregion #endregion } }