git_bsmd/ENI2/Excel/DakosyUtil.cs

447 lines
23 KiB
C#

// Copyright (c) 2017- schick Informatik
// Description: Helper class to read data from "Dakosy"-Style Excel Sheets
//
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using bsmd.database;
namespace ENI2.Excel
{
internal static class DakosyUtil
{
private static readonly ILog _log = LogManager.GetLogger(typeof(DakosyUtil));
internal static bool ProcessSheet(ExcelReader reader, out string readMessage, MessageCore messageCore, List<Message.NotificationClass> notificationClasses)
{
bool result = true;
readMessage = "";
// Test if an Id is found in the sheet. If so, it has to match the id of the current core
string visitTransitId = reader.ReadCellAsText("main", "C12");
if (visitTransitId != null)
{
MessageCore aMessageCore = null;
if (bsmd.database.Util.IsVisitId(visitTransitId))
{
aMessageCore = DBManager.Instance.GetMessageCoreByVisitId(visitTransitId);
}
else if (bsmd.database.Util.IsTransitId(visitTransitId))
{
aMessageCore = DBManager.Instance.GetMessageCoreByTransitId(visitTransitId);
}
if ((aMessageCore == null) || (aMessageCore.Id.Value != messageCore.Id.Value))
{
readMessage = "Id not matching in import sheet";
return false;
}
}
// load messages if already present
List<Message> messages = DBManager.Instance.GetMessagesForCore(messageCore, DBManager.MessageLoad.ALL);
messages.Sort();
// now parse all data into the message classes
try
{
foreach (Message message in messages)
{
if ((message.MessageNotificationClass == Message.NotificationClass.AGNT) && notificationClasses.Contains(Message.NotificationClass.AGNT))
{ if (ScanAGNT(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.ATA) && notificationClasses.Contains(Message.NotificationClass.ATA))
{ if (ScanATA(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.ATD) && notificationClasses.Contains(Message.NotificationClass.ATD))
{ if (ScanATD(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.BKRA) && notificationClasses.Contains(Message.NotificationClass.BKRA))
{ if (ScanBKRA(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.BKRD) && notificationClasses.Contains(Message.NotificationClass.BKRD))
{ if (ScanBKRD(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.BPOL) && notificationClasses.Contains(Message.NotificationClass.BPOL))
{ if (ScanBPOL(message, messages, messageCore, reader)) ExcelReader.SaveMessage(message); }
if ((message.MessageNotificationClass == Message.NotificationClass.CREW) && notificationClasses.Contains(Message.NotificationClass.CREW))
{ if (ScanCREW(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.CREWD) && notificationClasses.Contains(Message.NotificationClass.CREWD))
{ if (ScanCREWD(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.HAZA) && notificationClasses.Contains(Message.NotificationClass.HAZA))
{ if (ScanHAZA(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.HAZD) && notificationClasses.Contains(Message.NotificationClass.HAZD))
{ if (ScanHAZD(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.INFO) && notificationClasses.Contains(Message.NotificationClass.INFO))
{ if (ScanINFO(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.LADG) && notificationClasses.Contains(Message.NotificationClass.LADG))
{ if (ScanLADG(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.MDH) && notificationClasses.Contains(Message.NotificationClass.MDH))
{ if (ScanMDH(message, messages, messageCore, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.NAME) && notificationClasses.Contains(Message.NotificationClass.NAME))
{ if (ScanNAME(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.NOA_NOD) && notificationClasses.Contains(Message.NotificationClass.NOA_NOD))
{ if (ScanNOA_NOD(message, messageCore, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.PAS) && notificationClasses.Contains(Message.NotificationClass.PAS))
{ if (ScanPAS(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.PASD) && notificationClasses.Contains(Message.NotificationClass.PASD))
{ if (ScanPASD(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.POBA) && notificationClasses.Contains(Message.NotificationClass.POBA))
{ if (ScanPOBA(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.POBD) && notificationClasses.Contains(Message.NotificationClass.POBD))
{ if (ScanPOBD(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.PRE72H) && notificationClasses.Contains(Message.NotificationClass.PRE72H))
{ if (ScanPRE72H(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.SEC) && notificationClasses.Contains(Message.NotificationClass.SEC))
{ if (ScanSEC(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.SERV) && notificationClasses.Contains(Message.NotificationClass.SERV))
{ if (ScanSERV(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.STAT) && notificationClasses.Contains(Message.NotificationClass.STAT))
{ if (ScanSTAT(message, messageCore, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.STO) && notificationClasses.Contains(Message.NotificationClass.STO))
{ if (ScanSTO(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.TIEFA) && notificationClasses.Contains(Message.NotificationClass.TIEFA))
{ if (ScanTIEFA(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.TIEFD) && notificationClasses.Contains(Message.NotificationClass.TIEFD))
{ if (ScanTIEFD(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.TOWA) && notificationClasses.Contains(Message.NotificationClass.TOWA))
{ if (ScanTOWA(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.TOWD) && notificationClasses.Contains(Message.NotificationClass.TOWD))
{ if (ScanTOWD(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.WAS) && notificationClasses.Contains(Message.NotificationClass.WAS))
{ if (ScanWAS(message, reader)) ExcelReader.SaveMessage(message); continue; }
if ((message.MessageNotificationClass == Message.NotificationClass.WAS_RCPT) && notificationClasses.Contains(Message.NotificationClass.WAS_RCPT))
{ if (ScanWAS_RCPT(message, reader)) ExcelReader.SaveMessage(message); continue; }
}
DBManager.Instance.Save(messageCore); // muss das eigentlich sein?
}
catch (Exception ex)
{
readMessage = ex.Message;
result = false;
}
return result;
}
private static bool ScanWAS_RCPT(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanWAS(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanTOWD(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanTOWA(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanTIEFD(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanTIEFA(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanSTO(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanSTAT(Message statMessage, MessageCore core, ExcelReader reader)
{
if (statMessage.Elements.Count == 0)
{
STAT newSTAT = new STAT();
newSTAT.MessageHeader = statMessage;
statMessage.Elements.Add(newSTAT);
}
STAT stat = statMessage.Elements[0] as STAT;
stat.ShipName = reader.ReadCellAsText("ship", "C8");
stat.CallSign = reader.ReadCellAsText("ship", "C9");
core.IMO = reader.ReadCellAsText("ship", "C10");
core.ENI = reader.ReadCellAsText("ship", "C11");
core.PoC = reader.ReadCellAsText("port message", "C8");
DBManager.Instance.Save(core);
stat.MMSINumber = reader.ReadCellAsText("ship", "C12");
stat.Flag = reader.ReadCellAsText("ship", "C13");
stat.LengthOverall_MTR = reader.ReadCellAsDecimal("ship", "C14");
stat.Beam_MTR = reader.ReadCellAsDecimal("ship", "C15");
stat.GrossTonnage = (int?) reader.ReadCellAsDecimal("ship", "C16");
stat.PortOfRegistry = reader.ReadCellAsText("ship", "C17");
stat.InmarsatCallNumber = string.Format("{0} {1} {2} {3} {4}",
reader.ReadCellAsText("ship", "C18"),
reader.ReadCellAsText("ship", "C19"),
reader.ReadCellAsText("ship", "C20"),
reader.ReadCellAsText("ship", "C21"),
reader.ReadCellAsText("ship", "C22")
).Trim();
stat.ShipType = reader.ReadCellAsText("ship", "C23");
stat.TransportMode = reader.ReadCellAsText("ship", "C24");
stat.ISMCompanyName = reader.ReadCellAsText("ship", "C26");
stat.ISMCompanyId = reader.ReadCellAsText("ship", "C27");
string street = reader.ReadCellAsText("ship", "C28");
string number = reader.ReadCellAsText("ship", "C29");
stat.ISMCompanyStreetAndNumber = string.Format("{0} {1}", street, number).Trim();
stat.ISMCompanyPostalCode = reader.ReadCellAsText("ship", "C30");
stat.ISMCompanyCity = reader.ReadCellAsText("ship", "C31");
stat.ISMCompanyCountry = reader.ReadCellAsText("ship", "C32");
return true;
}
private static bool ScanSERV(Message servMessage, ExcelReader reader)
{
bool result = false;
for(int i=0;i<10000;i++)
{
string serviceName = reader.ReadCellAsText("port message", string.Format("B{0}", i + 50));
string serviceBeneficiary = reader.ReadCellAsText("port message", string.Format("C{0}", i + 50));
string serviceInvoiceRecipient = reader.ReadCellAsText("port message", string.Format("D{0}", i + 50));
if (serviceName.IsNullOrEmpty() && serviceBeneficiary.IsNullOrEmpty()) break;
if (!(servMessage.GetSublistElementWithIdentifier((i + 1).ToString()) is SERV serv))
{
serv = new SERV();
serv.MessageHeader = servMessage;
serv.Identifier = (i + 1).ToString();
servMessage.Elements.Add(serv);
}
serv.ServiceName = serviceName;
serv.ServiceBeneficiary = serviceBeneficiary;
serv.ServiceInvoiceRecipient = serviceInvoiceRecipient;
result = true;
}
return result;
}
private static bool ScanSEC(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanPRE72H(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanPOBD(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanPOBA(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanPASD(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanPAS(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanNOA_NOD(Message noa_nodMessage, MessageCore messageCore, ExcelReader reader)
{
if (noa_nodMessage.Elements.Count == 0)
{
NOA_NOD newNAME = new NOA_NOD();
newNAME.MessageHeader = noa_nodMessage;
noa_nodMessage.Elements.Add(newNAME);
}
NOA_NOD noa_nod = noa_nodMessage.Elements[0] as NOA_NOD;
noa_nod.ETAToPortOfCall = reader.ReadCellAsDateTime("port message", "C26");
noa_nod.ETDFromPortOfCall = reader.ReadCellAsDateTime("port message", "C27");
// read call purposes
for (int i = 0; i < 9; i++)
{
int? callPurposeCode = (int?) reader.ReadCellAsDecimal("port message", string.Format("B{0}", 29 + i));
string callPurposeDescription = reader.ReadCellAsText("port message", string.Format("C{0}", 29 + i));
if (callPurposeCode.HasValue)
{
if (!(noa_nod.GetSublistElementWithIdentifier(i.ToString()) is CallPurpose callPurpose))
{
callPurpose = new CallPurpose();
callPurpose.NOA_NOD = noa_nod;
callPurpose.Identifier = i.ToString();
noa_nod.CallPurposes.Add(callPurpose);
}
callPurpose.CallPurposeCode = callPurposeCode.Value;
callPurpose.CallPurposeDescription = callPurposeDescription;
}
}
noa_nod.ETAToKielCanal = reader.ReadCellAsDateTime("port message", "C39");
noa_nod.ETDFromKielCanal = reader.ReadCellAsDateTime("port message", "C40");
noa_nod.LastPort = reader.ReadCellAsText("port message", "C42");
noa_nod.ETDFromLastPort = reader.ReadCellAsDateTime("port message", "C43");
noa_nod.NextPort = reader.ReadCellAsText("port message", "C44");
noa_nod.ETAToNextPort = reader.ReadCellAsDateTime("port message", "C45");
return true;
}
#region NAME
private static bool ScanNAME(Message nameMessage, ExcelReader reader)
{
if (nameMessage.Elements.Count == 0)
{
NAME newNAME = new NAME();
newNAME.MessageHeader = nameMessage;
nameMessage.Elements.Add(newNAME);
}
NAME name = nameMessage.Elements[0] as NAME;
name.NameOfMaster = reader.ReadCellAsText("port message", "C11");
if (name.NameOfMaster.IsNullOrEmpty() && name.IsNew)
return false;
return true;
}
#endregion
private static bool ScanMDH(Message message, List<Message> messages, MessageCore messageCore, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanLADG(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanINFO(Message infoMessage, ExcelReader reader)
{
if (infoMessage.Elements.Count == 0)
{
INFO newINFO = new INFO();
newINFO.MessageHeader = infoMessage;
infoMessage.Elements.Add(newINFO);
}
INFO info = infoMessage.Elements[0] as INFO;
string shippingArea = reader.ReadCellAsText("port message", "C14");
if (shippingArea.Equals("NORTH_BALTIC_SEA", StringComparison.OrdinalIgnoreCase)) info.ShippingArea = 0;
if (shippingArea.Equals("EUROPE", StringComparison.OrdinalIgnoreCase)) info.ShippingArea = 1;
if (shippingArea.Equals("OVERSEAS", StringComparison.OrdinalIgnoreCase)) info.ShippingArea = 2;
info.RequestedPositionInPortOfCall = reader.ReadCellAsText("port message", "C15");
info.SpecialRequirementsOfShipAtBerth = reader.ReadCellAsText("port message", "C16");
info.ConstructionCharacteristicsOfShip = reader.ReadCellAsText("port message", "C17");
info.BowThrusterPower = reader.ReadCellAsText("port message", "C18");
info.SternThrusterPower = reader.ReadCellAsText("port message", "C19");
info.FumigatedBulkCargoBool = reader.ReadCellAsBool("port message", "C20");
info.DeplacementSummerDraught_TNE = reader.ReadCellAsDecimal("port message", "C21");
string portArea = reader.ReadCellAsText("port message", "C22")?.Trim().ToUpper();
if (!portArea.IsNullOrEmpty() && DBManager.Instance.GetPortAreaDict().ContainsKey(portArea))
info.PortArea = portArea;
else
info.PortArea = "";
return true;
}
private static bool ScanHAZD(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanHAZA(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanCREWD(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanCREW(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanBPOL(Message message, List<Message> messages, MessageCore messageCore, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanBKRD(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanBKRA(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanATD(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
private static bool ScanATA(Message message, ExcelReader reader)
{
throw new NotImplementedException();
}
#region AGNT
private static bool ScanAGNT(Message agntMessage, ExcelReader reader)
{
if (agntMessage.Elements.Count == 0)
{
AGNT newAgnt = new AGNT();
newAgnt.MessageHeader = agntMessage;
agntMessage.Elements.Add(newAgnt);
}
AGNT agnt = agntMessage.Elements[0] as AGNT;
agnt.AgentCompanyName = reader.ReadCellAsText("main", "C32");
agnt.AgentCity = reader.ReadCellAsText("main", "C36");
agnt.AgentCountry = reader.ReadCellAsText("main", "C37");
agnt.AgentEMail = reader.ReadCellAsText("main", "C42");
agnt.AgentFax = reader.ReadCellAsText("main", "C41");
agnt.AgentFirstName = reader.ReadCellAsText("main", "C39");
agnt.AgentLastName = reader.ReadCellAsText("main", "C38");
agnt.AgentPhone = reader.ReadCellAsText("main", "C40");
agnt.AgentPostalCode = reader.ReadCellAsText("main", "C35");
string street = reader.ReadCellAsText("main", "C33") ?? "";
string number = reader.ReadCellAsText("main", "C34") ?? "";
agnt.AgentStreetAndNumber = string.Format("{0} {1}", street, number);
if (agnt.AgentLastName.IsNullOrEmpty()) agnt.AgentLastName = "-";
// wird nicht mehr entfernt, egal welche Felder gelesen werden
return true;
}
#endregion
}
}