|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using SIL.FieldWorks.Common.FwUtils; |
| 8 | +using SIL.FieldWorks.FwCoreDlgs; |
| 9 | +using SIL.LCModel.Infrastructure; |
| 10 | +using SIL.LCModel; |
| 11 | + |
| 12 | +namespace SIL.FieldWorks.XWorks.MorphologyEditor |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// This class serves to remove all analyses that are only approved by the user. |
| 16 | + /// Analyses that have a parser evaluation (approved or disapproved) remain afterwards. |
| 17 | + /// </summary> |
| 18 | + public class UserAnalysisRemover : IUtility |
| 19 | + { |
| 20 | + #region Data members |
| 21 | + |
| 22 | + private UtilityDlg m_dlg; |
| 23 | + const string kPath = "/group[@id='Linguistics']/group[@id='Morphology']/group[@id='RemoveUserAnalyses']/"; |
| 24 | + |
| 25 | + #endregion Data members |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Override method to return the Label property. |
| 29 | + /// </summary> |
| 30 | + /// <returns></returns> |
| 31 | + public override string ToString() |
| 32 | + { |
| 33 | + return Label; |
| 34 | + } |
| 35 | + |
| 36 | + #region IUtility implementation |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Get the main label describing the utility. |
| 40 | + /// </summary> |
| 41 | + public string Label |
| 42 | + { |
| 43 | + get |
| 44 | + { |
| 45 | + Debug.Assert(m_dlg != null); |
| 46 | + return StringTable.Table.GetStringWithXPath("Label", kPath); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Set the UtilityDlg. |
| 52 | + /// </summary> |
| 53 | + /// <remarks> |
| 54 | + /// This must be set, before calling any other property or method. |
| 55 | + /// </remarks> |
| 56 | + public UtilityDlg Dialog |
| 57 | + { |
| 58 | + set |
| 59 | + { |
| 60 | + Debug.Assert(value != null); |
| 61 | + Debug.Assert(m_dlg == null); |
| 62 | + |
| 63 | + m_dlg = value; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Load 0 or more items in the list box. |
| 69 | + /// </summary> |
| 70 | + public void LoadUtilities() |
| 71 | + { |
| 72 | + Debug.Assert(m_dlg != null); |
| 73 | + m_dlg.Utilities.Items.Add(this); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Notify the utility that has been selected in the dlg. |
| 79 | + /// </summary> |
| 80 | + public void OnSelection() |
| 81 | + { |
| 82 | + Debug.Assert(m_dlg != null); |
| 83 | + m_dlg.WhenDescription = StringTable.Table.GetStringWithXPath("WhenDescription", kPath); |
| 84 | + m_dlg.WhatDescription = StringTable.Table.GetStringWithXPath("WhatDescription", kPath); |
| 85 | + m_dlg.RedoDescription = StringTable.Table.GetStringWithXPath("RedoDescription", kPath); |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Have the utility do what it does. |
| 90 | + /// </summary> |
| 91 | + public void Process() |
| 92 | + { |
| 93 | + Debug.Assert(m_dlg != null); |
| 94 | + var cache = m_dlg.PropTable.GetValue<LcmCache>("cache"); |
| 95 | + IWfiAnalysis[] analyses = cache.ServiceLocator.GetInstance<IWfiAnalysisRepository>().AllInstances().ToArray(); |
| 96 | + if (analyses.Length == 0) |
| 97 | + return; |
| 98 | + |
| 99 | + // Set up progress bar. |
| 100 | + m_dlg.ProgressBar.Minimum = 0; |
| 101 | + m_dlg.ProgressBar.Maximum = analyses.Length; |
| 102 | + m_dlg.ProgressBar.Step = 1; |
| 103 | + |
| 104 | + NonUndoableUnitOfWorkHelper.Do(cache.ActionHandlerAccessor, () => |
| 105 | + { |
| 106 | + foreach (IWfiAnalysis analysis in analyses) |
| 107 | + { |
| 108 | + ICmAgentEvaluation[] humanEvals = analysis.EvaluationsRC.Where(evaluation => evaluation.Human).ToArray(); |
| 109 | + foreach (ICmAgentEvaluation humanEval in humanEvals) |
| 110 | + analysis.EvaluationsRC.Remove(humanEval); |
| 111 | + |
| 112 | + IWfiWordform wordform = analysis.Wordform; |
| 113 | + if (analysis.EvaluationsRC.Count == 0) |
| 114 | + wordform.AnalysesOC.Remove(analysis); |
| 115 | + |
| 116 | + if (humanEvals.Length > 0) |
| 117 | + { |
| 118 | + wordform.Checksum = 0; |
| 119 | + if (analysis.Cache != null) |
| 120 | + analysis.MoveConcAnnotationsToWordform(); |
| 121 | + } |
| 122 | + |
| 123 | + m_dlg.ProgressBar.PerformStep(); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + m_dlg.Mediator.SendMessage("RefreshInterlin", null); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + #endregion IUtility implementation |
| 132 | + } |
| 133 | +} |
0 commit comments