314 lines
12 KiB
C#
314 lines
12 KiB
C#
using System;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace bsmd.database
|
|
{
|
|
/// <summary>
|
|
/// Basisklasse aller Nachrichtentypen, zentrale Klasse für die NSW App
|
|
/// </summary>
|
|
public class Message : DatabaseEntity
|
|
{
|
|
private Guid? messageCoreId;
|
|
private Guid? reportingPartyId;
|
|
private ReportingParty reportingParty;
|
|
private DateTime? created;
|
|
private List<MessageError> errorList = new List<MessageError>();
|
|
private List<MessageViolation> violationList = new List<MessageViolation>();
|
|
private List<DatabaseEntity> elements = new List<DatabaseEntity>();
|
|
|
|
#region Enumerations
|
|
|
|
/// <summary>
|
|
/// NSW notification class
|
|
/// </summary>
|
|
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 }
|
|
|
|
/// <summary>
|
|
/// Anhand dieses Filters und den Parametern können die Klassen das passende LoadQuery generieren
|
|
/// </summary>
|
|
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
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dieser Status wird von den Services (MessageService, ResponseService) ausgewertet
|
|
/// </summary>
|
|
public enum BSMDStatus
|
|
{
|
|
UNDEFINED = 0,
|
|
PREPARE,
|
|
TOSEND,
|
|
SENT,
|
|
RESPONDED,
|
|
FAILURE,
|
|
REPORTREQUESTED,
|
|
ARCHIVED,
|
|
HIS_FAILURE_DBH
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spezifiziert das gewünschte HIS zur Übertragung der Daten
|
|
/// </summary>
|
|
public enum NSWProvider
|
|
{
|
|
UNDEFINED,
|
|
DBH,
|
|
DAKOSY,
|
|
DUDR
|
|
}
|
|
|
|
#endregion
|
|
|
|
public Message()
|
|
{
|
|
this.tablename = "[dbo].[MessageHeader]";
|
|
}
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Dieser Wert wird vom NSW / HIS vergeben
|
|
/// </summary>
|
|
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; }
|
|
|
|
public DateTime? Created { get { return this.created; } }
|
|
|
|
/// <summary>
|
|
/// 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
|
|
/// </summary>
|
|
public List<DatabaseEntity> Elements { get { return this.elements; } }
|
|
|
|
/// <summary>
|
|
/// Der Meldende
|
|
/// </summary>
|
|
public ReportingParty ReportingParty
|
|
{
|
|
get { return this.reportingParty; }
|
|
set
|
|
{
|
|
if (value == null)
|
|
{
|
|
this.reportingPartyId = null;
|
|
}
|
|
else
|
|
{
|
|
this.reportingPartyId = value.Id;
|
|
}
|
|
this.reportingParty = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// die zur Kommunikation zu verwendende HIS Schnittstelle
|
|
/// </summary>
|
|
public NSWProvider HIS { get; set; }
|
|
|
|
/// <summary>
|
|
/// Fehlerliste (Rückgabe vom NSW)
|
|
/// </summary>
|
|
public List<MessageError> ErrorList { get { return this.errorList; } }
|
|
|
|
/// <summary>
|
|
/// Violation-Liste (Rückgabe vom NSW)
|
|
/// </summary>
|
|
public List<MessageViolation> ViolationList { get { return this.violationList; } }
|
|
|
|
#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<DatabaseEntity> LoadList(IDataReader reader)
|
|
{
|
|
List<DatabaseEntity> result = new List<DatabaseEntity>();
|
|
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<Message> messages, Dictionary<Guid, ReportingParty> 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<Message> messages, Dictionary<Guid, MessageCore> messageCores)
|
|
{
|
|
foreach (Message message in messages)
|
|
{
|
|
if (message.messageCoreId.HasValue && messageCores.ContainsKey(message.messageCoreId.Value))
|
|
message.MessageCore = messageCores[message.messageCoreId.Value];
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|