git_bsmd/bsmd.dbh/ResponseUtil.cs

125 lines
4.5 KiB
C#

// Copyright (c) 2020-present schick Informatik
// Description: Verarbeitung von empfangenen Rückmeldungen
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Serialization;
using log4net;
using bsmd.database;
namespace bsmd.dbh
{
internal static class ResponseUtil
{
private static readonly ILog _log = LogManager.GetLogger(typeof(ResponseUtil));
private static readonly Regex _regexFilename = new Regex(@".*NSW\.DBH\.BSMD\.(.*)\.xml");
internal static bool Read(string inputFile)
{
bool result = false;
if(!inputFile.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
{
_log.ErrorFormat("no xml file: {0}", inputFile);
return result;
}
Match m = _regexFilename.Match(inputFile);
if(!m.Success) {
_log.WarnFormat("returned file doesn't follow naming convention NSW.DBH.BSMD.*:{0}", inputFile);
return result;
}
string fileSeqString = m.Groups[1].Value;
if(!Int32.TryParse(fileSeqString, out int fileSeqNum))
{
_log.ErrorFormat("matched file sequence number couldn't be parsed: {0}", fileSeqString);
return result;
}
// load message(s?) by file seq string
Message sentMessage = DBManager.Instance.GetMessageByFileSeqNum(fileSeqNum);
if(sentMessage == null)
{
_log.ErrorFormat("cannot find a message for file sequence number {0}", fileSeqNum);
return result;
}
MessageCore aCore = DBManager.Instance.GetMessageCoreById(sentMessage.MessageCoreId.Value);
if(aCore == null)
{
_log.ErrorFormat("There is no core with id {0}", sentMessage.MessageCoreId.Value);
return result;
}
bsmd.dbh.Response.Root root = null;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(bsmd.dbh.Response.Root));
using (Stream s = new FileStream(inputFile, FileMode.Open))
{
root = (bsmd.dbh.Response.Root) serializer.Deserialize(s);
}
if(Guid.TryParse(root.SenderReference, out Guid refGuid))
{
if (!sentMessage.Id.Equals(refGuid))
_log.WarnFormat("sender ref {0} does not match sent message id {1}", refGuid, sentMessage.Id);
}
else
{
_log.WarnFormat("sender ref {0} is no guid", root.SenderReference);
}
switch(sentMessage.MessageNotificationClass)
{
case Message.NotificationClass.VISIT:
if(root.Type == Response.RootType.VISIT)
{
if(aCore.VisitId.IsNullOrEmpty() && !root.VisitId.IsNullOrEmpty())
{
aCore.VisitId = root.VisitId;
_log.InfoFormat("Received Visit-Id {0} for core {1}", root.VisitId, aCore.Id);
}
}
break;
case Message.NotificationClass.TRANSIT:
if (root.Type == Response.RootType.TRANSIT)
{
if (aCore.TransitId.IsNullOrEmpty() && !root.TransitId.IsNullOrEmpty())
{
aCore.TransitId = root.TransitId;
_log.InfoFormat("Received Transit-Id {0} for core {1}", root.TransitId, aCore.Id);
}
}
break;
default:
break;
}
if (!(aCore.Cancelled ?? false))
aCore.BSMDStatusInternal = MessageCore.BSMDStatus.RESPONDED;
DBManager.Instance.Save(aCore);
result = true;
}
catch(Exception ex)
{
_log.ErrorFormat("Failed to deserialize return message: {0}", ex.Message);
return result;
}
return result;
}
}
}