public void Action_WithPreviousMongoGrammarWithMatchingGuids_ShouldBeUpdatedFromLcmGrammar() { // Setup var lfProject = _lfProj; LcmCache cache = lfProject.FieldWorksProject.Cache; int wsEn = cache.WritingSystemFactory.GetWsFromStr("en"); var converter = new ConvertLcmToMongoOptionList(null, wsEn, MagicStrings.LfOptionListCodeForGrammaticalInfo, new LfMerge.Core.Logging.NullLogger(), _cache.WritingSystemFactory); LfOptionList lfGrammar = converter.PrepareOptionListUpdate(cache.LanguageProject.PartsOfSpeechOA); LfOptionListItem itemForTest = lfGrammar.Items.First(); Guid g = itemForTest.Guid.Value; itemForTest.Abbreviation = "Different abbreviation"; itemForTest.Value = "Different name"; itemForTest.Key = "Different key"; _conn.UpdateMockOptionList(lfGrammar); // Exercise SutLcmToMongo.Run(lfProject); // Verify lfGrammar = _conn.GetLfOptionLists() .FirstOrDefault(optionList => optionList.Code == MagicStrings.LfOptionListCodeForGrammaticalInfo); Assert.That(lfGrammar, Is.Not.Null); Assert.That(lfGrammar.Items, Is.Not.Empty); Assert.That(lfGrammar.Items.Count, Is.EqualTo(lfProject.FieldWorksProject.Cache.LanguageProject.AllPartsOfSpeech.Count)); itemForTest = lfGrammar.Items.FirstOrDefault(x => x.Guid == g); Assert.That(itemForTest, Is.Not.Null); Assert.That(itemForTest.Abbreviation, Is.Not.EqualTo("Different abbreviation")); Assert.That(itemForTest.Value, Is.Not.EqualTo("Different name")); Assert.That(itemForTest.Key, Is.EqualTo("Different key")); // NOTE: Is.EqualTo, because keys shouldn't be updated }
// Returns true if the item passed in has been modified at all, false otherwise protected bool SetOptionListItemFromCmPossibility(LfOptionListItem item, ICmPossibility poss, bool setKey = false) { bool modified = false; string abbreviation = ConvertFdoToMongoTsStrings.TextFromTsString(poss.Abbreviation.BestAnalysisVernacularAlternative, _wsf); if (item.Abbreviation != abbreviation) { modified = true; } item.Abbreviation = abbreviation; if (setKey) { string key = FindAppropriateKey(ConvertFdoToMongoTsStrings.TextFromTsString(poss.Abbreviation.get_String(_wsForKeys), _wsf)); if (item.Key != key) { modified = true; } item.Key = key; } string value = ConvertFdoToMongoTsStrings.TextFromTsString(poss.Name.BestAnalysisVernacularAlternative, _wsf); if (item.Value != value) { modified = true; } item.Value = value; if (item.Guid != poss.Guid) { modified = true; } item.Guid = poss.Guid; return(modified); }
protected LfOptionListItem CmPossibilityToOptionListItem(ICmPossibility pos) { var item = new LfOptionListItem(); SetOptionListItemFromCmPossibility(item, pos, true); // Ignore the bool result since this will always modify the item return(item); }
public void PopulateCmPossibilityFromOptionListItem(ICmPossibility poss, LfOptionListItem item, string wsStr = null) { // Should only be called when NO canonical item can be found if (wsStr == null) { wsStr = "en"; // TODO: Set this from LF user's writing system rather than English, once LF allows setting interface languages } int wsId = poss.Cache.WritingSystemFactory.GetWsFromStr(wsStr); poss.Abbreviation.set_String(wsId, item.Abbreviation); poss.Name.set_String(wsId, item.Value); }
protected ICmPossibility LookupByItem(LfOptionListItem item) { if (item == null) { return(null); } ICmPossibility result; if (item.Guid.HasValue) { if (_possRepo.TryGetObject(item.Guid.Value, out result)) { return(result); } } #if false // Once we are populating Lcm from LF, we might also need to fall back to abbreviation and name for these lookups, because Guids might not be available return(FromAbbrevAndName(item.Abbreviation, item.Value)); #endif return(null); }
/// <summary> /// Update a CmPossibility object from the corresponding LF item. Will draw from canonical sources if available. /// Will try very hard to find a CmPossibility item matching the LF OptionList item, but will eventually create /// one if nothing remotely matching could be found. /// /// NOTE: This is currently commented out (via #if false...#endif block) because we currently don't want to update /// Lcm from the values of LF option lists. Once that changes, this code can be uncommented. /// </summary> /// <param name="item">Item.</param> /// <param name="wsForOptionListItems">Ws for option list items.</param> public void UpdateOrCreatePossibilityFromOptionListItem(LfOptionListItem item, string wsForOptionListItems = null) { ICmPossibility poss = null; CanonicalItem canonicalItem = null; if (item.Guid != null) { if (_possRepo.TryGetObject(item.Guid.Value, out poss)) { // Currently we do NOT want to change the name, abbreviation, etc. in Lcm for already-existing possibility items. // Once we do, uncomment the next line: //PopulateCmPossibilityFromOptionListItem(poss, item, wsForOptionListItems); // For now, however, just return without touching the Lcm CmPossibility object return; } else { if (_canonicalSource != null && _canonicalSource.TryGetByGuid(item.Guid.Value, out canonicalItem)) { canonicalItem.PopulatePossibility(poss); return; } // No canonical item? At least set name and abbreviation from LF var factory = _parentList.Cache.ServiceLocator.GetInstance <ICmPossibilityFactory>(); poss = factory.Create(item.Guid.Value, _parentList); // Note that this does NOT handle "parent" possibilities; new one gets created as a TOP-level item PopulateCmPossibilityFromOptionListItem(poss, item, wsForOptionListItems); } } else { // Can't look it up by GUID, so search by key. If key not found, fall back to abbreviation and name. if (_canonicalSource != null && _canonicalSource.TryGetByKey(item.Key, out canonicalItem)) { canonicalItem.PopulatePossibility(poss); return; } // No canonical source? Then we're in fallback-of-fallback land. First try the key, as that's most likely to be found. poss = _parentList.FindPossibilityByName(_parentList.PossibilitiesOS, item.Key, _wsForKeys); if (poss != null) { PopulateCmPossibilityFromOptionListItem(poss, item, wsForOptionListItems); return; } // Then try the abbreviation, and finally the name -- these, though, should be in the LF user's interface language poss = _parentList.FindPossibilityByName(_parentList.PossibilitiesOS, item.Abbreviation, wsForOptionListItems); if (poss != null) { PopulateCmPossibilityFromOptionListItem(poss, item, wsForOptionListItems); return; } // In LF, OptionListItems have their name in the "Value" property poss = _parentList.FindPossibilityByName(_parentList.PossibilitiesOS, item.Value, wsForOptionListItems); if (poss != null) { PopulateCmPossibilityFromOptionListItem(poss, item, wsForOptionListItems); return; } // If we STILL haven't found it, then just create a new item and populate it. This is the final, last-ditch fallback. poss = _parentList.FindOrCreatePossibility(item.Value, wsForOptionListItems); PopulateCmPossibilityFromOptionListItem(poss, item, wsForOptionListItems); } }