git_bsmd/ENI2/DetailViewControls/SecurityDetailControl.xaml.cs
Daniel Schick d1c131c52b Added logic to return to the previously selected tab page on a detail control,
but only if the control has a tab control and the save button was hit. In this case a complete reload happens.
2025-03-07 16:04:53 +01:00

515 lines
25 KiB
C#

// Copyright (c) 2017 schick Informatik
// Description: SEC Meldung Bearbeitungsseite
//
using System.Windows;
using System.Windows.Controls;
using ENI2.EditControls;
using ENI2.Util;
using bsmd.database;
using System.Windows.Data;
using System;
using Microsoft.Win32;
using System.IO;
using ExcelDataReader;
using System.Collections.Generic;
namespace ENI2.DetailViewControls
{
/// <summary>
/// Interaction logic for SecurityDetailControl.xaml
/// </summary>
public partial class SecurityDetailControl : DetailBaseControl
{
private Message _secMessage;
private SEC _sec;
private static readonly string[] isscTypes = { "Final", "Preliminary" };
private static readonly string[] isscIssuerTypes = { "Authority", "RSO (Recognized security org.)" };
private static readonly string[] cargoDescriptions = {
"Container",
"Vehicles",
"Conventional general cargo",
"Dry bulk cargo",
"Liquid bulk cargo",
"In ballast"
};
public SecurityDetailControl()
{
InitializeComponent();
Loaded += SecurityDetailControl_Loaded;
}
public override void Initialize()
{
base.Initialize();
foreach (Message aMessage in this.Messages)
{
if (aMessage.MessageNotificationClass == Message.NotificationClass.SEC) { this._secMessage = aMessage; this.ControlMessages.Add(aMessage); }
}
#region init SEC
if (this._secMessage == null)
{
this._secMessage = this.Core.CreateMessage(Message.NotificationClass.SEC);
this.Messages.Add(this._secMessage);
}
SEC sec = null;
if (this._secMessage.Elements.Count > 0)
sec = this._secMessage.Elements[0] as SEC;
if (sec == null)
{
sec = new SEC
{
MessageCore = this.Core,
MessageHeader = this._secMessage
};
_secMessage.Elements.Add(sec);
}
this.mainFrame.DataContext = sec;
this._sec = sec;
this.comboBoxCurrentShipSecurityLevel.ItemsSource = Util.GlobalStructures.ShipSecurityLevels;
//this.comboBoxCurrentShipSecurityLevel.KeyUp += ComboBox_KeyUp;
this.comboBoxISSCType.ItemsSource = isscTypes;
//this.comboBoxISSCType.KeyUp += ComboBox_KeyUp;
this.comboBoxISSCIssuerType.ItemsSource = isscIssuerTypes;
//this.comboBoxISSCIssuerType.KeyUp += ComboBox_KeyUp;
this.comboBoxGeneralDescriptionOfCargo.ItemsSource = cargoDescriptions;
//this.comboBoxGeneralDescriptionOfCargo.KeyUp += ComboBox_KeyUp;
this.dataGridLast10PortFacilities.Initialize();
this.dataGridLast10PortFacilities.ItemsSource = sec.LastTenPortFacilitesCalled;
this.dataGridLast10PortFacilities.AddingNewItem += DataGridLast10PortFacilities_AddingNewItem;
this.dataGridLast10PortFacilities.EditRequested += DataGridLast10PortFacilities_EditRequested;
this.dataGridLast10PortFacilities.DeleteRequested += DataGridLast10PortFacilities_DeleteRequested;
this.dataGridLast10PortFacilities.CreateRequested += DataGridLast10PortFacilities_CreateRequested;
this.dataGridShip2ShipActivities.Initialize();
foreach(ShipToShipActivitiesDuringLastTenPortFacilitiesCalled s2s in sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
{
if (s2s.ShipToShipActivityTypeCode.HasValue && s2s.ShipToShipActivityType.IsNullOrEmpty() && GlobalStructures.Edifact8025.ContainsKey(s2s.ShipToShipActivityTypeCode.Value))
s2s.ShipToShipActivityType = GlobalStructures.Edifact8025[s2s.ShipToShipActivityTypeCode.Value];
}
this.dataGridShip2ShipActivities.ItemsSource = sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled;
this.dataGridShip2ShipActivities.AddingNewItem += DataGridShip2ShipActivities_AddingNewItem;
this.dataGridShip2ShipActivities.EditRequested += DataGridShip2ShipActivities_EditRequested;
this.dataGridShip2ShipActivities.DeleteRequested += DataGridShip2ShipActivities_DeleteRequested;
this.dataGridShip2ShipActivities.CreateRequested += DataGridShip2ShipActivities_CreateRequested;
this.checkBoxSECSimplification.Checked += CheckBoxSECSimplification_Checked;
this.checkBoxSECSimplification.Unchecked += CheckBoxSECSimplification_Checked;
this.checkBoxKielCanalPassagePlanned.Checked += CheckBoxKielCanalPassagePlanned_Checked;
this.CheckBoxKielCanalPassagePlanned_Checked(null, null);
#endregion
}
public override int SelectedTabIndex
{
get { return this.mainFrame.SelectedIndex; }
set { this.mainFrame.SelectedIndex = value; }
}
#region SetEnabled
public override void SetEnabled(bool enabled)
{
this.secGroupBox.IsEnabled = enabled;
this.dataGridLast10PortFacilities.IsEnabled = enabled;
this.dataGridShip2ShipActivities.IsEnabled = enabled;
}
#endregion
#region data grid ship 2 ship activities
private void DataGridShip2ShipActivities_CreateRequested()
{
EditShip2ShipActivitiesDialog epd = new EditShip2ShipActivitiesDialog
{
ShipToShipActivity = new ShipToShipActivitiesDuringLastTenPortFacilitiesCalled()
};
epd.ShipToShipActivity.Identifier = ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.GetNewIdentifier(_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled);
epd.ShipToShipActivity.SEC = this._sec;
epd.AddClicked += () =>
{
epd.CopyValuesToEntity();
if(!this._sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Contains(epd.ShipToShipActivity))
this._sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Add(epd.ShipToShipActivity);
this.dataGridShip2ShipActivities.Items.Refresh();
epd.ShipToShipActivity = new ShipToShipActivitiesDuringLastTenPortFacilitiesCalled
{
SEC = this._sec,
Identifier = ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.GetNewIdentifier(_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
};
this.SublistElementChanged(Message.NotificationClass.SEC);
};
if (epd.ShowDialog() ?? false)
{
if(!_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Contains(epd.ShipToShipActivity))
_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Add(epd.ShipToShipActivity);
this.dataGridShip2ShipActivities.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SEC);
}
}
private void DataGridShip2ShipActivities_DeleteRequested(DatabaseEntity obj)
{
if (obj is ShipToShipActivitiesDuringLastTenPortFacilitiesCalled s2s)
{
// are you sure dialog is in base class
_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Remove(s2s);
DBManager.GetSingleCon(Properties.Settings.Default.ConnectionString).Delete(s2s);
List<DatabaseEntity> tmpList = new List<DatabaseEntity>(_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled);
DatabaseEntity.ResetIdentifiers(tmpList);
this.SublistElementChanged(Message.NotificationClass.SEC);
this.dataGridShip2ShipActivities.Items.Refresh();
}
}
private void DataGridShip2ShipActivities_EditRequested(DatabaseEntity obj)
{
EditShip2ShipActivitiesDialog ecpd = new EditShip2ShipActivitiesDialog
{
ShipToShipActivity = obj as ShipToShipActivitiesDuringLastTenPortFacilitiesCalled
};
ecpd.AddClicked += () =>
{
ecpd.CopyValuesToEntity();
if (!_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Contains(ecpd.ShipToShipActivity))
_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Add(ecpd.ShipToShipActivity);
this.dataGridShip2ShipActivities.Items.Refresh();
ecpd.ShipToShipActivity = new ShipToShipActivitiesDuringLastTenPortFacilitiesCalled
{
SEC = this._sec,
Identifier = ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.GetNewIdentifier(_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled)
};
this.SublistElementChanged(Message.NotificationClass.SEC);
};
if (ecpd.ShowDialog() ?? false)
{
if (!_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Contains(ecpd.ShipToShipActivity))
_sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Add(ecpd.ShipToShipActivity);
this.dataGridShip2ShipActivities.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SEC);
}
}
private void DataGridShip2ShipActivities_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
this.DataGridShip2ShipActivities_CreateRequested();
}
#endregion
#region data grid last 10 port facilities
private void DataGridLast10PortFacilities_CreateRequested()
{
EditLast10PortFacilitiesDialog epd = new EditLast10PortFacilitiesDialog
{
LastTenPortFacilitiesCalled = new LastTenPortFacilitiesCalled()
};
epd.LastTenPortFacilitiesCalled.Identifier = LastTenPortFacilitiesCalled.GetNewIdentifier(_sec.LastTenPortFacilitesCalled);
epd.LastTenPortFacilitiesCalled.SEC = this._sec;
epd.AddClicked += () =>
{
epd.CopyValuesToEntity();
if(!this._sec.LastTenPortFacilitesCalled.Contains(epd.LastTenPortFacilitiesCalled))
this._sec.LastTenPortFacilitesCalled.Add(epd.LastTenPortFacilitiesCalled);
this.dataGridLast10PortFacilities.Items.Refresh();
epd.LastTenPortFacilitiesCalled = new LastTenPortFacilitiesCalled
{
SEC = this._sec,
Identifier = LastTenPortFacilitiesCalled.GetNewIdentifier(_sec.LastTenPortFacilitesCalled)
};
this.SublistElementChanged(Message.NotificationClass.SEC);
};
if (epd.ShowDialog() ?? false)
{
if(!_sec.LastTenPortFacilitesCalled.Contains(epd.LastTenPortFacilitiesCalled))
_sec.LastTenPortFacilitesCalled.Add(epd.LastTenPortFacilitiesCalled);
this.dataGridLast10PortFacilities.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SEC);
}
}
private void DataGridLast10PortFacilities_DeleteRequested(DatabaseEntity obj)
{
if (obj is LastTenPortFacilitiesCalled l10c)
{
// are you sure dialog is in base class
_sec.LastTenPortFacilitesCalled.Remove(l10c);
DBManager.GetSingleCon(Properties.Settings.Default.ConnectionString).Delete(l10c);
List<DatabaseEntity> tmpList = new List<DatabaseEntity>(_sec.LastTenPortFacilitesCalled);
DatabaseEntity.ResetIdentifiers(tmpList);// );
this.SublistElementChanged(Message.NotificationClass.SEC);
this.dataGridLast10PortFacilities.Items.Refresh();
}
}
private void DataGridLast10PortFacilities_EditRequested(DatabaseEntity obj)
{
EditLast10PortFacilitiesDialog ecpd = new EditLast10PortFacilitiesDialog
{
LastTenPortFacilitiesCalled = obj as LastTenPortFacilitiesCalled
};
ecpd.AddClicked += () =>
{
ecpd.CopyValuesToEntity();
if (!_sec.LastTenPortFacilitesCalled.Contains(ecpd.LastTenPortFacilitiesCalled))
_sec.LastTenPortFacilitesCalled.Add(ecpd.LastTenPortFacilitiesCalled);
this.dataGridLast10PortFacilities.Items.Refresh();
ecpd.LastTenPortFacilitiesCalled = new LastTenPortFacilitiesCalled
{
SEC = this._sec,
Identifier = LastTenPortFacilitiesCalled.GetNewIdentifier(_sec.LastTenPortFacilitesCalled)
};
this.SublistElementChanged(Message.NotificationClass.SEC);
};
if (ecpd.ShowDialog() ?? false)
{
if (!_sec.LastTenPortFacilitesCalled.Contains(ecpd.LastTenPortFacilitiesCalled))
_sec.LastTenPortFacilitesCalled.Add(ecpd.LastTenPortFacilitiesCalled);
this.dataGridLast10PortFacilities.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SEC);
}
}
private void DataGridLast10PortFacilities_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
this.DataGridLast10PortFacilities_CreateRequested();
}
#endregion
#region event handler
private void SecurityDetailControl_Loaded(object sender, RoutedEventArgs e)
{
this.RegisterTextboxChange(this.textBoxCSOEMailName, Message.NotificationClass.SEC);
this.RegisterTextboxChange(this.textBoxCSOFaxName, Message.NotificationClass.SEC);
this.RegisterTextboxChange(this.textBoxCSOFirstName, Message.NotificationClass.SEC);
this.RegisterTextboxChange(this.textBoxCSOLastName, Message.NotificationClass.SEC);
this.RegisterTextboxChange(this.textBoxCSOPhoneName, Message.NotificationClass.SEC);
this.RegisterTextboxChange(this.textBoxISSCIssuerName, Message.NotificationClass.SEC);
this.RegisterTextboxChange(this.textBoxPortFacilityOfArrival, Message.NotificationClass.SEC);
this.RegisterTextboxChange(this.textBoxReasonsForNoValidISSC, Message.NotificationClass.SEC);
this.RegisterComboboxIndexChange(this.comboBoxCurrentShipSecurityLevel, Message.NotificationClass.SEC);
this.RegisterComboboxIndexChange(this.comboBoxGeneralDescriptionOfCargo, Message.NotificationClass.SEC);
this.RegisterComboboxIndexChange(this.comboBoxISSCIssuerType, Message.NotificationClass.SEC);
this.RegisterComboboxIndexChange(this.comboBoxISSCType, Message.NotificationClass.SEC);
this.RegisterDatePickerChange(this.datePickerISSCDateOfExpiration, Message.NotificationClass.SEC);
this.RegisterDateTimePickerChange(this.dateTimePickerKielCanalPassagePlannedIncomming, Message.NotificationClass.SEC);
this.RegisterDateTimePickerChange(this.dateTimePickerKielCanalPassagePlannedOutgoing, Message.NotificationClass.SEC);
this.RegisterCheckboxChange(this.checkBoxApprovedSecurityPlanOnBoard, Message.NotificationClass.SEC);
this.RegisterCheckboxChange(this.checkBoxKielCanalPassagePlanned, Message.NotificationClass.SEC);
this.RegisterCheckboxChange(this.checkBoxSECSimplification, Message.NotificationClass.SEC);
this.RegisterCheckboxChange(this.checkBoxValidISSCOnBoard, Message.NotificationClass.SEC);
this.RegisterLocodeChange(this.locodePortOfCallWhereCompleteSECNotified, Message.NotificationClass.SEC);
}
#endregion
#region Excel import last 10 port facilities
private void buttonImportExcel_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog
{
Filter = "Excel Files|*.xls;*.xlsx"
};
if (ofd.ShowDialog() ?? false)
{
FileStream stream;
try
{
stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
List<LastTenPortFacilitiesCalled> importL10C = new List<LastTenPortFacilitiesCalled>();
try
{
do
{
int cnt = 0;
while (reader.Read() && (cnt < 10))
{
if (((IExcelDataReader)reader).FieldCount < 8)
{
throw new InvalidDataException("Sheet must have 8 Columns of data");
}
LastTenPortFacilitiesCalled l10c = new LastTenPortFacilitiesCalled();
if (reader.IsDBNull(0) && reader.IsDBNull(1)) continue;
if (!reader.IsDBNull(0)) l10c.PortFacilityPortName = reader.GetString(0);
if (!reader.IsDBNull(2)) l10c.PortFacilityPortCountry = reader.GetString(2);
if (!reader.IsDBNull(3)) l10c.PortFacilityPortLoCode = reader.GetString(3);
object o = null;
if (!reader.IsDBNull(4)) o = reader.GetValue(4);
if(o != null) l10c.PortFacilityDateOfArrival = Convert.ToDateTime(o);
if (!reader.IsDBNull(5)) o = reader.GetValue(5);
if (o != null) l10c.PortFacilityDateOfDeparture = Convert.ToDateTime(o);
// if (!reader.IsDBNull(4)) l10c.PortFacilityDateOfArrival = reader.GetDateTime(4);
// if (!reader.IsDBNull(5)) l10c.PortFacilityDateOfDeparture = reader.GetDateTime(5);
if (!reader.IsDBNull(6)) o = reader.GetValue(6);
if (o != null) l10c.PortFacilityShipSecurityLevel = Convert.ToByte(o);
if (!reader.IsDBNull(7)) o = reader.GetValue(7);
int gisis = Convert.ToInt32(o);
if (!reader.IsDBNull(7)) l10c.PortFacilityGISISCode = gisis.ToString().PadLeft(4, '0');
if (!reader.IsDBNull(8)) l10c.PortFacilitySecurityMattersToReport = reader.GetString(8);
if (l10c.PortFacilitySecurityMattersToReport.Equals("nil", StringComparison.CurrentCultureIgnoreCase))
l10c.PortFacilitySecurityMattersToReport = null;
if (!reader.IsDBNull(9)) l10c.PortFacilityGISISCodeLocode = reader.GetString(9);
l10c.SEC = this._sec;
l10c.IsDirty = true;
l10c.Identifier = LastTenPortFacilitiesCalled.GetNewIdentifier(this._sec.LastTenPortFacilitesCalled);
this._sec.LastTenPortFacilitesCalled.Add(l10c);
importL10C.Add(l10c);
cnt++;
}
} while (reader.NextResult());
}
catch (Exception ex)
{
MessageBox.Show("Error reading Excel: " + ex.Message, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Error);
}
if (importL10C.Count > 0)
{
this.dataGridLast10PortFacilities.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SEC);
MessageBox.Show(String.Format(Properties.Resources.textL10PImported, importL10C.Count), Properties.Resources.textCaptionInformation, MessageBoxButton.OK, MessageBoxImage.Information);
}
}
stream.Close();
}
}
#endregion
#region Copy l10p into Ship2Ship
private void buttonImportFromL10P_Click(object sender, RoutedEventArgs e)
{
if (this._sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Count > 0)
{
MessageBox.Show(Properties.Resources.textOnlyIfGridIsEmpty, Properties.Resources.textCaptionError, MessageBoxButton.OK, MessageBoxImage.Warning);
}
else
{
foreach(LastTenPortFacilitiesCalled l10c in this._sec.LastTenPortFacilitesCalled)
{
ShipToShipActivitiesDuringLastTenPortFacilitiesCalled s2s = new ShipToShipActivitiesDuringLastTenPortFacilitiesCalled
{
SEC = this._sec,
Identifier = ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.GetNewIdentifier(this._sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled),
ShipToShipActivityLocationName = l10c.PortFacilityPortName,
ShipToShipActivityLocationLoCode = l10c.PortFacilityPortLoCode,
ShipToShipActivityDateFrom = l10c.PortFacilityDateOfArrival,
ShipToShipActivityDateTo = l10c.PortFacilityDateOfDeparture,
ShipToShipActivitySecurityMattersToReport = l10c.PortFacilitySecurityMattersToReport
};
this._sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Add(s2s);
}
if(this._sec.ShipToShipActivitiesDuringLastTenPortFacilitiesCalled.Count > 0)
{
this.dataGridShip2ShipActivities.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SEC);
}
}
}
#endregion
#region enable / disable controls
private void CheckBoxKielCanalPassagePlanned_Checked(object sender, RoutedEventArgs e)
{
this.dateTimePickerKielCanalPassagePlannedIncomming.IsEnabled = this.checkBoxKielCanalPassagePlanned.IsChecked ?? false;
this.dateTimePickerKielCanalPassagePlannedOutgoing.IsEnabled = this.checkBoxKielCanalPassagePlanned.IsChecked ?? false;
}
private void CheckBoxSECSimplification_Checked(object sender, RoutedEventArgs e)
{
this.locodePortOfCallWhereCompleteSECNotified.IsEnabled = this.checkBoxSECSimplification.IsChecked ?? false;
bool enable = !(this.checkBoxSECSimplification.IsChecked ?? false);
this.textBoxCSOEMailName.IsEnabled = enable;
this.textBoxCSOFaxName.IsEnabled = enable;
this.textBoxCSOFirstName.IsEnabled = enable;
this.textBoxCSOLastName.IsEnabled = enable;
this.textBoxCSOPhoneName.IsEnabled = enable;
this.textBoxISSCIssuerName.IsEnabled = enable;
this.textBoxPortFacilityOfArrival.IsEnabled = enable;
this.textBoxReasonsForNoValidISSC.IsEnabled = enable;
this.checkBoxApprovedSecurityPlanOnBoard.IsEnabled = enable;
this.checkBoxValidISSCOnBoard.IsEnabled = enable;
this.comboBoxGeneralDescriptionOfCargo.IsEnabled = enable;
this.comboBoxISSCIssuerType.IsEnabled = enable;
this.comboBoxISSCType.IsEnabled = enable;
this.datePickerISSCDateOfExpiration.IsEnabled = enable;
this.dataGridLast10PortFacilities.IsEnabled = enable;
this.dataGridShip2ShipActivities.IsEnabled = enable;
}
#endregion
#region Highlighting
public override void HighlightErrorMessageContainer()
{
if (this._secMessage.HasErrors)
HighlightService.HighlightControl(this.secGroupBox, HighlightService.HighlightStyle.ERROR, this._secMessage);
}
public override void HighlightViolationMessageContainer()
{
if (this._secMessage.HasViolations)
HighlightService.HighlightControl(this.secGroupBox, HighlightService.HighlightStyle.VIOLATION, this._secMessage);
}
#endregion
}
}