224 lines
8.9 KiB
C#
224 lines
8.9 KiB
C#
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);
|
|
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
|
|
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
|
|
string version = fvi.FileVersion;
|
|
_log.InfoFormat("Report Service started. v.{0}", version);
|
|
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<MessageCore> reportCores = DBManager.Instance.GetMessageCoresByReportStatus(MessageCore.ReportStatusEnum.COMPLETE);
|
|
reportCores.AddRange(DBManager.Instance.GetMessageCoresByReportStatus(MessageCore.ReportStatusEnum.HE_REPORTTYPE));
|
|
reportCores.AddRange(DBManager.Instance.GetMessageCoresByReportStatus(MessageCore.ReportStatusEnum.HE_REVISION));
|
|
// create report documents for each of the messages
|
|
foreach (MessageCore reportCore in reportCores)
|
|
{
|
|
this.CreateReport(reportCore);
|
|
}
|
|
|
|
DBManager.Instance.Disconnect();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.ErrorFormat("Exception occurred: {0}", ex.ToString());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.EventLog.WriteEntry("ReportService DB connection failed", EventLogEntryType.Error);
|
|
}
|
|
|
|
lock (this._timerlock)
|
|
{
|
|
this.processRunning = false;
|
|
}
|
|
}
|
|
|
|
protected override void OnStop()
|
|
{
|
|
_log.Info("Report Service stopping.");
|
|
}
|
|
|
|
internal void DoOnce()
|
|
{
|
|
this._timer_Elapsed(null, null);
|
|
}
|
|
|
|
#region create and send report
|
|
|
|
private void CreateReport(MessageCore reportCore)
|
|
{
|
|
List<Message> messages = DBManager.Instance.GetMessagesForCore(reportCore, DBManager.MessageLoad.ALL);
|
|
messages.Sort(new ANSWMessageComparer());
|
|
Dictionary<string, string> coverInfos = new Dictionary<string, string>();
|
|
bool isReportUpdate = reportCore.ReportStatus != MessageCore.ReportStatusEnum.COMPLETE;
|
|
|
|
coverInfos.Add("Type", reportCore.HerbergReportType);
|
|
// 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("E-Mail Ship", reportCore.HerbergEmailContactReportingVessel);
|
|
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);
|
|
}
|
|
|
|
// create document and print header area (with message core data)
|
|
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,
|
|
isReportUpdate);
|
|
|
|
// 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("NEW EU-NOAD message IMO {0}", reportCore.IMO);
|
|
if(isReportUpdate)
|
|
subject = string.Format("UPDATE EU-NOAD message IMO {0}", reportCore.IMO);
|
|
List<string> attachments = new List<string>();
|
|
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, null);
|
|
// reset report status
|
|
reportCore.ReportStatus = MessageCore.ReportStatusEnum.NONE;
|
|
DBManager.Instance.Save(reportCore);
|
|
|
|
if (passengerCSV != null)
|
|
{
|
|
try
|
|
{
|
|
File.Delete(passengerCSV);
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
_log.WarnFormat("Exception trying to delete passenger csv:{0}", ex.Message);
|
|
}
|
|
}
|
|
if (crewCSV != null)
|
|
{
|
|
try
|
|
{
|
|
File.Delete(crewCSV);
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
_log.WarnFormat("Exception trying to delete crew csv:{0}", ex.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|