Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 34 additions & 51 deletions Src/xWorks/DictionaryExportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class DictionaryExportService
private readonly Mediator m_mediator;
private readonly LcmCache m_cache;

private const string DictionaryType = "Dictionary";
private const string ReversalType = "Reversal Index";
internal const string DictionaryType = "Dictionary";
internal const string ReversalType = "Reversal Index";
private const int BatchSize = 50; // number of entries to send to Webonary in a single post

public DictionaryExportService(PropertyTable propertyTable, Mediator mediator)
Expand Down Expand Up @@ -77,70 +77,53 @@ internal int CountReversalIndexEntries(IReversalIndex ri)
return entries.Length;
}

public void ExportDictionaryForWord(string filePath, DictionaryConfigurationModel configuration = null, IThreadedProgress progress = null)
public void ExportDictionaryForWord(string filePath, int[] entriesToSave, DictionaryPublicationDecorator pubDecorator,
IThreadedProgress progress)
{
using (ClerkActivator.ActivateClerkMatchingExportType(DictionaryType, m_propertyTable, m_mediator))
{
configuration = configuration ?? new DictionaryConfigurationModel(DictionaryConfigurationListener.GetCurrentConfiguration(m_propertyTable, "Dictionary"), m_cache);
var publicationDecorator = ConfiguredLcmGenerator.GetPublicationDecoratorAndEntries(m_propertyTable, out var entriesToSave, DictionaryType);
if (progress != null)
progress.Maximum = entriesToSave.Length;
if (progress != null)
progress.Maximum = entriesToSave.Length;

LcmWordGenerator.SavePublishedDocx(entriesToSave, publicationDecorator, int.MaxValue, configuration, m_propertyTable, filePath, progress);
}
var dictConfig = new DictionaryConfigurationModel(
DictionaryConfigurationListener.GetCurrentConfiguration(m_propertyTable, "Dictionary"), m_cache);
LcmWordGenerator.SavePublishedDocx(entriesToSave, pubDecorator, int.MaxValue, dictConfig, m_propertyTable,
filePath, progress);
}

public void ExportReversalForWord(string filePath, string reversalWs, DictionaryConfigurationModel configuration = null, IThreadedProgress progress = null)
public void ExportReversalForWord(string filePath, string reversalWs, int[] entriesToSave,
DictionaryPublicationDecorator pubDecorator, DictionaryConfigurationModel revConfig, IThreadedProgress progress)
{
Guard.AgainstNullOrEmptyString(reversalWs, nameof(reversalWs));
using (ClerkActivator.ActivateClerkMatchingExportType(ReversalType, m_propertyTable, m_mediator))
using (ReversalIndexActivator.ActivateReversalIndex(reversalWs, m_propertyTable, m_cache))
{
configuration = configuration ?? new DictionaryConfigurationModel(
DictionaryConfigurationListener.GetCurrentConfiguration(m_propertyTable, "ReversalIndex"), m_cache);
var publicationDecorator = ConfiguredLcmGenerator.GetPublicationDecoratorAndEntries(m_propertyTable, out var entriesToSave, ReversalType);

// Don't export empty reversals
if (entriesToSave.Length == 0)
return;

if (progress != null)
progress.Maximum = entriesToSave.Length;

string reversalFilePath = filePath.Split(new string[] { ".docx"}, StringSplitOptions.None)[0] + "-reversal-" + reversalWs + ".docx";
if (progress != null)
progress.Maximum = entriesToSave.Length;

LcmWordGenerator.SavePublishedDocx(entriesToSave, publicationDecorator, int.MaxValue, configuration, m_propertyTable, reversalFilePath, progress);
}
string reversalFilePath = filePath.Split(new string[] { ".docx"}, StringSplitOptions.None)[0] + "-reversal-" + reversalWs + ".docx";
LcmWordGenerator.SavePublishedDocx(entriesToSave, pubDecorator, int.MaxValue, revConfig, m_propertyTable,
reversalFilePath, progress);
}

public void ExportDictionaryContent(string xhtmlPath, DictionaryConfigurationModel configuration = null, IThreadedProgress progress = null)
public void ExportDictionaryContent(string xhtmlPath, DictionaryPublicationDecorator pubDecorator, int[] entriesToSave,
IThreadedProgress progress)
{
using (ClerkActivator.ActivateClerkMatchingExportType(DictionaryType, m_propertyTable, m_mediator))
{
configuration = configuration ?? new DictionaryConfigurationModel(
DictionaryConfigurationListener.GetCurrentConfiguration(m_propertyTable, "Dictionary"), m_cache);
ExportConfiguredXhtml(xhtmlPath, configuration, DictionaryType, progress);
}
}
if (progress != null)
progress.Maximum = entriesToSave.Length;

