// // 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 log4net; namespace bsmd.database { public class DBManager { private SqlConnection _con; private static DBManager _instance; private ILog _log = LogManager.GetLogger(typeof(DBManager)); #region Properties public static DBManager Instance { get { if (_instance == null) _instance = new DBManager(); return _instance; } } 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.Error("DBManager cannot connect", ex); return false; } } public void Disconnect() { if ((this._con != null) && (this._con.State == ConnectionState.Open)) this._con.Close(); } #endregion #region public helper funcs public Dictionary GetToSendMessageCoreList() { MessageCore aMessageCore = new MessageCore(); SqlCommand cmd = new SqlCommand(); Message.LoadFilter filter = Message.LoadFilter.BSMDSTATUS; aMessageCore.PrepareLoadCommand(cmd, filter, Message.BSMDStatus.TOSEND); SqlDataReader reader = this.PerformCommand(cmd); List cores = aMessageCore.LoadList(reader); Dictionary result = new Dictionary(); foreach (MessageCore core in cores) result.Add(core.Id.Value, core); return result; } public List GetToSendMessageList() { Message aMessage = new Message(); SqlCommand cmd = new SqlCommand(); Message.LoadFilter filter = Message.LoadFilter.BSMDSTATUS; aMessage.PrepareLoadCommand(cmd, filter, Message.BSMDStatus.TOSEND); SqlDataReader reader = this.PerformCommand(cmd); List messages = aMessage.LoadList(reader); List result = new List(); foreach (Message message in messages) result.Add(message); this.LoadMessageDependencies(result); return result; } public Dictionary GetReportingPartyDict() { ReportingParty aRep = new ReportingParty(); SqlCommand cmd = new SqlCommand(); aRep.PrepareLoadCommand(cmd, Message.LoadFilter.ALL); SqlDataReader reader = this.PerformCommand(cmd); List reportingParties = aRep.LoadList(reader); Dictionary result = new Dictionary(); foreach (ReportingParty rp in reportingParties) result.Add(rp.Id.Value, rp); return result; } #endregion #region internal/private funcs 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); SqlCommand cmd = new SqlCommand(); switch(message.MessageNotificationClass) { case Message.NotificationClass.STAT: STAT stat = new STAT(); stat.PrepareLoadCommand(cmd, Message.LoadFilter.MESSAGEHEADER, message.Id); SqlDataReader reader = this.PerformCommand(cmd); List statList = stat.LoadList(reader); if (statList.Count > 0) message.DerivedMessage = statList[0]; ((STAT)statList[0]).MessageHeader = message; break; default: break; } } } 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 SqlDataReader PerformCommand(SqlCommand cmd) { try { cmd.Connection = this._con; SqlDataReader reader = cmd.ExecuteReader(); return reader; } catch (SqlException ex) { System.Diagnostics.Trace.WriteLine("SQL Exception:" + ex.Message); _log.Error("Error performing command", ex); return null; } } #endregion } }