using System.ComponentModel; using System.ComponentModel.Composition; using System.Threading.Tasks; using DTS.Common.Events; using DTS.Common.Interface; using Prism.Events; using Prism.Regions; using Unity; using DTS.Common.Utils; using System.Text; using DTS.Common.Interactivity; using Prism.Commands; // ReSharper disable CheckNamespace // ReSharper disable MemberCanBePrivate.Global // ReSharper disable InconsistentNaming namespace DBImportExport { /// /// this class handles DB import/export functionality /// (note that since a lot of of DataPRO objects still live in the datapro project it's functionality is limited to the xml string going back and forth right now) /// [Export(typeof(IDBImportView))] [Export(typeof(IDBExportView))] [PartCreationPolicy(CreationPolicy.Shared)] public class DBViewModel : IDBViewModel { /// /// the import view /// public IDBImportView ImportView { get; set; } /// /// the export view /// public IDBExportView ExportView { get; set; } private IEventAggregator _eventAggregator { get; set; } private IRegionManager _regionManager; private IUnityContainer UnityContainer { get; set; } public InteractionRequest NotificationRequest { get; private set; } public InteractionRequest ConfirmationRequest { get; private set; } /// /// Creates a new instance of the TechnologyDomainEditViewModel. /// /// The Power And Battery View. /// 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 DBViewModel(IDBImportView importView, IDBExportView exportView, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) { ImportView = importView; ImportView.DataContext = this; ExportView = exportView; ExportView.DataContext = this; NotificationRequest = new InteractionRequest(); ConfirmationRequest = new InteractionRequest(); _eventAggregator = eventAggregator; UnityContainer = unityContainer; _regionManager = regionManager; _eventAggregator.GetEvent().Subscribe(OnRaiseNotification); _eventAggregator.GetEvent().Subscribe(OnBusyIndicatorNotification, ThreadOption.PublisherThread, true); } #region Methods 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 }); } /// /// exports ExportData to ExportFileName /// public void Export() { //System.IO.File.WriteAllText(_exportFile, sb.ToString(), Encoding.Unicode); if (string.IsNullOrWhiteSpace(ExportFileName)) { _eventAggregator.GetEvent() .Publish(new StatusInfo(StatusInfo.StatusState.DoneFailed, Resources.StringResources.ExportFileName_Empty)); return; } if (string.IsNullOrWhiteSpace(ExportData)) { _eventAggregator.GetEvent().Publish(new StatusInfo(StatusInfo.StatusState.DoneFailed, Resources.StringResources.ExportFileData_Empty)); return; } if (System.IO.File.Exists(ExportFileName)) { FileUtils.DeleteFileOrMove(ExportFileName, LogDummyFunc); } System.IO.File.WriteAllText(ExportFileName, ExportData, Encoding.Unicode); } private void LogDummyFunc(params object[] paramlist) { return; } #endregion #region Properties public bool IsDirty { get; private set; } private bool _isBusy = false; public bool IsBusy { get => _isBusy; set { _isBusy = value; OnPropertyChanged("IsBusy"); } } private bool _isMenuIncluded = false; public bool IsMenuIncluded { get => _isMenuIncluded; set { _isMenuIncluded = value; OnPropertyChanged("IsMenuIncluded"); } } private bool _isNavigationIncluded = false; public bool IsNavigationIncluded { get => _isNavigationIncluded; set { _isNavigationIncluded = value; OnPropertyChanged("IsNavigationIncluded"); } } /// /// Gets the HeaderInfo. /// public string HeaderInfo => "MainRegion"; /// /// xml string containing all data imported /// public string ImportData { get; set; } /// /// xml string containing all data exported /// public string ExportData { get; set; } /// /// exports ExportData to the ExportFileName /// #endregion Properties #region Commands /// /// browse to a file to import, should be xml, maybe needs a few other criteria /// private DelegateCommand _importBrowseCommand; public DelegateCommand ImportBrowseCommand => _importBrowseCommand ?? (_importBrowseCommand = new DelegateCommand(ImportBrowseMethod)); private void ImportBrowseMethod() { using (var dlg = new System.Windows.Forms.OpenFileDialog()) { dlg.CheckFileExists = true; dlg.CheckPathExists = true; dlg.Filter = Resources.StringResources.ImportFileBrowse_Filter; dlg.FilterIndex = 0; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { ImportFileName = dlg.FileName; ImportStatusText = "Pressing the Import button will delete your existing database and replace it using " + ImportFileName; } } } /// /// browse to a file to import, should be xml, maybe needs a few other criteria /// private DelegateCommand _exportBrowseCommand; public DelegateCommand ExportBrowseCommand => _exportBrowseCommand ?? (_exportBrowseCommand = new DelegateCommand(ExportBrowseMethod)); private void ExportBrowseMethod() { using (var dlg = new System.Windows.Forms.SaveFileDialog()) { dlg.CheckFileExists = false; dlg.CheckPathExists = true; dlg.OverwritePrompt = false; dlg.Filter = Resources.StringResources.ExportFileBrowse_Filter; dlg.FilterIndex = 0; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { ExportFileName = dlg.FileName; } } } #endregion #region properties /// /// all properties that are exposed /// public enum PropertyNames { ExportFileName, ImportFileName, ImportStatusText } private string _exportFileName = ""; /// /// full path to export file /// public string ExportFileName { get => _exportFileName; set { _exportFileName = value; OnPropertyChanged(PropertyNames.ExportFileName.ToString()); } } private string _importFileName = ""; /// /// full path to import file /// public string ImportFileName { get => _importFileName; set { _importFileName = value; OnPropertyChanged(PropertyNames.ImportFileName.ToString()); } } private string _importStatusText; public string ImportStatusText { get => _importStatusText; set { _importStatusText = value; OnPropertyChanged(PropertyNames.ImportStatusText.ToString()); } } #endregion /// ///Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { var eventHandler = PropertyChanged; if (eventHandler != null) { eventHandler(this, new PropertyChangedEventArgs(propertyName)); } } } }