203 lines
10 KiB
C#
203 lines
10 KiB
C#
// 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 log4net;
|
|
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using System;
|
|
using System.Net;
|
|
using ENI2.LockingServiceReference;
|
|
using ENI2.Util;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Office.Interop.Excel;
|
|
using System.Drawing.Drawing2D;
|
|
|
|
namespace ENI2
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for App.xaml
|
|
/// </summary>
|
|
public partial class App : System.Windows.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;
|
|
|
|
private ILog _log = LogManager.GetLogger(typeof(App).Name);
|
|
|
|
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<int, string> cargoHandlingDict = LocalizedLookup.getLADGCargoHandlingStrings(langKey);
|
|
foreach (int key in cargoHandlingDict.Keys)
|
|
LADG.CargoHandlingDict.Add(key, cargoHandlingDict[key]);
|
|
LADG.MVSHLocodes.AddRange(LocalizedLookup.getMVSHLocodes());
|
|
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
|
|
Task.Run(async () => await ValueMapping.LoadDicts());
|
|
|
|
// Preload validation fields
|
|
List<ValidationField> 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<Waste>(typeof(IList<Waste>));
|
|
// NOA_NOD
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(NOA_NOD));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(CallPurpose));
|
|
TypeDecorationManager.AddExpandableIListConverter<CallPurpose>(typeof(IList<CallPurpose>));
|
|
// SEC
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(SEC));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(LastTenPortFacilitiesCalled));
|
|
TypeDecorationManager.AddExpandableIListConverter<LastTenPortFacilitiesCalled>(typeof(IList<LastTenPortFacilitiesCalled>));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(ShipToShipActivitiesDuringLastTenPortFacilitiesCalled));
|
|
TypeDecorationManager.AddExpandableIListConverter<ShipToShipActivitiesDuringLastTenPortFacilitiesCalled>(typeof(IList<ShipToShipActivitiesDuringLastTenPortFacilitiesCalled>));
|
|
// MDH
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(MDH));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(SanitaryMeasuresDetail));
|
|
TypeDecorationManager.AddExpandableIListConverter<SanitaryMeasuresDetail>(typeof(IList<SanitaryMeasuresDetail>));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(StowawaysJoiningLocation));
|
|
TypeDecorationManager.AddExpandableIListConverter<StowawaysJoiningLocation>(typeof(IList<StowawaysJoiningLocation>));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(PortOfCallLast30Days));
|
|
TypeDecorationManager.AddExpandableIListConverter<PortOfCallLast30Days>(typeof(IList<PortOfCallLast30Days>));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(PortOfCallLast30DaysCrewJoinedShip));
|
|
TypeDecorationManager.AddExpandableIListConverter<PortOfCallLast30Days>(typeof(IList<PortOfCallLast30DaysCrewJoinedShip>));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(InfectedArea));
|
|
TypeDecorationManager.AddExpandableIListConverter<InfectedArea>(typeof(IList<InfectedArea>));
|
|
// BPOL
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(BPOL));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(PortOfItinerary));
|
|
TypeDecorationManager.AddExpandableIListConverter<PortOfItinerary>(typeof(IList<PortOfItinerary>));
|
|
// HAZ
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(HAZ));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(IMDGPosition));
|
|
TypeDecorationManager.AddExpandableIListConverter<IMDGPosition>(typeof(IList<IMDGPosition>));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(IBCPosition));
|
|
TypeDecorationManager.AddExpandableIListConverter<IBCPosition>(typeof(IList<IBCPosition>));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(IGCPosition));
|
|
TypeDecorationManager.AddExpandableIListConverter<IGCPosition>(typeof(IList<IGCPosition>));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(IMSBCPosition));
|
|
TypeDecorationManager.AddExpandableIListConverter<IMSBCPosition>(typeof(IList<IMSBCPosition>));
|
|
TypeDecorationManager.AddExpandableObjectConverter(typeof(MARPOL_Annex_I_Position));
|
|
TypeDecorationManager.AddExpandableIListConverter<MARPOL_Annex_I_Position>(typeof(IList<MARPOL_Annex_I_Position>));
|
|
|
|
// 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);
|
|
Xceed.Wpf.Toolkit.MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
e.Handled = true;
|
|
_log.Error(errorMessage);
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|