/// <summary> /// 取得章节的行列表. /// </summary> /// <param name="chapterCode"></param> /// <returns></returns> public List <Line> GetChapterLineList(string chapterCode) { using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.Lines.Include("Chapter") where data.ChapterCode == chapterCode orderby data.LineNumber select data; List <Line> resultList = query.ToList(); // 为 Null 的数据, 变更为字符. foreach (var oneResult in resultList) { if (oneResult.MachineText == null) { oneResult.MachineText = String.Empty; } if (oneResult.TranslateText == null) { oneResult.TranslateText = String.Empty; } } return(resultList); } }
/// <summary> /// 新章节. /// </summary> /// <param name="chapter"></param> /// <param name="lineTextArray"></param> /// <returns></returns> public bool NewChapter(Chapter chapter, string[] lineTextArray) { try { using (MyTranslateContext context = new MyTranslateContext()) { Chapter oldChapter = context.Chapters.Find(chapter.ChapterCode); if (oldChapter != null) { ResultMessage = String.Format("章节代码 {0} 已存在!", chapter.ChapterCode); return(false); } chapter.IsActive = true; chapter.BeforeInsertOperation(); context.Chapters.Add(chapter); List <Line> lineList = BuildSourceLineList(lineTextArray); foreach (Line line in lineList) { line.ChapterCode = chapter.ChapterCode; if (line.IsBlank) { // 空行不需后续翻译操作. line.MachineText = ""; line.TranslateText = ""; line.GotoDone(); } else { line.GotoWait(); } line.BeforeInsertOperation(); context.Lines.Add(line); } context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 导出. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExp_Click(object sender, EventArgs e) { if (this.saveFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK) { // 用户取消. return; } // 取得代码. string chapterCode = this.cboChapters.SelectedValue as string; if (String.IsNullOrEmpty(chapterCode)) { MyMessage.Warn("未选择章节!"); return; } Chapter dbChapter = null; using (MyTranslateContext context = new MyTranslateContext()) { dbChapter = context.Chapters.Include("Book").Include("Lines").FirstOrDefault(p => p.ChapterCode == chapterCode); } if (dbChapter == null) { MyMessage.Warn("章节数据不存在!"); return; } Chapter expChapter = new Chapter(); CommonModelCopyer.ModelCopy(dbChapter, expChapter); expChapter.Lines = new List <Line>(); foreach (Line dbLine in dbChapter.Lines) { Line expLine = new Line(); CommonModelCopyer.ModelCopy(dbLine, expLine); expLine.Status = dbLine.Status; expChapter.Lines.Add(expLine); } // 输出 UTF-8 的 XML 文件. XmlSerializer xs = new XmlSerializer(typeof(Chapter)); using (StreamWriter sw = new StreamWriter(this.saveFileDialog1.FileName)) { xs.Serialize(sw, expChapter); } MyMessage.Success("导出完毕!"); }
/// <summary> /// 新增一行. /// </summary> /// <param name="line"></param> /// <returns></returns> public bool NewOneLine(Line line) { try { using (MyTranslateContext context = new MyTranslateContext()) { line.BeforeInsertOperation(); context.Lines.Add(line); // 物理保存. context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
public void InitChapters() { using (MyTranslateContext context = new MyTranslateContext()) { List <Chapter> chapters = context.Chapters.ToList(); this.DataSource = chapters; } }
public void InitChapters(string bookCode) { using (MyTranslateContext context = new MyTranslateContext()) { List <Chapter> chapters = context.Chapters.Where(p => p.BookCode == bookCode).OrderBy(p => p.ChapterCode).ToList(); this.DataSource = chapters; } }
/// <summary> /// 插入或更新 自动替换数据. /// </summary> /// <param name="autoReplaceData"></param> /// <returns></returns> public bool InsertOrUpdateAutoReplace(AutoReplace autoReplaceData) { try { if (String.IsNullOrEmpty(autoReplaceData.SourceText)) { ResultMessage = "原始文本不能为空!"; return(false); } if (String.IsNullOrEmpty(autoReplaceData.MachineText)) { ResultMessage = "机翻文本不能为空!"; return(false); } if (String.IsNullOrEmpty(autoReplaceData.TranslateText)) { ResultMessage = "结果文本不能为空!"; return(false); } using (MyTranslateContext context = new MyTranslateContext()) { AutoReplace dbData = context.AutoReplaces.FirstOrDefault(p => p.SourceText == autoReplaceData.SourceText); if (dbData == null) { // 数据库中不存在. // 新增. autoReplaceData.BeforeInsertOperation(); // 插入. context.AutoReplaces.Add(autoReplaceData); } else { dbData.MachineText = autoReplaceData.MachineText; dbData.TranslateText = autoReplaceData.TranslateText; dbData.Status = autoReplaceData.Status; dbData.BeforeUpdateOperation(); } // 物理保存. context.SaveChanges(); } return(true); } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 获取书籍. /// </summary> /// <param name="bookCode"></param> /// <returns></returns> public Book GetBook(string bookCode) { using (MyTranslateContext context = new MyTranslateContext()) { Book book = context.Books.Find(bookCode); return(book); } }
/// <summary> /// 取得指定书的, 包含指定名词的 行列表 /// </summary> /// <param name="bookCode"></param> /// <param name="sourceName"></param> /// <returns></returns> public List <Line> GetLineListByName(string bookCode, string sourceName) { // 结果列表. List <Line> resultList = new List <Line>(); using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.Lines.Include("Chapter") where data.Chapter.BookCode == bookCode && data.SourceText.Contains(sourceName) orderby data.ChapterCode, data.LineNumber select data; // 2016-3-11 修改开始. // 好像日文字符,上面的查询, 在 SQL Server 下面, 没有问题, 但是在 SQLite 数据库里面,存在问题。 // 只能在这里, 作后续的 手工筛选。 // return query.ToList(); foreach (Line line in query) { if (line.SourceText.Contains(sourceName)) { resultList.Add(line); } // 区分大小写方式, 尝试 区分 平假/片假. //int idx = line.SourceText.IndexOf(sourceName, StringComparison.CurrentCulture); //if (idx >= 0) //{ // resultList.Add(line); //} } // 2016-3-11 修改结束. // 把行号=0 的,放到最前. resultList = resultList.OrderBy(p => p.LineNumber == 0 ? 0 : 1).ToList(); // 返回. return(resultList); } }
/// <summary> /// 取得 自动替换数据列表. /// </summary> /// <returns></returns> public List <AutoReplace> GetAutoReplaceList() { using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.AutoReplaces select data; return(query.ToList()); } }
/// <summary> /// 新增书籍数据. /// </summary> /// <param name="book"></param> /// <returns></returns> public bool NewBook(Book book) { try { using (MyTranslateContext context = new MyTranslateContext()) { Book oldBook = context.Books.Find(book.BookCode); if (oldBook != null) { ResultMessage = String.Format("书籍代码 {0} 已存在!", book.BookCode); return(false); } book.IsActive = true; book.BeforeInsertOperation(); context.Books.Add(book); // 同时, 新增 “名称” 章节. Chapter nameChapter = new Chapter() { // 书代码. BookCode = book.BookCode, // 章节代码. ChapterCode = book.BookCode + "/NAMES", ChapterName = "命名", }; nameChapter.IsActive = true; nameChapter.BeforeInsertOperation(); context.Chapters.Add(nameChapter); context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 取得章节. /// </summary> /// <param name="chapterCode"></param> /// <returns></returns> public Chapter GetChapter(string chapterCode) { using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.Chapters where data.ChapterCode == chapterCode select data; return(query.FirstOrDefault()); } }
/// <summary> /// 取得章节的 翻译文本. /// </summary> /// <param name="chapterCode"></param> /// <returns></returns> public string[] GetChapterLineTranslateText(string chapterCode) { using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.Lines where data.ChapterCode == chapterCode orderby data.LineNumber select data.TranslateText; return(query.ToArray()); } }
public void InitBooks() { using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.Books where data.Status == Book.STATUS_IS_ACTIVE select data; List <Book> books = query.ToList(); this.DataSource = books; } }
/// <summary> /// 书下拉列表发生变化. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cboBooks_SelectedIndexChanged(object sender, EventArgs e) { // 取得代码. bookCode = this.cboBooks.SelectedValue as string; // 再次检索. using (MyTranslateContext context = new MyTranslateContext()) { chapterList = context.Chapters.Where(p => p.BookCode == bookCode).ToList(); this.gvChapters.DataSource = null; this.gvChapters.DataSource = chapterList; } }
/// <summary> /// 取得章节的行列表. /// </summary> /// <param name="chapterCode"></param> /// <returns></returns> public List <Line> GetChapterLineList(string chapterCode) { using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.Lines.Include("Chapter") where data.ChapterCode == chapterCode orderby data.LineNumber select data; return(query.ToList()); } }
public void InitNames(string bookCode) { using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.Lines where data.Chapter.BookCode == bookCode && data.ChapterCode == bookCode + "/NAMES" orderby data.SourceText select data; List <Line> nameList = query.ToList(); this.DataSource = nameList; } }
/// <summary> /// 删除章节. /// </summary> /// <param name="chapter"></param> /// <returns></returns> public bool Delete(Chapter chapter) { if (chapter.ChapterCode.Contains("NAMES")) { ResultMessage = "命名章节不能删除"; return(false); } try { using (MyTranslateContext context = new MyTranslateContext()) { // 先判断 数据是否存在. Chapter dbChapter = context.Chapters.Include("Lines").FirstOrDefault(p => p.ChapterCode == chapter.ChapterCode); if (dbChapter == null) { // 数据 不存在. ResultMessage = String.Format("未能检索到代码为 {0} 的章节", chapter.ChapterCode); return(false); } else { // 删除逻辑. // 首先删除该章节下面的 行. context.Lines.RemoveRange(dbChapter.Lines); // 删除章节. context.Chapters.Remove(dbChapter); } // 物理保存. context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 更新一行. /// </summary> /// <param name="line"></param> /// <returns></returns> public bool UpdateOneLine(Line line) { try { using (MyTranslateContext context = new MyTranslateContext()) { Line oldLine = context.Lines.Find(line.LineID); if (oldLine == null) { ResultMessage = "数据不存在!!!"; return(false); } // 只更新 译文 与 状态. oldLine.TranslateText = line.TranslateText; oldLine.Status = line.Status; // 如果是名称行。 额外更新. if (line.ChapterCode.Contains("NAMES") && !String.IsNullOrEmpty(line.MachineText)) { oldLine.MachineText = line.MachineText; } oldLine.BeforeUpdateOperation(); // 物理保存. context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 取得指定书的, 包含指定名词的 行列表 /// </summary> /// <param name="bookCode"></param> /// <param name="sourceName"></param> /// <returns></returns> public List <Line> GetLineListByName(string bookCode, string sourceName) { using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.Lines.Include("Chapter") where data.Chapter.BookCode == bookCode && data.SourceText.Contains(sourceName) orderby data.ChapterCode, data.LineNumber select data; return(query.ToList()); } }
/// <summary> /// 取得书籍列表. /// </summary> /// <param name="status"></param> /// <returns></returns> public List <Book> GetBookList(string status = null) { using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.Books select data; if (!String.IsNullOrEmpty(status)) { query = query.Where(p => p.Status == status); } List <Book> resultList = query.ToList(); return(resultList); } }
/// <summary> /// 取得章节列表. /// </summary> /// <param name="bookCode"></param> /// <param name="status"></param> /// <returns></returns> public List <Chapter> GetChapterList(string bookCode, string status = null) { using (MyTranslateContext context = new MyTranslateContext()) { var query = from data in context.Chapters where data.BookCode == bookCode select data; if (!String.IsNullOrEmpty(status)) { query = query.Where(p => p.Status == status); } List <Chapter> resultList = query.ToList(); return(resultList); } }
/// <summary> /// 更新书籍数据. /// </summary> /// <param name="book"></param> /// <returns></returns> public bool UpdateBook(Book book) { try { using (MyTranslateContext context = new MyTranslateContext()) { Book oldBook = context.Books.Find(book.BookCode); if (oldBook == null) { ResultMessage = String.Format("书籍代码 {0} 不存在!", book.BookCode); return(false); } // 书名. oldBook.BookName = book.BookName; // Url 地址. oldBook.BookUrl = book.BookUrl; // 有效性. oldBook.IsActive = book.IsActive; // 更新前处理. oldBook.BeforeUpdateOperation(); // 物理保存. context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 新增一行. /// </summary> /// <param name="line"></param> /// <returns></returns> public bool NewOneLine(Line line) { try { using (MyTranslateContext context = new MyTranslateContext()) { // 名称需要先去除空格. line.SourceText = line.SourceText.Trim(); // 对于名称, 需要先判断, 名称是否已存在 Line dbLine = context.Lines.FirstOrDefault(p => p.ChapterCode == line.ChapterCode && p.SourceText == line.SourceText); if (dbLine != null) { ResultMessage = String.Format("名词 {0} 已存在!", line.SourceText); return(false); } line.BeforeInsertOperation(); context.Lines.Add(line); // 物理保存. context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 更新一行. /// </summary> /// <param name="line"></param> /// <returns></returns> public bool UpdateOneLine(Line line) { try { using (MyTranslateContext context = new MyTranslateContext()) { Line oldLine = context.Lines.Find(line.LineID); if (oldLine == null) { ResultMessage = "数据不存在!!!"; return(false); } // 只更新 译文 与 状态. oldLine.TranslateText = line.TranslateText; oldLine.Status = line.Status; oldLine.BeforeUpdateOperation(); // 物理保存. context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 新增或更新章节. /// </summary> /// <param name="chapter"></param> /// <returns></returns> public bool InsertOrUpdate(Chapter chapter) { try { using (MyTranslateContext context = new MyTranslateContext()) { // 先判断 书籍是否存在. Book book = context.Books.Find(chapter.BookCode); if (book == null) { ResultMessage = String.Format("未检索到代码为{0}的书籍!", chapter.BookCode); return(false); } // 先判断是 新增还是更新. Chapter dbChapter = context.Chapters.Include("Lines").FirstOrDefault(p => p.ChapterCode == chapter.ChapterCode); if (dbChapter == null) { // 新增逻辑. // 新增前处理. chapter.BeforeInsertOperation(); foreach (Line line in chapter.Lines) { // 新增前处理. line.BeforeInsertOperation(); } context.Chapters.Add(chapter); } else { // 更新逻辑. // 名称. dbChapter.ChapterName = chapter.ChapterName; dbChapter.ChapterTranslateName = chapter.ChapterTranslateName; List <Line> newLineList = new List <Line>(); // 行. foreach (Line line in chapter.Lines) { Line dbLine = null; if (chapter.ChapterCode.Contains("NAMES")) { // 命名章节. // 按照 原始文本进行匹配. dbLine = dbChapter.Lines.FirstOrDefault(p => p.SourceText == line.SourceText); } else { // 普通章节. // 按照 行号匹配. dbLine = dbChapter.Lines.FirstOrDefault(p => p.LineNumber == line.LineNumber); } if (dbLine == null) { // 新增 newLineList.Add(line); } else { // 更新. dbLine.SourceText = line.SourceText; dbLine.MachineText = line.MachineText; dbLine.TranslateText = line.TranslateText; dbLine.Status = line.Status; dbLine.BeforeUpdateOperation(); } } if (newLineList.Count > 0) { foreach (Line newLine in newLineList) { // 新增的,需要重置 流水ID. newLine.LineID = 0; newLine.Chapter = null; newLine.CreateUser = null; newLine.BeforeInsertOperation(); dbChapter.Lines.Add(newLine); } } } // 物理保存. context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 新增或更新 书籍与章节.. /// </summary> /// <param name="book"></param> /// <param name="replaceAll"></param> /// <returns></returns> public bool InsertOrUpdateBookAndChapter(Book book, bool replaceAll = true) { try { using (MyTranslateContext context = new MyTranslateContext()) { Book oldBook = context.Books.Find(book.BookCode); if (oldBook == null) { // 数据库中,数据不存在. // 新增逻辑. // 新增前处理. book.BeforeInsertOperation(); foreach (Chapter chapter in book.Chapters) { // 新增前处理. chapter.BeforeInsertOperation(); foreach (Line line in chapter.Lines) { // 新增前处理. line.BeforeInsertOperation(); } } context.Books.Add(book); } else { // 数据库中, 数据已存在, // 更新逻辑. // 书名. oldBook.BookName = book.BookName; // Url 地址. oldBook.BookUrl = book.BookUrl; // 有效性. oldBook.IsActive = book.IsActive; // 更新前处理. oldBook.BeforeUpdateOperation(); foreach (Chapter chapter in book.Chapters) { // 检查数据库中, 是否存在指定章节. Chapter dbChapter = context.Chapters.Include("Lines").FirstOrDefault(p => p.ChapterCode == chapter.ChapterCode); if (dbChapter != null && !replaceAll) { // 数据库中, 已存在章节。 // 但是本次操作, 是只更新。 // 那么 本章节则忽略. continue; } if (dbChapter == null) { // 数据库中, 章节不存在. // 新增逻辑. // 新增前处理. chapter.BeforeInsertOperation(); foreach (Line line in chapter.Lines) { // 新增前处理. line.BeforeInsertOperation(); } context.Chapters.Add(chapter); } else { // 数据库中,章节存在. // 更新逻辑. // 名称. dbChapter.ChapterName = chapter.ChapterName; dbChapter.ChapterTranslateName = chapter.ChapterTranslateName; dbChapter.BeforeUpdateOperation(); List <Line> newLineList = new List <Line>(); // 行. foreach (Line line in chapter.Lines) { Line dbLine = null; if (chapter.ChapterCode.Contains("NAMES")) { // 命名章节. // 按照 原始文本进行匹配. dbLine = dbChapter.Lines.FirstOrDefault(p => p.SourceText == line.SourceText); } else { // 普通章节. // 按照 行号匹配. dbLine = dbChapter.Lines.FirstOrDefault(p => p.LineNumber == line.LineNumber); } if (dbLine == null) { // 新增 newLineList.Add(line); } else { // 更新. dbLine.SourceText = line.SourceText; dbLine.MachineText = line.MachineText; dbLine.TranslateText = line.TranslateText; dbLine.Status = line.Status; dbLine.BeforeUpdateOperation(); } } if (newLineList.Count > 0) { foreach (Line newLine in newLineList) { // 新增的,需要重置 流水ID. newLine.LineID = 0; newLine.Chapter = null; newLine.CreateUser = null; newLine.BeforeInsertOperation(); dbChapter.Lines.Add(newLine); } } } } } // 物理保存. context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }
/// <summary> /// 导出书籍. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExpBook_Click(object sender, EventArgs e) { // 取得代码. string bookCode = this.cboBooks.SelectedValue as string; if (String.IsNullOrEmpty(bookCode)) { MyMessage.Warn("未选择书籍!"); return; } if (this.saveFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK) { // 用户取消. return; } try { this.Cursor = Cursors.WaitCursor; this.gvLines.Enabled = false; Book dbBook = null; Book expBook = new Book(); using (MyTranslateContext context = new MyTranslateContext()) { dbBook = context.Books.Include("Chapters").FirstOrDefault(p => p.BookCode == bookCode); if (dbBook == null) { MyMessage.Warn("书籍数据不存在!"); return; } CommonModelCopyer.ModelCopy(dbBook, expBook); expBook.Chapters = new List <Chapter>(); foreach (Chapter dbChapter in dbBook.Chapters) { Chapter expChapter = new Chapter(); CommonModelCopyer.ModelCopy(dbChapter, expChapter); expChapter.Status = dbChapter.Status; expChapter.Lines = new List <Line>(); foreach (Line dbLine in dbChapter.Lines) { Line expLine = new Line(); CommonModelCopyer.ModelCopy(dbLine, expLine); expLine.Status = dbLine.Status; expChapter.Lines.Add(expLine); } expBook.Chapters.Add(expChapter); } } // 输出 UTF-8 的 XML 文件. XmlSerializer xs = new XmlSerializer(typeof(Book)); using (StreamWriter sw = new StreamWriter(this.saveFileDialog1.FileName)) { xs.Serialize(sw, expBook); } MyMessage.Success("导出完毕!"); } catch (Exception ex) { MyMessage.Fail(ex.Message); } finally { this.Cursor = Cursors.Default; this.gvLines.Enabled = true; } }
/// <summary> /// 机翻. /// </summary> /// <param name="chapterCode"></param> /// <param name="lineTextArray"></param> /// <returns></returns> public bool AutoTrans(string chapterCode, string[] lineTextArray) { try { using (MyTranslateContext context = new MyTranslateContext()) { Chapter oldChapter = context.Chapters.Find(chapterCode); if (oldChapter == null) { ResultMessage = String.Format("章节代码 {0} 不存在!", chapterCode); return(false); } // 取得 行信息. List <Line> lineList = oldChapter.Lines.OrderBy(p => p.LineNumber).ToList(); // 取得机翻行. List <Line> transLineList = BuildAutoTransLineList(lineTextArray); // 取得自动替换信息. List <AutoReplace> autoReplaceList = context.AutoReplaces.Where(p => p.Status == AutoReplace.STATUS_IS_ACTIVE).ToList(); foreach (Line line in lineList) { // 空行不需要翻译. if (line.IsBlank) { continue; } // 检索机翻行. Line transLine = transLineList.FirstOrDefault(p => p.SourceText == line.SourceText); // 找到机翻行. if (transLine != null) { // 更新. line.MachineText = transLine.MachineText; if (String.IsNullOrEmpty(line.TranslateText)) { // 为非空的情况下, 提前设置. line.TranslateText = line.MachineText; } // 检测是否存在 自动替换的标志. foreach (var replaceData in autoReplaceList) { if (line.SourceText.Contains(replaceData.SourceText) && line.TranslateText.Contains(replaceData.MachineText)) { line.TranslateText = line.TranslateText.Replace(replaceData.MachineText, replaceData.TranslateText); } } // 机翻标志. line.GotoMachine(); } } // 物理保存. context.SaveChanges(); return(true); } } catch (Exception ex) { ResultMessage = ex.Message; return(false); } }