public void ExportReversalContent(string xhtmlPath, string reversalWs = null, DictionaryConfigurationModel configuration = null,
IThreadedProgress progress = null)
{
using (ClerkActivator.ActivateClerkMatchingExportType(ReversalType, m_propertyTable, m_mediator))
using (ReversalIndexActivator.ActivateReversalIndex(reversalWs, m_propertyTable, m_cache))
{
configuration = configuration ?? new DictionaryConfigurationModel(
DictionaryConfigurationListener.GetCurrentConfiguration(m_propertyTable, "ReversalIndex"), m_cache);
ExportConfiguredXhtml(xhtmlPath, configuration, ReversalType, progress);
}
var dictConfig = new DictionaryConfigurationModel(
DictionaryConfigurationListener.GetCurrentConfiguration(m_propertyTable, "Dictionary"), m_cache);
LcmXhtmlGenerator.SavePublishedHtmlWithStyles(entriesToSave, pubDecorator, int.MaxValue, dictConfig,
m_propertyTable, xhtmlPath, progress, true);
}

private void ExportConfiguredXhtml(string xhtmlPath, DictionaryConfigurationModel configuration, string exportType, IThreadedProgress progress)
public void ExportReversalContent(string xhtmlPath, DictionaryPublicationDecorator pubDecorator, int[] entriesToSave,
IThreadedProgress progress)
{
var publicationDecorator = ConfiguredLcmGenerator.GetPublicationDecoratorAndEntries(m_propertyTable, out var entriesToSave, exportType);
if (progress != null)
progress.Maximum = entriesToSave.Length;
LcmXhtmlGenerator.SavePublishedHtmlWithStyles(entriesToSave, publicationDecorator, int.MaxValue, configuration, m_propertyTable, xhtmlPath, progress, true);

var revConfig = new DictionaryConfigurationModel(
DictionaryConfigurationListener.GetCurrentConfiguration(m_propertyTable, "ReversalIndex"), m_cache);
LcmXhtmlGenerator.SavePublishedHtmlWithStyles(entriesToSave, pubDecorator, int.MaxValue, revConfig,
m_propertyTable, xhtmlPath, progress, true);
}

public List<JArray> ExportConfiguredJson(string folderPath, DictionaryConfigurationModel configuration, out int[] entryIds)
Expand Down Expand Up @@ -168,7 +151,7 @@ public List<JArray> ExportConfiguredReversalJson(string folderPath, string rever
}
}

private sealed class ClerkActivator : IDisposable
internal sealed class ClerkActivator : IDisposable
{
private static RecordClerk s_dictionaryClerk;
private static RecordClerk s_reversalIndexClerk;
Expand Down Expand Up @@ -250,7 +233,7 @@ private static bool DoesClerkMatchParams(RecordClerk clerk, XmlNode parameters)
return id == clerk.Id;
}
}
private sealed class ReversalIndexActivator : IDisposable
internal sealed class ReversalIndexActivator : IDisposable
{
private readonly string m_sCurrentRevIdxGuid;
private readonly PropertyTable m_propertyTable;
Expand Down
91 changes: 87 additions & 4 deletions Src/xWorks/ExportDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
using PropertyTable = XCore.PropertyTable;
using ReflectionHelper = SIL.LCModel.Utils.ReflectionHelper;
using Newtonsoft.Json;
using static SIL.FieldWorks.XWorks.DictionaryExportService;

namespace SIL.FieldWorks.XWorks
{
Expand Down Expand Up @@ -892,16 +893,90 @@ protected void DoExport(string outPath, bool fLiftOutput)
}
}

/// <summary>
/// When there is filtering applied to the entries, and the user is viewing reversals instead of the main
/// dictionary, then we need to get the list of entries while on the main thread. LT-21960
/// </summary>
private void GetDictionaryDecoratorAndEntries(out DictionaryPublicationDecorator pubDecorator, out int[] entriesToSave)
{
int[] entries = null;
DictionaryPublicationDecorator decorator = null;
if (btnExport.InvokeRequired)
{
btnExport.Invoke(() =>
{
using (ClerkActivator.ActivateClerkMatchingExportType(DictionaryType, m_propertyTable, m_mediator))
{
decorator = ConfiguredLcmGenerator.GetPublicationDecoratorAndEntries(m_propertyTable, out entries, DictionaryType);
}
});
}
else
{
using (ClerkActivator.ActivateClerkMatchingExportType(DictionaryType, m_propertyTable, m_mediator))
{
decorator = ConfiguredLcmGenerator.GetPublicationDecoratorAndEntries(m_propertyTable, out entries, DictionaryType);
}

}
entriesToSave = entries;
pubDecorator = decorator;
}

