using System; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections.ObjectModel; namespace bsmd.database { /// /// Basisklasse aller Nachrichtentypen, zentrale Klasse für die NSW App /// public class Message : DatabaseEntity, ISublistContainer, IComparable { #region Fields private Guid? messageCoreId; private Guid? reportingPartyId; private ReportingParty reportingParty; private DateTime? created; private List errorList = new List(); private List violationList = new List(); private List elements = new List(); #endregion #region ANSWSortList public static IList ANSWSortList = new ReadOnlyCollection( new List{ NotificationClass.TIEFA, NotificationClass.POBA, NotificationClass.BKRA, NotificationClass.TOWA, NotificationClass.NOA_NOD, NotificationClass.STAT, NotificationClass.NAME, NotificationClass.INFO, NotificationClass.SERV, NotificationClass.LADG, NotificationClass.TIEFD, NotificationClass.POBD, NotificationClass.BKRD, NotificationClass.TOWD, NotificationClass.SEC, NotificationClass.PRE72H, NotificationClass.BPOL, NotificationClass.CREW, NotificationClass.PAS, NotificationClass.MDH, NotificationClass.HAZA, NotificationClass.HAZD, NotificationClass.WAS, NotificationClass.ATA, NotificationClass.ATD }); #endregion #region Enumerations /// /// NSW notification class /// public enum NotificationClass { VISIT, TRANSIT, NOA_NOD, ATA, ATD, SEC, POBA, POBD, NAME, TIEFA, TIEFD, BKRA, BKRD, STAT, LADG, INFO, SERV, PRE72H, MDH, WAS, CREW, PAS, BPOL, TOWA, TOWD, HAZA, HAZD } public enum MessageStatus { ACCEPTED, REJECTED } /// /// Anhand dieses Filters und den Parametern können die Klassen das passende LoadQuery generieren /// public enum LoadFilter { ALL, MESSAGETYPE, REPORTINGPARTY, MESSAGEHEADER, BSMDSTATUS, WETRIS_SHIP_ID, MDH_ID, POC30_ID, WAS_ID, WDSP_ID, BPOL_ID, SEC_ID, HERBERG_FORMGUID, BY_ID, BY_CORE, NOA_NODID, HAZ_ID, IMDG_ID, REPORTSTATUS, IMO_ETA_POC } /// /// Message Status einer NSW Einzelnachricht /// public enum BSMDStatus { UNDEFINED = 0, PREPARE, TOSEND, SENT, SEND_FAILED, CONFIRMED, VIOLATION, ERROR, } /// /// Spezifiziert das gewünschte HIS zur Übertragung der Daten /// public enum NSWProvider { UNDEFINED, DBH, DAKOSY, DUDR } #endregion #region Construction public Message() { this.tablename = "[dbo].[MessageHeader]"; } #endregion #region Properties /// /// Dieser Wert wird vom NSW / HIS vergeben /// public string ClientRequestId { set; get; } public Guid? MessageId { get; set; } public Guid? MessageCoreId { get { return this.messageCoreId; } set { this.messageCoreId = value; } } public DateTime? SentAt { get; set; } public DateTime? ReceivedAt { get; set; } public DateTime? RequestedAt { get; set; } public bool Reset { get; set; } public bool Cancel { get; set; } public MessageStatus? Status { get; set; } [ShowReport] public DateTime? Created { get { return this.created; } } /// /// Vorwärts-Referenzen auf die von diesem Header-Element abhängigen speziellen Nachrichten-Datensätzen /// Folgende Objekte können pro Nachricht n-fach vorkommen /// BRKA, BRKD, LADG, CREW, PAS, SERV, TOWA, TOWD /// sonst hat die Liste immer ein Element /// public List Elements { get { return this.elements; } } /// /// Der Meldende /// public ReportingParty ReportingParty { get { return this.reportingParty; } set { if (value == null) { this.reportingPartyId = null; } else { this.reportingPartyId = value.Id; } this.reportingParty = value; } } /// /// Status für Services /// public BSMDStatus InternalStatus { get; set; } /// /// die zur Kommunikation zu verwendende HIS Schnittstelle /// public NSWProvider HIS { get; set; } /// /// Fehlerliste (Rückgabe vom NSW) /// public List ErrorList { get { return this.errorList; } } /// /// Violation-Liste (Rückgabe vom NSW) /// public List ViolationList { get { return this.violationList; } } /// /// Property to overwrite reporting party in certain circumstances (other melder meldet) /// public Guid? ReportingPartyId { get { return this.reportingPartyId; } set { this.reportingPartyId = value; } } #endregion #region IDatabaseEntity implementation public override void PrepareSave(IDbCommand cmdParam) { SqlCommand cmd = cmdParam as SqlCommand; if (this.ClientRequestId != null) cmd.Parameters.AddWithValue("@CLIENTREQUESTID", Guid.Parse(this.ClientRequestId)); else cmd.Parameters.AddWithValue("@CLIENTREQUESTID", DBNull.Value); cmd.Parameters.AddWithValue("@MESSAGECOREID", this.MessageCore.Id); if (this.MessageId.HasValue) cmd.Parameters.AddWithValue("@MESSAGEID", this.MessageId.Value); else cmd.Parameters.AddWithValue("@MESSAGEID", DBNull.Value); if (this.SentAt.HasValue) cmd.Parameters.AddWithValue("@SENTAT", this.SentAt.Value); else cmd.Parameters.AddWithValue("@SENTAT", DBNull.Value); if (this.ReceivedAt.HasValue) cmd.Parameters.AddWithValue("@RECEIVEDAT", this.ReceivedAt.Value); else cmd.Parameters.AddWithValue("@RECEIVEDAT", DBNull.Value); if (this.RequestedAt.HasValue) cmd.Parameters.AddWithValue("@REQUESTEDAT", this.RequestedAt.Value); else cmd.Parameters.AddWithValue("@REQUESTEDAT", DBNull.Value); cmd.Parameters.AddWithValue("@NOTIFICATIONCLASS", (int)this.MessageNotificationClass); cmd.Parameters.AddWithValue("@RESET", this.Reset); cmd.Parameters.AddWithValue("@CANCEL", this.Cancel); if (this.Status.HasValue) cmd.Parameters.AddWithValue("@STATUS", (int)this.Status.Value); else cmd.Parameters.AddWithValue("@STATUS", DBNull.Value); cmd.Parameters.AddWithNullableValue("@REPORTINGPARTYID", this.reportingPartyId); cmd.Parameters.AddWithValue("@BSMDSTATUS", this.InternalStatus); cmd.Parameters.AddWithValue("@HIS", this.HIS); if (this.IsNew) { this.CreateId(); cmd.Parameters.AddWithValue("@ID", this.Id); string query = string.Format("INSERT INTO {0} (Id, ClientRequestId, MessageCoreId, MessageId, SentAt, ReceivedAt, RequestedAt, NotificationClass, Reset, Cancel, Status, ReportingPartyId, BSMDStatus, HIS) " + "VALUES (@ID, @CLIENTREQUESTID, @MESSAGECOREID, @MESSAGEID, @SENTAT, @RECEIVEDAT, @REQUESTEDAT, @NOTIFICATIONCLASS, @RESET, @CANCEL, @STATUS, @REPORTINGPARTYID, @BSMDSTATUS, @HIS)", this.Tablename); cmd.CommandText = query; } else { cmd.Parameters.AddWithValue("@ID", this.Id); cmd.CommandText = string.Format("UPDATE {0} SET ClientRequestId = @CLIENTREQUESTID, MessageId = @MESSAGEID, SentAt = @SENTAT, ReceivedAt = @RECEIVEDAT, RequestedAt = @REQUESTEDAT, " + "NotificationClass = @NOTIFICATIONCLASS, Reset = @RESET, Cancel = @CANCEL, Status = @STATUS, ReportingPartyId = @REPORTINGPARTYID, BSMDStatus = @BSMDSTATUS, HIS = @HIS " + "WHERE Id = @ID", this.Tablename); } } public override void PrepareLoadCommand(IDbCommand cmd, LoadFilter filter, params object[] criteria ) { string query = string.Format("SELECT Id, ClientRequestId, MessageCoreId, MessageId, SentAt, ReceivedAt, RequestedAt, NotificationClass, " + "Reset, Cancel, Status, ReportingPartyId, BSMDStatus, HIS, Created FROM {0} ", this.Tablename); switch (filter) { case LoadFilter.REPORTINGPARTY: { query += "WHERE ReportingPartyId = @RPID"; ((SqlCommand)cmd).Parameters.AddWithValue("RPID", criteria[0]); break; } case LoadFilter.BSMDSTATUS: { query += "WHERE BSMDStatus = @BSMDSTATUS"; ((SqlCommand)cmd).Parameters.AddWithValue("@BSMDSTATUS", criteria[0]); break; } case LoadFilter.BY_ID: { query += "WHERE Id = @ID"; ((SqlCommand)cmd).Parameters.AddWithValue("@ID", criteria[0]); break; } case LoadFilter.BY_CORE: { query += "WHERE MessageCoreId = @COREID"; ((SqlCommand)cmd).Parameters.AddWithValue("@COREID", criteria[0]); break; } case LoadFilter.ALL: default: break; } cmd.CommandText = query; } public override List LoadList(IDataReader reader) { List result = new List(); while (reader.Read()) { Message msg = new Message(); msg.id = reader.GetGuid(0); if (!reader.IsDBNull(1)) msg.ClientRequestId = reader.GetGuid(1).ToString(); msg.messageCoreId = reader.GetGuid(2); if(!reader.IsDBNull(3)) msg.MessageId = reader.GetGuid(3); if (!reader.IsDBNull(4)) msg.SentAt = reader.GetDateTime(4); if (!reader.IsDBNull(5)) msg.ReceivedAt = reader.GetDateTime(5); if (!reader.IsDBNull(6)) msg.RequestedAt = reader.GetDateTime(6); if (!reader.IsDBNull(7)) msg.MessageNotificationClass = (NotificationClass) Enum.ToObject(typeof(NotificationClass), reader.GetByte(7)); if (!reader.IsDBNull(8)) msg.Reset = reader.GetBoolean(8); if (!reader.IsDBNull(9)) msg.Cancel = reader.GetBoolean(9); if (!reader.IsDBNull(10)) msg.Status = (MessageStatus)Enum.ToObject(typeof(MessageStatus), reader.GetByte(10)); if (!reader.IsDBNull(11)) msg.reportingPartyId = reader.GetGuid(11); if (!reader.IsDBNull(12)) msg.InternalStatus = (BSMDStatus)Enum.ToObject(typeof(BSMDStatus), reader.GetByte(12)); if (!reader.IsDBNull(13)) msg.HIS = (NSWProvider)Enum.ToObject(typeof(NSWProvider), reader.GetByte(13)); if (!reader.IsDBNull(14)) msg.created = reader.GetDateTime(14); result.Add(msg); } reader.Close(); return result; } #endregion #region public static helpers public static void AssignReportingParties(List messages, Dictionary reportingParties) { foreach (Message message in messages) { if (message.reportingPartyId.HasValue && reportingParties.ContainsKey(message.reportingPartyId.Value)) message.reportingParty = reportingParties[message.reportingPartyId.Value]; } } public static void AssignMessageCores(List messages, Dictionary messageCores) { foreach (Message message in messages) { if (message.messageCoreId.HasValue && messageCores.ContainsKey(message.messageCoreId.Value)) message.MessageCore = messageCores[message.messageCoreId.Value]; } } #endregion #region ISublistContainer implementation public ISublistElement GetSublistElementWithIdentifier(string identifier) { foreach (DatabaseEntity entity in this.Elements) { ISublistElement sublistElement = entity as ISublistElement; if ((sublistElement != null ) && (sublistElement.Identifier != null)) { if (((ISublistElement)entity).Identifier.Equals(identifier)) return entity as ISublistElement; } } return null; } #endregion #region IMessageParagraph implementation public override string Title { get { if (this.Elements.Count > 0) { return this.Elements[0].Title; } else { if (this.MessageNotificationClass == NotificationClass.VISIT) return "VISIT"; if (this.MessageNotificationClass == NotificationClass.TRANSIT) return "TRANSIT"; return "unknown"; } } } public override string Subtitle { get { if (this.Elements.Count > 0) return this.Elements[0].Subtitle; else return string.Empty; } } public override List> MessageText { get { switch (this.MessageNotificationClass) { case NotificationClass.VISIT: case NotificationClass.TRANSIT: return base.MessageText; case NotificationClass.ATA: case NotificationClass.ATD: case NotificationClass.BPOL: case NotificationClass.HAZA: case NotificationClass.HAZD: case NotificationClass.INFO: case NotificationClass.MDH: case NotificationClass.NAME: case NotificationClass.NOA_NOD: case NotificationClass.POBA: case NotificationClass.POBD: case NotificationClass.PRE72H: case NotificationClass.SEC: case NotificationClass.STAT: case NotificationClass.TIEFA: case NotificationClass.TIEFD: case NotificationClass.WAS: if (this.Elements.Count > 0) { return this.Elements[0].MessageText; } else { return null; } default: return null; } } } public override bool ShowChildrenAsTable { get { switch (this.MessageNotificationClass) { case NotificationClass.CREW: case NotificationClass.PAS: return true; default: return false; } } } public override List ChildParagraphs { get { switch (this.MessageNotificationClass) { case NotificationClass.VISIT: case NotificationClass.TRANSIT: return null; case NotificationClass.ATA: case NotificationClass.ATD: case NotificationClass.BPOL: case NotificationClass.HAZA: case NotificationClass.HAZD: case NotificationClass.INFO: case NotificationClass.MDH: case NotificationClass.NAME: case NotificationClass.NOA_NOD: case NotificationClass.POBA: case NotificationClass.POBD: case NotificationClass.PRE72H: case NotificationClass.SEC: case NotificationClass.STAT: case NotificationClass.TIEFA: case NotificationClass.TIEFD: case NotificationClass.WAS: if (this.Elements.Count > 0) { return this.Elements[0].ChildParagraphs; } else { return null; } default: List result = new List(); foreach (DatabaseEntity element in this.Elements) result.Add(element); return result; } } } #endregion #region IComparable implementation /// /// Sort messages by notification class /// public int CompareTo(Message other) { return this.MessageNotificationClass.CompareTo(other.MessageNotificationClass); } #endregion } }