// 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; using ENI2.Report; using ENI2.Util; namespace ENI2.Excel { internal class ExcelWriter : ExcelBase { #region Fields private readonly string _saveFilePath; #endregion #region Construction public ExcelWriter(string filePath, bool isRefSheet) : base(filePath) { string filename = @"Excel\EU-NoAD-Data-Collecting-Tool-5_0.xlsx"; if (isRefSheet) filename = @"Excel\Reference_Sheet_DE.xlsx"; string refFilePath = System.IO.Path.Combine(Environment.CurrentDirectory, filename); 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, bool isRefSheet) { resultMessage = ""; foreach (Message message in messages) { try { switch(message.MessageNotificationClass) { case Message.NotificationClass.AGNT: if (message.Elements[0] is AGNT agnt) this.WriteMessage(agnt); break; case Message.NotificationClass.ATA: if (message.Elements[0] is ATA ata) { WriteDate("ATA.ATADatePortOfCall", ata.ATAPortOfCall); WriteTime("ATA.ATATimePortOfCall", ata.ATAPortOfCall); } break; case Message.NotificationClass.ATD: if (message.Elements[0] is ATD atd) { WriteDate("ATD.ATDDatePortOfCall", atd.ATDPortOfCall); WriteTime("ATD.ATDTimePortOfCall", atd.ATDPortOfCall); } break; case Message.NotificationClass.BKRA: this.WriteBKRA(message); break; case Message.NotificationClass.BKRD: this.WriteBKRD(message); break; case Message.NotificationClass.BPOL: if (message.Elements[0] is BPOL bpol) { this.WriteMessage(bpol); this.WriteItineraries(bpol); } break; case Message.NotificationClass.CREW: this.WriteCREW(message, true, isRefSheet); break; case Message.NotificationClass.CREWD: this.WriteCREW(message, false, isRefSheet); break; case Message.NotificationClass.HAZA: this.WriteHAZ(message, true); break; case Message.NotificationClass.HAZD: this.WriteHAZ(message, false); break; case Message.NotificationClass.INFO: if (message.Elements[0] is INFO info) { this.WriteMessage(info); this.WriteINFO(info, isRefSheet); } break; case Message.NotificationClass.LADG: this.WriteLADG(message, isRefSheet); break; case Message.NotificationClass.MDH: if (message.Elements[0] is MDH mdh) { this.WriteMessage(mdh); this.WriteMDH(mdh); } break; case Message.NotificationClass.NAME: if (message.Elements[0] is NAME name) this.WriteMessage(name); break; case Message.NotificationClass.NOA_NOD: if (message.Elements[0] is NOA_NOD noa_nod) this.WriteNOA_NOD(noa_nod, core.IsTransit, isRefSheet); break; case Message.NotificationClass.PAS: this.WritePAS(message, true, isRefSheet); break; case Message.NotificationClass.PASD: this.WritePAS(message, false, isRefSheet); break; case Message.NotificationClass.POBA: if (message.Elements[0] is POBA poba) this.WriteMessage(poba); break; case Message.NotificationClass.POBD: if (message.Elements[0] is POBD pobd) this.WriteMessage(pobd); break; case Message.NotificationClass.PRE72H: if (message.Elements[0] is PRE72H pre72h) { this.WriteMessage(pre72h); this.WritePRE72H(pre72h); } break; case Message.NotificationClass.SEC: if (message.Elements[0] is SEC sec) this.WriteMessage(sec); this.WriteSEC(message, isRefSheet); break; case Message.NotificationClass.SERV: this.WriteSERV(message); break; case Message.NotificationClass.STAT: if (message.Elements[0] is STAT stat) { this.WriteMessage(stat); this.WriteSTAT(stat, isRefSheet); } break; case Message.NotificationClass.STO: this.WriteSTO(message); break; case Message.NotificationClass.TIEFA: if (message.Elements[0] is TIEFA tiefa) { if (tiefa.DraughtUponArrival_DMT.HasValue) WriteNumber("TIEFA.DraughtUponArrival_DMT", tiefa.DraughtUponArrival_DMT.Value / 10.0); } break; case Message.NotificationClass.TIEFD: if (message.Elements[0] is TIEFD tiefd) { if (tiefd.DraughtUponDeparture_DMT.HasValue) WriteNumber("TIEFD.DraughtUponDeparture_DMT", tiefd.DraughtUponDeparture_DMT.Value / 10.0); } break; case Message.NotificationClass.TOWA: WriteTOWA(message, isRefSheet); break; case Message.NotificationClass.TOWD: WriteTOWD(message, isRefSheet); break; case Message.NotificationClass.WAS: if (message.Elements[0] is WAS was) this.WriteMessage(was); WriteWAS(message, isRefSheet); break; case Message.NotificationClass.WAS_RCPT: WriteWAS_RCPT(message); break; default: _log.InfoFormat("skip writing message class {0}", message.MessageNotificationClassDisplay); break; } } 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; } #endregion #region private excel field writing // generische Methode zum Schreiben der Felder nach Excel für Meldeklassen, die kein // Listentyp sind 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?)) { object boolVal = property.GetValue(dbEntity); if(boolVal != null) success = this.WriteBoolean(lookupNameAttribute.LookupName, boolVal); } 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); System.Diagnostics.Trace.WriteLine(message); // throw new FormatException(message); } } } private void WriteCore(MessageCore core) { WriteText("ID", core.IsTransit ? core.TransitId : core.VisitId); WriteText("Visit.PortOfCall", core.PoC); WriteText("Visit.IMONumber", core.IMO); WriteText("ReferenceNumber", core.HerbergReportType); WriteText("ShipMail", core.HerbergEmailContactReportingVessel); } #region list / special message classes #region LADG private void WriteLADG(Message ladgMessage, bool isRefSheet) { for (int i = 0; i < Math.Min(ladgMessage.NumberOfExcelRows, ladgMessage.Elements.Count); i++) { string lnCHT = string.Format("LADG.CargoHandlingType_{0}", i + 1); string lnType = string.Format("LADG.CargoType_{0}", i + 1); string lnCNOI = string.Format("LADG.CargoNumberOfItems_{0}", i + 1); string lnCGQ = string.Format("LADG.CargoGrossQuantity_TNE_{0}", i + 1); string lnLoad = string.Format("LADG.CargoPortOfLoading_{0}", i + 1); string lnDis = string.Format("LADG.CargoPortOfDischarge_{0}", i + 1); string lnLACode = string.Format("LADG.CargoLACode_{0}", i + 1); string lnZusatz = string.Format("LADG.CargoTypeZusatz_{0}", i + 1); LADG ladg = ladgMessage.Elements[i] as LADG; if (ladg.CargoHandlingType.HasValue) { switch (ladg.CargoHandlingType) { case 0: WriteText(lnCHT, "load"); break; case 1: WriteText(lnCHT, "discharge"); break; case 2: WriteText(lnCHT, "transit"); break; default: break; } } if (isRefSheet) { WriteText(lnType, ladg.CargoCodeNST); } else { if (ReportDocument.cargoCodesNST.TryGetValue(ladg.CargoCodeNST, out string cargoCode)) WriteText(lnType, cargoCode); } WriteText(lnZusatz, ladg.CargoCodeNST_3); WriteNumber(lnCNOI, ladg.CargoNumberOfItems); WriteNumber(lnCGQ, ladg.CargoGrossQuantity_TNE); WriteText(lnLoad, isRefSheet ? ladg.PortOfLoading : Locode.LocodeDB.LocationNameFromLocode(ladg.PortOfLoading)); WriteText(lnDis, isRefSheet ? ladg.PortOfDischarge : Locode.LocodeDB.LocationNameFromLocode(ladg.PortOfDischarge)); WriteNumber(lnLACode, ladg.CargoLACode); } } #endregion #region BKRA private void WriteBKRA(Message bkraMessage) { for (int i = 0; i < Math.Min(bkraMessage.NumberOfExcelRows, bkraMessage.Elements.Count); i++) { string lnQuantity = string.Format("BKRA.BunkerFuelQuantity_TNE_{0}", i + 1); string lnType = string.Format("BKRA.BunkerFuelType_{0}", i + 1); BRKA brka = bkraMessage.Elements[i] as BRKA; WriteNumber(lnQuantity, brka.BunkerFuelQuantity_TNE); WriteText(lnType, brka.BunkerFuelType); } } #endregion #region BKRD private void WriteBKRD(Message bkrdMessage) { for (int i = 0; i < Math.Min(bkrdMessage.NumberOfExcelRows, bkrdMessage.Elements.Count); i++) { string lnQuantity = string.Format("BKRD.BunkerFuelQuantity_TNE_{0}", i + 1); string lnType = string.Format("BKRD.BunkerFuelType_{0}", i + 1); BRKD brkd = bkrdMessage.Elements[i] as BRKD; WriteNumber(lnQuantity, brkd.BunkerFuelQuantity_TNE); WriteText(lnType, brkd.BunkerFuelType); } } #endregion #region BPOL private void WriteItineraries(BPOL bpol) { for (int i = 0; i < Math.Min(10, bpol.PortOfItineraries.Count); i++) { string bpolName = string.Format("BPOL.PortOfItineraryName_{0}", i + 1); string bpolLocode = string.Format("BPOL.PortOfItineraryLoCode_{0}", i + 1); string bpolETADate = string.Format("BPOL.PortOfItineraryETADate_{0}", i + 1); string bpolETATime = string.Format("BPOL.PortOfItineraryETATime_{0}", i + 1); PortOfItinerary poi = bpol.PortOfItineraries[i]; WriteText(bpolName, poi.PortOfItineraryName); WriteText(bpolLocode, poi.PortOfItineraryLocode); if (poi.PortOfItineraryETA.HasValue) { WriteDate(bpolETADate, poi.PortOfItineraryETA.Value); WriteTime(bpolETATime, poi.PortOfItineraryETA.Value); } } } #endregion #region CREW private void WriteCREW(Message crewMessage, bool isArrival, bool isRefSheet) { if (crewMessage.Elements.Count > 0) { WriteBoolean(string.Format("CREW{0}.NotificationSchengen", isArrival ? "" : "D"), ((CREW)crewMessage.Elements[0]).NotificationSchengen); WriteBoolean(string.Format("CREW{0}.NotificationPAX", isArrival ? "" : "D"), ((CREW)crewMessage.Elements[0]).NotificationPAX); } for(int i = 0; i 0) { WriteBoolean(string.Format("PAS{0}.NotificationSchengen", isArrival ? "" : "D"), ((PAS)pasMessage.Elements[0]).NotificationSchengen); WriteBoolean(string.Format("PAS{0}.NotificationPAX", isArrival ? "" : "D"), ((PAS)pasMessage.Elements[0]).NotificationPAX); } for(int i = 0; i < Math.Min(pasMessage.NumberOfExcelRows, pasMessage.Elements.Count); i++) { string pasLastName = string.Format("PAS{1}.PassengerLastName_{0}", i + 1, isArrival ? "" : "D"); string pasFirstName = string.Format("PAS{1}.PassengerFirstName_{0}", i + 1, isArrival ? "" : "D"); string pasGender = string.Format("PAS{1}.PassengerGender_{0}", i + 1, isArrival ? "" : "D"); string pasNationality = string.Format("PAS{1}.PassengerNationality_{0}", i + 1, isArrival ? "" : "D"); string pasEmbarkation = string.Format("PAS{1}.PassengerPortOfEmbarkation_{0}", i + 1, isArrival ? "" : "D"); string pasDebarkation = string.Format("PAS{1}.PassengerPortOfDisembarkation_{0}", i + 1, isArrival ? "" : "D"); string pasTransit = string.Format("PAS{1}.PassengerInTransit_{0}", i + 1, isArrival ? "" : "D"); string pasPlaceOfBirth = string.Format("PAS{1}.PassengerPlaceOfBirth_{0}", i + 1, isArrival ? "" : "D"); string pasDateOfBirth = string.Format("PAS{1}.PassengerDateOfBirth_{0}", i + 1, isArrival ? "" : "D"); string pasIdentDocType = string.Format("PAS{1}.PassengerIdentityDocumentType_{0}", i + 1, isArrival ? "" : "D"); string pasIdentDocId = string.Format("PAS{1}.PassengerIdentityDocumentId_{0}", i + 1, isArrival ? "" : "D"); string pasVisaNo = string.Format("PAS{1}.PassengerVisaNumber_{0}", i + 1, isArrival ? "" : "D"); string pasIssuing = string.Format("PAS{1}.PassengerIdentityDocumentIssuingState_{0}", i + 1, isArrival ? "" : "D"); string pasExpiry = string.Format("PAS{1}.PassengerIdentityDocumentExpiryDate_{0}", i + 1, isArrival ? "" : "D"); string pasCountry = string.Format("PAS{1}.CountryOfBirth_{0}", i + 1, isArrival ? "" : "D"); string pasEmergencyCare = string.Format("PAS{1}.EmergencyCare_{0}", i + 1, isArrival ? "" : "D"); string pasEmergencyContact = string.Format("PAS{1}.EmergencyContactNumber_{0}", i + 1, isArrival ? "" : "D"); PAS pas = pasMessage.Elements[i] as PAS; // PASD is derived from PAS daher ok! WriteText(pasLastName, pas.PassengerLastName); WriteText(pasFirstName, pas.PassengerFirstName); WriteGender(pasGender, pas.PassengerGender); WriteText(pasNationality, pas.PassengerNationality); WriteText(pasEmbarkation, pas.PassengerPortOfEmbarkation); WriteText(pasDebarkation, pas.PassengerPortOfDisembarkation); WriteBoolean(pasTransit, pas.PassengerInTransit); WriteText(pasPlaceOfBirth, pas.PassengerPlaceOfBirth); if(pas.PassengerDateOfBirth.HasValue) WriteDate(pasDateOfBirth, pas.PassengerDateOfBirth); WriteText(pasIdentDocType, pas.PassengerIdentityDocumentTypeDisplay); WriteText(pasIdentDocId, pas.PassengerIdentityDocumentId); WriteText(pasVisaNo, pas.PassengerVisaNumber); if (isRefSheet) { WriteText(pasIssuing, pas.PassengerIdentityDocumentIssuingState); } else { if(!pas.PassengerIdentityDocumentIssuingState.Equals("XX")) WriteText(pasIssuing, pas.PassengerIdentityDocumentIssuingState); } if (isRefSheet) { WriteDate(pasExpiry, pas.PassengerIdentityDocumentExpiryDate); } else { if (pas.PassengerIdentityDocumentExpiryDate != new DateTime(2100, 12, 31)) WriteDate(pasExpiry, pas.PassengerIdentityDocumentExpiryDate); } WriteText(pasCountry, pas.PassengerCountryOfBirth); WriteText(pasEmergencyCare, pas.EmergencyCare); WriteText(pasEmergencyContact, pas.EmergencyContactNumber); } } #endregion #region SEC private void WriteSEC(Message secMessage, bool isRefSheet) { if (secMessage.Elements.Count == 0) return; SEC sec = secMessage.Elements[0] as SEC; if(sec.ISSCType.HasValue) { switch(sec.ISSCType.Value) { case 0: WriteText("SEC.ISSCType", "full"); break; case 1: WriteText("SEC.ISSCType", "interim"); break; } } if (sec.CurrentShipSecurityLevel.HasValue) WriteText("SEC.CurrentShipSecurityLevel", sec.CurrentShipSecurityLevel.Value.ToString()); WriteText("SEC.ISSCIssuerType", sec.ISSCIssuerTypeDisplay); if(sec.GeneralDescriptionOfCargo.HasValue) { switch(sec.GeneralDescriptionOfCargo.Value) { case 0: WriteText("SEC.GeneralDescriptionOfCargo", "container"); break; case 1: WriteText("SEC.GeneralDescriptionOfCargo", "vehicles"); break; case 2: WriteText("SEC.GeneralDescriptionOfCargo", "convent"); break; case 3: WriteText("SEC.GeneralDescriptionOfCargo", "dry"); break; case 4: WriteText("SEC.GeneralDescriptionOfCargo", "liquid"); break; case 5: WriteText("SEC.GeneralDescriptionOfCargo", "empty"); break; } } WriteBoolean("SEC.KielCanalPassagePlanned_Arrival", sec.KielCanalPassagePlannedIncomming.HasValue); WriteBoolean("SEC.KielCanalPassagePlanned_Departure", sec.KielCanalPassagePlannedOutgoing.HasValue); if(sec.KielCanalPassagePlannedIncomming.HasValue) { WriteDate("SEC.ETADateKielCanalPassagePlannedIncomming", sec.KielCanalPassagePlannedIncomming.Value); WriteTime("SEC.ETATimeKielCanalPassagePlannedIncomming", sec.KielCanalPassagePlannedIncomming.Value); } if(sec.KielCanalPassagePlannedOutgoing.HasValue) { WriteDate("SEC.ETADateKielCanalPassagePlannedOutgoing", sec.KielCanalPassagePlannedOutgoing.Value); WriteTime("SEC.ETATimeKielCanalPassagePlannedOutgoing", sec.KielCanalPassagePlannedOutgoing.Value); } for(int i = 0; i < Math.Min(sec.LastTenPortFacilitesCalled.Count, 10); i++) { string portName = string.Format("SEC.PortFacilityPortName_{0}", i + 1); string portCountry = string.Format("SEC.PortFacilityPortCountry_{0}", i + 1); string portLocode = string.Format("SEC.PortFacilityPortLoCode_{0}", i + 1); string portDateOfArrival = string.Format("SEC.PortFacilityDateOfArrival_{0}", i + 1); string portDateOfDeparture = string.Format("SEC.PortFacilityDateOfDeparture_{0}", i + 1); string portShipSecLevel = string.Format("SEC.PortFacilityShipSecurityLevel_{0}", i + 1); string portGISISCode = string.Format("SEC.PortFacilityGISISCode_{0}", i + 1); string portSecMatters = string.Format("SEC.PortFacilitySecurityMattersToReport_{0}", i + 1); LastTenPortFacilitiesCalled l10 = sec.LastTenPortFacilitesCalled[i]; WriteText(portName, l10.PortFacilityPortName); WriteText(portCountry, l10.PortFacilityPortCountry); WriteText(portLocode, l10.PortFacilityPortLoCode); if (l10.PortFacilityDateOfArrival.HasValue) WriteDate(portDateOfArrival, l10.PortFacilityDateOfArrival.Value); if (l10.PortFacilityDateOfDeparture.HasValue) WriteDate(portDateOfDeparture, l10.PortFacilityDateOfDeparture.Value); if (l10.PortFacilityShipSecurityLevel.HasValue) WriteText(portShipSecLevel, l10.PortFacilityShipSecurityLevel.Value.ToString()); WriteText(portGISISCode, l10.PortFacilityGISISCode); WriteText(portSecMatters, l10.PortFacilitySecurityMattersToReport); } for(int i = 0; i < Math.Min(sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Count, 10); i++) { string s2sName = string.Format("SEC.ShipToShipActivityLocationName_{0}", i + 1); string s2sLocode = string.Format("SEC.ShipToShipActivityLocationLoCode_{0}", i + 1); string s2sLatitude = string.Format("SEC.ShipToShipActivityLocationCoordinatesLatitude_{0}", i + 1); string s2sLongitude = string.Format("SEC.ShipToShipActivityLocationCoordinatesLongitude_{0}", i + 1); string s2sFromDate = string.Format("SEC.ShipToShipActivityDateFrom_{0}", i + 1); string s2sToDate = string.Format("SEC.ShipToShipActivityDateTo_{0}", i + 1); string s2sSec = string.Format("SEC.ShipToShipActivitySecurityMattersToReport_{0}", i + 1); string s2sActivityString = string.Format("SEC.ShipToShipActivityType_{0}", i + 1); ShipToShipActivitiesDuringLastTenPortFacilitiesCalled s2s = sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled[i]; WriteText(s2sName, s2s.ShipToShipActivityLocationName); WriteText(s2sLocode, s2s.ShipToShipActivityLocationLoCode); if(s2s.ShipToShipActivityLocationCoordinatesLatitude.HasValue) WriteNumber(s2sLatitude, s2s.ShipToShipActivityLocationCoordinatesLatitude.Value); if (s2s.ShipToShipActivityLocationCoordinatesLongitude.HasValue) WriteNumber(s2sLongitude, s2s.ShipToShipActivityLocationCoordinatesLongitude.Value); if (s2s.ShipToShipActivityDateFrom.HasValue) WriteDate(s2sFromDate, s2s.ShipToShipActivityDateFrom.Value); if (s2s.ShipToShipActivityDateTo.HasValue) WriteDate(s2sToDate, s2s.ShipToShipActivityDateTo.Value); WriteText(s2sSec, s2s.ShipToShipActivitySecurityMattersToReport); if (s2s.ShipToShipActivityTypeCode.HasValue) { if (isRefSheet) { WriteText(s2sActivityString, s2s.ShipToShipActivityTypeCode.Value.ToString()); } else { if (GlobalStructures.Edifact8025.TryGetValue(s2s.ShipToShipActivityTypeCode.Value, out string code)) WriteText(s2sActivityString, code); } } } } #endregion #region SERV private void WriteSERV(Message servMessage) { if(servMessage.Elements.Count > 0) { SERV serv = servMessage.Elements[0] as SERV; WriteMessage(serv); } } #endregion #region STO private void WriteSTO(Message stoMessage) { for(int i = 0; i < Math.Min(stoMessage.Elements.Count, stoMessage.NumberOfExcelRows); i++) { string stoName = string.Format("STO.Name_{0}", i + 1); string stoQuantity = string.Format("STO.Quantity_{0}", i + 1); string stoQuantityUnit = string.Format("STO.QuantityUnit_{0}", i + 1); string stoLocationOnBoard = string.Format("STO.LocationOnBoard_{0}", i + 1); string stoOfficialUse = string.Format("STO.OfficialUse_{0}", i + 1); STO sto = stoMessage.Elements[i] as STO; WriteText(stoName, sto.Name); if (sto.Quantity.HasValue) WriteNumber(stoQuantity, sto.Quantity.Value); WriteText(stoQuantityUnit, sto.QuantityUnit); WriteText(stoLocationOnBoard, sto.LocationOnBoard); WriteText(stoOfficialUse, sto.OfficialUse); } } #endregion #region TOWA private void WriteTOWA(Message towaMessage, bool isRefSheet) { for(int i = 0; i < Math.Min(towaMessage.Elements.Count, towaMessage.NumberOfExcelRows); i++) { string tName = string.Format("TOWA.TowageOnArrivalName_{0}", i + 1); string tFlag = string.Format("TOWA.TowageOnArrivalFlag_{0}", i + 1); string tPoC = string.Format("TOWA.TowageOnArrivalPurposeOfCall_{0}", i + 1); string tDraft = string.Format("TOWA.TowageOnArrivalDraught_DMT_{0}", i + 1); string tGT = string.Format("TOWA.TowageOnArrivalGrossTonnage_{0}", i + 1); string tLen = string.Format("TOWA.TowageOnArrivalLengthOverall_MTR_{0}", i + 1); string tBeam = string.Format("TOWA.TowageOnArrivalBeam_MTR_{0}", i + 1); string tOp = string.Format("TOWA.TowageOnArrivalOperatorCompanyName_{0}", i + 1); TOWA towa = towaMessage.Elements[i] as TOWA; WriteText(tName, towa.TowageOnArrivalName); if(isRefSheet) { WriteText(tFlag, towa.TowageOnArrivalFlag); } else { if(CREW.NationalityDict.TryGetValue(towa.TowageOnArrivalFlag, out string country)) WriteText(tFlag, country.Substring(3)); } WriteText(tPoC, towa.TowageOnArrivalPurposeOfCall); if (towa.TowageOnArrivalDraught_DMT.HasValue) WriteNumber(tDraft, towa.TowageOnArrivalDraught_DMT.Value); if (towa.TowageOnArrivalGrossTonnage.HasValue) WriteNumber(tGT, towa.TowageOnArrivalGrossTonnage.Value); if (towa.TowageOnArrivalLengthOverall_MTR.HasValue) WriteNumber(tLen, towa.TowageOnArrivalLengthOverall_MTR.Value); if (towa.TowageOnArrivalBeam_MTR.HasValue) WriteNumber(tBeam, towa.TowageOnArrivalBeam_MTR.Value); WriteText(tOp, towa.TowageOnArrivalOperatorCompanyName); } } #endregion #region TOWD private void WriteTOWD(Message towdMessage, bool isRefSheet) { for (int i = 0; i < Math.Min(towdMessage.Elements.Count, towdMessage.NumberOfExcelRows); i++) { string tName = string.Format("TOWD.TowageOnDepartureName_{0}", i + 1); string tFlag = string.Format("TOWD.TowageOnDepartureFlag_{0}", i + 1); string tDraft = string.Format("TOWD.TowageOnDepartureDraught_DMT_{0}", i + 1); string tLen = string.Format("TOWD.TowageOnDepartureLengthOverall_MTR_{0}", i + 1); string tBeam = string.Format("TOWD.TowageOnDepartureBeam_MTR_{0}", i + 1); string tOp = string.Format("TOWD.TowageOnDepartureOperatorCompanyName_{0}", i + 1); string tPoc = string.Format("TOWD.TowageOnDeparturePurposeOfCall_{0}", i + 1); TOWD towd = towdMessage.Elements[i] as TOWD; WriteText(tName, towd.TowageOnDepartureName); if (isRefSheet) { WriteText(tFlag, towd.TowageOnDepartureFlag); } else { if (CREW.NationalityDict.TryGetValue(towd.TowageOnDepartureFlag, out string country)) WriteText(tFlag, country.Substring(3)); } if (towd.TowageOnDepartureDraught_DMT.HasValue) WriteNumber(tDraft, towd.TowageOnDepartureDraught_DMT.Value); if (towd.TowageOnDepartureLengthOverall_MTR.HasValue) WriteNumber(tLen, towd.TowageOnDepartureLengthOverall_MTR.Value); if (towd.TowageOnDepartureBeam_MTR.HasValue) WriteNumber(tBeam, towd.TowageOnDepartureBeam_MTR.Value); WriteText(tOp, towd.TowageOnDepartureOperatorCompanyName); // WriteText(tPoc, towd.Tow) } } #endregion #region WAS private void WriteWAS(Message wasMessage, bool isRefSheet) { if (wasMessage.Elements.Count == 0) return; WAS was = wasMessage.Elements[0] as WAS; if (was.WasteDisposalDelivery.HasValue) // TODO: CH schreibt für DK erforderlich -> nachfragen { switch (was.WasteDisposalDelivery) { case 0: WriteText("WAS.WasteDisposalDelivery", "ALL"); break; case 1: WriteText("WAS.WasteDisposalDelivery", "SOME"); break; case 2: WriteText("WAS.WasteDisposalDelivery", "NONE"); break; default: break; } } WriteText("WAS.LastWasteDisposalPort", was.LastWasteDisposalPort); WriteText("WAS.WasteDisposalServiceProviderName", was.WasteDisposalServiceProviderText); WriteText("WAS.NextWasteDisposalPort", was.NextWasteDisposalPort); WriteBoolean("WAS.ConfirmationOfCorrectness", true); WriteBoolean("WAS.ConfirmationOfSufficiency", true); for(int i = 0; i < Math.Min(was.NumberOfExcelRows, was.Waste.Count); i++) { string wastetype = string.Format("WAS.WasteType_{0}", i + 1); string wasteCode = string.Format("WAS.WasteCode_{0}", i + 1); string wasteDescription = string.Format("WAS.WasteDescription_{0}", i + 1); string wasteAmount = string.Format("WAS.WasteDisposalAmount_MTQ_{0}", i + 1); string wasteCapacity = string.Format("WAS.WasteCapacity_MTQ_{0}", i + 1); string wasteRetained = string.Format("WAS.WasteAmountRetained_MTQ_{0}", i + 1); string wastePort = string.Format("WAS.WasteDisposalPort_{0}", i + 1); string amountGen = string.Format("WAS.WasteAmountGeneratedTillNextPort_MTQ_{0}", i + 1); Waste waste = was.Waste[i]; if (waste.WasteType.HasValue) { WriteNumber(wasteCode, waste.WasteType.Value); if (WAS.WasteCodeDict.ContainsKey(waste.WasteType.Value.ToString())) WriteText(wastetype, WAS.WasteCodeDict[waste.WasteType.Value.ToString()]); } WriteText(wasteDescription, waste.WasteDescription); if (waste.WasteDisposalAmount_MTQ.HasValue) WriteNumber(wasteAmount, waste.WasteDisposalAmount_MTQ.Value); if (waste.WasteCapacity_MTQ.HasValue) WriteNumber(wasteCapacity, waste.WasteCapacity_MTQ.Value); if (waste.WasteAmountRetained_MTQ.HasValue) WriteNumber(wasteRetained, waste.WasteAmountRetained_MTQ.Value); WriteText(wastePort, isRefSheet ? waste.WasteDisposalPort : Locode.LocodeDB.LocationNameFromLocode(waste.WasteDisposalPort)); if (waste.WasteAmountGeneratedTillNextPort_MTQ.HasValue) WriteNumber(amountGen, waste.WasteAmountGeneratedTillNextPort_MTQ.Value); } } #endregion #region WAS_RCPT private void WriteWAS_RCPT(Message wasRcptmessage) { for (int i = 1; i <= Math.Min(wasRcptmessage.NumberOfExcelRows, wasRcptmessage.Elements.Count); i++) { string ident = string.Format("WAS_RCPT.WasteDeliveryReceipt_{0}.IdentificationNumber", i); string prfn = string.Format("WAS_RCPT.WasteDeliveryReceipt_{0}.PortReceptionFacilityName", i); string prfpn = string.Format("WAS_RCPT.WasteDeliveryReceipt_{0}.PortReceptionFacilityProviderName", i); string tfpn = string.Format("WAS_RCPT.WasteDeliveryReceipt_{0}.TreatmentFacilityProviderName", i); string wddfd = string.Format("WAS_RCPT.WasteDeliveryReceipt_{0}.WasteDeliveryDateFromDate", i); string wddft = string.Format("WAS_RCPT.WasteDeliveryReceipt_{0}.WasteDeliveryDateFromTime", i); string wddtd = string.Format("WAS_RCPT.WasteDeliveryReceipt_{0}.WasteDeliveryDateToDate", i); string wddtt = string.Format("WAS_RCPT.WasteDeliveryReceipt_{0}.WasteDeliveryDateToTime", i); WAS_RCPT wrcpt = wasRcptmessage.Elements[i-1] as WAS_RCPT; WriteText(ident, wrcpt.IdentificationNumber); WriteText(prfn, wrcpt.PortReceptionFacilityName); WriteText(prfpn, wrcpt.PortReceptionFacilityProviderName); WriteText(tfpn, wrcpt.TreatmentFacilityProviderText); WriteDate(wddfd, wrcpt.WasteDeliveryDateFrom); WriteTime(wddft, wrcpt.WasteDeliveryDateFrom); WriteDate(wddtd, wrcpt.WasteDeliveryDateTo); WriteTime(wddtt, wrcpt.WasteDeliveryDateTo); for(int j = 1; j <= Math.Min(wrcpt.NumberOfExcelRows, wrcpt.WasteReceived.Count); j++) { string wdesc = string.Format("WAS_RCPT.WasteDeliveryReceipt_{0}.WasteDescription_{1}", i, j); string wamount = string.Format("WAS_RCPT.WasteDeliveryReceipt_{0}.AmountWasteReceived_MTQ_{1}", i, j); WasteReceived wReceived = wrcpt.WasteReceived[j - 1]; WriteText(wdesc, wReceived.WasteDescription); WriteNumber(wamount, wReceived.AmountWasteReceived_MTQ); } } } #endregion #region INFO private void WriteINFO(INFO info, bool isRefSheet) { if (info != null) { this.WriteBoolean("INFO.FumigatedBulkCargo", info.FumigatedBulkCargoBool); if (info.ShippingArea.HasValue) { switch (info.ShippingArea.Value) { case 0: this.WriteText("INFO.ShippingArea", "NORTH_BALTIC_SEA"); break; case 1: this.WriteText("INFO.ShippingArea", "EUROPE"); break; case 2: this.WriteText("INFO.ShippingArea", "OVERSEAS"); break; } } if(isRefSheet) { WriteText("INFO.PortArea", info.PortArea); } else { WriteText("INFO.PortArea", LocalizedLookup.GetPortAreaFromCode(info.PortArea)); } } } #endregion #region STAT private void WriteSTAT(STAT stat, bool isRefSheet) { if (stat != null) { WriteText("STAT.PortOfRegistry", isRefSheet ? stat.PortOfRegistry : Locode.LocodeDB.LocationNameFromLocode(stat.PortOfRegistry)); WriteText("STAT.TransportMode", stat.TransportModeDisplay); if(isRefSheet) { WriteText("STAT.Flag", stat.Flag); } else { if (CREW.NationalityDict.TryGetValue(stat.Flag, out string country)) WriteText("STAT.Flag", country.Substring(3)); } if(isRefSheet) { WriteText("STAT.ShipType", stat.ShipType); } else { if (LocalizedLookup.getVesselTypes().TryGetValue(stat.ShipType, out string shipType)) WriteText("STAT.ShipType", shipType); } } } #endregion #region PRE72H private void WritePRE72H(PRE72H pre72h) { if(pre72h != null) { if(pre72h.TankerHullConfiguration.HasValue) { switch(pre72h.TankerHullConfiguration.Value) { case 0: WriteText("PRE72H.TankerHullConfiguration", "SINGLE HULL"); break; case 1: WriteText("PRE72H.TankerHullConfiguration", "SINGLE HULL WITH SBT"); break; case 2: WriteText("PRE72H.TankerHullConfiguration", "DOUBLE HULL"); break; } } if(pre72h.ConditionCargoBallastTanks.HasValue) { switch(pre72h.ConditionCargoBallastTanks.Value) { case 0: WriteText("PRE72H.ConditionCargoBallastTanks", "FULL"); break; case 1: WriteText("PRE72H.ConditionCargoBallastTanks", "EMPTY"); break; case 2: WriteText("PRE72H.ConditionCargoBallastTanks", "INERTED"); break; } } } } #endregion #endregion #region write simple things private void WriteGender(string label, byte? gender) { if(gender.HasValue) { if (gender == 0) WriteText(label, "?"); if (gender == 1) WriteText(label, "m"); if (gender == 2) WriteText(label, "f"); if (gender == 9) WriteText(label, "d"); } } #endregion #region write value types private bool WriteBoolean(string lookupName, object v) { bool result = _nameDict.ContainsKey(lookupName); bool? b = (bool?) v; try { if (result && b.HasValue) { _nameDict[lookupName].RefersToRange.Value = b.Value ? "Y" : "N"; } } catch(Exception) { System.Diagnostics.Trace.WriteLine(string.Format("Error writing {0} to excel, field missing", lookupName)); } return result; } private bool WriteText(string lookupName, object v) { bool result = _nameDict.ContainsKey(lookupName); try { if (result) { _nameDict[lookupName].RefersToRange.Value = v; } } catch(Exception) { System.Diagnostics.Trace.WriteLine(string.Format("Error writing {0} to excel, field missing", lookupName)); } return result; } private bool WriteNumber(string lookupName, object v) { bool result = _nameDict.ContainsKey(lookupName); if (result) { _nameDict[lookupName].RefersToRange.Value = v; } return result; } private bool WriteDate(string lookupName, object v) { bool result = _nameDict.ContainsKey(lookupName); if (result) { if(v != null) { _nameDict[lookupName].RefersToRange.Value = ((DateTime) v).ToOADate(); // Test this I dont believe it } } return result; } private bool WriteTime(string lookupName, object v) { bool result = _nameDict.ContainsKey(lookupName); if (result) { if (v != null) { _nameDict[lookupName].RefersToRange.Value = ((DateTime)v).ToLocalTime().ToShortTimeString(); } } return result; } #endregion #endregion } }