|
| 1 | +// Copyright (c) 2024 SIL International |
| 2 | +// This software is licensed under the LGPL, version 2.1 or later |
| 3 | +// (http://www.gnu.org/licenses/lgpl-2.1.html) |
| 4 | + |
| 5 | +using SIL.LCModel; |
| 6 | +using SIL.LCModel.Core.Cellar; |
| 7 | +using SIL.LCModel.DomainServices; |
| 8 | +using SIL.LCModel.Infrastructure; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Linq; |
| 12 | +using System.Xml.Linq; |
| 13 | +using System.Collections; |
| 14 | +using System.Xml.XPath; |
| 15 | + |
| 16 | +namespace SIL.FieldWorks.WordWorks.Parser |
| 17 | +{ |
| 18 | + // Class to prepare custom XAmple properties |
| 19 | + public class XAmplePropertiesPreparer |
| 20 | + { |
| 21 | + public LcmCache Cache { get; set; } |
| 22 | + private bool ShowMessages { get; set; } |
| 23 | + private string entryCustomFieldName = ""; |
| 24 | + private string formCustomFieldName = ""; |
| 25 | + private string customListName = ""; |
| 26 | + private XElement Root { get; set; } |
| 27 | + public XAmplePropertiesPreparer(LcmCache cache, XElement root, bool fShowMessages = true) |
| 28 | + { |
| 29 | + this.Cache = cache; |
| 30 | + this.Root = root; |
| 31 | + this.ShowMessages = fShowMessages; |
| 32 | + InitStringItems(); |
| 33 | + } |
| 34 | + |
| 35 | + private void InitStringItems() |
| 36 | + { |
| 37 | + if (Root != null) { |
| 38 | + customListName = findElementValue("CustomList/Name"); |
| 39 | + entryCustomFieldName = findElementValue("EntryLevelCustomField/Name"); |
| 40 | + formCustomFieldName = findElementValue("FormLevelCustomField/Name"); |
| 41 | + } |
| 42 | + } |
| 43 | + private string findElementValue(string xpath) |
| 44 | + { |
| 45 | + string result = ""; |
| 46 | + var item = Root.XPathSelectElement(xpath); |
| 47 | + if (item != null) |
| 48 | + { |
| 49 | + result = item.Value; |
| 50 | + } |
| 51 | + return result; |
| 52 | + } |
| 53 | + public List<FieldDescription> GetListOfCustomFields() |
| 54 | + { |
| 55 | + return (from fd in FieldDescription.FieldDescriptors(Cache) |
| 56 | + where fd.IsCustomField |
| 57 | + select fd).ToList(); |
| 58 | + } |
| 59 | + |
| 60 | + public void AddListsAndFields() |
| 61 | + { |
| 62 | + if (Root == null) |
| 63 | + { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + AddXAmplePropertiesList(); |
| 68 | + var customFields = GetListOfCustomFields(); |
| 69 | + AddXAmplePropertiesCustomField(entryCustomFieldName, LexEntryTags.kClassId); |
| 70 | + AddXAmplePropertiesCustomField(formCustomFieldName, MoFormTags.kClassId); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Creates a new custom field for properties. |
| 75 | + /// </summary> |
| 76 | + public void AddXAmplePropertiesCustomField(string fieldName, int fieldClassId) |
| 77 | + { |
| 78 | + var customFields = GetListOfCustomFields(); |
| 79 | + if (customFields.Find(fd => fd.Name == fieldName) != null) |
| 80 | + { |
| 81 | + // already done; quit |
| 82 | + return; |
| 83 | + } |
| 84 | + var possListRepository = Cache.ServiceLocator.GetInstance<ICmPossibilityListRepository>(); |
| 85 | + var customList = possListRepository.AllInstances().FirstOrDefault(list => list.Name.BestAnalysisAlternative.Text == customListName); |
| 86 | + if (customList == null) |
| 87 | + { |
| 88 | + // need the master possibility list and it does not exist |
| 89 | + if (ShowMessages) |
| 90 | + { |
| 91 | + Console.WriteLine("Need to create the master list of possibilities first."); |
| 92 | + } |
| 93 | + return; |
| 94 | + } |
| 95 | + NonUndoableUnitOfWorkHelper.Do(Cache.ActionHandlerAccessor, () => |
| 96 | + { |
| 97 | + int ws = Cache.DefaultAnalWs; |
| 98 | + // create new custom field |
| 99 | + var fd = new FieldDescription(Cache) |
| 100 | + { |
| 101 | + Name = fieldName, |
| 102 | + Userlabel = fieldName, |
| 103 | + HelpString = string.Empty, |
| 104 | + Class = fieldClassId |
| 105 | + }; |
| 106 | + fd.Type = CellarPropertyType.ReferenceCollection; |
| 107 | + fd.DstCls = CmCustomItemTags.kClassId; |
| 108 | + fd.WsSelector = WritingSystemServices.kwsAnal; |
| 109 | + |
| 110 | + fd.ListRootId = customList.Guid; |
| 111 | + fd.UpdateCustomField(); |
| 112 | + FieldDescription.ClearDataAbout(); |
| 113 | + }); |
| 114 | + } |
| 115 | + /// <summary> |
| 116 | + /// Creates a new possibility list for Quechua properties. |
| 117 | + /// </summary> |
| 118 | + public void AddXAmplePropertiesList() |
| 119 | + { |
| 120 | + if (Root == null) |
| 121 | + { |
| 122 | + // nothing to do |
| 123 | + return; |
| 124 | + } |
| 125 | + var possListRepository = Cache.ServiceLocator.GetInstance<ICmPossibilityListRepository>(); |
| 126 | + var customList = possListRepository.AllInstances().FirstOrDefault(list => list.Name.BestAnalysisAlternative.Text == customListName); |
| 127 | + if (customList != null) |
| 128 | + { |
| 129 | + return; |
| 130 | + } |
| 131 | + NonUndoableUnitOfWorkHelper.Do(Cache.ActionHandlerAccessor, () => |
| 132 | + { |
| 133 | + int ws = WritingSystemServices.kwsAnal; |
| 134 | + Cache.ServiceLocator.GetInstance<ICmPossibilityListFactory>().CreateUnowned(customListName, ws); |
| 135 | + customList = possListRepository.AllInstances().Last(); |
| 136 | + var propPoss = Cache.ServiceLocator.GetInstance<ICmCustomItemFactory>(); |
| 137 | + ws = Cache.DefaultAnalWs; |
| 138 | + var elements = Root.XPathSelectElements("CustomList/Contents/Element"); |
| 139 | + foreach (var element in elements) |
| 140 | + { |
| 141 | + var name = element.XPathSelectElement("Name").Value; |
| 142 | + var poss = CreateNewPropertyPossibility(ws, customList, propPoss, name); |
| 143 | + if (poss != null) |
| 144 | + { |
| 145 | + XElement item = element.XPathSelectElement("Abbreviation"); |
| 146 | + string value = ""; |
| 147 | + if (item != null) |
| 148 | + { |
| 149 | + value = item.Value; |
| 150 | + if (!String.IsNullOrEmpty(value)) |
| 151 | + { |
| 152 | + poss.Abbreviation.set_String(ws, value); |
| 153 | + } |
| 154 | + } |
| 155 | + item = element.XPathSelectElement("Description"); |
| 156 | + if (item != null) |
| 157 | + { |
| 158 | + value = item.Value; |
| 159 | + if (!String.IsNullOrEmpty(value)) |
| 160 | + { |
| 161 | + poss.Description.set_String(ws, value); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + private ICmCustomItem CreateNewPropertyPossibility(int ws, ICmPossibilityList newList, ICmCustomItemFactory propPoss, string name) |
| 170 | + { |
| 171 | + var poss = propPoss.Create(); |
| 172 | + newList.PossibilitiesOS.Add(poss); |
| 173 | + poss.Name.set_String(ws, name); |
| 174 | + return poss; |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + } |
| 179 | +} |
0 commit comments