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 { /// /// this class handles DatabaseCopy functionality /// [PartCreationPolicy(CreationPolicy.Shared)] public class DatabaseSwitchViewModel : IDatabaseSwitchViewModel { /// /// The DatabaseCopy view /// public IDatabaseSwitchView View { get; set; } private IEventAggregator _eventAggregator { get; } private IUnityContainer UnityContainer { get; } public InteractionRequest NotificationRequest { get; } public InteractionRequest ConfirmationRequest { get; } /// /// /// Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #region constructors and initializers /// /// Creates a new instance of the DatabaseCopyViewModel /// /// /// The logical placeholder defined within the application's UI (in the shell or within views) into which views are displayed. /// The EventAggregator which allows different components to publish/subscribe to events without being coupled to each other. /// The unityContainer. public DatabaseSwitchViewModel(IDatabaseSwitchView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) { View = view; View.DataContext = this; NotificationRequest = new InteractionRequest(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; UnityContainer = unityContainer; _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); _eventAggregator.GetEvent() .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() { } /// /// Private Event handler for RaiseNotification event. /// private void OnBusyIndicatorNotification(bool eventArg) { IsBusy = eventArg; } /// /// Private Event handler for RaiseNotification event. /// 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().Publish(AppStatusArg.Busy); try { DTS.Common.Storage.DatabaseServices.SetupForRemoteDb(DbHost, NTLMAuthentication, DbUser, DbPassword, DefaultDbName); if (!DTS.Common.Storage.DatabaseServices.SimpleDbTest()) { _eventAggregator.GetEvent().Publish( new DbStatusArg(DbStatusArg.EventTypes.FailedToConnectToRemote, null)); SwitchLocal(); return; } } catch (Exception ex) { _eventAggregator.GetEvent() .Publish(new DbStatusArg(DbStatusArg.EventTypes.FailedToConnectToRemote, ex)); APILogger.Log(ex); SwitchLocal(); return; } OnPropertyChanged("RemoteIsActive"); _eventAggregator.GetEvent().Publish(new DbStatusArg(DbStatusArg.EventTypes.LegacyStatus, null)); _eventAggregator.GetEvent().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().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().Publish(new PageErrorArg(new[] { Resources.StringResources.SwitchFileNotFound, ex.Message }, null)); return; } _eventAggregator.GetEvent().Publish(AppStatusArg.Busy); DTS.Common.Storage.DatabaseServices.SetupLocal(DefaultDbName); OnPropertyChanged("RemoteIsActive"); _eventAggregator.GetEvent().Publish(new DbStatusArg(DbStatusArg.EventTypes.LegacyStatus, null)); _eventAggregator.GetEvent().Publish(AppStatusArg.Available); _eventAggregator.GetEvent() .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 } }