// // Class: Util // Current CLR: 4.0.30319.34209 // System: Microsoft Visual Studio 10.0 // Author: dani // Created: 3/1/2015 8:05:05 PM // // Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved. using System; using System.Collections.Generic; using log4net; using bsmd.database; //using bsmd.dbh.request; using bsmd.dbh.DBHWebReference; namespace bsmd.dbh { public class Request { private static ILog _log = LogManager.GetLogger(typeof(Request)); public static bool SendMessage(DatabaseEntity dbEntity) { if (dbEntity == null) return false; Message aMessage = null; RootType rootType = RootType.DATA; if (dbEntity.GetType().IsAssignableFrom(typeof(Message))) { aMessage = (Message)dbEntity; rootType = aMessage.MessageCore.IsTransit ? RootType.TRANSIT : RootType.VISIT; aMessage.SentAt = DateTime.Now; } else { aMessage = dbEntity.MessageHeader; } if (aMessage.ReportingParty == null) { _log.ErrorFormat("Reporting party not set on message {0}", aMessage.Id); return false; } // map message to dbh NSWRequest object Dbh_Osis_Answ_Ws client = new Dbh_Osis_Answ_Ws(); RootReportingParty rp = new RootReportingParty(); rp.RPCity = aMessage.ReportingParty.City; rp.RPCountry = aMessage.ReportingParty.Country; rp.RPEMail = aMessage.ReportingParty.EMail; rp.RPFax = aMessage.ReportingParty.Fax; rp.RPFirstName = aMessage.ReportingParty.FirstName; rp.RPLastName = aMessage.ReportingParty.LastName; rp.RPName = aMessage.ReportingParty.Name; rp.RPPhone = aMessage.ReportingParty.Phone; rp.RPPostalCode = aMessage.ReportingParty.PostalCode; rp.RPStreetAndNumber = aMessage.ReportingParty.StreetAndNumber; rp.RPTypeSpecified = aMessage.ReportingParty.ReportingPartyType.HasValue; if (rp.RPTypeSpecified) rp.RPType = (RootReportingPartyRPType)aMessage.ReportingParty.ReportingPartyType.Value; DateTime timestamp = DateTime.Now; string version = "2.0"; object item = null; string senderReference = dbEntity.Id.Value.ToString("N"); ItemChoiceType2 itemChoiceType2 = ItemChoiceType2.Visit; // ? switch (aMessage.MessageNotificationClass) { #region VISIT case Message.NotificationClass.VISIT: { RootVisit rootVisit = new RootVisit(); rootType = RootType.VISIT; if (aMessage.MessageCore.IMO != null) { rootVisit.ItemElementName = ItemChoiceType.IMONumber; rootVisit.Item = aMessage.MessageCore.IMO; } else { rootVisit.ItemElementName = ItemChoiceType.ENINumber; rootVisit.Item = aMessage.MessageCore.ENI; } rootVisit.PortOfCall = aMessage.MessageCore.PoC; rootVisit.ETAPortOfCall = aMessage.MessageCore.ETA.Value; item = rootVisit; break; } #endregion #region TRANSIT case Message.NotificationClass.TRANSIT: { RootTransit rootTransit = new RootTransit(); rootType = RootType.TRANSIT; if (aMessage.MessageCore.IMO != null) { rootTransit.ItemElementName = ItemChoiceType1.IMONumber; rootTransit.Item = aMessage.MessageCore.IMO; } else { rootTransit.ItemElementName = ItemChoiceType1.ENINumber; rootTransit.Item = aMessage.MessageCore.ENI; } rootTransit.ETAKielCanal = aMessage.MessageCore.ETA.Value; item = rootTransit; break; } #endregion #region NOA_NOD case Message.NotificationClass.NOA_NOD: { NOA_NOD noa_nod = dbEntity as NOA_NOD; if (noa_nod != null) { RootNOA_NOD rootNoaNod = new RootNOA_NOD(); rootType = RootType.DATA; if (noa_nod.ETAToNextPort.HasValue) rootNoaNod.ETAToNextPort = noa_nod.ETAToNextPort.Value; rootNoaNod.ETAToNextPortSpecified = noa_nod.ETAToNextPort.HasValue; if (noa_nod.ETDFromLastPort.HasValue) rootNoaNod.ETDFromLastPort = noa_nod.ETDFromLastPort.Value; rootNoaNod.ETDFromLastPortSpecified = noa_nod.ETDFromLastPort.HasValue; rootNoaNod.LastPort = noa_nod.LastPort; rootNoaNod.NextPort = noa_nod.NextPort; ItemsChoiceType[] choiceArray = new ItemsChoiceType[3]; object[] choices = new object[3]; choiceArray[0] = ItemsChoiceType.CallPurpose; RootNOA_NODCallPurpose rnncp = new RootNOA_NODCallPurpose(); if (noa_nod.CallPurposeCode.HasValue) rnncp.CallPurposeCode = noa_nod.CallPurposeCode.Value; rnncp.CallPurposeDescription = noa_nod.CallPurposeDescription; choices[0] = rnncp; if (noa_nod.ETAToKielCanal.HasValue) { choiceArray[1] = ItemsChoiceType.ETAToKielCanal; choices[1] = noa_nod.ETAToKielCanal.Value; } if (noa_nod.ETAToPortOfCall.HasValue) { choiceArray[1] = ItemsChoiceType.ETAToPortOfCall; choices[1] = noa_nod.ETAToPortOfCall.Value; } if (noa_nod.ETDFromKielCanal.HasValue) { choiceArray[2] = ItemsChoiceType.ETDFromKielCanal; choices[2] = noa_nod.ETDFromKielCanal.Value; } if (noa_nod.ETDFromPortOfCall.HasValue) { choiceArray[2] = ItemsChoiceType.ETDFromPortOfCall; choices[2] = noa_nod.ETDFromPortOfCall.Value; } item = rootNoaNod; } break; } #endregion #region ATA case Message.NotificationClass.ATA: { ATA ata = dbEntity as ATA; if (ata != null) { RootATA rootATA = new RootATA(); rootType = RootType.DATA; if(ata.ATAPortOfCall.HasValue) rootATA.ATAPortOfCall = ata.ATAPortOfCall.Value; item = rootATA; } break; } #endregion #region ATD case Message.NotificationClass.ATD: { ATD atd = dbEntity as ATD; if (atd != null) { RootATD rootATD = new RootATD(); rootType = RootType.DATA; rootATD.ATDPortOfCall = atd.ATDPortOfCall.Value; item = rootATD; } break; } #endregion #region BPOL case Message.NotificationClass.BPOL: { BPOL bpol = dbEntity as BPOL; if (bpol != null) { RootBPOL rootBPOL = new RootBPOL(); rootType = RootType.DATA; RootBPOLPortOfItinerary[] poiArray = new RootBPOLPortOfItinerary[bpol.PortOfItineraries.Count]; rootBPOL.PortOfItinerary = poiArray; for (int i = 0; i < bpol.PortOfItineraries.Count; i++) { RootBPOLPortOfItinerary port = new RootBPOLPortOfItinerary(); if (bpol.PortOfItineraries[i].PortOfItineraryETA.HasValue) port.PortOfItineraryETA = bpol.PortOfItineraries[i].PortOfItineraryETA.Value; port.PortOfItineraryName = bpol.PortOfItineraries[i].PortOfItineraryName; poiArray[i] = port; } if (bpol.StowawaysOnBoard.HasValue) rootBPOL.StowawayOnBoard = bpol.StowawaysOnBoard.Value ? RootBPOLStowawayOnBoard.Y : RootBPOLStowawayOnBoard.N; item = rootBPOL; } break; } #endregion #region CREW case Message.NotificationClass.CREW: { if (aMessage.Elements.Count > 0) { RootCREW rootCREW = new RootCREW(); rootType = RootType.DATA; rootCREW.CrewMember = new RootCREWCrewMember[aMessage.Elements.Count]; for (int i = 0; i < aMessage.Elements.Count; i++) { CREW crew = aMessage.Elements[i] as CREW; rootCREW.CrewMember[i] = new RootCREWCrewMember(); if (crew.CrewMemberDateOfBirth.HasValue) rootCREW.CrewMember[i].CrewMemberDateOfBirth = crew.CrewMemberDateOfBirth.Value; rootCREW.CrewMember[i].CrewMemberDuty = crew.CrewMemberDuty; rootCREW.CrewMember[i].CrewMemberFirstName = crew.CrewMemberFirstName; rootCREW.CrewMember[i].CrewMemberGenderSpecified = crew.CrewMemberGender.HasValue; if (crew.CrewMemberGender.HasValue) rootCREW.CrewMember[i].CrewMemberGender = (RootCREWCrewMemberCrewMemberGender) crew.CrewMemberGender.Value; rootCREW.CrewMember[i].CrewMemberIdentityDocumentId = crew.CrewMemberIdentityDocumentId; if(crew.CrewMemberIdentityDocumentType.HasValue) rootCREW.CrewMember[i].CrewMemberIdentityDocumentType = (RootCREWCrewMemberCrewMemberIdentityDocumentType)crew.CrewMemberIdentityDocumentType.Value; rootCREW.CrewMember[i].CrewMemberLastName = crew.CrewMemberLastName; rootCREW.CrewMember[i].CrewMemberNationality = crew.CrewMemberNationality; rootCREW.CrewMember[i].CrewMemberPlaceOfBirth = crew.CrewMemberPlaceOfBirth; rootCREW.CrewMember[i].CrewMemberVisaNumber = crew.CrewMemberVisaNumber; } item = rootCREW; } } break; #endregion default: { _log.ErrorFormat("DBH send message: message type {0} not implemented", aMessage.MessageNotificationClass); break; } } // send object bool returnval = true; try { object[] items = null; if ((rootType == RootType.CANCEL) || (rootType == RootType.DATA) || (rootType == RootType.RESET)) { items = new object[1]; items[0] = item; if (aMessage.MessageCore.IsTransit) { item = aMessage.MessageCore.TransitId; itemChoiceType2 = ItemChoiceType2.TransitId; } else { item = aMessage.MessageCore.VisitId; itemChoiceType2 = ItemChoiceType2.VisitId; } } string result = client.Root( version, timestamp, Properties.Settings.Default.Sender, // BSMD sender Kennung (festverdrahtet) senderReference, rootType, item, itemChoiceType2, null, // sisnumbers (weglassen) rp, items, null // violations (weglassen) ); } catch (Exception ex) { _log.ErrorFormat("exception on message send: {0}", ex.Message); returnval = false; } return returnval; } } }