void menu_saveToXmlFile_Click(object sender, EventArgs e) { string strError = ""; //int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选定要导出的事项"; goto ERROR1; } #if NO Encoding preferredEncoding = this.CurrentEncoding; { // 观察要保存的第一条记录的marc syntax } #endif // 询问文件名 SaveFileDialog dlg = new SaveFileDialog(); dlg.Title = "请指定要创建的 XML 文件名"; dlg.CreatePrompt = false; dlg.OverwritePrompt = true; dlg.FileName = ""; // dlg.InitialDirectory = Environment.CurrentDirectory; dlg.Filter = "XML 文件 (*.xml)|*.xml|All files (*.*)|*.*"; dlg.RestoreDirectory = true; if (dlg.ShowDialog() != DialogResult.OK) return; #if NO if (bExist == true) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已存在,是否以追加方式写入记录?\r\n\r\n--------------------\r\n注:(是)追加 (否)覆盖 (取消)放弃", "BiblioSearchForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) bAppend = true; if (result == DialogResult.No) bAppend = false; if (result == DialogResult.Cancel) { strError = "放弃处理..."; goto ERROR1; } } #endif this.EnableControls(false); LibraryChannel channel = this.GetChannel(); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在导出到 XML 文件 ..."); stop.BeginLoop(); XmlTextWriter writer = null; try { writer = new XmlTextWriter(dlg.FileName, Encoding.UTF8); } catch (Exception ex) { strError = "创建文件 " + dlg.FileName + " 失败,原因: " + ex.Message; goto ERROR1; } try { stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); writer.Formatting = Formatting.Indented; writer.Indentation = 4; writer.WriteStartDocument(); writer.WriteStartElement("dprms", "collection", DpNs.dprms); writer.WriteAttributeString("xmlns", "dprms", null, DpNs.dprms); #if NO writer.WriteAttributeString("xmlns", "unimarc", null, DigitalPlatform.Xml.Ns.unimarcxml); writer.WriteAttributeString("xmlns", "marc21", null, DigitalPlatform.Xml.Ns.usmarcxml); #endif List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewBiblioLoader loader = new ListViewBiblioLoader(channel, // this.Channel, stop, items, this.m_biblioTable); loader.Prompt -= new MessagePromptEventHandler(loader_Prompt); loader.Prompt += new MessagePromptEventHandler(loader_Prompt); int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } #if NO string strRecPath = item.Text; if (string.IsNullOrEmpty(strRecPath) == true) continue; string[] results = null; byte[] baTimestamp = null; stop.SetMessage("正在获取书目记录 " + strRecPath); long lRet = Channel.GetBiblioInfos( stop, strRecPath, "", new string[] { "xml" }, // formats out results, out baTimestamp, out strError); if (lRet == 0) goto ERROR1; if (lRet == -1) goto ERROR1; if (results == null || results.Length == 0) { strError = "results error"; goto ERROR1; } string strXml = results[0]; #endif BiblioInfo info = item.BiblioInfo; string strXml = ""; { if (string.IsNullOrEmpty(info.NewXml) == false) strXml = info.NewXml; else strXml = info.OldXml; } if (string.IsNullOrEmpty(strXml) == false) { XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strXml); } catch (Exception ex) { strError = "XML 装入 DOM 时出错: " + ex.Message; goto ERROR1; } if (dom.DocumentElement != null) { // 给根元素设置几个参数 DomUtil.SetAttr(dom.DocumentElement, "path", DpNs.dprms, this.MainForm.LibraryServerUrl + "?" + item.BiblioInfo.RecPath); // strRecPath DomUtil.SetAttr(dom.DocumentElement, "timestamp", DpNs.dprms, ByteArray.GetHexTimeStampString(item.BiblioInfo.Timestamp)); // baTimestamp dom.DocumentElement.WriteTo(writer); } } stop.SetProgressValue(++i); } writer.WriteEndElement(); // </collection> writer.WriteEndDocument(); } catch (Exception ex) { strError = "写入文件 " + dlg.FileName + " 失败,原因: " + ex.Message; goto ERROR1; } finally { writer.Close(); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); this.ReturnChannel(channel); this.EnableControls(true); } MainForm.StatusBarMessage = this.listView_records.SelectedItems.Count.ToString() + "条记录成功保存到文件 " + dlg.FileName; return; ERROR1: MessageBox.Show(this, strError); }
// 2015/10/10 // 保存到 MARC 文件 void menu_saveToMarcFile_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选定要导出的事项"; goto ERROR1; } Encoding preferredEncoding = this.CurrentEncoding; { // 观察要保存的第一条记录的marc syntax } OpenMarcFileDlg dlg = new OpenMarcFileDlg(); MainForm.SetControlFont(dlg, this.Font); dlg.IsOutput = true; dlg.AddG01Visible = false; dlg.RuleVisible = true; dlg.Rule = this.LastCatalogingRule; dlg.FileName = this.LastIso2709FileName; dlg.CrLf = this.LastCrLfIso2709; dlg.RemoveField998 = this.LastRemoveField998; dlg.EncodingListItems = Global.GetEncodingList(false); dlg.EncodingName = (String.IsNullOrEmpty(this.LastEncodingName) == true ? Global.GetEncodingName(preferredEncoding) : this.LastEncodingName); dlg.EncodingComment = "注: 原始编码方式为 " + Global.GetEncodingName(preferredEncoding); dlg.MarcSyntax = "<自动>"; // strPreferedMarcSyntax; dlg.EnableMarcSyntax = false; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; string strCatalogingRule = dlg.Rule; if (strCatalogingRule == "<无限制>") strCatalogingRule = null; Encoding targetEncoding = null; nRet = Global.GetEncoding(dlg.EncodingName, out targetEncoding, out strError); if (nRet == -1) { goto ERROR1; } string strLastFileName = this.LastIso2709FileName; string strLastEncodingName = this.LastEncodingName; bool bExist = File.Exists(dlg.FileName); bool bAppend = false; if (bExist == true) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已存在,是否以追加方式写入记录?\r\n\r\n--------------------\r\n注:(是)追加 (否)覆盖 (取消)放弃", "BiblioSearchForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) bAppend = true; if (result == DialogResult.No) bAppend = false; if (result == DialogResult.Cancel) { strError = "放弃处理..."; goto ERROR1; } } // 检查同一个文件连续存时候的编码方式一致性 if (strLastFileName == dlg.FileName && bAppend == true) { if (strLastEncodingName != "" && strLastEncodingName != dlg.EncodingName) { DialogResult result = MessageBox.Show(this, "文件 '" + dlg.FileName + "' 已在先前已经用 " + strLastEncodingName + " 编码方式存储了记录,现在又以不同的编码方式 " + dlg.EncodingName + " 追加记录,这样会造成同一文件中存在不同编码方式的记录,可能会令它无法被正确读取。\r\n\r\n是否继续? (是)追加 (否)放弃操作", "BiblioSearchForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.No) { strError = "放弃处理..."; goto ERROR1; } } } this.LastIso2709FileName = dlg.FileName; this.LastCrLfIso2709 = dlg.CrLf; this.LastEncodingName = dlg.EncodingName; this.LastCatalogingRule = dlg.Rule; this.LastRemoveField998 = dlg.RemoveField998; Stream s = null; try { s = File.Open(this.LastIso2709FileName, FileMode.OpenOrCreate); if (bAppend == false) s.SetLength(0); else s.Seek(0, SeekOrigin.End); } catch (Exception ex) { strError = "打开或创建文件 " + this.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } LibraryChannel channel = this.GetChannel(); this.EnableControls(false); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在导出到 MARC 文件 ..."); stop.BeginLoop(); try { stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewBiblioLoader loader = new ListViewBiblioLoader(channel, // this.Channel, stop, items, this.m_biblioTable); loader.Prompt -= new MessagePromptEventHandler(loader_Prompt); loader.Prompt += new MessagePromptEventHandler(loader_Prompt); int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } #if NO string[] results = null; byte[] baTimestamp = null; stop.SetMessage("正在获取书目记录 " + strRecPath); long lRet = Channel.GetBiblioInfos( stop, strRecPath, "", new string[] { "xml" }, // formats out results, out baTimestamp, out strError); if (lRet == 0) goto ERROR1; if (lRet == -1) goto ERROR1; if (results == null || results.Length == 0) { strError = "results error"; goto ERROR1; } string strXml = results[0]; #endif BiblioInfo info = item.BiblioInfo; string strXml = ""; { if (string.IsNullOrEmpty(info.NewXml) == false) strXml = info.NewXml; else strXml = info.OldXml; } string strMARC = ""; string strMarcSyntax = ""; // 将XML格式转换为MARC格式 // 自动从数据记录中获得MARC语法 nRet = MarcUtil.Xml2Marc(strXml, true, null, out strMarcSyntax, out strMARC, out strError); if (nRet == -1) { strError = "XML转换到MARC记录时出错: " + strError; goto ERROR1; } byte[] baTarget = null; Debug.Assert(strMarcSyntax != "", ""); // 按照编目规则过滤 // 获得一个特定风格的 MARC 记录 // parameters: // strStyle 要匹配的style值。如果为null,表示任何$*值都匹配,实际上效果是去除$*并返回全部字段内容 // return: // 0 没有实质性修改 // 1 有实质性修改 nRet = MarcUtil.GetMappedRecord(ref strMARC, strCatalogingRule); if (dlg.RemoveField998 == true) { MarcRecord record = new MarcRecord(strMARC); record.select("field[@name='998']").detach(); strMARC = record.Text; } if (dlg.Mode880 == true && strMarcSyntax == "usmarc") { MarcRecord record = new MarcRecord(strMARC); MarcQuery.To880(record); strMARC = record.Text; } // 将MARC机内格式转换为ISO2709格式 // parameters: // strSourceMARC [in]机内格式MARC记录。 // strMarcSyntax [in]为"unimarc"或"usmarc" // targetEncoding [in]输出ISO2709的编码方式。为UTF8、codepage-936等等 // baResult [out]输出的ISO2709记录。编码方式受targetEncoding参数控制。注意,缓冲区末尾不包含0字符。 // return: // -1 出错 // 0 成功 nRet = MarcUtil.CvtJineiToISO2709( strMARC, strMarcSyntax, targetEncoding, out baTarget, out strError); if (nRet == -1) goto ERROR1; s.Write(baTarget, 0, baTarget.Length); if (dlg.CrLf == true) { byte[] baCrLf = targetEncoding.GetBytes("\r\n"); s.Write(baCrLf, 0, baCrLf.Length); } stop.SetProgressValue(++i); } } catch (Exception ex) { strError = "写入文件 " + this.LastIso2709FileName + " 失败,原因: " + ex.Message; goto ERROR1; } finally { s.Close(); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); this.ReturnChannel(channel); this.EnableControls(true); } // if (bAppend == true) MainForm.StatusBarMessage = this.listView_records.SelectedItems.Count.ToString() + "条记录成功追加到文件 " + this.LastIso2709FileName + " 尾部"; else MainForm.StatusBarMessage = this.listView_records.SelectedItems.Count.ToString() + "条记录成功保存到新文件 " + this.LastIso2709FileName; return; ERROR1: MessageBox.Show(this, strError); }
// 导出到书目转储文件 void menu_saveToBiblioDumpFile_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选定要导出的事项"; goto ERROR1; } #if NO // 询问文件名 SaveFileDialog dlg = new SaveFileDialog(); dlg.Title = "请指定要创建的书目转储件名"; dlg.CreatePrompt = false; dlg.OverwritePrompt = true; dlg.FileName = ""; // dlg.InitialDirectory = Environment.CurrentDirectory; dlg.Filter = "书目转储文件 (*.bdf)|*.bdf|All files (*.*)|*.*"; dlg.RestoreDirectory = true; if (dlg.ShowDialog() != DialogResult.OK) return; #endif OpenBiblioDumpFileDialog dlg = new OpenBiblioDumpFileDialog(); MainForm.SetControlFont(dlg, this.Font); dlg.CreateMode = true; this.MainForm.AppInfo.LinkFormState(dlg, "bibliosearchform_OpenBiblioDumpFileDialog"); dlg.ShowDialog(this); this.MainForm.AppInfo.UnlinkFormState(dlg); if (dlg.DialogResult != System.Windows.Forms.DialogResult.OK) return; // 观察对象目录中是否已经存在文件 if (dlg.IncludeObjectFile) { if (ExistFiles(dlg.ObjectDirectoryName) == true) { DialogResult result = MessageBox.Show(this, "您选定的对象文件夹 " + dlg.ObjectDirectoryName + " 内已经存在一些文件。若用它来保存对象文件,则新旧文件会混杂在一起。\r\n\r\n要继续处理么? (是:继续; 否: 放弃处理)", "BiblioSearchForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == System.Windows.Forms.DialogResult.No) return; } } int nProcessRemoteRecord = 0; // 0: 本变量尚未使用; 1: 处理外部记录; -1: 跳过外部记录 this.EnableControls(false); LibraryChannel channel = this.GetChannel(); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在导出到 XML 文件 ..."); stop.BeginLoop(); XmlTextWriter writer = null; try { writer = new XmlTextWriter(dlg.FileName, Encoding.UTF8); } catch (Exception ex) { strError = "创建文件 " + dlg.FileName + " 失败,原因: " + ex.Message; goto ERROR1; } try { stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); writer.Formatting = Formatting.Indented; writer.Indentation = 4; writer.WriteStartDocument(); writer.WriteStartElement("dprms", "collection", DpNs.dprms); writer.WriteAttributeString("xmlns", "dprms", null, DpNs.dprms); #if NO writer.WriteAttributeString("xmlns", "unimarc", null, DigitalPlatform.Xml.Ns.unimarcxml); writer.WriteAttributeString("xmlns", "marc21", null, DigitalPlatform.Xml.Ns.usmarcxml); #endif List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewBiblioLoader loader = new ListViewBiblioLoader(channel, // this.Channel, stop, items, this.m_biblioTable); loader.Prompt -= new MessagePromptEventHandler(loader_Prompt); loader.Prompt += new MessagePromptEventHandler(loader_Prompt); int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } BiblioInfo info = item.BiblioInfo; string strXml = ""; { if (string.IsNullOrEmpty(info.NewXml) == false) strXml = info.NewXml; else strXml = info.OldXml; } if (string.IsNullOrEmpty(strXml) == true) goto CONTINUE; XmlDocument biblio_dom = new XmlDocument(); try { biblio_dom.LoadXml(strXml); } catch (Exception ex) { strError = "书目记录 '" + info.RecPath + "' 的 XML 装入 DOM 时出错: " + ex.Message; goto ERROR1; } if (biblio_dom.DocumentElement == null) goto CONTINUE; // 是否为外部记录 bool bRemote = info.RecPath.IndexOf("@") != -1; if (bRemote == true && nProcessRemoteRecord == 0) { DialogResult temp_result = MessageBox.Show(this, "发现一些书目记录是来自共享检索的外部书目记录,是否要处理它们? (是: 处理它们; 否: 跳过它们)", "BiblioSearchForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (temp_result == System.Windows.Forms.DialogResult.Yes) nProcessRemoteRecord = 1; else nProcessRemoteRecord = -1; } if (bRemote == true && nProcessRemoteRecord == -1) goto CONTINUE; if (dlg.IncludeObjectFile && bRemote == false) { // 将书目记录中的对象资源写入外部文件 nRet = WriteObjectFiles(stop, channel, info.RecPath, ref biblio_dom, dlg.ObjectDirectoryName, out strError); if (nRet == -1) { DialogResult temp_result = MessageBox.Show(this, strError + "\r\n\r\n是否继续处理?", "BiblioSearchForm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (temp_result == DialogResult.Cancel) goto ERROR1; } } // 写入 dprms:record 元素 writer.WriteStartElement("dprms", "record", DpNs.dprms); { // 写入 dprms:biblio 元素 writer.WriteStartElement("dprms", "biblio", DpNs.dprms); if (bRemote == true) writer.WriteAttributeString("path", item.BiblioInfo.RecPath); else writer.WriteAttributeString("path", this.MainForm.LibraryServerUrl + "?" + item.BiblioInfo.RecPath); writer.WriteAttributeString("timestamp", ByteArray.GetHexTimeStampString(item.BiblioInfo.Timestamp)); biblio_dom.DocumentElement.WriteTo(writer); writer.WriteEndElement(); } if (bRemote == false) { string strBiblioDbName = StringUtil.GetDbName(item.BiblioInfo.RecPath); BiblioDbProperty prop = this.MainForm.GetBiblioDbProperty(strBiblioDbName); if (prop == null) { strError = "数据库名 '" + strBiblioDbName + "' 没有找到属性定义"; goto ERROR1; } if (string.IsNullOrEmpty(prop.OrderDbName) == false) { // dprms:orderCollection nRet = OutputEntities( stop, channel, item.BiblioInfo.RecPath, "order", writer, out strError); if (nRet == -1) goto ERROR1; } if (string.IsNullOrEmpty(prop.IssueDbName) == false) { // dprms:issueCollection nRet = OutputEntities( stop, channel, item.BiblioInfo.RecPath, "issue", writer, out strError); if (nRet == -1) goto ERROR1; } if (string.IsNullOrEmpty(prop.ItemDbName) == false) { // dprms:itemCollection nRet = OutputEntities( stop, channel, item.BiblioInfo.RecPath, "item", writer, out strError); if (nRet == -1) goto ERROR1; } if (string.IsNullOrEmpty(prop.CommentDbName) == false) { // dprms:commentCollection nRet = OutputEntities( stop, channel, item.BiblioInfo.RecPath, "comment", writer, out strError); if (nRet == -1) goto ERROR1; } } // 收尾 dprms:record 元素 writer.WriteEndElement(); CONTINUE: stop.SetProgressValue(++i); } writer.WriteEndElement(); // </collection> writer.WriteEndDocument(); } catch (Exception ex) { strError = "写入文件 " + dlg.FileName + " 失败,原因: " + ex.Message; goto ERROR1; } finally { writer.Close(); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); this.ReturnChannel(channel); this.EnableControls(true); } MainForm.StatusBarMessage = this.listView_records.SelectedItems.Count.ToString() + "条记录成功保存到文件 " + dlg.FileName; return; ERROR1: MessageBox.Show(this, strError); }
int ExportTo856SearchForm(out string strError) { strError = ""; int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选定要导出的事项"; return -1; } Marc856SearchForm form = new Marc856SearchForm(); form.MdiParent = this.MainForm; form.MainForm = this.MainForm; form.Show(); this.EnableControls(false); LibraryChannel channel = this.GetChannel(); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在导出到 MARC 文件 ..."); stop.BeginLoop(); try { List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewBiblioLoader loader = new ListViewBiblioLoader(channel, // this.Channel, stop, items, this.m_biblioTable); loader.Prompt -= new MessagePromptEventHandler(loader_Prompt); loader.Prompt += new MessagePromptEventHandler(loader_Prompt); List<ListViewItem> new_items = new List<ListViewItem>(); int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; return -1; } string strBiblioSummary = ListViewUtil.GetItemText(item.ListViewItem, 1); BiblioInfo info = item.BiblioInfo; string strXml = ""; { if (string.IsNullOrEmpty(info.NewXml) == false) strXml = info.NewXml; else strXml = info.OldXml; } string strMARC = ""; string strMarcSyntax = ""; // 将XML格式转换为MARC格式 // 自动从数据记录中获得MARC语法 nRet = MarcUtil.Xml2Marc(strXml, true, null, out strMarcSyntax, out strMARC, out strError); if (nRet == -1) { strError = "XML转换到MARC记录时出错: " + strError; return -1; } MarcRecord record = new MarcRecord(strMARC); MarcNodeList fields = record.select("field[@name='856']"); if (fields.count == 0) goto CONTINUE; int index = 0; foreach (MarcField field in fields) { ListViewItem new_item = form.AddLine(info.RecPath, info, field, index); new_items.Add(new_item); index++; } CONTINUE: stop.SetProgressValue(++i); } nRet = form.FillBiblioSummaryColumn( channel, new_items, 0, true, true, out strError); if (nRet == -1) return -1; return 0; } catch (Exception ex) { strError = "导出 856 字段的过程出现异常: " + ExceptionUtil.GetAutoText(ex); return -1; } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); this.ReturnChannel(channel); this.EnableControls(true); } }
void menu_quickFilterRecords_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选择要快速执行脚本的事项"; goto ERROR1; } OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "请指定 MARC 过滤器脚本文件"; dlg.FileName = this.m_strUsedMarcFilterFilename; dlg.Filter = "MARC过滤器脚本文件 (*.fltx)|*.fltx|All files (*.*)|*.*"; dlg.RestoreDirectory = true; if (dlg.ShowDialog() != DialogResult.OK) return; this.m_strUsedMarcFilterFilename = dlg.FileName; ColumnFilterDocument filter = new ColumnFilterDocument(); nRet = PrepareMarcFilter( this, this.MainForm.DataDir, this.m_strUsedMarcFilterFilename, filter, out strError); if (nRet == -1) goto ERROR1; LibraryChannel channel = this.GetChannel(); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在针对书目记录执行 .fltx 脚本 ..."); stop.BeginLoop(); this.EnableControls(false); this.listView_records.Enabled = false; try { if (stop != null) stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewBiblioLoader loader = new ListViewBiblioLoader(channel, // this.Channel, stop, items, this.m_biblioTable); loader.Prompt -= new MessagePromptEventHandler(loader_Prompt); loader.Prompt += new MessagePromptEventHandler(loader_Prompt); int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } #if NO string strRecPath = item.Text; if (string.IsNullOrEmpty(strRecPath) == true) continue; if (stop != null) { stop.SetMessage("正在获取书目记录 " + strRecPath); stop.SetProgressValue(i); } string[] results = null; byte[] baTimestamp = null; // 获得书目记录 long lRet = Channel.GetBiblioInfos( stop, strRecPath, "", new string[] { "xml" }, // formats out results, out baTimestamp, out strError); if (lRet == 0) goto ERROR1; if (lRet == -1) goto ERROR1; if (results == null || results.Length == 0) { strError = "results error"; goto ERROR1; } string strXml = results[0]; #endif BiblioInfo info = item.BiblioInfo; string strXml = ""; { if (string.IsNullOrEmpty(info.NewXml) == false) strXml = info.NewXml; else strXml = info.OldXml; } string strMARC = ""; string strMarcSyntax = ""; // 将XML格式转换为MARC格式 // 自动从数据记录中获得MARC语法 nRet = MarcUtil.Xml2Marc(strXml, true, null, out strMarcSyntax, out strMARC, out strError); if (nRet == -1) { strError = "XML转换到MARC记录时出错: " + strError; goto ERROR1; } filter.Host = new ColumnFilterHost(); filter.Host.ColumnTable = new System.Collections.Hashtable(); nRet = filter.DoRecord( null, strMARC, strMarcSyntax, i, out strError); if (nRet == -1) goto ERROR1; this.MainForm.OperHistory.AppendHtml("<p>" + HttpUtility.HtmlEncode(item.BiblioInfo.RecPath) + "</p>"); // strRecPath foreach (string key in filter.Host.ColumnTable.Keys) { string strHtml = "<p>" + HttpUtility.HtmlEncode(key + "=" + (string)filter.Host.ColumnTable[key]) + "</p>"; this.MainForm.OperHistory.AppendHtml(strHtml); } #if NO if (nRet == -1) { DialogResult result = MessageBox.Show(this, "刷新浏览内容时出错: " + strError + "。\r\n\r\n是否继续刷新? (OK 刷新;Cancel 放弃刷新)", "BiblioSearchForm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == System.Windows.Forms.DialogResult.Cancel) break; } #endif i++; } } finally { this.listView_records.Enabled = true; stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.ReturnChannel(channel); this.EnableControls(true); } return; ERROR1: MessageBox.Show(this, strError); }
void menu_quickMarcQueryRecords_Click(object sender, EventArgs e) { string strError = ""; int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选择要执行 MarcQuery 脚本的事项"; goto ERROR1; } // 书目信息缓存 // 如果已经初始化,则保持 if (this.m_biblioTable == null) this.m_biblioTable = new Hashtable(); // this.m_biblioTable.Clear(); OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "请指定 MarcQuery 脚本文件"; dlg.FileName = this.m_strUsedMarcQueryFilename; dlg.Filter = "MarcQuery 脚本文件 (*.cs)|*.cs|All files (*.*)|*.*"; dlg.RestoreDirectory = true; if (dlg.ShowDialog() != DialogResult.OK) return; this.m_strUsedMarcQueryFilename = dlg.FileName; MarcQueryHost host = null; Assembly assembly = null; nRet = PrepareMarcQuery(this.m_strUsedMarcQueryFilename, out assembly, out host, out strError); if (nRet == -1) goto ERROR1; host.CodeFileName = this.m_strUsedMarcQueryFilename; { host.MainForm = this.MainForm; host.UiForm = this; host.RecordPath = ""; host.MarcRecord = null; host.MarcSyntax = ""; host.Changed = false; host.UiItem = null; StatisEventArgs args = new StatisEventArgs(); host.OnInitial(this, args); if (args.Continue == ContinueType.SkipAll) return; if (args.Continue == ContinueType.Error) { strError = args.ParamString; goto ERROR1; } } this.MainForm.OperHistory.AppendHtml("<div class='debug begin'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 开始执行脚本 " + dlg.FileName + "</div>"); LibraryChannel channel = this.GetChannel(); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在针对书目记录执行 MarcQuery 脚本 ..."); stop.BeginLoop(); this.EnableControls(false); this.listView_records.Enabled = false; try { if (stop != null) stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); { host.MainForm = this.MainForm; host.RecordPath = ""; host.MarcRecord = null; host.MarcSyntax = ""; host.Changed = false; host.UiItem = null; StatisEventArgs args = new StatisEventArgs(); host.OnBegin(this, args); if (args.Continue == ContinueType.SkipAll) return; if (args.Continue == ContinueType.Error) { strError = args.ParamString; goto ERROR1; } } List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } bool bOldSource = true; // 是否要从 OldXml 开始做起 int nChangeCount = this.GetItemsChangeCount(items); if (nChangeCount > 0) { bool bHideMessageBox = true; DialogResult result = MessageDialog.Show(this, "当前选定的 " + items.Count.ToString() + " 个事项中有 " + nChangeCount + " 项修改尚未保存。\r\n\r\n请问如何进行修改? \r\n\r\n(重新修改) 重新进行修改,忽略以前内存中的修改; \r\n(继续修改) 以上次的修改为基础继续修改; \r\n(放弃) 放弃整个操作", MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button1, null, ref bHideMessageBox, new string[] { "重新修改", "继续修改", "放弃" }); if (result == DialogResult.Cancel) { // strError = "放弃"; return; } if (result == DialogResult.No) { bOldSource = false; } } ListViewBiblioLoader loader = new ListViewBiblioLoader(channel, // this.Channel, stop, items, this.m_biblioTable); loader.Prompt -= new MessagePromptEventHandler(loader_Prompt); loader.Prompt += new MessagePromptEventHandler(loader_Prompt); int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; goto ERROR1; } stop.SetProgressValue(i); BiblioInfo info = item.BiblioInfo; string strXml = ""; if (bOldSource == true) { strXml = info.OldXml; // 放弃上一次的修改 if (string.IsNullOrEmpty(info.NewXml) == false) { info.NewXml = ""; this.m_nChangedCount--; } } else { if (string.IsNullOrEmpty(info.NewXml) == false) strXml = info.NewXml; else strXml = info.OldXml; } string strMARC = ""; string strMarcSyntax = ""; // 将XML格式转换为MARC格式 // 自动从数据记录中获得MARC语法 nRet = MarcUtil.Xml2Marc(strXml, // info.OldXml, true, null, out strMarcSyntax, out strMARC, out strError); if (nRet == -1) { strError = "XML转换到MARC记录时出错: " + strError; goto ERROR1; } this.MainForm.OperHistory.AppendHtml("<div class='debug recpath'>" + HttpUtility.HtmlEncode(info.RecPath) + "</div>"); host.MainForm = this.MainForm; host.RecordPath = info.RecPath; host.MarcRecord = new MarcRecord(strMARC); host.MarcSyntax = strMarcSyntax; host.Changed = false; host.UiItem = item.ListViewItem; StatisEventArgs args = new StatisEventArgs(); host.OnRecord(this, args); if (args.Continue == ContinueType.SkipAll) break; if (args.Continue == ContinueType.Error) { strError = args.ParamString; goto ERROR1; } if (host.Changed == true) { strXml = info.OldXml; nRet = MarcUtil.Marc2XmlEx(host.MarcRecord.Text, strMarcSyntax, ref strXml, out strError); if (nRet == -1) goto ERROR1; if (info != null) { if (string.IsNullOrEmpty(info.NewXml) == true) this.m_nChangedCount++; info.NewXml = strXml; } item.ListViewItem.BackColor = SystemColors.Info; item.ListViewItem.ForeColor = SystemColors.InfoText; } // 显示为工作单形式 i++; } { host.MainForm = this.MainForm; host.RecordPath = ""; host.MarcRecord = null; host.MarcSyntax = ""; host.Changed = false; host.UiItem = null; StatisEventArgs args = new StatisEventArgs(); host.OnEnd(this, args); if (args.Continue == ContinueType.Error) { strError = args.ParamString; goto ERROR1; } } } catch (Exception ex) { strError = "执行 MarcQuery 脚本的过程中出现异常: " + ExceptionUtil.GetDebugText(ex); goto ERROR1; } finally { if (host != null) host.FreeResources(); this.listView_records.Enabled = true; stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.ReturnChannel(channel); this.EnableControls(true); this.MainForm.OperHistory.AppendHtml("<div class='debug end'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 结束执行脚本 " + dlg.FileName + "</div>"); } RefreshPropertyView(false); return; ERROR1: MessageBox.Show(this, strError); }
int VerifyBiblioRecord(out string strError) { strError = ""; int nRet = 0; if (this.listView_records.SelectedItems.Count == 0) { strError = "尚未选定要进行批处理的事项"; return -1; } if (stop != null && stop.State == 0) // 0 表示正在处理 { strError = "目前有长操作正在进行,无法进行校验书目记录的操作"; return -1; } // 切换到“操作历史”属性页 this.MainForm.ActivateFixPage("history"); int nCount = 0; this.MainForm.OperHistory.AppendHtml("<div class='debug begin'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 开始进行书目记录校验</div>"); LibraryChannel channel = this.GetChannel(); stop.Style = StopStyle.EnableHalfStop; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在进行校验书目记录的操作 ..."); stop.BeginLoop(); this.EnableControls(false); try { stop.SetProgressRange(0, this.listView_records.SelectedItems.Count); List<ListViewItem> items = new List<ListViewItem>(); foreach (ListViewItem item in this.listView_records.SelectedItems) { if (string.IsNullOrEmpty(item.Text) == true) continue; items.Add(item); } ListViewBiblioLoader loader = new ListViewBiblioLoader(channel, // this.Channel, stop, items, this.m_biblioTable); loader.Prompt -= new MessagePromptEventHandler(loader_Prompt); loader.Prompt += new MessagePromptEventHandler(loader_Prompt); int i = 0; foreach (LoaderItem item in loader) { Application.DoEvents(); // 出让界面控制权 if (stop != null && stop.State != 0) { strError = "用户中断"; return -1; } BiblioInfo info = item.BiblioInfo; XmlDocument itemdom = new XmlDocument(); try { itemdom.LoadXml(info.OldXml); } catch (Exception ex) { strError = "记录 '" + info.RecPath + "' 的 XML 装入 DOM 时出错: " + ex.Message; return -1; } List<string> errors = new List<string>(); // 校验 XML 记录中是否有非法字符 string strReplaced = DomUtil.ReplaceControlCharsButCrLf(info.OldXml, '*'); if (strReplaced != info.OldXml) { errors.Add("XML 记录中有非法字符"); } if (errors.Count > 0) { this.MainForm.OperHistory.AppendHtml("<div class='debug recpath'>" + HttpUtility.HtmlEncode(info.RecPath) + "</div>"); foreach (string error in errors) { this.MainForm.OperHistory.AppendHtml("<div class='debug error'>" + HttpUtility.HtmlEncode(error) + "</div>"); } { item.ListViewItem.BackColor = Color.FromArgb(155, 0, 0); item.ListViewItem.ForeColor = Color.FromArgb(255, 255, 255); } } nCount++; stop.SetProgressValue(++i); } return nCount; } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); stop.Style = StopStyle.None; this.ReturnChannel(channel); this.EnableControls(true); this.MainForm.OperHistory.AppendHtml("<div class='debug end'>" + HttpUtility.HtmlEncode(DateTime.Now.ToLongTimeString()) + " 结束执行书目记录校验</div>"); } }