git_bsmd/nsw/Source/bsmd.dbh/Response.cs

195 lines
9.6 KiB
C#

//
// Class: Response
// Current CLR: 4.0.30319.34209
// System: Microsoft Visual Studio 10.0
// Author: dani
// Created: 3/1/2015 8:12:08 PM
//
// Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved.
using System;
using System.Collections.Generic;
using bsmd.database;
using log4net;
using System.IO;
using System.Xml.Serialization;
namespace bsmd.dbh
{
public class Response
{
private static ILog _log = LogManager.GetLogger("dbh Response");
public static void ProcessResponse(string VisitId, string TransitId, DateTime Timestamp,
string SenderReference, response.RootType Type, bsmd.dbh.response.Message[] Messages,
bsmd.dbh.response.RootReportingClassesFull ReportingClassesFull,
bsmd.dbh.response.RootReportingClassesPartial ReportingClassesPartial,
bsmd.dbh.response.RootReportingClassesError RootReportingClassesError,
bsmd.dbh.response.RootReportingClassesResetted ReportingClassesResetted,
string connectionString)
{
/*
XmlSerializer serializer = new XmlSerializer(typeof(response.Root));
using(StringWriter textWriter = new StringWriter())
{
serializer.Serialize(textWriter, aResponse);
_log.Debug(textWriter.ToString());
}
*/
// _log.Debug(aResponse.Serialize());
if (DBManager.Instance.Connect(connectionString))
{
_log.Debug("Start PROCESS");
Guid messageId;
if (!Guid.TryParseExact(SenderReference, "N", out messageId))
{
_log.WarnFormat("SenderReference {0} is not a guid, skipping message processing!", SenderReference);
return;
}
DatabaseEntity dbEntity = DBManager.Instance.GetMessageById(messageId);
if (dbEntity != null)
_log.InfoFormat("Message type {0} found for SenderReference {1}", ((Message)dbEntity).MessageNotificationClassDisplay, messageId);
MessageCore aCore = null;
if (dbEntity == null)
{
aCore = DBManager.Instance.GetMessageCoreById(messageId);
if((aCore != null) && (Type == response.RootType.CANCEL))
{
_log.InfoFormat("Cancel confirmation received for {0} ({1})", aCore.Id, aCore.DisplayId);
aCore.BSMDStatusInternal = MessageCore.BSMDStatus.RESPONDED;
aCore.Cancelled = true;
DBManager.Instance.Save(aCore);
}
}
if (dbEntity == null)
{
_log.WarnFormat("Message / Core with ID {0} not found in database, skipping", messageId);
return;
}
if (!(dbEntity is Message) && (Type != dbh.response.RootType.CANCEL))
{
_log.WarnFormat("SenderReference DB Entity Object is no MessageHeader, aborting ({0})", dbEntity.GetType());
return;
}
Message aMessage = dbEntity as Message;
switch (Type)
{
case dbh.response.RootType.VISIT:
// neue VISIT - ID
if(!VisitId.IsNullOrEmpty())
aMessage.MessageCore.VisitId = VisitId;
aMessage.MessageCore.BSMDStatusInternal = MessageCore.BSMDStatus.PREPARE;
aMessage.SendSuccess = true;
DBManager.Instance.Save(aMessage.MessageCore);
break;
case dbh.response.RootType.TRANSIT:
aMessage.MessageCore.TransitId = TransitId;
aMessage.MessageCore.BSMDStatusInternal = MessageCore.BSMDStatus.PREPARE;
aMessage.SendSuccess = true;
DBManager.Instance.Save(aMessage.MessageCore);
break;
case dbh.response.RootType.RESET:
// Die Liste ist auch bei erfolgtem RESET offenbar immer NULL...
//if ((ReportingClassesResetted != null) && (ReportingClassesResetted.Count > 0))
// das Mapping stimmt bei uns natürlich so nicht, aber da die Nachrichten einzeln gehen sollte es trotzdem funktionieren
// (int) ReportingClassesResetted[0].ReportingClass[0] == (int)aMessage.MessageNotificationClass)
//{
//_log.InfoFormat("RESET response received for {0}", ReportingClassesResetted[0].ReportingClass[0]);
aMessage.Reset = true;
aMessage.SendSuccess = false; // zurücksetzen des "grünen Punkts" im ENI-2
aMessage.InternalStatus = Message.BSMDStatus.CONFIRMED;
//}
break;
case dbh.response.RootType.DATA:
if((ReportingClassesFull != null) && (ReportingClassesFull.ReportingClass.Length > 0))
{
// this was successful, save status to MessageHeader
aMessage.SendSuccess = true;
aMessage.InternalStatus = Message.BSMDStatus.CONFIRMED;
aMessage.Status = Message.MessageStatus.ACCEPTED;
_log.InfoFormat("CONFIRMED");
}
else
{
_log.InfoFormat("no match");
}
break;
}
if (Messages != null)
{
// Status zu den jeweiligen Nachrichten. Bei uns sollte die Anzahl hier immer 1 sein, da wir die Dinger
// einzeln verschicken.
for (int i = 0; i < Messages.Length; i++)
{
_log.InfoFormat("message {0} type {1}: {2}", i,
Messages[i].Type,
Messages[i].Text ?? "null");
switch (Messages[i].Type)
{
case dbh.response.RootMessageType.ERROR:
MessageError error = new MessageError();
error.ErrorText = Messages[i].Text;
_log.WarnFormat("Error received for {0}: {1}", Messages[i].Type, error.ErrorText);
error.MessageHeaderId = aMessage.Id.Value;
aMessage.InternalStatus = Message.BSMDStatus.ERROR;
aMessage.StatusInfo = string.Format("Id:{0} Loc:{1} - {2}", Messages[i].ID, Messages[i].Location, Messages[i].Text);
DBManager.Instance.Save(error);
aMessage.SendSuccess = false;
break;
case dbh.response.RootMessageType.VIOLATION:
MessageViolation violation = new MessageViolation();
violation.ViolationText = Messages[i].Text;
_log.WarnFormat("Violation received for {0}: {1}", Messages[i].Type, violation.ViolationText);
violation.MessageHeaderId = aMessage.Id.Value;
aMessage.InternalStatus = Message.BSMDStatus.VIOLATION;
aMessage.StatusInfo = string.Format("Id:{0} Loc:{1} - {2}", Messages[i].ID, Messages[i].Location, Messages[i].Text);
aMessage.SendSuccess = true;
DBManager.Instance.Save(violation);
break;
case dbh.response.RootMessageType.WARNING:
_log.InfoFormat("WARNING received for {0}: {1}", Messages[i].Type, Messages[i].Text);
aMessage.StatusInfo = string.Format("Id:{0} Loc:{1} - {2}", Messages[i].ID, Messages[i].Location, Messages[i].Text);
aMessage.SendSuccess = true;
break;
case dbh.response.RootMessageType.INFO:
default:
_log.InfoFormat("INFO received for {0}: {1}", Messages[i].Type, Messages[i].Text);
aMessage.StatusInfo = string.Format("Id:{0} Loc:{1} - {2}", Messages[i].ID, Messages[i].Location, Messages[i].Text);
aMessage.SendSuccess = true;
break;
}
if (aMessage.Reset) aMessage.SendSuccess = false; // Send flag zurücksetzen
}
}
aMessage.ReceivedAt = DateTime.Now;
DBManager.Instance.Save(aMessage);
DBManager.Instance.Disconnect();
}
else
{
_log.Fatal("cannot connect to database");
}
}
}
}