using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.ServiceProcess; using System.Timers; using log4net; using MigraDoc.DocumentObjectModel; using bsmd.database; using bsmd.email; namespace bsmd.ReportGenerator { public partial class ReportService : ServiceBase { private Timer _timer; private object _timerlock = new object(); private bool processRunning = false; private ILog _log = LogManager.GetLogger(typeof(ReportService)); public ReportService() { Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); InitializeComponent(); } protected override void OnStart(string[] args) { this.EventLog.Source = this.ServiceName; this.EventLog.Log = "Application"; this.Init(args); this.EventLog.WriteEntry("Report Service started.", EventLogEntryType.Information); _log.Info("Report Service started."); this.DoOnce(); } private void Init(string[] args) { this._timer = new Timer(); this._timer.Interval = Properties.Settings.Default.SleepSeconds * 1000; this._timer.Elapsed += _timer_Elapsed; this._timer.Enabled = true; } private void _timer_Elapsed(object sender, ElapsedEventArgs e) { lock (this._timerlock) { if (this.processRunning) return; else this.processRunning = true; } if (DBManager.Instance.Connect(Properties.Settings.Default.ConnectionString)) { try { // load all messages with report flag set List reportCores = DBManager.Instance.GetMessageCoresByReportStatus(MessageCore.ReportStatusEnum.COMPLETE); // create report documents for each of the messages foreach (MessageCore reportCore in reportCores) { List messages = DBManager.Instance.GetMessagesForCore(reportCore); Dictionary coverInfos = new Dictionary(); // Schiffsname aus der STAT meldung fischen foreach (Message msg in messages) { if (msg.MessageNotificationClass == Message.NotificationClass.STAT) { if (msg.Elements.Count > 0) { STAT stat = msg.Elements[0] as STAT; if (stat != null) { coverInfos.Add("Ship", stat.ShipName); } } } } coverInfos.Add("IMO", reportCore.IMO); DateTime eta = reportCore.ETA ?? (reportCore.ETAKielCanal ?? new DateTime(0)); coverInfos.Add("ETA", eta.ToShortDateString()); coverInfos.Add("Port", reportCore.Portname); coverInfos.Add("", ""); if (reportCore.Customer != null) { coverInfos.Add("Name", reportCore.Customer.Name); coverInfos.Add("Contact first name", reportCore.Customer.ContactFirstName); coverInfos.Add("Contact last name", reportCore.Customer.ContactLastName); coverInfos.Add("Customer number", reportCore.Customer.CustomerNumber); coverInfos.Add("Street", reportCore.Customer.StreetAndNumber); coverInfos.Add("Postal code", reportCore.Customer.PostalCode); coverInfos.Add("City", reportCore.Customer.City); coverInfos.Add("Country", reportCore.Customer.Country); coverInfos.Add("Phone", reportCore.Customer.Phone); coverInfos.Add("E-Mail", reportCore.Customer.Email); } Document migraDocument = BSMDDocument.CreateDocument( string.Format("NSW Eingangsdatenübersicht für IMO {0}, ETA {1}", reportCore.IMO, reportCore.ETA), "NSW Meldung", Properties.Settings.Default.ReportAuthor, coverInfos); // print header area (with message core data) // print messages in subsequent tables foreach (Message message in messages) { BSMDDocument.AddNSWMessageParagraph(migraDocument, message); } // prepare and send E-Mail with generated attachment string fullPath = string.Format("{0}\\{1}.pdf", Properties.Settings.Default.OutputDirectory, reportCore.Id); BSMDDocument.RenderDocument(migraDocument, fullPath); _log.InfoFormat("Document created for MessageCoreId {0}, IMO {1}", reportCore.Id, reportCore.IMO); string subject = string.Format("NSW message report for {0}", reportCore.IMO); List attachments = new List(); attachments.Add(fullPath); // 10.7.15: Check PAS/CREW messages to create extra csv files and add them as well Message pas = messages.Find(x => x.MessageNotificationClass == Message.NotificationClass.PAS); string passengerCSV = null; if ((pas != null) && (pas.Elements.Count > 0)) { passengerCSV = CrewPasHelper.CreateCSV(pas); attachments.Add(passengerCSV); } Message crew = messages.Find(x => x.MessageNotificationClass == Message.NotificationClass.CREW); string crewCSV = null; if ((crew != null) && (crew.Elements.Count > 0)) { crewCSV = CrewPasHelper.CreateCSV(crew); attachments.Add(crewCSV); } BSMDMail.SendNSWReportWithAttachments(subject, attachments); // reset report status reportCore.ReportStatus = MessageCore.ReportStatusEnum.NONE; DBManager.Instance.Save(reportCore); if (passengerCSV != null) File.Delete(passengerCSV); if (crewCSV != null) File.Delete(crewCSV); } DBManager.Instance.Disconnect(); } catch (Exception ex) { _log.ErrorFormat("Exception occurred: {0}", ex.ToString()); } } else { this.EventLog.WriteEntry("ReportService stopped: DB connection failed", EventLogEntryType.Error); this.Stop(); } lock (this._timerlock) { this.processRunning = false; } } protected override void OnStop() { _log.Info("Report Service stopping."); } internal void DoOnce() { this._timer_Elapsed(null, null); } } }