261 lines
10 KiB
C#
261 lines
10 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 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)
|
|
/// </summary>
|
|
[Export(typeof(IDBImportView))]
|
|
[Export(typeof(IDBExportView))]
|
|
[PartCreationPolicy(CreationPolicy.Shared)]
|
|
public class DBViewModel : IDBViewModel
|
|
{
|
|
/// <summary>
|
|
/// the import view
|
|
/// </summary>
|
|
public IDBImportView ImportView { get; set; }
|
|
/// <summary>
|
|
/// the export view
|
|
/// </summary>
|
|
public IDBExportView ExportView { get; set; }
|
|
|
|
private IEventAggregator _eventAggregator { get; set; }
|
|
private IRegionManager _regionManager;
|
|
private IUnityContainer UnityContainer { get; set; }
|
|
|
|
public InteractionRequest<Notification> NotificationRequest { get; private set; }
|
|
public InteractionRequest<Confirmation> ConfirmationRequest { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the TechnologyDomainEditViewModel.
|
|
/// </summary>
|
|
/// <param name="view">The Power And Battery 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 DBViewModel(IDBImportView importView, IDBExportView exportView, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer)
|
|
{
|
|
ImportView = importView;
|
|
ImportView.DataContext = this;
|
|
|
|
ExportView = exportView;
|
|
ExportView.DataContext = this;
|
|
|
|
NotificationRequest = new InteractionRequest<Notification>();
|
|
ConfirmationRequest = new InteractionRequest<Confirmation>();
|
|
|
|
_eventAggregator = eventAggregator;
|
|
UnityContainer = unityContainer;
|
|
_regionManager = regionManager;
|
|
_eventAggregator.GetEvent<RaiseNotification>().Subscribe(OnRaiseNotification);
|
|
_eventAggregator.GetEvent<BusyIndicatorChangeNotification>().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() { }
|
|
|
|
/// <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 });
|
|
}
|
|
/// <summary>
|
|
/// exports ExportData to ExportFileName
|
|
/// </summary>
|
|
public void Export()
|
|
{
|
|
//System.IO.File.WriteAllText(_exportFile, sb.ToString(), Encoding.Unicode);
|
|
if (string.IsNullOrWhiteSpace(ExportFileName))
|
|
{
|
|
_eventAggregator.GetEvent<ShowStatus>()
|
|
.Publish(new StatusInfo(StatusInfo.StatusState.DoneFailed,
|
|
Resources.StringResources.ExportFileName_Empty));
|
|
return;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(ExportData))
|
|
{
|
|
_eventAggregator.GetEvent<ShowStatus>().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"); } }
|
|
/// <summary>
|
|
/// Gets the HeaderInfo.
|
|
/// </summary>
|
|
public string HeaderInfo => "MainRegion";
|
|
/// <summary>
|
|
/// xml string containing all data imported
|
|
/// </summary>
|
|
public string ImportData { get; set; }
|
|
/// <summary>
|
|
/// xml string containing all data exported
|
|
/// </summary>
|
|
public string ExportData { get; set; }
|
|
/// <summary>
|
|
/// exports ExportData to the ExportFileName
|
|
/// </summary>
|
|
#endregion Properties
|
|
|
|
#region Commands
|
|
/// <summary>
|
|
/// browse to a file to import, should be xml, maybe needs a few other criteria
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// browse to a file to import, should be xml, maybe needs a few other criteria
|
|
/// </summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// all properties that are exposed
|
|
/// </summary>
|
|
public enum PropertyNames
|
|
{
|
|
ExportFileName,
|
|
ImportFileName,
|
|
ImportStatusText
|
|
}
|
|
private string _exportFileName = "";
|
|
/// <summary>
|
|
/// full path to export file
|
|
/// </summary>
|
|
public string ExportFileName
|
|
{
|
|
get => _exportFileName;
|
|
set
|
|
{
|
|
_exportFileName = value;
|
|
OnPropertyChanged(PropertyNames.ExportFileName.ToString());
|
|
}
|
|
}
|
|
|
|
private string _importFileName = "";
|
|
/// <summary>
|
|
/// full path to import file
|
|
/// </summary>
|
|
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
|
|
|
|
///<summary>
|
|
///Occurs when a property value changes.
|
|
///</summary>
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
public void OnPropertyChanged(string propertyName)
|
|
{
|
|
var eventHandler = PropertyChanged;
|
|
if (eventHandler != null) { eventHandler(this, new PropertyChangedEventArgs(propertyName)); }
|
|
}
|
|
}
|
|
}
|