public void RunConversion() { LfProjectConfig config = _factory.Create(_project).Config; FieldLists fieldConfigs = FieldListsForEntryAndSensesAndExamples(config); var fixedComments = new List <LfComment>(_conn.GetComments(_project)); string allCommentsJson = JsonConvert.SerializeObject(fixedComments); // _logger.Debug("Doing Lcm->Mongo direction. The json for ALL comments from Mongo would be: {0}", allCommentsJson); // _logger.Debug("Doing Lcm->Mongo direction. About to call LfMergeBridge with that JSON..."); string bridgeOutput; if (CallLfMergeBridge(allCommentsJson, out bridgeOutput)) { string newCommentsStr = ConvertMongoToLcmComments.GetPrefixedStringFromLfMergeBridgeOutput(bridgeOutput, "New comments not yet in LF: "); string newRepliesStr = ConvertMongoToLcmComments.GetPrefixedStringFromLfMergeBridgeOutput(bridgeOutput, "New replies on comments already in LF: "); string newStatusChangesStr = ConvertMongoToLcmComments.GetPrefixedStringFromLfMergeBridgeOutput(bridgeOutput, "New status changes on comments already in LF: "); List <LfComment> comments = JsonConvert.DeserializeObject <List <LfComment> >(newCommentsStr); List <Tuple <string, List <LfCommentReply> > > replies = JsonConvert.DeserializeObject <List <Tuple <string, List <LfCommentReply> > > >(newRepliesStr); List <KeyValuePair <string, Tuple <string, string> > > statusChanges = JsonConvert.DeserializeObject <List <KeyValuePair <string, Tuple <string, string> > > >(newStatusChangesStr); foreach (LfComment comment in comments) { // LfMergeBridge only sets the Guid in comment.Regarding, and leaves it to the LfMerge side to set the rest of the fields meaningfully if (comment.Regarding != null) { Guid guid; if (Guid.TryParse(comment.Regarding.TargetGuid ?? "", out guid)) { // The GUID in Chorus notes MIGHT be an entry, or it might be a sense or an example sentence. // We want to handle these three cases differently -- see FromTargetGuid below. comment.Regarding = FromTargetGuid(guid, fieldConfigs); } } // _logger.Debug("Comment by {6} regarding field {0} (containing {1}) of word {2} (GUID {7}, meaning {3}) has content {4}{5} and status {8} (GUID {9})", // comment.Regarding.FieldNameForDisplay, // comment.Regarding.FieldValue, // comment.Regarding.Word, // comment.Regarding.Meaning, // comment.Content, // comment.Replies.Count <= 0 ? "" : " and replies [" + String.Join(", ", comment.Replies.Select(reply => "\"" + reply.Content + "\"")) + "]", // comment.AuthorNameAlternate ?? "<null>", // comment.Regarding.TargetGuid, // comment.Status, // comment.StatusGuid // ); } _conn.UpdateComments(_project, comments); _conn.UpdateReplies(_project, replies); _conn.UpdateCommentStatuses(_project, statusChanges); } else { // Failure, which has already been logged so we don't need to log it again } }
public FieldLists FieldListsForEntryAndSensesAndExamples(LfProjectConfig config) { FieldLists result = new FieldLists(); result.entryConfig = config.Entry; result.senseConfig = null; result.exampleConfig = null; if (result.entryConfig != null && result.entryConfig.Fields.ContainsKey("senses")) { result.senseConfig = result.entryConfig.Fields["senses"] as LfConfigFieldList; } if (result.senseConfig != null && result.senseConfig.Fields.ContainsKey("examples")) { result.exampleConfig = result.senseConfig.Fields["examples"] as LfConfigFieldList; } return(result); }
public LfCommentRegarding FromTargetGuid(Guid guidOfUnknownLcmObject, FieldLists fieldConfigs) { var result = new LfCommentRegarding(); // Worst case, we'll return this empty object rather than null if (!_servLoc.ObjectRepository.IsValidObjectId(guidOfUnknownLcmObject)) { return(result); } ICmObject lcmObject = _servLoc.ObjectRepository.GetObject(guidOfUnknownLcmObject); if (lcmObject == null) { return(result); } result.TargetGuid = lcmObject.Guid.ToString(); switch (lcmObject.ClassID) { case LexEntryTags.kClassId: { var entry = lcmObject as ILexEntry; if (entry != null) // Paranoia; should never happen, but check anyway { result.Word = entry.ShortName; result.Meaning = entry.NumberOfSensesForEntry < 1 ? "" : Definition(entry.SensesOS[0]); } break; } case LexSenseTags.kClassId: { var sense = lcmObject as ILexSense; if (sense == null || sense.Entry == null) // Paranoia; should never happen, but check anyway { return(result); } ILexEntry entry = sense.Entry; result.TargetGuid = entry.Guid.ToString(); result.Word = entry.ShortName; result.Meaning = Definition(sense); result.Field = MagicStrings.LfFieldNameForDefinition; // Even if it's really the gloss, we'll say it's the definition for LF display purposes result.FieldNameForDisplay = FieldNameForDisplay(result.Field, fieldConfigs.senseConfig); result.FieldValue = result.Meaning; break; } case LexExampleSentenceTags.kClassId: { var example = lcmObject as ILexExampleSentence; if (example == null || example.Owner == null) // Paranoia; should never happen, but check anyway { return(result); } var sense = example.Owner as ILexSense; // Example sentences are always owned by senses if (sense == null || sense.Entry == null) // Paranoia; should never happen, but check anyway { return(result); } ILexEntry entry = sense.Entry; result.TargetGuid = entry.Guid.ToString(); result.Word = entry.ShortName; result.Meaning = Definition(sense); result.Field = MagicStrings.LfFieldNameForExampleSentence; // Even if it's really a subfield of the example, we'll say it's the example for LF display purposes result.FieldNameForDisplay = FieldNameForDisplay(result.Field, fieldConfigs.exampleConfig); result.FieldValue = ExampleValue(example); break; } default: { // Last-ditch effort: climb the owner chain and maybe we'll hit a LexEntry while (lcmObject.Owner != null && lcmObject.ClassID != LexEntryTags.kClassId) { lcmObject = lcmObject.Owner; } if (lcmObject != null && lcmObject.ClassID == LexEntryTags.kClassId) { var entry = lcmObject as ILexEntry; if (entry != null) { result.Word = entry.ShortName; result.Meaning = entry.NumberOfSensesForEntry < 1 ? "" : Definition(entry.SensesOS[0]); } } // But if we didn't, then just give up break; } } return(result); }