260 lines
9.1 KiB
C#
260 lines
9.1 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.ComponentModel.Composition;
|
|
using System.Threading.Tasks;
|
|
using DTS.Common.Events;
|
|
using DTS.Common.Events.Database;
|
|
using Prism.Events;
|
|
using Prism.Regions;
|
|
using Unity;
|
|
using DTS.Common.Interactivity;
|
|
using Prism.Commands;
|
|
using DTS.Common.Interface.Database;
|
|
using DTS.Common.Storage;
|
|
using DTS.Common.Utilities.Logging;
|
|
|
|
// ReSharper disable CheckNamespace
|
|
// ReSharper disable MemberCanBePrivate.Global
|
|
// ReSharper disable InconsistentNaming
|
|
|
|
namespace DatabaseServices
|
|
{
|
|
/// <summary>
|
|
/// this class handles DatabaseCopy functionality
|
|
/// </summary>
|
|
[PartCreationPolicy(CreationPolicy.Shared)]
|
|
public class DatabaseSwitchViewModel : IDatabaseSwitchViewModel
|
|
{
|
|
/// <summary>
|
|
/// The DatabaseCopy view
|
|
/// </summary>
|
|
public IDatabaseSwitchView View { get; set; }
|
|
|
|
private IEventAggregator _eventAggregator { get; }
|
|
private IUnityContainer UnityContainer { get; }
|
|
|
|
public InteractionRequest<Notification> NotificationRequest { get; }
|
|
public InteractionRequest<Confirmation> ConfirmationRequest { get; }
|
|
|
|
/// <inheritdoc />
|
|
/// <summary>
|
|
/// Occurs when a property value changes.
|
|
/// </summary>
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
public void OnPropertyChanged(string propertyName)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
#region constructors and initializers
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the DatabaseCopyViewModel
|
|
/// </summary>
|
|
/// <param name="view"></param>
|
|
/// <param name="regionManager">The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed.</param>
|
|
/// <param name="eventAggregator">The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other.</param>
|
|
/// <param name="unityContainer">The unityContainer.</param>
|
|
public DatabaseSwitchViewModel(IDatabaseSwitchView view, IRegionManager regionManager,
|
|
IEventAggregator eventAggregator, IUnityContainer unityContainer)
|
|
{
|
|
View = view;
|
|
View.DataContext = this;
|
|
|
|
NotificationRequest = new InteractionRequest<Notification>();
|
|
ConfirmationRequest = new InteractionRequest<Confirmation>();
|
|
|
|
_eventAggregator = eventAggregator;
|
|
UnityContainer = unityContainer;
|
|
_eventAggregator.GetEvent<RaiseNotification>().Subscribe(OnRaiseNotification);
|
|
_eventAggregator.GetEvent<BusyIndicatorChangeNotification>()
|
|
.Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
public void Unset()
|
|
{
|
|
}
|
|
|
|
public void Cleanup()
|
|
{
|
|
}
|
|
|
|
public Task CleanupAsync()
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
}
|
|
|
|
public void Initialize(object parameter)
|
|
{
|
|
}
|
|
|
|
public void Initialize(object parameter, object model)
|
|
{
|
|
}
|
|
|
|
public Task InitializeAsync()
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task InitializeAsync(object parameter)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void Activated()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Private Event handler for RaiseNotification event.
|
|
/// </summary>
|
|
private void OnBusyIndicatorNotification(bool eventArg)
|
|
{
|
|
IsBusy = eventArg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Private Event handler for RaiseNotification event.
|
|
/// </summary>
|
|
private void OnRaiseNotification(NotificationContentEventArgs eventArgsWithTitle)
|
|
{
|
|
// The NotificationRequest.Raise triggers the Invoke() method of the PopupWindowAction object to show the NotificationWindow window
|
|
// Notification object expects a NotificationContentEventArgsWithoutTitle object and a Title string.
|
|
var eventArgsWithoutTitle = new NotificationContentEventArgs(eventArgsWithTitle.Message, "",
|
|
eventArgsWithTitle.Image, string.Empty);
|
|
|
|
NotificationRequest.Raise(new Notification
|
|
{
|
|
Content = eventArgsWithoutTitle,
|
|
Title = eventArgsWithTitle.Title
|
|
});
|
|
}
|
|
|
|
public void SwitchRemote()
|
|
{
|
|
_eventAggregator.GetEvent<AppStatusEvent>().Publish(AppStatusArg.Busy);
|
|
try
|
|
{
|
|
DTS.Common.Storage.DatabaseServices.SetupForRemoteDb(DbHost, NTLMAuthentication, DbUser, DbPassword, DefaultDbName);
|
|
if (!DTS.Common.Storage.DatabaseServices.SimpleDbTest())
|
|
{
|
|
_eventAggregator.GetEvent<DbStatusEvent>().Publish(
|
|
new DbStatusArg(DbStatusArg.EventTypes.FailedToConnectToRemote, null));
|
|
SwitchLocal();
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_eventAggregator.GetEvent<DbStatusEvent>()
|
|
.Publish(new DbStatusArg(DbStatusArg.EventTypes.FailedToConnectToRemote, ex));
|
|
APILogger.Log(ex);
|
|
SwitchLocal();
|
|
return;
|
|
}
|
|
OnPropertyChanged("RemoteIsActive");
|
|
_eventAggregator.GetEvent<DbStatusEvent>().Publish(new DbStatusArg(DbStatusArg.EventTypes.LegacyStatus, null));
|
|
_eventAggregator.GetEvent<AppStatusEvent>().Publish(AppStatusArg.Available);
|
|
//http://manuscript.dts.local/f/cases/39203/Local-mode-interacts-with-central-db
|
|
//we are switching to a different database connection, make sure we make the user log in again
|
|
//we aren't guaranteed the user details are sync'd between local and remote
|
|
_eventAggregator.GetEvent<LogoutUserEvent>().Publish(new LogoutUserArg(LogoutUserArg.Reasons.DatabaseSwitch));
|
|
}
|
|
|
|
public void SwitchLocal()
|
|
{
|
|
try
|
|
{
|
|
DTS.Common.Utils.Database.CheckLocalDatabaseFilesExist(DefaultDbName, DTS.Common.Storage.DatabaseServices.LOCAL_DB_FOLDER);
|
|
}
|
|
catch (System.IO.FileNotFoundException ex)
|
|
{
|
|
_eventAggregator.GetEvent<PageErrorEvent>().Publish(new PageErrorArg(new[] { Resources.StringResources.SwitchFileNotFound, ex.Message }, null));
|
|
return;
|
|
}
|
|
_eventAggregator.GetEvent<AppStatusEvent>().Publish(AppStatusArg.Busy);
|
|
DTS.Common.Storage.DatabaseServices.SetupLocal(DefaultDbName);
|
|
OnPropertyChanged("RemoteIsActive");
|
|
_eventAggregator.GetEvent<DbStatusEvent>().Publish(new DbStatusArg(DbStatusArg.EventTypes.LegacyStatus, null));
|
|
_eventAggregator.GetEvent<AppStatusEvent>().Publish(AppStatusArg.Available);
|
|
_eventAggregator.GetEvent<LogoutUserEvent>()
|
|
.Publish(new LogoutUserArg(LogoutUserArg.Reasons.DatabaseSwitch));
|
|
}
|
|
|
|
public void InitializeDbSettings(string defaultDbName, string dbHost, bool ntlmAuthentication, string dbUser,
|
|
string dbPassword)
|
|
{
|
|
DefaultDbName = defaultDbName;
|
|
DbHost = dbHost;
|
|
NTLMAuthentication = ntlmAuthentication;
|
|
DbUser = dbUser;
|
|
DbPassword = dbPassword;
|
|
}
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public string DefaultDbName
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
public bool RemoteIsActive => DbOperations._usingCentralizedDB;
|
|
|
|
public bool IsDirty { get; private set; }
|
|
private bool _isBusy = false;
|
|
public bool IsBusy
|
|
{
|
|
get => _isBusy;
|
|
set
|
|
{
|
|
_isBusy = value;
|
|
OnPropertyChanged("IsBusy");
|
|
}
|
|
}
|
|
|
|
private bool _isMenuIncluded;
|
|
public bool IsMenuIncluded
|
|
{
|
|
get => _isMenuIncluded;
|
|
set
|
|
{
|
|
_isMenuIncluded = value;
|
|
OnPropertyChanged("IsMenuIncluded");
|
|
}
|
|
}
|
|
|
|
private bool _isNavigationIncluded;
|
|
public bool IsNavigationIncluded
|
|
{
|
|
get => _isNavigationIncluded;
|
|
set
|
|
{
|
|
_isNavigationIncluded = value;
|
|
OnPropertyChanged("IsNavigationIncluded");
|
|
}
|
|
}
|
|
|
|
public string DbHost { get; private set; }
|
|
public bool NTLMAuthentication { get; private set; }
|
|
public string DbUser { get; private set; }
|
|
public string DbPassword { get; private set; }
|
|
#endregion Properties
|
|
|
|
#region Commands
|
|
|
|
#endregion
|
|
}
|
|
}
|