Files

176 lines
6.3 KiB
C#
Raw Permalink Normal View History

2020-12-29 19:23:02 +01:00
using System;
using System.IO;
using System.Linq;
using System.Text;
using DataImporterTool.Dialogs;
using DataImporterTool.Importer;
namespace DataImporterTool
{
public class MainFormPresenter
{
private readonly IDialogService _dialogService;
private readonly JsonFilesImporter _jsonFilesImporter;
2021-01-05 10:27:21 +01:00
private readonly SqlImporter _sqlImporter;
2020-12-29 19:23:02 +01:00
private string[] _selectedFileAsAccounts = null;
private string[] _selectedFileAsTanks = null;
public MainFormPresenter(
IMainFormView view,
IDialogService dialogService,
2021-01-05 10:27:21 +01:00
JsonFilesImporter jsonFilesImporter,
SqlImporter sqlImporter)
2020-12-29 19:23:02 +01:00
{
_dialogService = dialogService;
_jsonFilesImporter = jsonFilesImporter;
2021-01-05 10:27:21 +01:00
_sqlImporter = sqlImporter;
2020-12-29 19:23:02 +01:00
View = view;
View.OpenJsonFolderClick += OpenJsonFolder;
View.AddFilesAsAccountsInfoList += AddFilesAsAccountsInfoList;
View.AddFilesAsTanksInfoList += AddFilesAsTanksInfoList;
View.StartJsonConvert += StartJsonConvert;
2021-01-05 10:27:21 +01:00
View.FetchSqlAccounts += FetchSqlAccounts;
View.StartSqlImport += StartSqlImport;
2020-12-29 19:23:02 +01:00
}
public IMainFormView View { get; }
private void OpenJsonFolder()
{
View.JsonFolderPath = _dialogService.ChooseFolder() ?? string.Empty;
if (!string.IsNullOrEmpty(View.JsonFolderPath))
{
var files = Directory.GetFiles(View.JsonFolderPath, "*.json").Select(Path.GetFileName).ToArray();
View.ShowJsonFiles(files);
}
}
private void AddFilesAsAccountsInfoList(string[] files)
{
_selectedFileAsAccounts = files;
View.ShowJsonAccountFiles(files);
UpdateStatusInformation();
}
private void AddFilesAsTanksInfoList(string[] files)
{
_selectedFileAsTanks = files;
View.ShowJsonTankFiles(files);
UpdateStatusInformation();
}
private void UpdateStatusInformation()
{
var text = new StringBuilder();
if (_selectedFileAsAccounts != null)
{
text.Append($"{_selectedFileAsAccounts.Length} accounts json files selected");
}
if (_selectedFileAsTanks != null)
{
text.Append($"; {_selectedFileAsTanks.Length} tanks json files selected");
}
View.StatusInformation = text.ToString();
}
private async void StartJsonConvert()
{
if (string.IsNullOrEmpty(View.MongoDbConnectionString))
{
_dialogService.ShowWarning("MongoDB connection string is empty!");
return;
}
if (_selectedFileAsAccounts == null || _selectedFileAsAccounts.Length == 0
|| _selectedFileAsTanks == null || _selectedFileAsTanks.Length == 0)
{
_dialogService.ShowWarning("Should be selected some account and tank json files");
return;
}
if (_selectedFileAsTanks != null && _selectedFileAsAccounts != null &&
_selectedFileAsAccounts.Length != _selectedFileAsTanks.Length)
{
_dialogService.ShowWarning("Account files count should be equal to tank files count");
return;
}
View.ClearLog();
View.SetProcessPercentage(0);
2021-01-05 10:27:21 +01:00
var progress = new Progress<FileImportProgress>();
progress.ProgressChanged += FileImportProgressChanged;
2020-12-29 19:23:02 +01:00
await _jsonFilesImporter.Import(View.MongoDbConnectionString,
View.JsonFolderPath,
_selectedFileAsAccounts,
_selectedFileAsTanks,
progress);
}
2021-01-05 10:27:21 +01:00
private void FileImportProgressChanged(object sender, FileImportProgress e)
2020-12-29 19:23:02 +01:00
{
View.StatusInformation = $"{e.FilesConverted} files converted ({e.Progress}%)";
View.AppendLogRow($"Converted: '{e.AccountFileConverted}' - '{e.TankFileConverted}': '{e.AccountName}', {e.BattlesCount} battles, '{e.LastBattle.ToShortDateString()}', {e.TanksCount} tanks");
View.SetProcessPercentage(e.Progress);
}
2021-01-05 10:27:21 +01:00
private async void FetchSqlAccounts()
{
if (string.IsNullOrWhiteSpace(View.SqlConnectionString))
{
_dialogService.ShowWarning("Empty SQL connection string!");
return;
}
View.ClearLog();
View.AppendLogRow("Start fetching account from SQL...");
var accounts = await _sqlImporter.FetchAllAccounts(View.SqlConnectionString);
if(accounts != null)
{
View.ShowSqlAccounts(accounts);
}
View.AppendLogRow($"Found {accounts?.Count ?? 0} accounts");
}
private async void StartSqlImport(long[] accounts)
{
if (accounts == null || accounts.Length == 0)
{
_dialogService.ShowWarning("Please select at least one account");
return;
}
if (string.IsNullOrEmpty(View.MongoDbConnectionString))
{
_dialogService.ShowWarning("MongoDB connection string is empty!");
return;
}
if (string.IsNullOrWhiteSpace(View.SqlConnectionString))
{
_dialogService.ShowWarning("Empty SQL connection string!");
return;
}
var progress = new Progress<ImportProgress>();
progress.ProgressChanged += SqlImport_ProgressChanged;
View.ClearLog();
View.SetProcessPercentage(0);
await _sqlImporter.StartImport(View.MongoDbConnectionString, View.SqlConnectionString, accounts, progress);
}
private void SqlImport_ProgressChanged(object sender, ImportProgress e)
{
View.StatusInformation = $"{e.Progress}% Converted";
View.AppendLogRow($"Imported: '{e.AccountName}', {e.BattlesCount} battles, '{e.LastBattle.ToShortDateString()}', {e.TanksCount} tanks");
View.SetProcessPercentage(e.Progress);
}
2020-12-29 19:23:02 +01:00
}
}