// Copyright (c) 2017- schick Informatik
// Description:
//
// Description: Detailansicht Gruppe Port Notification
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ENI2.EditControls;
using ENI2.Util;
using bsmd.database;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ENI2.DetailViewControls
{
///
/// Interaction logic for PortNotificationDetailControl.xaml
///
public partial class PortNotificationDetailControl : DetailBaseControl
{
#region Fields
private Message _nameMessage;
private Message _infoMessage;
private Message _servMessage;
private Message _ladgMessage;
private Message _crewaMessage;
private Dictionary portAreas = null;
private static List _servTemplates = null;
private SERV_Template _currentTemplate;
#endregion
private static readonly string[] shippingAreas = {
Properties.Resources.textShippingAreaNORTHBALTIC,
Properties.Resources.textShippingAreaEUROPE,
Properties.Resources.textShippingAreaOverseas
};
public PortNotificationDetailControl()
{
InitializeComponent();
this.Loaded += PortNotificationDetailControl_Loaded;
}
private void PortNotificationDetailControl_Loaded(object sender, RoutedEventArgs e)
{
this.RegisterTextboxChange(this.textBox_NameMaster, Message.NotificationClass.NAME);
this.RegisterComboboxIndexChange(this.comboBoxShippingArea, Message.NotificationClass.INFO);
this.RegisterComboboxValueChange(this.comboBoxPortArea, Message.NotificationClass.INFO);
this.RegisterTextboxChange(this.textRequestedPostionInPortOfCall, Message.NotificationClass.INFO);
this.RegisterTextboxChange(this.textBowThrusterPower, Message.NotificationClass.INFO);
this.RegisterTextboxChange(this.textSternThrusterPower, Message.NotificationClass.INFO);
this.RegisterCheckboxChange(this.checkBoxFumigatedBulkCargo, Message.NotificationClass.INFO);
this.RegisterDoubleUpDownChange(this.doubleUpDownDisplacementSummerDraught, Message.NotificationClass.INFO);
this.RegisterTextboxChange(this.textSpecialRequirements, Message.NotificationClass.INFO);
this.RegisterTextboxChange(this.textConstructionCharacteristics, Message.NotificationClass.INFO);
this.dataGridLADG.CellEditEnding += (obj, ev) => { this.OnNotificationClassChanged(Message.NotificationClass.LADG); };
}
public override async void Initialize()
{
base.Initialize();
foreach (Message aMessage in this.Messages)
{
if (aMessage.MessageNotificationClass == Message.NotificationClass.NAME) { this._nameMessage = aMessage; this.ControlMessages.Add(aMessage); }
if (aMessage.MessageNotificationClass == Message.NotificationClass.INFO) { this._infoMessage = aMessage; this.ControlMessages.Add(aMessage); }
if (aMessage.MessageNotificationClass == Message.NotificationClass.SERV) { this._servMessage = aMessage; this.ControlMessages.Add(aMessage); }
if (aMessage.MessageNotificationClass == Message.NotificationClass.LADG) { this._ladgMessage = aMessage; this.ControlMessages.Add(aMessage); }
if(aMessage.MessageNotificationClass == Message.NotificationClass.CREWA) { this._crewaMessage = aMessage;}
}
#region init NAME
if (this._nameMessage == null)
{
this._nameMessage = this.Core.CreateMessage(Message.NotificationClass.NAME);
this.Messages.Add(this._nameMessage);
}
NAME name = null;
if(this._nameMessage.Elements.Count > 0)
name = this._nameMessage.Elements[0] as NAME;
if (name == null) {
name = new NAME();
name.MessageCore = this.Core;
name.MessageHeader = this._nameMessage;
_nameMessage.Elements.Add(name);
}
this.textBox_NameMaster.DataContext = name;
#endregion
#region init INFO
if(this._infoMessage == null)
{
this._infoMessage = this.Core.CreateMessage(Message.NotificationClass.INFO);
this.Messages.Add(this._infoMessage);
}
INFO info = null;
if(this._infoMessage.Elements.Count > 0)
info = this._infoMessage.Elements[0] as INFO;
if(info == null)
{
info = new INFO();
info.MessageCore = this.Core;
info.MessageHeader = this._infoMessage;
_infoMessage.Elements.Add(info);
}
portAreas = LocalizedLookup.getPortAreasForLocode(this.Core.PoC);
this.comboBoxPortArea.ItemsSource = portAreas;
this.comboBoxShippingArea.ItemsSource = shippingAreas;
this.infoGroupBox.DataContext = info;
#endregion
#region init SERV
if(this._servMessage == null)
{
this._servMessage = this.Core.CreateMessage(Message.NotificationClass.SERV);
this.Messages.Add(this._servMessage);
}
this.dataGridSERV.Initialize();
this.dataGridSERV.ItemsSource = this._servMessage.Elements;
this.dataGridSERV.AddingNewItem += DataGridSERV_AddingNewItem;
this.dataGridSERV.EditRequested += DataGridSERV_EditRequested;
this.dataGridSERV.DeleteRequested += DataGridSERV_DeleteRequested;
this.dataGridSERV.CreateRequested += DataGridSERV_CreateRequested;
#endregion
#region init LADG
if(this._ladgMessage == null)
{
this._ladgMessage = this.Core.CreateMessage(Message.NotificationClass.LADG);
this.Messages.Add(this._ladgMessage);
}
this.dataGridLADG.Initialize();
this.dataGridLADG.ItemsSource = this._ladgMessage.Elements;
this.dataGridLADG.AddingNewItem += DataGridLADG_AddingNewItem;
this.dataGridLADG.EditRequested += DataGridLADG_EditRequested;
this.dataGridLADG.DeleteRequested += DataGridLADG_DeleteRequested;
this.dataGridLADG.CreateRequested += DataGridLADG_CreateRequested;
#endregion
#region init SERV templates
if(_servTemplates == null)
{
_servTemplates = await DBManagerAsync.GetSERVTemplatesAsync(); // initial load
_servTemplates.Sort();
Trace.WriteLine($"{_servTemplates.Count} SERV templates loaded");
}
this.comboBoxGroup.ItemsSource = _servTemplates;
this.buttonDeleteTemplate.Visibility = DBManager.Instance.GetReportingPartyDict()[App.UserId.Value].IsEditor ? Visibility.Visible : Visibility.Hidden;
this.buttonEditTemplate.Visibility = DBManager.Instance.GetReportingPartyDict()[App.UserId.Value].IsEditor ? Visibility.Visible : Visibility.Hidden;
this.buttonNewTemplate.Visibility = DBManager.Instance.GetReportingPartyDict()[App.UserId.Value].IsEditor ? Visibility.Visible : Visibility.Hidden;
#endregion
}
#region datagrid LADG
private void DataGridLADG_CreateRequested()
{
this.dataGridLADG.CancelEdit();
this.dataGridLADG.CancelEdit();
EditLADGDialog eld = new EditLADGDialog();
eld.LADG = new LADG();
eld.LADG.MessageHeader = _ladgMessage;
eld.LADG.Identifier = LADG.GetNewIdentifier(_ladgMessage.Elements);
eld.Core = this.Core;
eld.AddClicked += () =>
{
eld.CopyValuesToEntity();
if (!this._ladgMessage.Elements.Contains(eld.LADG))
this._ladgMessage.Elements.Add(eld.LADG);
this.dataGridLADG.Items.Refresh();
eld.LADG = new LADG();
eld.LADG.MessageHeader = _ladgMessage;
eld.LADG.Identifier = LADG.GetNewIdentifier(_ladgMessage.Elements);
this.SublistElementChanged(Message.NotificationClass.LADG);
};
if (eld.ShowDialog() ?? false)
{
if(!_ladgMessage.Elements.Contains(eld.LADG))
_ladgMessage.Elements.Add(eld.LADG);
this.dataGridLADG.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.LADG);
}
}
private void DataGridLADG_DeleteRequested(DatabaseEntity obj)
{
if (obj is LADG ladg)
{
this.dataGridLADG.CancelEdit();
this.dataGridLADG.CancelEdit();
// are you sure dialog is in base class
this._ladgMessage.Elements.Remove(ladg);
DBManager.GetSingleCon(Properties.Settings.Default.ConnectionString).Delete(ladg);
DatabaseEntity.ResetIdentifiers(_ladgMessage.Elements);
this.SublistElementChanged(Message.NotificationClass.LADG);
this.dataGridLADG.Items.Refresh();
}
}
private void DataGridLADG_EditRequested(DatabaseEntity obj)
{
this.dataGridLADG.CancelEdit();
this.dataGridLADG.CancelEdit();
LADG ladg = obj as LADG;
EditLADGDialog eld = new EditLADGDialog();
eld.Core = this.Core;
eld.LADG = ladg;
eld.AddClicked += () =>
{
eld.CopyValuesToEntity();
if(!_ladgMessage.Elements.Contains(eld.LADG))
_ladgMessage.Elements.Add(eld.LADG);
this.dataGridLADG.Items.Refresh();
eld.LADG = new LADG();
eld.LADG.Identifier = LADG.GetNewIdentifier(_ladgMessage.Elements);
eld.LADG.MessageHeader = _ladgMessage;
this.SublistElementChanged(Message.NotificationClass.LADG);
};
if (eld.ShowDialog() ?? false)
{
if (!_ladgMessage.Elements.Contains(eld.LADG))
_ladgMessage.Elements.Add(eld.LADG);
this.dataGridLADG.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.LADG);
}
}
private void DataGridLADG_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
this.DataGridLADG_CreateRequested();
}
#endregion
#region datagrid SERV
private void DataGridSERV_CreateRequested()
{
EditSERVDialog esd = new EditSERVDialog();
esd.SERV = new SERV();
esd.SERV.Identifier = SERV.GetNewIdentifier(_servMessage.Elements);
esd.SERV.MessageHeader = _servMessage;
esd.AddClicked += () =>
{
esd.CopyValuesToEntity();
if(!_servMessage.Elements.Contains(esd.SERV))
_servMessage.Elements.Add(esd.SERV);
this.dataGridSERV.Items.Refresh();
esd.SERV = new SERV();
esd.SERV.MessageHeader = _servMessage;
esd.SERV.Identifier = SERV.GetNewIdentifier(_servMessage.Elements);
this.SublistElementChanged(Message.NotificationClass.SERV);
};
if(esd.ShowDialog() ?? false)
{
if(!_servMessage.Elements.Contains(esd.SERV))
_servMessage.Elements.Add(esd.SERV);
this.dataGridSERV.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SERV);
}
}
private void DataGridSERV_DeleteRequested(DatabaseEntity obj)
{
if (obj is SERV serv)
{
// are you sure dialog is in base class
_servMessage.Elements.Remove(serv);
DBManager.GetSingleCon(Properties.Settings.Default.ConnectionString).Delete(serv);
DatabaseEntity.ResetIdentifiers(_servMessage.Elements);
this.SublistElementChanged(Message.NotificationClass.SERV);
this.dataGridSERV.Items.Refresh();
}
}
private void DataGridSERV_EditRequested(DatabaseEntity obj)
{
EditSERVDialog esd = new EditSERVDialog();
esd.SERV = obj as SERV;
esd.AddClicked += () =>
{
esd.CopyValuesToEntity();
if(!_servMessage.Elements.Contains(esd.SERV))
_servMessage.Elements.Add(esd.SERV);
this.dataGridSERV.Items.Refresh();
esd.SERV = new SERV();
esd.SERV.Identifier = SERV.GetNewIdentifier(_servMessage.Elements);
esd.SERV.MessageHeader = _servMessage;
this.SublistElementChanged(Message.NotificationClass.SERV);
};
if (esd.ShowDialog() ?? false)
{
if (!_servMessage.Elements.Contains(esd.SERV))
_servMessage.Elements.Add(esd.SERV);
this.dataGridSERV.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SERV);
}
}
private void DataGridSERV_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
this.DataGridSERV_CreateRequested();
}
#endregion
#region Highlighting
public override void HighlightErrorMessageContainer()
{
if (this._nameMessage.HasErrors)
HighlightService.HighlightControl(this.nameGroupBox, HighlightService.HighlightStyle.ERROR, this._nameMessage);
if (this._infoMessage.HasErrors)
HighlightService.HighlightControl(this.infoGroupBox, HighlightService.HighlightStyle.ERROR, this._infoMessage);
if (this._servMessage.HasErrors)
HighlightService.HighlightControl(this.servGroupBox, HighlightService.HighlightStyle.ERROR, this._servMessage);
if (this._ladgMessage.HasErrors)
HighlightService.HighlightControl(this.ladgGroupBox, HighlightService.HighlightStyle.ERROR, this._ladgMessage);
}
public override void HighlightViolationMessageContainer()
{
if (this._nameMessage.HasViolations)
HighlightService.HighlightControl(this.nameGroupBox, HighlightService.HighlightStyle.VIOLATION, this._nameMessage);
if (this._infoMessage.HasViolations)
HighlightService.HighlightControl(this.infoGroupBox, HighlightService.HighlightStyle.VIOLATION, this._infoMessage);
if (this._servMessage.HasViolations)
HighlightService.HighlightControl(this.servGroupBox, HighlightService.HighlightStyle.VIOLATION, this._servMessage);
if (this._ladgMessage.HasViolations)
HighlightService.HighlightControl(this.ladgGroupBox, HighlightService.HighlightStyle.VIOLATION, this._ladgMessage);
}
#endregion
#region Spezialbalkon für die Gruppenauswahl im Core (Maersk BHV / Seago usw.)
private void comboBoxGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(this.comboBoxGroup.SelectedItem is SERV_Template st)
{
this.buttonDeleteTemplate.IsEnabled = true;
this.buttonSetTemplate.IsEnabled = true;
this._currentTemplate = st;
}
}
private void buttonSetTemplate_Click(object sender, RoutedEventArgs e)
{
if (this.comboBoxGroup.SelectedItem is SERV_Template st)
{
bool found = false;
foreach (SERV serv in _servMessage.Elements.Cast())
{
if (serv.ServiceName.Equals(st.ServiceName))
{
found = true; break;
}
}
if (!found)
{
SERV newServ = new SERV();
newServ.ServiceName = st.ServiceName;
newServ.ServiceBeneficiary = st.ServiceBeneficiary;
newServ.ServiceInvoiceRecipient = st.ServiceInvoiceRecipient;
newServ.MessageHeader = this._servMessage;
newServ.Identifier = SERV.GetNewIdentifier(_servMessage.Elements);
this._servMessage.Elements.Add(newServ);
this.dataGridSERV.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SERV);
}
}
}
private void buttonNewTemplate_Click(object sender, RoutedEventArgs e)
{
SERV_Template newTemplate = new SERV_Template();
EditSERVDialog esd = new EditSERVDialog();
esd.AddVisible = false;
esd.SERV_Template = newTemplate;
if(esd.ShowDialog() ?? false)
{
_ = DBManagerAsync.SaveAsync(esd.SERV_Template);
this.comboBoxGroup.ItemsSource = null;
_servTemplates.Add(newTemplate);
_servTemplates.Sort();
this.comboBoxGroup.ItemsSource = _servTemplates;
}
}
private void buttonEditTemplate_Click(object sender, RoutedEventArgs e)
{
if (this.comboBoxGroup.SelectedItem is SERV_Template st)
{
EditSERVDialog editSERVDialog = new EditSERVDialog();
editSERVDialog.AddVisible = false;
editSERVDialog.SERV_Template = st;
if (editSERVDialog.ShowDialog() ?? false)
{
_ = DBManagerAsync.SaveAsync(st);
this.comboBoxGroup.ItemsSource = null;
_servTemplates.Sort();
this.comboBoxGroup.ItemsSource = _servTemplates;
}
}
}
private void buttonDeleteTemplate_Click(object sender, RoutedEventArgs e)
{
if (_currentTemplate != null)
{
if (MessageBox.Show("Delete this template?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
{
this.comboBoxGroup.SelectedItem = null;
this.comboBoxGroup.ItemsSource = null;
_ = DBManagerAsync.DeleteAsync(_currentTemplate);
_servTemplates.Remove(_currentTemplate);
this.buttonDeleteTemplate.IsEnabled = false;
this.comboBoxGroup.ItemsSource = _servTemplates;
this.buttonSetTemplate.IsEnabled = false;
}
}
}
#endregion
#region special entry ship service check
private void CheckServiceEntryMaerskBHV()
{
bool found = false;
foreach(SERV serv in _servMessage.Elements.Cast())
{
if (serv.ServiceBeneficiary.Equals("Maersk A/S, Esplanaden 50, DK-1263 Copenhagen K, VAT-ID: DK53139655"))
found = true;
}
if(!found)
{
SERV newServ = new SERV();
newServ.ServiceBeneficiary = "Maersk A/S, Esplanaden 50, DK-1263 Copenhagen K, VAT-ID: DK53139655";
newServ.ServiceInvoiceRecipient = "Maersk Deutschland A/S & Co.KG, Johanniswall 7, 20095 Hamburg";
newServ.ServiceName = "Maersk BHV";
newServ.MessageHeader = this._servMessage;
newServ.Identifier = SERV.GetNewIdentifier(_servMessage.Elements);
this._servMessage.Elements.Add(newServ);
this.dataGridSERV.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SERV);
}
}
private void CheckServiceEntrySeaGoBHV()
{
bool found = false;
foreach (SERV serv in _servMessage.Elements.Cast())
{
if (serv.ServiceBeneficiary.Equals("Sealand Europe A/S, Dampfaergevej 10, 3.tv, DK- 2100 Copenhagen, VAT-ID: DK53139655"))
found = true;
}
if (!found)
{
SERV newServ = new SERV();
newServ.ServiceBeneficiary = "Sealand Europe A/S, Dampfaergevej 10, 3.tv, DK- 2100 Copenhagen, VAT-ID: DK53139655";
newServ.ServiceInvoiceRecipient = "Maersk Deutschland A/S & Co. KG on behalf of Sealand Europe A/S, Johanniswall 7, 20095 Hamburg";
newServ.ServiceName = "SeaGo BHV";
newServ.MessageHeader = this._servMessage;
newServ.Identifier = SERV.GetNewIdentifier(_servMessage.Elements);
this._servMessage.Elements.Add(newServ);
this.dataGridSERV.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SERV);
}
}
private void CheckServiceEntryHoegh()
{
bool found = false;
foreach (SERV serv in _servMessage.Elements.Cast())
{
if (serv.ServiceBeneficiary.Equals("Höegh Autoliners AS, Oslo, Norway"))
found = true;
}
if (!found)
{
SERV newServ = new SERV();
newServ.ServiceBeneficiary = "Höegh Autoliners AS, Oslo, Norway";
newServ.ServiceInvoiceRecipient = " PWL Port Services GmbH & Co. KG";
newServ.ServiceName = "HOEGH BHV";
newServ.MessageHeader = this._servMessage;
newServ.Identifier = SERV.GetNewIdentifier(_servMessage.Elements);
this._servMessage.Elements.Add(newServ);
this.dataGridSERV.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SERV);
}
}
private void CheckServiceEntryFctJunge()
{
bool found = false;
foreach (SERV serv in _servMessage.Elements.Cast())
{
if (serv.ServiceName.Equals("Fct Junge - Hamburg"))
found = true;
}
if (!found)
{
SERV newServ = new SERV();
newServ.ServiceBeneficiary = "";
newServ.ServiceInvoiceRecipient = " Frachtcontor Junge & Co. GmbH";
newServ.ServiceName = "Fct Junge - Hamburg";
newServ.MessageHeader = this._servMessage;
newServ.Identifier = SERV.GetNewIdentifier(_servMessage.Elements);
this._servMessage.Elements.Add(newServ);
this.dataGridSERV.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SERV);
}
}
private void CheckServiceEntryElbeBulk()
{
bool found = false;
foreach (SERV serv in _servMessage.Elements.Cast())
{
if (serv.ServiceName.Equals("Elbe Bulk Schiffe - Hamburg"))
found = true;
}
if (!found)
{
SERV newServ = new SERV();
newServ.ServiceBeneficiary = "";
newServ.ServiceInvoiceRecipient = " Division Elbe Bulk, Frachtcontor Junge & Co. GmbH";
newServ.ServiceName = "Elbe Bulk Schiffe - Hamburg";
newServ.MessageHeader = this._servMessage;
newServ.Identifier = SERV.GetNewIdentifier(_servMessage.Elements);
this._servMessage.Elements.Add(newServ);
this.dataGridSERV.Items.Refresh();
this.SublistElementChanged(Message.NotificationClass.SERV);
}
}
private void buttonSearchPortArea_Click(object sender, RoutedEventArgs e)
{
if (portAreas != null)
{
SelectPortAreaDialog spad = new SelectPortAreaDialog(this.Core.PoC);
if (spad.ShowDialog() ?? false)
{
if (spad.SelectedArea != null)
{
if (portAreas.ContainsKey(spad.SelectedArea))
{
var pair = portAreas.SingleOrDefault(p => p.Key == spad.SelectedArea);
this.comboBoxPortArea.SelectedItem = pair;
}
}
}
}
}
#endregion
private void buttonCopyNameFromCREWA_Click(object sender, RoutedEventArgs e)
{
CREW crewA = null;
if (this._crewaMessage.Elements.Count > 0)
{
crewA = this._crewaMessage.Elements[0] as CREW;
}
SelectCrewMemberDialog scmd = new SelectCrewMemberDialog();
scmd.CREW = crewA;
scmd.AddVisible = false;
if (scmd.ShowDialog() ?? false)
{
this.textBox_NameMaster.Text = $"{crewA.CrewMemberFirstName} {crewA.CrewMemberLastName}";
this.SublistElementChanged(Message.NotificationClass.NAME);
}
}
}
}