285 lines
11 KiB
C#
285 lines
11 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 messageHeaderId;
|
|
private Guid? messageCoreId;
|
|
private MessageCore messageCore;
|
|
private Guid? reportingPartyId;
|
|
private ReportingParty reportingParty;
|
|
private DateTime? created;
|
|
private List<MessageError> errorList = new List<MessageError>();
|
|
private List<MessageViolation> violationList = new List<MessageViolation>();
|
|
|
|
#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
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dieser Status wird von den Services (MessageService, ResponseService) ausgewertet
|
|
/// </summary>
|
|
public enum BSMDStatus
|
|
{
|
|
UNDEFINED,
|
|
PREPARE,
|
|
TOSEND,
|
|
SENT,
|
|
RESPONDED,
|
|
FAILURE,
|
|
REPORTREQUESTED,
|
|
ARCHIVED
|
|
}
|
|
|
|
/// <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; }
|
|
|
|
/// <summary>
|
|
/// Referenz zur eigentlichen Schiffankunft
|
|
/// </summary>
|
|
public MessageCore MessageCore { get { return this.messageCore; } }
|
|
|
|
public Guid? MessageId { get; set; }
|
|
|
|
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>
|
|
/// Nachrichtentyp der abgeleiteten Meldeklassen
|
|
/// </summary>
|
|
public NotificationClass MessageNotificationClass { get; set; }
|
|
|
|
/// <summary>
|
|
/// Der Meldende
|
|
/// </summary>
|
|
public ReportingParty ReportingParty { get { return this.reportingParty; } }
|
|
|
|
/// <summary>
|
|
/// Status für Services
|
|
/// </summary>
|
|
public BSMDStatus InternalStatus { get; set; }
|
|
|
|
/// <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; } }
|
|
|
|
/// <summary>
|
|
/// Vorwärtsreferenz auf spezielle Message (damit wir über Message loopen können)
|
|
/// </summary>
|
|
public DatabaseEntity DerivedMessage { get; set; }
|
|
|
|
#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.AddWithValue("@REPORTINGPARTYID", this.ReportingParty.Id);
|
|
cmd.Parameters.AddWithValue("@BSMDSTATUS", this.InternalStatus);
|
|
cmd.Parameters.AddWithValue("@HIS", this.HIS);
|
|
|
|
if (this.IsNew)
|
|
{
|
|
string query = string.Format("INSERT INTO {0} (ClientRequestId, MessageCoreId, MessageId, SentAt, ReceivedAt, RequestedAt, NotificationClass, Reset, Cancel, Status, ReportingPartyId, BSMDStatus, HIS) " +
|
|
"VALUES (@CLIENTREQUESTID, @MESSAGECOREID, @MESSAGEID, @SENTAT, @RECEIVEDAT, @REQUESTEDAT, @NOTIFICATIONCLASS, @RESET, @CANCEL, @STATUS, @REPORTINGPARTYID, @BSMDSTATUS, @HIS)",
|
|
this.Tablename);
|
|
cmd.CommandText = query;
|
|
}
|
|
else
|
|
{
|
|
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);
|
|
cmd.Parameters.AddWithValue("@ID", this.messageHeaderId);
|
|
}
|
|
}
|
|
|
|
|
|
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.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
|
|
|
|
}
|
|
}
|