// Copyright (c) 2017 Informatibüro Daniel Schick
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Markup;
using bsmd.database;
using System.Windows.Controls;
using System.Windows.Input;
using System;
using System.Net;
using ENI2.LockingServiceReference;
using ENI2.Util;
using System.Threading;
namespace ENI2
{
///
/// Interaction logic for App.xaml
///
public partial class App : Application
{
// "global" statics, da gibt es sicher noch einen eleganteren Weg..
private static ServiceClient _lockingServiceClient = null;
private static Guid? _userId = null;
// Splash screen
private static SplashScreenWindow _splashScreenWindow;
private ManualResetEvent ResetSplashCreated;
private Thread _splashThread;
public App() : base()
{
this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
}
public static ServiceClient LockingServiceClient { get { return _lockingServiceClient; } }
public static void RestartLockingService()
{
_lockingServiceClient = new ServiceClient("BasicHttpBinding_IService", ENI2.Properties.Settings.Default.LockingServerAddress);
}
public static SplashScreenWindow SplashScreen { get { return _splashScreenWindow; } }
public static Guid? UserId { get { return _userId; } set { _userId = value; } }
protected override void OnStartup(StartupEventArgs e)
{
this.ResetSplashCreated = new ManualResetEvent(false);
this._splashThread = new Thread(ShowSplash);
_splashThread.SetApartmentState(ApartmentState.STA);
_splashThread.IsBackground = true;
_splashThread.Name = "Splash Screen";
_splashThread.Start();
ResetSplashCreated.WaitOne();
base.OnStartup(e);
// set connectnion string to async loader
DBManagerAsync.ConnectionString = ENI2.Properties.Settings.Default.ConnectionString;
// initialize static / localized lookups from sqlite database
string langKey = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
Dictionary cargoHandlingDict = LocalizedLookup.getLADGCargoHandlingStrings(langKey);
foreach (int key in cargoHandlingDict.Keys)
LADG.CargoHandlingDict.Add(key, cargoHandlingDict[key]);
EventManager.RegisterClassHandler(typeof(DatePicker), DatePicker.PreviewKeyDownEvent, new KeyEventHandler(this.DatePicker_PreviewKeyDown));
CREW.NationalityDict = LocalizedLookup.getNationalities();
STAT.VesselTypeDict = LocalizedLookup.getVesselTypes();
STAT.TransportModeDict = LocalizedLookup.getTransportModes();
HAZ.PackageTypes = LocalizedLookup.getPackageTypes();
LADG.LACodes = LocalizedLookup.getLACodes();
LADG.CargoCodesNST = LocalizedLookup.getCargoCodesNST();
LADG.CargoCodesNST3 = LocalizedLookup.getCargoCodesNST3();
// Load import value mappings
ValueMapping.LoadDicts();
// Preload validation fields
List vFields = bsmd.database.ValidationRule.ValidationFields;
RuleEngine.RegisterLocodeChecker(Util.GlobalStructures.IsValidLocode);
RuleEngine.RegisterPortAreaChecker(LocalizedLookup.PortAreaExists);
RuleEngine.RegisterNationalityChecker(LocalizedLookup.CheckNationality);
// Register expandable Properties
// WAS
TypeDecorationManager.AddExpandableObjectConverter(typeof(WAS));
TypeDecorationManager.AddExpandableObjectConverter(typeof(Waste));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
// NOA_NOD
TypeDecorationManager.AddExpandableObjectConverter(typeof(NOA_NOD));
TypeDecorationManager.AddExpandableObjectConverter(typeof(CallPurpose));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
// SEC
TypeDecorationManager.AddExpandableObjectConverter(typeof(SEC));
TypeDecorationManager.AddExpandableObjectConverter(typeof(LastTenPortFacilitiesCalled));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
TypeDecorationManager.AddExpandableObjectConverter(typeof(ShipToShipActivitiesDuringLastTenPortFacilitiesCalled));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
// MDH
TypeDecorationManager.AddExpandableObjectConverter(typeof(MDH));
TypeDecorationManager.AddExpandableObjectConverter(typeof(SanitaryMeasuresDetail));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
TypeDecorationManager.AddExpandableObjectConverter(typeof(StowawaysJoiningLocation));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
TypeDecorationManager.AddExpandableObjectConverter(typeof(PortOfCallLast30Days));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
TypeDecorationManager.AddExpandableObjectConverter(typeof(PortOfCallLast30DaysCrewJoinedShip));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
TypeDecorationManager.AddExpandableObjectConverter(typeof(InfectedArea));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
// BPOL
TypeDecorationManager.AddExpandableObjectConverter(typeof(BPOL));
TypeDecorationManager.AddExpandableObjectConverter(typeof(PortOfItinerary));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
// HAZ
TypeDecorationManager.AddExpandableObjectConverter(typeof(HAZ));
TypeDecorationManager.AddExpandableObjectConverter(typeof(IMDGPosition));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
TypeDecorationManager.AddExpandableObjectConverter(typeof(IBCPosition));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
TypeDecorationManager.AddExpandableObjectConverter(typeof(IGCPosition));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
TypeDecorationManager.AddExpandableObjectConverter(typeof(IMSBCPosition));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
TypeDecorationManager.AddExpandableObjectConverter(typeof(MARPOL_Annex_I_Position));
TypeDecorationManager.AddExpandableIListConverter(typeof(IList));
// Connect to locking service (if enabled)
try
{
if (ENI2.Properties.Settings.Default.UseLocking)
{
App._lockingServiceClient = new ServiceClient("BasicHttpBinding_IService", ENI2.Properties.Settings.Default.LockingServerAddress);
}
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine("Exception creating locking service client: {0}", ex.ToString());
}
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
string errorMessage = string.Format("An unhandled exception occurred: {0}\r\n{1}", e.Exception.Message, e.Exception.StackTrace);
MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
// TODO: Dieser Fehler muss irgendwohin gesendet / gespeichert werden
e.Handled = true;
}
private void DatePicker_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!(sender is DatePicker dp)) return;
if (e.Key == Key.D && Keyboard.Modifiers == ModifierKeys.Control)
{
e.Handled = true;
dp.SetValue(DatePicker.SelectedDateProperty, DateTime.Today);
return;
}
if (!dp.SelectedDate.HasValue) return;
var date = dp.SelectedDate.Value;
if (e.Key == Key.Up)
{
e.Handled = true;
dp.SetValue(DatePicker.SelectedDateProperty, date.AddDays(1));
}
else if (e.Key == Key.Down)
{
e.Handled = true;
dp.SetValue(DatePicker.SelectedDateProperty, date.AddDays(-1));
}
}
private void ShowSplash()
{
_splashScreenWindow = new SplashScreenWindow();
_splashScreenWindow.Show();
ResetSplashCreated.Set();
System.Windows.Threading.Dispatcher.Run();
}
}
}