/// <summary>
/// When there is filtering applied to the entries, and the user is viewing the main dictionary
/// instead of a reversal, then we need to get the list of entries while on the main thread. LT-21960
/// </summary>
private void GetReversalDecoratorAndEntries(Guid reversalGuid, out DictionaryPublicationDecorator pubDecorator, out int[] entriesToSave)
{
int[] entries = null;
DictionaryPublicationDecorator decorator = null;
if (btnExport.InvokeRequired)
{
btnExport.Invoke(() =>
{
using (ClerkActivator.ActivateClerkMatchingExportType(ReversalType, m_propertyTable, m_mediator))
using (ReversalIndexActivator.ActivateReversalIndex(reversalGuid, m_propertyTable))
{
decorator = ConfiguredLcmGenerator.GetPublicationDecoratorAndEntries(m_propertyTable, out entries, ReversalType);
}
});
}
else
{
using (ClerkActivator.ActivateClerkMatchingExportType(ReversalType, m_propertyTable, m_mediator))
using (ReversalIndexActivator.ActivateReversalIndex(reversalGuid, m_propertyTable))
{
decorator = ConfiguredLcmGenerator.GetPublicationDecoratorAndEntries(m_propertyTable, out entries, ReversalType);
}
}
entriesToSave = entries;
pubDecorator = decorator;
}

private object ExportWordOpenXml(IThreadedProgress progress, object[] args)
{
if (args.Length < 1)
return null;
var filePath = (string)args[0];
var exportService = new DictionaryExportService(m_propertyTable, m_mediator);
exportService.ExportDictionaryForWord(filePath, null, progress);

// Export the main dictionary.
GetDictionaryDecoratorAndEntries(out var pubDecorator, out var entriesToSave);
exportService.ExportDictionaryForWord(filePath, entriesToSave, pubDecorator, progress);

// Export all the reversals.
var revConfig = new DictionaryConfigurationModel(
DictionaryConfigurationListener.GetCurrentConfiguration(m_propertyTable, "ReversalIndex"), m_cache);
foreach (var reversal in m_cache.ServiceLocator.GetInstance<IReversalIndexRepository>().AllInstances())
{
exportService.ExportReversalForWord(filePath, reversal.WritingSystem);
GetReversalDecoratorAndEntries(reversal.Guid, out pubDecorator, out entriesToSave);

if (entriesToSave.Length > 0)
{
exportService.ExportReversalForWord(filePath, reversal.WritingSystem, entriesToSave,
pubDecorator, revConfig, progress);
}
}
return null;
}
Expand All @@ -911,13 +986,21 @@ private object ExportConfiguredXhtml(IThreadedProgress progress, object[] args)
if(args.Length < 1)
return null;
var xhtmlPath = (string)args[0];
var exportService = new DictionaryExportService(m_propertyTable, m_mediator);
switch (m_rgFxtTypes[FxtIndex((string)m_exportItems[0].Tag)].m_ft)
{
case FxtTypes.kftConfigured:
new DictionaryExportService(m_propertyTable, m_mediator).ExportDictionaryContent(xhtmlPath, progress: progress);
GetDictionaryDecoratorAndEntries(out var pubDecorator, out var entriesToSave);
exportService.ExportDictionaryContent(xhtmlPath, pubDecorator, entriesToSave, progress);
break;
case FxtTypes.kftReversal:
new DictionaryExportService(m_propertyTable, m_mediator).ExportReversalContent(xhtmlPath, progress: progress);
var reversalGuidStr = m_propertyTable.GetStringProperty("ReversalIndexGuid", null);
if (!string.IsNullOrEmpty(reversalGuidStr))
{
var reversalGuid = new Guid(reversalGuidStr);
GetReversalDecoratorAndEntries(reversalGuid, out pubDecorator, out entriesToSave);
exportService.ExportReversalContent(xhtmlPath, pubDecorator, entriesToSave, progress);
}
break;
}
return null;
Expand Down
Loading