using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using bsmd.database; using log4net; using bsmd.dbh.Request; namespace bsmd.dbh { internal static class RequestUtil { private static readonly ILog _log = LogManager.GetLogger(typeof(RequestUtil)); public static string CreateMessageFile(MessageCore core, Message message) { // create output structure try { Root root = new Root(); root.Version = RootVersion.Item70; root.SourceDocumentVersion = "7.0"; root.Timestamp = DateTime.Now; root.Sender = Properties.Settings.Default.Sender; root.SenderReference = "N"; bsmd.database.ReportingParty reportingParty = DBManager.Instance.GetReportingPartyDict()[core.DefaultReportingPartyId.Value]; Request.ReportingParty rp = new Request.ReportingParty(); rp.RPCity = reportingParty.City; rp.RPCountry = reportingParty.Country; rp.RPEMail = reportingParty.EMail; rp.RPFax = reportingParty.Fax; rp.RPFirstName = reportingParty.FirstName; rp.RPLastName = reportingParty.LastName; rp.RPName = reportingParty.Name; rp.RPPhone = reportingParty.Phone; rp.RPPostalCode = reportingParty.PostalCode; rp.RPStreetAndNumber = reportingParty.StreetAndNumber; rp.RPTypeSpecified = reportingParty.ReportingPartyType.HasValue; if (rp.RPTypeSpecified) rp.RPType = (ReportingPartyRPType)reportingParty.ReportingPartyType.Value; root.ReportingParty = rp; switch (message.MessageNotificationClass) { case Message.NotificationClass.VISIT: root.Type = RootType.VISIT; RootVisit rootVisit = new RootVisit(); rootVisit.PortOfCall = core.PoC; if (core.ETA.HasValue) rootVisit.ETAPortOfCall = core.ETA.Value; rootVisit.ETAPortOfCall = core.ETA.Value; if(!core.IMO.IsNullOrEmpty()) { rootVisit.ItemElementName = ItemChoiceType.IMONumber; rootVisit.Item = core.IMO; } else { rootVisit.ItemElementName = ItemChoiceType.ENINumber; rootVisit.Item = core.ENI; } root.Item = rootVisit; root.ItemElementName = ItemChoiceType2.Visit; break; case Message.NotificationClass.TRANSIT: root.Type = RootType.TRANSIT; RootTransit rootTransit = new RootTransit(); if (!core.IMO.IsNullOrEmpty()) { rootTransit.ItemElementName = ItemChoiceType1.IMONumber; rootTransit.Item = core.IMO; } else { rootTransit.ItemElementName = ItemChoiceType1.ENINumber; rootTransit.Item = core.ENI; } if (core.ETAKielCanal.HasValue) rootTransit.ETAKielCanal = core.ETAKielCanal.Value; root.Item = rootTransit; root.ItemElementName = ItemChoiceType2.Transit; break; case Message.NotificationClass.STAT: root.Type = RootType.DATA; RootSTAT rootStat = new RootSTAT(); STAT stat = message.Elements[0] as STAT; rootStat.ShipName = stat.ShipName; rootStat.CallSign = stat.CallSign; rootStat.MMSINumber = stat.MMSINumber; rootStat.Flag = stat.Flag; if (stat.LengthOverall_MTR.HasValue) rootStat.LengthOverall_MTR = Decimal.Round((decimal)(stat.LengthOverall_MTR.Value), 2); if (stat.Beam_MTR.HasValue) rootStat.Beam_MTR = Decimal.Round((decimal)(stat.Beam_MTR.Value), 2); if (stat.GrossTonnage.HasValue) rootStat.GrossTonnage = stat.GrossTonnage.Value; rootStat.PortOfRegistry = stat.PortOfRegistry; rootStat.ShipType = stat.ShipType; if (!stat.InmarsatCallNumber.IsNullOrEmpty()) { rootStat.InmarsatCallNumbers = stat.InmarsatCallNumber.Trim().Split('\n'); for (int i = 0; i < Math.Min(rootStat.InmarsatCallNumbers.Length, 5); i++) rootStat.InmarsatCallNumbers[i] = rootStat.InmarsatCallNumbers[i].Trim(); } rootStat.TransportMode = RootSTATTransportMode.Item1; // default is maritime transport! if (!stat.TransportMode.IsNullOrEmpty() && stat.TransportMode.Equals("8")) rootStat.TransportMode = RootSTATTransportMode.Item8; if (!stat.ISMCompanyName.IsNullOrEmpty()) { rootStat.ISMCompany = new RootSTATISMCompany(); rootStat.ISMCompany.ISMCompanyName = stat.ISMCompanyName; rootStat.ISMCompany.ISMCompanyId = stat.ISMCompanyId; rootStat.ISMCompany.ISMCompanyStreetAndNumber = stat.ISMCompanyStreetAndNumber; rootStat.ISMCompany.ISMCompanyPostalCode = stat.ISMCompanyPostalCode; rootStat.ISMCompany.ISMCompanyCity = stat.ISMCompanyCity; rootStat.ISMCompany.ISMCompanyCountry = stat.ISMCompanyCountry; } root.Items = new object[1]; root.Items[0] = rootStat; break; default: _log.WarnFormat("Message type {0} not (yet) supported for dbh", message.MessageNotificationClassDisplay); break; } // serialize output structure to file string filename = string.Format("BSMD_{1}-{2}-{0}.xml", (message == null) ? "CANCEL" : message.MessageNotificationClassDisplay, DateTime.Now.ToString("yyyyMMddHHmmss"), core.Id.Value); _log.InfoFormat("saving {0} to output directory", filename); string filePath = Path.Combine(Path.GetTempPath(), filename); XmlSerializer serializer = new XmlSerializer(typeof(Root)); using (TextWriter tw = new StreamWriter(filePath)) { serializer.Serialize(tw, root); } return filename; } catch(Exception ex) { _log.Error(ex.ToString()); return null; } } } }