| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- using Comal.Stores;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Database;
- using InABox.Database.SQLite;
- using InABox.DynamicGrid;
- using InABox.Wpf;
- using InABox.WPF;
- using PRSServices;
- using Stripe.Treasury;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace PRSServer.Forms.Version9Update;
- public class UpdateDatabaseFilesGridItem : BaseObject
- {
- public string Name { get; set; }
- public string FileName { get; set; }
- public IProviderFactory ProviderFactory { get; set; }
- public VersionNumber Version { get; set; }
- public string VersionText => Version?.ToString() ?? "";
- public bool NeedsUpdate => Version is not null && Version.MajorVersion < 9;
- }
- public class UpdateDatabaseFilesGrid : DynamicItemsListGrid<UpdateDatabaseFilesGridItem>
- {
- private static readonly BitmapImage? _warning = Properties.Resources.warning.AsBitmapImage();
- private static readonly BitmapImage? _tick = Properties.Resources.tick.AsBitmapImage();
- protected override void Init()
- {
- base.Init();
- ActionColumns.Add(new DynamicImageColumn(GetImage, UpdateClick)
- {
- ToolTip = UpdateToolTip
- });
- }
- private FrameworkElement? UpdateToolTip(DynamicActionColumn column, CoreRow? row)
- {
- if(row is null)
- {
- return null;
- }
- var item = LoadItem(row);
- if (item.NeedsUpdate)
- {
- return column.TextToolTip($"This database is currently version {item.VersionText} and needs updating to version 9.");
- }
- else
- {
- return null;
- }
- }
- protected override DynamicGridColumns LoadColumns()
- {
- var columns = new DynamicGridColumns();
- columns.Add<UpdateDatabaseFilesGridItem>(x => x.Name);
- columns.Add<UpdateDatabaseFilesGridItem>(x => x.VersionText, caption: "Current Version");
- return columns;
- }
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- options.Clear();
- }
- private bool UpdateClick(CoreRow? row)
- {
- if (row is null) return false;
- var item = LoadItem(row);
- if (!item.NeedsUpdate) return false;
- var currentVersion = DbFactory.GetDatabaseVersion(item.ProviderFactory);
- if(currentVersion.MajorVersion >= 9)
- {
- item.Version = currentVersion;
- return true;
- }
- var task = Task.Run(() =>
- {
- DbFactory.Stores = CoreUtils.TypeList(
- AppDomain.CurrentDomain.GetAssemblies(),
- myType =>
- myType.IsClass
- && !myType.IsAbstract
- && !myType.IsGenericType
- && myType.GetInterfaces().Contains(typeof(IStore))
- ).ToArray();
- DbFactory.DefaultStore = typeof(BaseStore<>);
- var factory = new SQLiteProviderFactory(item.FileName);
- DbFactory.ProviderFactory = factory;
- DbFactory.Start(); // Doing this will run all the update scripts.
- factory.InitializeNulls();
- });
- var window = Window.GetWindow(this);
- var console = new UpdateConsole("Console");
- console.Top = window.Top;
- console.Left = window.Left + window.Width + 2;
- console.Height = window.Height;
- console.Loaded += (_, args) =>
- {
- var worker = new BackgroundWorker();
- window.IsEnabled = false;
- worker.DoWork += (o, e) =>
- {
- task.Wait();
- };
- worker.RunWorkerCompleted +=
- (o, e) => console.Close();
- worker.RunWorkerAsync();
- };
- console.ShowDialog();
- try
- {
- task.Wait();
- }
- catch(Exception e)
- {
- MessageWindow.ShowError("An error occurred while trying to update this database.", e);
- }
- window.IsEnabled = true;
- currentVersion = DbFactory.GetDatabaseVersion(item.ProviderFactory);
- item.Version = currentVersion;
- return true;
- }
- private BitmapImage? GetImage(CoreRow? row)
- {
- if (row is null) return _tick;
- return LoadItem(row).NeedsUpdate ? _warning : _tick;
- }
- }
- /// <summary>
- /// Interaction logic for UpdateDatabaseFiles.xaml
- /// </summary>
- public partial class UpdateDatabaseFiles : Window
- {
- public UpdateDatabaseFiles()
- {
- InitializeComponent();
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- LoadData();
- }
- private void LoadData()
- {
- Progress.ShowModal("Loading Data", progress =>
- {
- var table = new CoreTable();
- table.LoadColumns(typeof(Server));
- var sections = PRSService.GetConfiguration().LoadAll();
- foreach (var section in sections.Where(x => x.Value.Type == ServerType.Database))
- {
- var properties = section.Value.DeserializeServerProperties();
- if (properties is not DatabaseServerProperties databaseProperties) continue;
- Grid.Items.Add(new()
- {
- Name = databaseProperties.Name,
- FileName = databaseProperties.FileName,
- });
- }
- var tasks = Grid.Items.ToArray(LoadProvider);
- Task.WaitAll(tasks);
- foreach(var (item, task) in Grid.Items.Zip(tasks))
- {
- item.ProviderFactory = task.Result;
- item.Version = DbFactory.GetDatabaseVersion(item.ProviderFactory);
- }
- });
- Grid.Refresh(true, true);
- }
- private Task<IProviderFactory> LoadProvider(UpdateDatabaseFilesGridItem item)
- {
- return Task.Run(() =>
- {
- IProviderFactory factory = new SQLiteProviderFactory(item.FileName);
- factory.Start();
- return factory;
- });
- }
- private void Proceed_Click(object sender, RoutedEventArgs e)
- {
- if(Grid.Items.Any(x => x.NeedsUpdate))
- {
- if(!MessageWindow.ShowYesNo("Some database files have not been updated to version 9! If you proceed, these " +
- "databases will not work anymore. Are you sure you wish to proceed?", "Unupdated files"))
- {
- return;
- }
- }
- DialogResult = true;
- }
- private void Cancel_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = false;
- }
- }
|