diff --git a/bsmd.dbh/Properties/Settings.Designer.cs b/bsmd.dbh/Properties/Settings.Designer.cs index d5640b4e..b3e3c500 100644 --- a/bsmd.dbh/Properties/Settings.Designer.cs +++ b/bsmd.dbh/Properties/Settings.Designer.cs @@ -80,7 +80,7 @@ namespace bsmd.dbh.Properties { [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("")] + [global::System.Configuration.DefaultSettingValueAttribute("test/bsmd_2_dbh")] public string RemoteIncomingFolder { get { return ((string)(this["RemoteIncomingFolder"])); @@ -89,7 +89,7 @@ namespace bsmd.dbh.Properties { [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("")] + [global::System.Configuration.DefaultSettingValueAttribute("test/dbh_2_bsmd")] public string RemoteOutgoingFolder { get { return ((string)(this["RemoteOutgoingFolder"])); diff --git a/bsmd.dbh/Properties/Settings.settings b/bsmd.dbh/Properties/Settings.settings index 11b06348..f078fddb 100644 --- a/bsmd.dbh/Properties/Settings.settings +++ b/bsmd.dbh/Properties/Settings.settings @@ -21,10 +21,10 @@ - + test/bsmd_2_dbh - + test/dbh_2_bsmd diff --git a/bsmd.dbh/RequestUtil.cs b/bsmd.dbh/RequestUtil.cs index 68b41126..b481c892 100644 --- a/bsmd.dbh/RequestUtil.cs +++ b/bsmd.dbh/RequestUtil.cs @@ -219,6 +219,164 @@ namespace bsmd.dbh break; #endregion + #region ATA + case Message.NotificationClass.ATA: + if (message.Elements[0] is ATA ata) + { + RootATA rootATA = new RootATA(); + if (ata.ATAPortOfCall.HasValue) + rootATA.ATAPortOfCall = ata.ATAPortOfCall.Value; + root.Items = new object[1]; + root.Items[0] = rootATA; + } + break; + #endregion + + #region ATD + case Message.NotificationClass.ATD: + { + if (message.Elements[0] is ATD atd) + { + RootATD rootATD = new RootATD(); + if(atd.ATDPortOfCall.HasValue) + rootATD.ATDPortOfCall = atd.ATDPortOfCall.Value; + root.Items = new object[1]; + root.Items[0] = rootATD; + } + break; + } + #endregion + + #region SEC + case Message.NotificationClass.SEC: + { + RootSEC rootSEC = new RootSEC(); + SEC sec = message.Elements[0] as SEC; + + List secItems = new List(); + List choiceType1s = new List(); + + if (!message.MessageCore.IsTransit && (sec.KielCanalPassagePlanned ?? false)) + { + rootSEC.KielCanalPassage = new RootSECKielCanalPassage(); + rootSEC.KielCanalPassage.KielCanalPassagePlanned = "Y"; + if (sec.KielCanalPassagePlannedIncomming.HasValue) + rootSEC.KielCanalPassage.KielCanalPassagePlannedIncoming = sec.KielCanalPassagePlannedIncomming.Value; + if (sec.KielCanalPassagePlannedOutgoing.HasValue) + rootSEC.KielCanalPassage.KielCanalPassagePlannedOutgoing = sec.KielCanalPassagePlannedOutgoing.Value; + } + + rootSEC.CurrentShipSecurityLevel = (int)sec.CurrentShipSecurityLevel.Value; + + if (sec.SECSimplification ?? false) + { + choiceType1s.Add(ItemsChoiceType1.SECSimplification); + secItems.Add("Y"); + choiceType1s.Add(ItemsChoiceType1.PortOfCallWhereCompleteSECNotified); + secItems.Add(sec.PortOfCallWhereCompleteSECNotified); + } + else + { // ACHTUNG Reihenfolge muss exakt dem .xsd entsprechen! + choiceType1s.Add(ItemsChoiceType1.CSOLastName); + secItems.Add(sec.CSOLastName); + choiceType1s.Add(ItemsChoiceType1.CSOFirstName); + secItems.Add(sec.CSOFirstName); + choiceType1s.Add(ItemsChoiceType1.CSOPhone); + secItems.Add(sec.CSOPhone); + choiceType1s.Add(ItemsChoiceType1.CSOFax); + secItems.Add(sec.CSOFax); + choiceType1s.Add(ItemsChoiceType1.CSOEMail); + secItems.Add(sec.CSOEMail); + choiceType1s.Add(ItemsChoiceType1.ValidISSCOnBoard); + secItems.Add(sec.ValidISSCOnBoard ?? false ? RootSECValidISSCOnBoard.Y : RootSECValidISSCOnBoard.N); + if (!(sec.ValidISSCOnBoard ?? false) && !sec.ReasonsForNoValidISSC.IsNullOrEmpty()) + { + choiceType1s.Add(ItemsChoiceType1.ReasonsForNoValidISSC); + secItems.Add(sec.ReasonsForNoValidISSC); + } + choiceType1s.Add(ItemsChoiceType1.ISSCType); + secItems.Add((ISSCType)(sec.ISSCType ?? 0)); + choiceType1s.Add(ItemsChoiceType1.ISSCIssuerType); + secItems.Add((ISSCIssuerType)(sec.ISSCIssuerType ?? 0)); + choiceType1s.Add(ItemsChoiceType1.ISSCIssuerName); + secItems.Add(sec.ISSCIssuerName); + if (sec.ISSCDateOfExpiration.HasValue) + { + choiceType1s.Add(ItemsChoiceType1.ISSCDateOfExpiration); + secItems.Add(sec.ISSCDateOfExpiration.Value); + } + choiceType1s.Add(ItemsChoiceType1.ApprovedSecurityPlanOnBoard); + secItems.Add(sec.ApprovedSecurityPlanOnBoard ?? false ? RootSECValidISSCOnBoard.Y : RootSECValidISSCOnBoard.N); + + choiceType1s.Add(ItemsChoiceType1.PortFacilityOfArrival); + secItems.Add(sec.PortFacilityOfArrival); + if (sec.GeneralDescriptionOfCargo.HasValue) + { + choiceType1s.Add(ItemsChoiceType1.GeneralDescriptionOfCargo); + secItems.Add((CargoDescription)sec.GeneralDescriptionOfCargo.Value); + } + + for (int i = 0; i < sec.LastTenPortFacilitesCalled.Count; i++) + { + choiceType1s.Add(ItemsChoiceType1.LastTenPortFacilitiesCalled); + RootSECLastTenPortFacilitiesCalled lastTen = new RootSECLastTenPortFacilitiesCalled(); + secItems.Add(lastTen); + LastTenPortFacilitiesCalled lastTenPortFacilitiesCalled = sec.LastTenPortFacilitesCalled[i] as LastTenPortFacilitiesCalled; + lastTen.PortFacilityPortName = lastTenPortFacilitiesCalled.PortFacilityPortName; + lastTen.PortFacilityPortCountry = lastTenPortFacilitiesCalled.PortFacilityPortCountry; + lastTen.PortFacilityPortLoCode = lastTenPortFacilitiesCalled.PortFacilityPortLoCode; + if (lastTenPortFacilitiesCalled.PortFacilityDateOfArrival.HasValue) + lastTen.PortFacilityDateOfArrival = lastTenPortFacilitiesCalled.PortFacilityDateOfArrival.Value; + if (lastTenPortFacilitiesCalled.PortFacilityDateOfDeparture.HasValue) + lastTen.PortFacilityDateOfDeparture = lastTenPortFacilitiesCalled.PortFacilityDateOfDeparture.Value; + if (lastTenPortFacilitiesCalled.PortFacilityShipSecurityLevel.HasValue) + lastTen.PortFacilityShipSecurityLevel = lastTenPortFacilitiesCalled.PortFacilityShipSecurityLevel.Value; + lastTen.PortFacilitySecurityMattersToReport = lastTenPortFacilitiesCalled.PortFacilitySecurityMattersToReport; + lastTen.PortFacilityGISISCode = lastTenPortFacilitiesCalled.PortFacilityGISISCode; + if (!lastTenPortFacilitiesCalled.PortFacilityGISISCodeLocode.IsNullOrEmpty()) + lastTen.PortFacilityGISISCodeLoCode = lastTenPortFacilitiesCalled.PortFacilityGISISCodeLocode; + } + + for (int i = 0; i < sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Count; i++) + { + choiceType1s.Add(ItemsChoiceType1.ShipToShipActivities); + RootSECShipToShipActivities s2s = new RootSECShipToShipActivities(); + secItems.Add(s2s); + ShipToShipActivitiesDuringLastTenPortFacilitiesCalled shipToShip = sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled[i] as ShipToShipActivitiesDuringLastTenPortFacilitiesCalled; + s2s.ShipToShipActivityLocation = new RootSECShipToShipActivitiesShipToShipActivityLocation(); + s2s.ShipToShipActivityLocation.ShipToShipActivityLocationName = shipToShip.ShipToShipActivityLocationName; + s2s.ShipToShipActivityLocation.ShipToShipActivityLocationLoCode = shipToShip.ShipToShipActivityLocationLoCode; + s2s.ShipToShipActivityLocation.ShipToShipActivityLocationCoordinates = new RootSECShipToShipActivitiesShipToShipActivityLocationShipToShipActivityLocationCoordinates(); + if (shipToShip.ShipToShipActivityLocationCoordinatesLatitude.HasValue) + s2s.ShipToShipActivityLocation.ShipToShipActivityLocationCoordinates.ShipToShipActivityLocationCoordinatesLatitude = shipToShip.ShipToShipActivityLocationCoordinatesLatitude.Value; + if (shipToShip.ShipToShipActivityLocationCoordinatesLongitude.HasValue) + s2s.ShipToShipActivityLocation.ShipToShipActivityLocationCoordinates.ShipToShipActivityLocationCoordinatesLongitude = shipToShip.ShipToShipActivityLocationCoordinatesLongitude.Value; + if (shipToShip.ShipToShipActivityDateFrom.HasValue) + s2s.ShipToShipActivityDateFrom = shipToShip.ShipToShipActivityDateFrom.Value; + if (shipToShip.ShipToShipActivityDateTo.HasValue) + s2s.ShipToShipActivityDateTo = shipToShip.ShipToShipActivityDateTo.Value; + + //int activityType; + //if (!Int32.TryParse(shipToShip.ShipToShipActivityType, out activityType)) + //activityType = NOA_NOD.getCallPurposeCodeFromDescription(shipToShip.ShipToShipActivityType) ?? 0; + //s2s.ShipToShipActivityType = activityType; + + if (shipToShip.ShipToShipActivityTypeCode.HasValue) + s2s.ShipToShipActivityType = shipToShip.ShipToShipActivityTypeCode.Value; + + s2s.ShipToShipActivitySecurityMattersToReport = shipToShip.ShipToShipActivitySecurityMattersToReport; + } + } + + rootSEC.Items = secItems.ToArray(); + rootSEC.ItemsElementName = choiceType1s.ToArray(); + + root.Items = new object[1]; + root.Items[0] = rootSEC; + } + break; + #endregion + default: _log.WarnFormat("Message type {0} not (yet) supported for dbh", message.MessageNotificationClassDisplay); break; @@ -226,7 +384,7 @@ namespace bsmd.dbh // 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); + string filename = string.Format("NSW.BSMD.DBH.{1}.{0}.xml", (message == null) ? "CANCEL" : message.MessageNotificationClassDisplay, core.Id.Value); _log.InfoFormat("saving {0} to output directory", filename); string filePath = Path.Combine(Path.GetTempPath(), filename); diff --git a/bsmd.dbh/ResponseUtil.cs b/bsmd.dbh/ResponseUtil.cs index 14d856ef..69bff92a 100644 --- a/bsmd.dbh/ResponseUtil.cs +++ b/bsmd.dbh/ResponseUtil.cs @@ -3,17 +3,75 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; +using System.Xml.Serialization; +using log4net; +using bsmd.database; namespace bsmd.dbh { internal static class ResponseUtil { + private static readonly ILog _log = LogManager.GetLogger(typeof(ResponseUtil)); + private static readonly Regex _regexFilename = new Regex(@".*NSW\.DBH\.BSMD\.(.*)\.(.*)\.xml"); + internal static bool Read(string inputFile) { - throw new NotImplementedException(); + bool result = false; + if(!inputFile.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) + { + _log.ErrorFormat("no xml file: {0}", inputFile); + return result; + } + + Match m = _regexFilename.Match(inputFile); + if(!m.Success) { + _log.WarnFormat("returned file doesn't follow naming convention NSW.DBH.BSMD.*:{0}", inputFile); + return result; + } + string guidString = m.Groups[0].Value; + if(!Guid.TryParse(guidString, out Guid coreId)) + { + _log.ErrorFormat("matched Guid couldn't be parsed: {0}", guidString); + return result; + } + string notificationClassString = m.Groups[1].Value; + if(!Enum.TryParse(notificationClassString, out Message.NotificationClass notificationClass)) + { + _log.WarnFormat("Notification class couldn't be parsed: {0}", notificationClassString); + return result; + } + + MessageCore aCore = DBManager.Instance.GetMessageCoreById(coreId); + if(aCore == null) + { + _log.ErrorFormat("There is no core with id {0}", coreId); + return result; + } + + bsmd.dbh.Response.Root root; + + try + { + XmlSerializer serializer = new XmlSerializer(typeof(bsmd.dbh.Response.Root)); + using (Stream s = new FileStream(inputFile, FileMode.Open)) + { + root = (bsmd.dbh.Response.Root) serializer.Deserialize(s); + } + } + catch(Exception ex) + { + _log.ErrorFormat("Failed to deserialize: {0}", ex.Message); + return result; + } + + + + return result; } } } diff --git a/bsmd.dbh/app.config b/bsmd.dbh/app.config index 0b071a84..b8defe63 100644 --- a/bsmd.dbh/app.config +++ b/bsmd.dbh/app.config @@ -26,10 +26,10 @@ - + test/bsmd_2_dbh - + test/dbh_2_bsmd