From 2eae1a43bb0db3d2b5ab1f48e16250444796f25e Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Sun, 26 Dec 2021 09:34:13 +0100 Subject: [PATCH] Writer erstellt und generische Schreibfunktionen vorbereitet --- .../OverViewDetailControl.xaml.cs | 2 +- ENI2/ENI2.csproj | 5 + ENI2/Excel/ExcelBase.cs | 98 ++++-- ENI2/Excel/ExcelManager.cs | 21 +- ENI2/Excel/ExcelReader.cs | 76 +---- ENI2/Excel/ExcelUtil.cs | 315 +++--------------- ENI2/Excel/ExcelWriter.cs | 154 +++++++++ 7 files changed, 300 insertions(+), 371 deletions(-) create mode 100644 ENI2/Excel/ExcelWriter.cs diff --git a/ENI2/DetailViewControls/OverViewDetailControl.xaml.cs b/ENI2/DetailViewControls/OverViewDetailControl.xaml.cs index 7f3d6bf3..200d467b 100644 --- a/ENI2/DetailViewControls/OverViewDetailControl.xaml.cs +++ b/ENI2/DetailViewControls/OverViewDetailControl.xaml.cs @@ -11,7 +11,7 @@ using System.Timers; using bsmd.database; using ENI2.EditControls; -using ENI2.Export; +using ENI2.Excel; using ENI2.Report; using System.Windows.Media.Imaging; diff --git a/ENI2/ENI2.csproj b/ENI2/ENI2.csproj index 8075cd0f..fc19fa55 100644 --- a/ENI2/ENI2.csproj +++ b/ENI2/ENI2.csproj @@ -395,6 +395,7 @@ VisitIdDialog.xaml + @@ -710,6 +711,10 @@ report.db PreserveNewest + + Excel\Reference_Sheet_DE.xlsx + PreserveNewest + diff --git a/ENI2/Excel/ExcelBase.cs b/ENI2/Excel/ExcelBase.cs index c2e3ca42..0ce4f50a 100644 --- a/ENI2/Excel/ExcelBase.cs +++ b/ENI2/Excel/ExcelBase.cs @@ -7,7 +7,7 @@ using Microsoft.Office.Interop.Excel; using System.Runtime.InteropServices; using log4net; -namespace ENI2.Export +namespace ENI2.Excel { internal class ExcelBase : IDisposable { @@ -19,52 +19,85 @@ namespace ENI2.Export protected CountryMode _countryMode = CountryMode.NONE; protected Workbooks _excelWorkbooks; - protected Workbook _portcall; + protected Workbook _workBook; protected Application _excelApp; protected Dictionary _nameDict; protected ILog _log; #endregion + #region Construction + + public ExcelBase(string filePath) + { + + _log = LogManager.GetLogger(this.GetType().Name); + + this._excelApp = new Application(); + this._excelApp.DisplayAlerts = false; + this._excelWorkbooks = _excelApp.Workbooks; + } + + #endregion + #region Properties internal CountryMode Mode { get { return _countryMode; } } #endregion - #region Saving + #region protected methods - internal bool Save(string filePath) + protected void InitNameFields() { - bool result = true; - if (this._excelApp == null) return false; - try + _nameDict = new Dictionary(); + int bookCnt = 0; + foreach (Name name in _workBook.Names) { - this._excelApp.SaveWorkspace(filePath); - } - catch (Exception ex) - { - _log.WarnFormat("cannot save workspace: {0}", ex.Message); - result = false; - } - return result; - } + string theValue = name.Value; + // Namensbezug: =SheetZelle. Leere Referenzen überspringen (kommt immer mal wieder vor!) - internal bool SaveCopy(string filePath) - { - bool result = true; - if (this._excelApp == null) return false; - try - { - this._portcall.Saved = true; - this._portcall.SaveCopyAs(filePath); + string nameKey = name.Name; + + try + { + if (nameKey.Contains("!")) + nameKey = nameKey.Substring(nameKey.IndexOf('!') + 1); + } + catch (Exception) + { + _log.DebugFormat("Strange name in Sheet: {0}", nameKey); + continue; + } + + if ((theValue != "=#REF!#REF!") && (theValue != "=#BEZUG!#BEZUG!")) + { + _nameDict[nameKey] = name; + bookCnt++; + } } - catch (Exception ex) + _log.DebugFormat("{0} named ranges found at Workbook level", bookCnt); + + + foreach (Worksheet ws in _workBook.Worksheets) { - _log.WarnFormat("cannot save copy of workbook: {0}", ex.Message); - result = false; + int wsCnt = 0; + foreach (Name name in ws.Names) + { + string theValue = name.Value; + // Namensbezug: =SheetZelle. Leere Referenzen überspringen (kommt immer mal wieder vor!) + if (!_nameDict.ContainsKey(name.Name)) + { + if ((theValue != "=#REF!#REF!") && (theValue != "=#BEZUG!#BEZUG!")) + { + _nameDict[name.Name] = name; + wsCnt++; + } + } + } + if (wsCnt > 0) + _log.DebugFormat("{0} named ranges found in Worksheet {1}", wsCnt, ws.Name); } - return result; } #endregion @@ -75,19 +108,18 @@ namespace ENI2.Export { try { - if (this._portcall != null) + if (this._workBook != null) { - this._portcall.Close(0); + this._workBook.Close(0); _log.Debug("Close Worksheet"); - Marshal.ReleaseComObject(this._portcall); + Marshal.ReleaseComObject(this._workBook); } if (this._excelWorkbooks != null) { this._excelWorkbooks.Close(); _log.Debug("Close Workbooks"); - Marshal.ReleaseComObject(this._excelWorkbooks); - // this._excelWorkbooks.Close(); + Marshal.ReleaseComObject(this._excelWorkbooks); } if (this._excelApp != null) { diff --git a/ENI2/Excel/ExcelManager.cs b/ENI2/Excel/ExcelManager.cs index 740526df..8af5a707 100644 --- a/ENI2/Excel/ExcelManager.cs +++ b/ENI2/Excel/ExcelManager.cs @@ -7,7 +7,7 @@ using System; using System.Collections.Generic; using System.IO; -namespace ENI2.Export +namespace ENI2.Excel { public class ExcelManager { @@ -52,23 +52,18 @@ namespace ENI2.Export _log.Error(ex); } return result; - } - - - // TODO: ich finde man müsste den Folder zu "excel" umbenennen es ist ja nicht nur "import".. + } internal void Export(string fileName, MessageCore core, List messages, out string resultMessage) { - throw new NotImplementedException(); + resultMessage = ""; // Create ExcelWriter - - // Open reference Sheet - - // somehow loop across all messages and derive their label names from it - - // save excel - + using (ExcelWriter ew = new ExcelWriter(fileName)) + { + ew.WriteData(messages, core, out resultMessage); + ew.Save(); + } } } } diff --git a/ENI2/Excel/ExcelReader.cs b/ENI2/Excel/ExcelReader.cs index 8a50c75a..41e056a3 100644 --- a/ENI2/Excel/ExcelReader.cs +++ b/ENI2/Excel/ExcelReader.cs @@ -19,7 +19,7 @@ using System.Runtime.InteropServices; using ENI2.Locode; using bsmd.database; -namespace ENI2.Export +namespace ENI2.Excel { internal class ExcelReader : ExcelBase { @@ -27,65 +27,13 @@ namespace ENI2.Export internal Dictionary ImportValues { get; } = new Dictionary(); - public ExcelReader(string filePath) - { - _log = LogManager.GetLogger(typeof(ExcelReader)); + public ExcelReader(string filePath) : base(filePath) + { - this._excelApp = new Application(); - this._excelApp.DisplayAlerts = false; - this._excelWorkbooks = _excelApp.Workbooks; - - this._portcall = _excelWorkbooks.Open(filePath, 0, true, 5, "", "", false, XlPlatform.xlWindows, "", false, false, 0, false, false, false); - _nameDict = new Dictionary(); - int bookCnt = 0; - foreach(Name name in _portcall.Names) - { - string theValue = name.Value; - // Namensbezug: =SheetZelle. Leere Referenzen überspringen (kommt immer mal wieder vor!) + this._workBook = _excelWorkbooks.Open(filePath, 0, true, 5, "", "", false, XlPlatform.xlWindows, "", false, false, 0, false, false, false); - string nameKey = name.Name; - - try - { - if (nameKey.Contains("!")) - nameKey = nameKey.Substring(nameKey.IndexOf('!') + 1); - } - catch(Exception) - { - _log.DebugFormat("Strange name in Sheet: {0}", nameKey); - continue; - } - - if ((theValue != "=#REF!#REF!") && (theValue != "=#BEZUG!#BEZUG!")) - { - _nameDict[nameKey] = name; - bookCnt++; - } - } - _log.DebugFormat("{0} named ranges found at Workbook level", bookCnt); - - - foreach(Worksheet ws in _portcall.Worksheets) - { - int wsCnt = 0; - foreach(Name name in ws.Names) - { - string theValue = name.Value; - // Namensbezug: =SheetZelle. Leere Referenzen überspringen (kommt immer mal wieder vor!) - if (!_nameDict.ContainsKey(name.Name)) - { - if ((theValue != "=#REF!#REF!") && (theValue != "=#BEZUG!#BEZUG!")) - { - _nameDict[name.Name] = name; - wsCnt++; - } - } - } - if (wsCnt > 0) - _log.DebugFormat("{0} named ranges found in Worksheet {1}", wsCnt, ws.Name); - } - } - + this.InitNameFields(); + } internal string ReadText(string lookup) { @@ -377,7 +325,7 @@ namespace ENI2.Export return result; } - internal DateTime? ReadDate(string lookup, bool noHighlight = false) + internal DateTime? ReadDate(string lookup) { try { @@ -431,11 +379,11 @@ namespace ENI2.Export } } - internal DateTime? ReadDateTime(string dateField, string timeField, bool noHighlight = false) + internal DateTime? ReadDateTime(string dateField, string timeField) { DateTime? result = null; - DateTime? etaDate = this.ReadDate(dateField, noHighlight); - DateTime? etaTime = this.ReadTime(timeField, noHighlight); + DateTime? etaDate = this.ReadDate(dateField); + DateTime? etaTime = this.ReadTime(timeField); if (etaDate != null) { result = new DateTime(etaDate.Value.Year, etaDate.Value.Month, etaDate.Value.Day); @@ -449,7 +397,7 @@ namespace ENI2.Export return result; } - internal DateTime? ReadTime(string lookup, bool noHighlight = false) + internal DateTime? ReadTime(string lookup) { DateTime? result = null; @@ -561,7 +509,7 @@ namespace ENI2.Export return result.Value; } - internal bool? ReadBoolean(string lookup, bool noHighlight = false) + internal bool? ReadBoolean(string lookup) { string val = this.ReadText(lookup); if (val == null) diff --git a/ENI2/Excel/ExcelUtil.cs b/ENI2/Excel/ExcelUtil.cs index ca37be17..b1ec1919 100644 --- a/ENI2/Excel/ExcelUtil.cs +++ b/ENI2/Excel/ExcelUtil.cs @@ -15,7 +15,7 @@ using log4net; using bsmd.database; using ENI2.Locode; -namespace ENI2.Export +namespace ENI2.Excel { public static class ExcelUtil { @@ -43,63 +43,63 @@ namespace ENI2.Export foreach (Message message in messages) { if ((message.MessageNotificationClass == Message.NotificationClass.AGNT) && notificationClasses.Contains(Message.NotificationClass.AGNT)) - { if (ScanAGNT(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanAGNT(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.ATA) && notificationClasses.Contains(Message.NotificationClass.ATA)) - { if (ScanATA(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanATA(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.ATD) && notificationClasses.Contains(Message.NotificationClass.ATD)) - { if (ScanATD(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanATD(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.BKRA) && notificationClasses.Contains(Message.NotificationClass.BKRA)) - { if (ScanBKRA(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanBKRA(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.BKRD) && notificationClasses.Contains(Message.NotificationClass.BKRD)) - { if (ScanBKRD(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanBKRD(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.BPOL) && notificationClasses.Contains(Message.NotificationClass.BPOL)) { if (ScanBPOL(message, messages, messageCore, reader)) SaveMessage(message); } if ((message.MessageNotificationClass == Message.NotificationClass.CREW) && notificationClasses.Contains(Message.NotificationClass.CREW)) - { if (ScanCREW(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanCREW(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.CREWD) && notificationClasses.Contains(Message.NotificationClass.CREWD)) - { if (ScanCREWD(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanCREWD(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.HAZA) && notificationClasses.Contains(Message.NotificationClass.HAZA)) - { if (ScanHAZA(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanHAZA(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.HAZD) && notificationClasses.Contains(Message.NotificationClass.HAZD)) - { if (ScanHAZD(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanHAZD(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.INFO) && notificationClasses.Contains(Message.NotificationClass.INFO)) - { if (ScanINFO(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanINFO(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.LADG) && notificationClasses.Contains(Message.NotificationClass.LADG)) - { if (ScanLADG(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanLADG(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.MDH) && notificationClasses.Contains(Message.NotificationClass.MDH)) { if (ScanMDH(message, messages, messageCore, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.NAME) && notificationClasses.Contains(Message.NotificationClass.NAME)) - { if (ScanNAME(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanNAME(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.NOA_NOD) && notificationClasses.Contains(Message.NotificationClass.NOA_NOD)) { if (ScanNOA_NOD(message, messageCore, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.PAS) && notificationClasses.Contains(Message.NotificationClass.PAS)) - { if (ScanPAS(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanPAS(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.PASD) && notificationClasses.Contains(Message.NotificationClass.PASD)) - { if (ScanPASD(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanPASD(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.POBA) && notificationClasses.Contains(Message.NotificationClass.POBA)) - { if (ScanPOBA(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanPOBA(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.POBD) && notificationClasses.Contains(Message.NotificationClass.POBD)) - { if (ScanPOBD(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanPOBD(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.PRE72H) && notificationClasses.Contains(Message.NotificationClass.PRE72H)) - { if (ScanPRE72H(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanPRE72H(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.SEC) && notificationClasses.Contains(Message.NotificationClass.SEC)) - { if (ScanSEC(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanSEC(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.SERV) && notificationClasses.Contains(Message.NotificationClass.SERV)) - { if (ScanSERV(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanSERV(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.STAT) && notificationClasses.Contains(Message.NotificationClass.STAT)) { if (ScanSTAT(message, messageCore, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.STO) && notificationClasses.Contains(Message.NotificationClass.STO)) - { if (ScanSTO(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanSTO(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.TIEFA) && notificationClasses.Contains(Message.NotificationClass.TIEFA)) - { if (ScanTIEFA(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanTIEFA(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.TIEFD) && notificationClasses.Contains(Message.NotificationClass.TIEFD)) - { if (ScanTIEFD(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanTIEFD(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.TOWA) && notificationClasses.Contains(Message.NotificationClass.TOWA)) - { if (ScanTOWA(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanTOWA(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.TOWD) && notificationClasses.Contains(Message.NotificationClass.TOWD)) - { if (ScanTOWD(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanTOWD(message, reader)) SaveMessage(message); continue; } if ((message.MessageNotificationClass == Message.NotificationClass.WAS) && notificationClasses.Contains(Message.NotificationClass.WAS)) - { if (ScanWAS(message, messageCore, reader)) SaveMessage(message); continue; } + { if (ScanWAS(message, reader)) SaveMessage(message); continue; } } @@ -129,7 +129,7 @@ namespace ENI2.Export #region ATA - private static bool ScanATA(Message ataMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanATA(Message ataMessage, ExcelReader reader) { if (ataMessage.Elements.Count == 0) { @@ -152,7 +152,7 @@ namespace ENI2.Export #region ATD - private static bool ScanATD(Message atdMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanATD(Message atdMessage, ExcelReader reader) { if (atdMessage.Elements.Count == 0) { @@ -175,7 +175,7 @@ namespace ENI2.Export #region TIEFA - private static bool ScanTIEFA(Message tiefaMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanTIEFA(Message tiefaMessage, ExcelReader reader) { if (tiefaMessage.Elements.Count == 0) { @@ -194,7 +194,7 @@ namespace ENI2.Export #region TIEFD - private static bool ScanTIEFD(Message tiefdMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanTIEFD(Message tiefdMessage, ExcelReader reader) { if(tiefdMessage.Elements.Count == 0) { @@ -213,7 +213,7 @@ namespace ENI2.Export #region NAME - private static bool ScanNAME(Message nameMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanNAME(Message nameMessage, ExcelReader reader) { if (nameMessage.Elements.Count == 0) { @@ -294,7 +294,7 @@ namespace ENI2.Export #region POBA - private static bool ScanPOBA(Message pobaMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanPOBA(Message pobaMessage, ExcelReader reader) { if(pobaMessage.Elements.Count == 0) { @@ -315,7 +315,7 @@ namespace ENI2.Export #region POBD - private static bool ScanPOBD(Message pobdMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanPOBD(Message pobdMessage, ExcelReader reader) { if(pobdMessage.Elements.Count == 0) { @@ -336,7 +336,7 @@ namespace ENI2.Export #region HAZA - private static bool ScanHAZA(Message hazaMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanHAZA(Message hazaMessage, ExcelReader reader) { if(hazaMessage.Elements.Count == 0) @@ -603,7 +603,7 @@ namespace ENI2.Export #region HAZD - private static bool ScanHAZD(Message hazdMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanHAZD(Message hazdMessage, ExcelReader reader) { if (hazdMessage.Elements.Count == 0) { @@ -864,7 +864,7 @@ namespace ENI2.Export #region INFO - private static bool ScanINFO(Message infoMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanINFO(Message infoMessage, ExcelReader reader) { if(infoMessage.Elements.Count == 0) { @@ -1011,7 +1011,7 @@ namespace ENI2.Export noa_nod.ETDFromLastPort = reader.ReadDateTime("NOA_NOD.ETDDateFromLastPort", "NOA_NOD.ETDTimeFromLastPort"); noa_nod.ETAToNextPort = reader.ReadDateTime("NOA_NOD.ETADateToNextPort", "NOA_NOD.ETATimeToNextPort"); // DK - noa_nod.IsAnchored = reader.ReadBoolean("NOA_NOD.IsAnchored", reader.Mode == ExcelReader.CountryMode.DE); + noa_nod.IsAnchored = reader.ReadBoolean("NOA_NOD.IsAnchored"); return true; } @@ -1020,7 +1020,7 @@ namespace ENI2.Export #region AGNT - private static bool ScanAGNT(Message agntMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanAGNT(Message agntMessage, ExcelReader reader) { if(agntMessage.Elements.Count == 0) { @@ -1041,7 +1041,7 @@ namespace ENI2.Export #region WAS - private static bool ScanWAS(Message wasMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanWAS(Message wasMessage, ExcelReader reader) { if (wasMessage.Elements.Count == 0) { @@ -1379,7 +1379,7 @@ namespace ENI2.Export #region SEC - private static bool ScanSEC(Message secMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanSEC(Message secMessage, ExcelReader reader) { if (secMessage.Elements.Count == 0) { @@ -1429,8 +1429,8 @@ namespace ENI2.Export sec.KielCanalPassagePlanned = (secKielArrival ?? false) || (secKielDeparture ?? false); // if (sec.KielCanalPassagePlanned ?? false) // { - sec.KielCanalPassagePlannedIncomming = reader.ReadDateTime("SEC.ETADateKielCanalPassagePlannedIncomming", "SEC.ETATimeKielCanalPassagePlannedIncomming", !(secKielArrival ?? false)); - sec.KielCanalPassagePlannedOutgoing = reader.ReadDateTime("SEC.ETADateKielCanalPassagePlannedOutgoing", "SEC.ETATimeKielCanalPassagePlannedOutgoing", !(secKielDeparture ?? false)); + sec.KielCanalPassagePlannedIncomming = reader.ReadDateTime("SEC.ETADateKielCanalPassagePlannedIncomming", "SEC.ETATimeKielCanalPassagePlannedIncomming"); + sec.KielCanalPassagePlannedOutgoing = reader.ReadDateTime("SEC.ETADateKielCanalPassagePlannedOutgoing", "SEC.ETATimeKielCanalPassagePlannedOutgoing"); // } if ((!sec.KielCanalPassagePlanned ?? false) && sec.KielCanalPassagePlannedIncomming.HasValue) sec.KielCanalPassagePlanned = true; @@ -1540,7 +1540,7 @@ namespace ENI2.Export #region BKRA - private static bool ScanBKRA(Message bkraMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanBKRA(Message bkraMessage,ExcelReader reader) { bkraMessage.DeleteElements(); for (int i = 1; i <= bkraMessage.NumberOfExcelRows; i++) @@ -1570,7 +1570,7 @@ namespace ENI2.Export #region BKRD - private static bool ScanBKRD(Message bkrdMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanBKRD(Message bkrdMessage, ExcelReader reader) { bkrdMessage.DeleteElements(); for (int i = 1; i <= bkrdMessage.NumberOfExcelRows; i++) @@ -1599,7 +1599,7 @@ namespace ENI2.Export #region TOWA - private static bool ScanTOWA(Message towaMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanTOWA(Message towaMessage, ExcelReader reader) { // 24.4.21: TOWA beim Excel import bool hasTOWAMarker = false; @@ -1655,7 +1655,7 @@ namespace ENI2.Export #region TOWD - private static bool ScanTOWD(Message towdMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanTOWD(Message towdMessage,ExcelReader reader) { // 24.4.21: TOWD beim Excel import @@ -1708,7 +1708,7 @@ namespace ENI2.Export #region PRE72H - private static bool ScanPRE72H(Message pre72hMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanPRE72H(Message pre72hMessage, ExcelReader reader) { if (pre72hMessage.Elements.Count == 0) { @@ -1729,7 +1729,7 @@ namespace ENI2.Export #region SERV - private static bool ScanSERV(Message servMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanSERV(Message servMessage, ExcelReader reader) { if(servMessage.Elements.Count == 0) { @@ -1750,7 +1750,7 @@ namespace ENI2.Export #region STO - private static bool ScanSTO(Message stoMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanSTO(Message stoMessage, ExcelReader reader) { stoMessage.DeleteElements(); @@ -1788,7 +1788,7 @@ namespace ENI2.Export #region LADG - private static bool ScanLADG(Message ladgMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanLADG(Message ladgMessage, ExcelReader reader) { ladgMessage.DeleteElements(); @@ -1875,7 +1875,7 @@ namespace ENI2.Export #region CREW - private static bool ScanCREW(Message crewMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanCREW(Message crewMessage, ExcelReader reader) { crewMessage.DeleteElements(); @@ -1921,7 +1921,7 @@ namespace ENI2.Export return true; } - private static bool ScanCREWD(Message crewdMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanCREWD(Message crewdMessage, ExcelReader reader) { crewdMessage.DeleteElements(); // CREW DEPARTURE @@ -1971,7 +1971,7 @@ namespace ENI2.Export #region PAS - private static bool ScanPAS(Message pasMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanPAS(Message pasMessage, ExcelReader reader) { pasMessage.DeleteElements(); @@ -2026,7 +2026,7 @@ namespace ENI2.Export #region PASD - private static bool ScanPASD(Message pasMessage, MessageCore messageCore, ExcelReader reader) + private static bool ScanPASD(Message pasMessage, ExcelReader reader) { pasMessage.DeleteElements(); @@ -2149,7 +2149,7 @@ namespace ENI2.Export } - } + } #endregion @@ -2179,211 +2179,6 @@ namespace ENI2.Export return result; } - /// - /// Check with cell values if this message core is already in our DB - /// - private static MessageCore LookupMessageCore(ExcelReader reader, out string message) - { - // lookup using field values - MessageCore result = null; - DateTime? eta = null; - string poc; - string imo = null; - message = string.Empty; - bool isTransit = false; - - // first check with visit/transit ID - string visitTransitId = reader.ReadTextNoWhitespace("ID"); - - if (visitTransitId != null) - { - if (bsmd.database.Util.IsVisitId(visitTransitId)) - { - result = DBManager.Instance.GetMessageCoreByVisitId(visitTransitId); - } - else if (bsmd.database.Util.IsTransitId(visitTransitId)) - { - result = DBManager.Instance.GetMessageCoreByTransitId(visitTransitId); - } - } - else - { - message = "Visit / Transit Id missing!"; - return null; - } - - if (result != null) - { - // copy poc/imo/eta to return sheet - poc = reader.ReadText("Visit.PortOfCall"); - imo = reader.ReadText("Visit.IMONumber"); - eta = reader.ReadDateTime("NOA_NOD.ETADateToPortOfCall", "NOA_NOD.ETATimeToPortOfCall"); - result.HerbergReportType = reader.ReadText("ReferenceNumber"); - } - else - { - // lookup poc, imo, eta - poc = reader.ReadText("Visit.PortOfCall"); - - if (poc != null) - { - - // Prüfen auf Transit - if (poc.IndexOf("CANAL", StringComparison.OrdinalIgnoreCase) >= 0 || poc.Equals("ZZNOK", StringComparison.OrdinalIgnoreCase)) - { - poc = "ZZNOK"; - isTransit = true; - } - else - { - // Im Sheet könnte der Name statt des LOCODES stehen! - if (!RuleEngine.IsGermanLocode(poc)) - { - string aGermanPortName = poc; - - if(poc.Contains(',')) // irgendwas wie "Hamburg, Germany" - { - aGermanPortName = poc.Split(',')[0]; - } - - if (RuleEngine.IsGermanLocode(aGermanPortName)) - { - poc = aGermanPortName; - } - else - { - // somehow lookup LOCODE from the port's name! - poc = LocodeDB.LocodeGERFromCity(aGermanPortName); - } - - // okay, könnte DK Locode etc sein.. - if(poc == null) - { - if(aGermanPortName?.Length == 5) // possible locode? - { - if (LocodeDB.PortNameFromLocode(aGermanPortName) != null) - poc = aGermanPortName; - } - } - - if(poc == null) - { - List locodes = LocodeDB.AllLocodesForCityName(aGermanPortName); - if (locodes.Count > 0) - poc = locodes[0]; - } - - } - - if (poc == null) - { - message = "invalid PoC"; - return null; - } - } - - imo = reader.ReadText("Visit.IMONumber"); - - // ETA - if (poc != null) - eta = reader.ReadDateTime("NOA_NOD.ETADateToPortOfCall", "NOA_NOD.ETATimeToPortOfCall"); - - if ((imo != null) && (eta.HasValue) && (poc != null)) - { - result = DBManager.Instance.GetMessageCoreByShipInfos(imo, eta.Value, poc); - if(result != null) - { - _log.InfoFormat("Core [{3}] found for IMO {0}, ETA {1}, Poc {2}", imo, eta, poc, result.Id); - result.HerbergReportType = reader.ReadText("ReferenceNumber"); - - // Spezialfall: Wenn ein Core gefunden wird, dieser jedoch eine andere Visit/Transit Id hat als die Anmeldung ist das eine Neuanlage! - // Eingebaut 17.11.17 - if(!visitTransitId.IsNullOrEmpty() && !result.DisplayId.IsNullOrEmpty() && - (visitTransitId != result.DisplayId)) - { - _log.WarnFormat("Different visit ID ({0} (Sheet) {1} (Core) for the same decl. treating as NEW!", visitTransitId, result.DisplayId); - result = null; - } - - } - } - } - - else - { - message = string.Format("Port of call missing or not found for IMO {0}", imo); - return null; - } - } - - if (result == null) - { - if (imo == null) - { - message = "IMO number missing or not found"; - return null; - } - - if(poc == null) - { - message = string.Format("Port of call missing or not found for IMO {0}", imo); - return null; - } - - if(eta == null) - { - message = string.Format("ETA missing or not found for IMO {0}", imo); - return null; - } - - // neuen MessageCore erzeugen - result = new MessageCore(); - - result.Incoming = true; - result.HerbergFormGuid = Guid.Empty; - result.IMO = imo; - result.IsTransit = isTransit; - result.ReportStatus = MessageCore.ReportStatusEnum.NONE; // Bestätigungs-Sheet reicht als Report, keine separate Generierung - result.BSMDStatusInternal = MessageCore.BSMDStatus.PREPARE; - result.PoC = poc; - result.Portname = LocodeDB.PortNameFromLocode(poc); - - result.HerbergEmailContactReportingVessel = reader.ReadText("ShipMail"); - result.HerbergReportType = reader.ReadText("ReferenceNumber"); - - if (result.IsTransit) - result.ETAKielCanal = eta; - else - result.ETA = eta; - - if (result.IMO.Length > 7) - { - _log.WarnFormat("IMO {0} is longer than 7 chars, truncating!", result.IMO); - result.IMO = result.IMO.Substring(0, 7); - } - - if((result.IMO.Length == 7) && !bsmd.database.Util.IsIMOValid(result.IMO)) - { - _log.WarnFormat("IMO {0} possibly invalid (checksum number violation)", result.IMO); - } - - if(visitTransitId != null) - { - if (bsmd.database.Util.IsTransitId(visitTransitId)) - result.TransitId = visitTransitId; - else - result.VisitId = visitTransitId; - } - - DBManager.Instance.Save(result); - // Meldeklassen erzeugen um doppelte Generierung über ENI-2 zu vermeiden - bsmd.database.Util.CreateMessagesForCore(result, new List(), null); - - } - - return result; - } - #endregion #region GetMessageWithType diff --git a/ENI2/Excel/ExcelWriter.cs b/ENI2/Excel/ExcelWriter.cs new file mode 100644 index 00000000..97ee8458 --- /dev/null +++ b/ENI2/Excel/ExcelWriter.cs @@ -0,0 +1,154 @@ +// Copyright (c) 2017- schick Informatik +// Description: +// + +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Reflection; + +using bsmd.database; + +namespace ENI2.Excel +{ + internal class ExcelWriter : ExcelBase + { + + #region Fields + + private readonly string _saveFilePath; + + #endregion + + #region Construction + + public ExcelWriter(string filePath) : base(filePath) + { + string refFilePath = @"Excel\Reference_Sheet_DE.xlsx"; + this._workBook = _excelWorkbooks.Open(refFilePath, 0, true, 5, "", "", false, XlPlatform.xlWindows, "", false, false, 0, false, false, false); + + this.InitNameFields(); + _saveFilePath = filePath; + } + + #endregion + + #region public methods + + public void WriteData(List messages, MessageCore core, out string resultMessage) + { + resultMessage = ""; + + foreach (Message message in messages) + { + try + { + this.WriteMessage(message); + } + catch (Exception ex) + { + resultMessage += string.Format("{2}:{0}{1}", ex.Message, Environment.NewLine, message.MessageNotificationClassDisplay); + } + } + + WriteCore(core); + + } + + public void Save() + { + this._workBook.SaveAs(_saveFilePath, XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, + Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); + this._workBook.Saved = true; + this._workBook.Close(); + } + + #endregion + + #region private excel field writing + + private void WriteMessage(DatabaseEntity dbEntity) + { + Type objType = dbEntity.GetType(); + List props = new List(); + + // add lookup properties to scan list + props.AddRange(objType.GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(LookupNameAttribute)))); + + foreach (PropertyInfo property in props) + { + object propValue = property.GetValue(dbEntity, null); + string value = (propValue == null) ? string.Empty : propValue.ToString(); + LookupNameAttribute lookupNameAttribute = Attribute.GetCustomAttribute(property, typeof(LookupNameAttribute)) as LookupNameAttribute; + bool success = true; + + if (property.PropertyType == typeof(DateTime?)) + { + success = this.WriteDate(lookupNameAttribute.LookupName, property.GetValue(dbEntity)); + } + else if (property.PropertyType == typeof(double?)) + { + success = this.WriteNumber(lookupNameAttribute.LookupName, property.GetValue(dbEntity)); + } + else if (property.PropertyType == typeof(string)) + { + success = this.WriteText(lookupNameAttribute.LookupName, property.GetValue(dbEntity)); + } + else if (property.PropertyType == typeof(int?)) + { + success = this.WriteNumber(lookupNameAttribute.LookupName, property.GetValue(dbEntity)); + } + else if (property.PropertyType == typeof(byte?)) + { + success = this.WriteNumber(lookupNameAttribute.LookupName, property.GetValue(dbEntity)); + } + else if (property.PropertyType == typeof(Boolean?)) + { + success = this.WriteBoolean(lookupNameAttribute.LookupName, property.GetValue(dbEntity)); + } + else + { + string message = string.Format("unhandled property type: {0} for lookup {1}", property.PropertyType, lookupNameAttribute.LookupName); + _log.Warn(message); + } + + if(!success) + { + string message = string.Format("Sheet does not contain lookup field {0}", lookupNameAttribute.LookupName); + _log.Error(message); + throw new FormatException(message); + } + } + } + + private void WriteCore(MessageCore core) + { + + } + + private bool WriteBoolean(string lookupName, object v) + { + throw new NotImplementedException(); + } + + private bool WriteText(string lookupName, object v) + { + throw new NotImplementedException(); + } + + private bool WriteNumber(string lookupName, object v) + { + throw new NotImplementedException(); + } + + private bool WriteDate(string lookupName, object v) + { + throw new NotImplementedException(); + } + + #endregion + + } +}