// ===================================================== // Things needed by the task itself... private void Main(ICmObject target) { var f = new RestructureDialectsDialog(); f.ShowDialog(); if (f.DialogResult == DialogResult.Cancel) { return; } var esb = new StringBuilder(); esb.AppendLine("Original pairs prior to swapping: "); //var sda = target.Cache.DomainDataByFlid; //TODO: figure out how to not need target ILexEntryRepository repo = target.Cache.ServiceLocator.GetInstance <ILexEntryRepository>(); var wsRepo = target.Cache.ServiceLocator.GetInstance <IWritingSystemContainer>(); _vern = wsRepo.DefaultVernacularWritingSystem.Handle; _ana = wsRepo.DefaultAnalysisWritingSystem.Handle; int i = 0; int max = f.GetMaxToProcess(); foreach (ILexEntry entry in repo.AllInstances()) { if (i >= max) { break; } ILexEntry vWinner = GetWinningVariant(entry, f.GetPeckingOrder()); if (vWinner != null) { // There is a dialectal variant we need to swap with string s = Swap(entry, vWinner); i++; if (i < 15) { esb.AppendLine(s); } else if (i == 15) { esb.AppendLine("..."); } } } MessageBox.Show(esb.ToString(), f.GetPeckingOrder() + ": Made " + i + " swaps. Done restructuring."); }
public void RunConversion() { Logger.Notice("FdoToMongo: Converting lexicon for project {0}", LfProject.ProjectCode); ILexEntryRepository repo = GetInstance <ILexEntryRepository>(); if (repo == null) { Logger.Error("Can't find LexEntry repository for FieldWorks project {0}", LfProject.ProjectCode); return; } // Custom field configuration AND view configuration should all be set at once Dictionary <string, LfConfigFieldBase> lfCustomFieldList = new Dictionary <string, LfConfigFieldBase>(); Dictionary <string, string> lfCustomFieldTypes = new Dictionary <string, string>(); _convertCustomField.WriteCustomFieldConfig(lfCustomFieldList, lfCustomFieldTypes); Connection.SetCustomFieldConfig(LfProject, lfCustomFieldList); _convertCustomField.CreateCustomFieldsConfigViews(LfProject, lfCustomFieldList, lfCustomFieldTypes); Dictionary <Guid, DateTime> previousModificationDates = Connection.GetAllModifiedDatesForEntries(LfProject); int i = 1; foreach (ILexEntry fdoEntry in repo.AllInstances()) { bool createdEntry = false; DateTime previousDateModified; if (!previousModificationDates.TryGetValue(fdoEntry.Guid, out previousDateModified)) { // Looks like this entry is new in FDO createdEntry = true; previousDateModified = DateTime.MinValue; // Ensure it will seem modified when comparing it later } // Remember that FDO's DateModified is stored in local time for some incomprehensible reason... if (!createdEntry && previousDateModified.ToLocalTime() == fdoEntry.DateModified) { // Hasn't been modified since last time: just skip this record entirely continue; } LfLexEntry lfEntry = FdoLexEntryToLfLexEntry(fdoEntry); lfEntry.IsDeleted = false; Logger.Info("{3} - FdoToMongo: {0} LfEntry {1} ({2})", createdEntry ? "Created" : "Modified", lfEntry.Guid, ConvertUtilities.EntryNameForDebugging(lfEntry), i++); Connection.UpdateRecord(LfProject, lfEntry); } LfProject.IsInitialClone = false; RemoveMongoEntriesDeletedInFdo(); // Logger.Debug("Running FtMComments, should see comments show up below:"); var commCvtr = new ConvertFdoToMongoComments(Connection, LfProject, Logger, Progress, ProjectRecordFactory); commCvtr.RunConversion(); }
public void Action_RunTwice_ShouldNotDuplicatePictures() { // Setup initial Mongo project has 1 picture and 2 captions var lfProj = _lfProj; var data = new SampleData(); int newMongoPictureCount = data.bsonTestData["senses"][0]["pictures"].AsBsonArray.Count; int newMongoCaptionCount = data.bsonTestData["senses"][0]["pictures"][0]["caption"].AsBsonDocument.Count(); Assert.That(newMongoPictureCount, Is.EqualTo(2)); Assert.That(newMongoCaptionCount, Is.EqualTo(2)); // Initial FDO project has 63 entries, 3 internal pictures, and 1 externally linked picture FdoCache cache = _cache; ILexEntryRepository entryRepo = _servLoc.GetInstance <ILexEntryRepository>(); int originalNumOfFdoPictures = entryRepo.AllInstances().Count( e => (e.SensesOS.Count > 0) && (e.SensesOS[0].PicturesOS.Count > 0)); Assert.That(entryRepo.Count, Is.EqualTo(OriginalNumOfFdoEntries)); Assert.That(originalNumOfFdoPictures, Is.EqualTo(3 + 1)); string expectedGuidStrBefore = data.bsonTestData["guid"].AsString; Guid expectedGuidBefore = Guid.Parse(expectedGuidStrBefore); var entryBefore = entryRepo.GetObject(expectedGuidBefore); Assert.That(entryBefore.SensesOS.Count, Is.GreaterThan(0)); Assert.That(entryBefore.SensesOS.First().PicturesOS.Count, Is.EqualTo(1)); // Exercise running Action twice data.bsonTestData["authorInfo"]["modifiedDate"] = DateTime.UtcNow; _conn.UpdateMockLfLexEntry(data.bsonTestData); sutMongoToFdo.Run(lfProj); data.bsonTestData["authorInfo"]["modifiedDate"] = DateTime.UtcNow; _conn.UpdateMockLfLexEntry(data.bsonTestData); sutMongoToFdo.Run(lfProj); string expectedGuidStr = data.bsonTestData["guid"].AsString; Guid expectedGuid = Guid.Parse(expectedGuidStr); var entry = entryRepo.GetObject(expectedGuid); Assert.IsNotNull(entry); Assert.That(entry.Guid, Is.EqualTo(expectedGuid)); Assert.That(entry.SensesOS.Count, Is.GreaterThan(0)); Assert.That(entry.SensesOS.First().PicturesOS.Count, Is.EqualTo(2)); }
public void Action_ChangedWithSampleData_ShouldUpdatePictures() { // Setup initial Mongo project has 1 picture and 2 captions var lfProj = _lfProj; var data = new SampleData(); _conn.UpdateMockLfLexEntry(data.bsonTestData); string expectedInternalFileName = Path.Combine("Pictures", data.bsonTestData["senses"][0]["pictures"][0]["fileName"].ToString()); string expectedExternalFileName = data.bsonTestData["senses"][0]["pictures"][1]["fileName"].ToString(); int newMongoPictureCount = data.bsonTestData["senses"][0]["pictures"].AsBsonArray.Count; int newMongoCaptionCount = data.bsonTestData["senses"][0]["pictures"][0]["caption"].AsBsonDocument.Count(); Assert.That(newMongoPictureCount, Is.EqualTo(2)); Assert.That(newMongoCaptionCount, Is.EqualTo(2)); // Initial FDO project has 63 entries, 3 internal pictures, and 1 externally linked picture FdoCache cache = _cache; ILexEntryRepository entryRepo = _servLoc.GetInstance <ILexEntryRepository>(); int originalNumOfFdoPictures = entryRepo.AllInstances(). Count(e => (e.SensesOS.Count > 0) && (e.SensesOS[0].PicturesOS.Count > 0)); Assert.That(entryRepo.Count, Is.EqualTo(OriginalNumOfFdoEntries)); Assert.That(originalNumOfFdoPictures, Is.EqualTo(3 + 1)); string expectedGuidStr = data.bsonTestData["guid"].AsString; Guid expectedGuid = Guid.Parse(expectedGuidStr); var entryBefore = cache.ServiceLocator.GetObject(expectedGuid) as ILexEntry; Assert.That(entryBefore.SensesOS.Count, Is.GreaterThan(0)); Assert.That(entryBefore.SensesOS.First().PicturesOS.Count, Is.EqualTo(1)); // Exercise adding 1 picture with 2 captions. Note that the picture that was previously attached // to this FDO entry will end up being deleted, because it does not have a corresponding picture in LF. data.bsonTestData["authorInfo"]["modifiedDate"] = DateTime.UtcNow; _conn.UpdateMockLfLexEntry(data.bsonTestData); sutMongoToFdo.Run(lfProj); // Verify "Added" picture is now the only picture on the sense (because the "old" picture was deleted), // and that it has 2 captions with the expected values. entryRepo = _servLoc.GetInstance <ILexEntryRepository>(); int numOfFdoPictures = entryRepo.AllInstances(). Count(e => (e.SensesOS.Count > 0) && (e.SensesOS[0].PicturesOS.Count > 0)); Assert.That(entryRepo.Count, Is.EqualTo(OriginalNumOfFdoEntries)); Assert.That(numOfFdoPictures, Is.EqualTo(originalNumOfFdoPictures)); var entry = cache.ServiceLocator.GetObject(expectedGuid) as ILexEntry; Assert.IsNotNull(entry); Assert.That(entry.Guid, Is.EqualTo(expectedGuid)); Assert.That(entry.SensesOS.Count, Is.GreaterThan(0)); Assert.That(entry.SensesOS.First().PicturesOS.Count, Is.EqualTo(2)); Assert.That(entry.SensesOS[0].PicturesOS[0].PictureFileRA.InternalPath.ToString(), Is.EqualTo(expectedInternalFileName)); Assert.That(entry.SensesOS[0].PicturesOS[1].PictureFileRA.InternalPath.ToString(), Is.EqualTo(expectedExternalFileName)); LfMultiText expectedNewCaption = ConvertFdoToMongoLexicon. ToMultiText(entry.SensesOS[0].PicturesOS[0].Caption, cache.ServiceLocator.WritingSystemManager); int expectedNumOfNewCaptions = expectedNewCaption.Count(); Assert.That(expectedNumOfNewCaptions, Is.EqualTo(2)); string expectedNewVernacularCaption = expectedNewCaption["qaa-x-kal"].Value; string expectedNewAnalysisCaption = expectedNewCaption["en"].Value; Assert.That(expectedNewVernacularCaption.Equals("First Vernacular caption")); Assert.That(expectedNewAnalysisCaption.Equals("Internal path reference")); var testSubEntry = cache.ServiceLocator.GetObject(Guid.Parse(TestSubEntryGuidStr)) as ILexEntry; Assert.That(testSubEntry, Is.Not.Null); Assert.That(testSubEntry.SensesOS[0].PicturesOS[0].PictureFileRA.InternalPath.ToString(), Is.EqualTo("Pictures\\TestImage.tif")); var kenEntry = cache.ServiceLocator.GetObject(Guid.Parse(KenEntryGuidStr)) as ILexEntry; Assert.That(kenEntry, Is.Not.Null); Assert.That(kenEntry.SensesOS[0].PicturesOS[0].PictureFileRA.InternalPath.ToString(), Is.EqualTo("F:\\src\\xForge\\web-languageforge\\test\\php\\common\\TestImage.jpg")); }