Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
better error handling in export Bible Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyCorbett committed Jan 17, 2022
1 parent 8d23b85 commit c6c77a7
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 14 deletions.
8 changes: 5 additions & 3 deletions JWLMerge.BackupFileServices/BackupFileService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace JWLMerge.BackupFileServices
using JWLMerge.ExcelServices.Models;

namespace JWLMerge.BackupFileServices
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -495,7 +497,7 @@ public BackupFile ImportBibleNotes(
return new BackupFile(newManifest, originalBackupFile.Database, originalBackupFile.FilePath);
}

public void ExportBibleNotesToExcel(
public ExportBibleNotesResult ExportBibleNotesToExcel(
BackupFile backupFile, string bibleNotesExportFilePath, IExcelService excelService)
{
File.Delete(bibleNotesExportFilePath);
Expand Down Expand Up @@ -548,7 +550,7 @@ public void ExportBibleNotesToExcel(

notesToWrite.Sort(SortBibleNotes);

excelService.AppendToBibleNotesFile(bibleNotesExportFilePath, notesToWrite, 0, bibleNotesExportFilePath);
return excelService.AppendToBibleNotesFile(bibleNotesExportFilePath, notesToWrite, 0, bibleNotesExportFilePath);
}

private static void RemoveSelectedTags(Database database, HashSet<int> tagIds)
Expand Down
6 changes: 4 additions & 2 deletions JWLMerge.BackupFileServices/IBackupFileService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace JWLMerge.BackupFileServices
using JWLMerge.ExcelServices.Models;

namespace JWLMerge.BackupFileServices
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -121,7 +123,7 @@ BackupFile ImportBibleNotes(
int mepsLanguageId,
ImportBibleNotesParams options);

void ExportBibleNotesToExcel(BackupFile backupFile, string bibleNotesExportFilePath, IExcelService excelService);
ExportBibleNotesResult ExportBibleNotesToExcel(BackupFile backupFile, string bibleNotesExportFilePath, IExcelService excelService);

/// <summary>
/// Cleans the database (ensuring integrity), then writes the specified backup to a "jwlibrary" file.
Expand Down
31 changes: 26 additions & 5 deletions JWLMerge.ExcelServices/ExcelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ public class ExcelService : IExcelService
{
private const string WorkbookName = "Notes";

// returns the last row written
public int AppendToBibleNotesFile(
/// <summary>
/// Appends Bible notes to spreadsheet page
/// </summary>
/// <param name="excelFilePath">Excel file path.</param>
/// <param name="notes">Notes.</param>
/// <param name="startRow">Row to start at.</param>
/// <param name="backupFilePath">Path to backup file.</param>
/// <returns>Results.</returns>
public ExportBibleNotesResult AppendToBibleNotesFile(
string excelFilePath,
IReadOnlyCollection<BibleNote>? notes,
int startRow,
string backupFilePath)
{
var result = new ExportBibleNotesResult { Row = startRow };

if (string.IsNullOrEmpty(excelFilePath))
{
throw new ArgumentNullException(nameof(excelFilePath));
Expand All @@ -32,7 +41,8 @@ public int AppendToBibleNotesFile(

if (notes == null || !notes.Any())
{
return startRow;
result.NoNotesFound = true;
return result;
}

using var workbook = new XLWorkbook(excelFilePath);
Expand All @@ -45,6 +55,16 @@ public int AppendToBibleNotesFile(
var row = startRow;
foreach (var note in notes)
{
var noteTooLarge = note.NoteContent != null && note.NoteContent.Length > Int16.MaxValue;
if (noteTooLarge)
{
result.SomeNotesTooLarge = true;
}

var noteContent = noteTooLarge
? note.NoteContent!.Substring(0, Int16.MaxValue)
: note.NoteContent;

SetCellStringValue(worksheet, row, 1, note.PubSymbol);
SetCellStringValue(worksheet, row, 2, note.BookName);
SetCellIntegerValue(worksheet, row, 3, note.BookNumber);
Expand All @@ -55,14 +75,15 @@ public int AppendToBibleNotesFile(
SetCellIntegerValue(worksheet, row, 8, note.ColorCode);
SetCellStringValue(worksheet, row, 9, note.TagsCsv);
SetCellStringValue(worksheet, row, 10, note.NoteTitle);
SetCellStringValue(worksheet, row, 11, note.NoteContent);
SetCellStringValue(worksheet, row, 11, noteContent);

++row;
}

workbook.Save();

return row;
result.Row = row;
return result;
}

private static void SetCellStringValue(IXLWorksheet worksheet, int row, int col, string? value)
Expand Down
2 changes: 1 addition & 1 deletion JWLMerge.ExcelServices/IExcelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace JWLMerge.ExcelServices
{
public interface IExcelService
{
int AppendToBibleNotesFile(
ExportBibleNotesResult AppendToBibleNotesFile(
string excelFilePath,
IReadOnlyCollection<BibleNote>? notes,
int startRow,
Expand Down
11 changes: 11 additions & 0 deletions JWLMerge.ExcelServices/Models/ExportBibleNotesResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace JWLMerge.ExcelServices.Models
{
public class ExportBibleNotesResult
{
public int Row { get; set; }

public bool SomeNotesTooLarge { get; set; }

public bool NoNotesFound { get; set; }
}
}
2 changes: 1 addition & 1 deletion JWLMerge/Services/SnackbarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void EnqueueWithOk(object content)
{
if (Application.Current.MainWindow?.WindowState != WindowState.Minimized)
{
TheSnackbarMessageQueue.Enqueue(content, "OK", () => { });
TheSnackbarMessageQueue.Enqueue(content, "OK", (obj) => { }, () => { }, true, true, TimeSpan.FromSeconds(20));
}
}));
}
Expand Down
19 changes: 17 additions & 2 deletions JWLMerge/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using JWLMerge.ExcelServices.Models;

namespace JWLMerge.ViewModel
{
using System;
Expand Down Expand Up @@ -202,16 +204,29 @@ private async Task ExportBibleNotesAsync(string? filePath)
try
{
EventTracker.Track(EventName.ExportNotes);

ExportBibleNotesResult? result = null;

await Task.Run(() =>
{
_backupFileService.ExportBibleNotesToExcel(
result = _backupFileService.ExportBibleNotesToExcel(
file.BackupFile,
bibleNotesExportFilePath,
_excelService);
});

_snackbarService.Enqueue("Bible notes exported successfully");
if (result is { NoNotesFound: true })
{
_snackbarService.Enqueue("No Bible notes found!");
}
else if (result is { SomeNotesTooLarge: true })
{
_snackbarService.EnqueueWithOk("Some notes were too large to store in a spreadsheet cell and were truncated!");
}
else
{
_snackbarService.Enqueue("Bible notes exported successfully");
}
}
catch (Exception ex)
{
Expand Down

0 comments on commit c6c77a7

Please sign in to comment.