Пример #1
0
        // return:
        //      0   普通返回
        //      1   要全部中断
        NormalResult DoImport(string strTargetDbName)
        {
            string   strError = "";
            Encoding encoding = null;

            if (string.IsNullOrEmpty(this.InputEncodingName) == true)
            {
                return(new NormalResult(-1, "尚未选定 ISO2709 文件的编码方式"));
            }

            if (StringUtil.IsNumber(this.InputEncodingName) == true)
            {
                encoding = Encoding.GetEncoding(Convert.ToInt32(this.InputEncodingName));
            }
            else
            {
                encoding = Encoding.GetEncoding(this.InputEncodingName);
            }

            ClearErrorInfoForm();

            string strInputFileName = "";

            strInputFileName = this.InputFileName;

            string strBiblioSyntax = Program.MainForm.GetBiblioSyntax(strTargetDbName);

            if (strBiblioSyntax == null)
            {
                strBiblioSyntax = Program.MainForm.GetAuthoritySyntax(strTargetDbName);
                if (strBiblioSyntax == null)
                {
                    strError = "没有找到书目或规范库 '" + strTargetDbName + "' 的 MARC 格式信息";
                    goto ERROR1;
                }
            }

            // 和第一个属性页的 MARC 格式进行对比,如果不符合,要报错
            if (strBiblioSyntax != this.InputMarcSyntax)
            {
                strError = "您在 数据来源 属性页为文件 '" + strInputFileName + "' 选定的 MARC 格式 '" + this.InputMarcSyntax + "' 与数据库 '" + strTargetDbName + "' 的预定义 MARC 格式 '" + strBiblioSyntax + "' 不符合。导入被终止";
                goto ERROR1;
            }

            string strRange = (string)this.Invoke(new Func <string>(() =>
            {
                return(this.textBox_importRange.Text);
            }));
            RangeList range = null;

            if (string.IsNullOrEmpty(strRange) == false)
            {
                range = new RangeList(strRange);
                range.Sort();
            }

            Stream file = null;

            try
            {
                file = File.Open(strInputFileName,
                                 FileMode.Open,
                                 FileAccess.Read);
            }
            catch (Exception ex)
            {
                return(new NormalResult(-1, "打开文件 " + strInputFileName + " 失败: " + ex.Message));
            }

            this.Invoke((Action)(() =>
            {
                this.progressBar_records.Minimum = 0;
                this.progressBar_records.Maximum = (int)file.Length;
                this.progressBar_records.Value = 0;
            }));

            stop.OnStop += new StopEventHandler(this.DoStop);
            stop.Initial("正在获取ISO2709记录 ...");
            stop.BeginLoop();

            bool dont_display_dialog = false;

            LibraryChannel channel = this.GetChannel();

            EnableControls(false);

            try
            {
                int nCount = 0;

                DialogResult retry_result = DialogResult.Yes;

                for (int i = 0; ; i++)
                {
                    Application.DoEvents(); // 出让界面控制权

                    if (stop != null && stop.State != 0)
                    {
                        DialogResult result = MessageBox.Show(this,
                                                              "准备中断。\r\n\r\n确实要中断全部操作? (Yes 全部中断;No 中断循环,但是继续收尾处理;Cancel 放弃中断,继续操作)",
                                                              "ImportMarcForm",
                                                              MessageBoxButtons.YesNoCancel,
                                                              MessageBoxIcon.Question,
                                                              MessageBoxDefaultButton.Button3);

                        if (result == DialogResult.Yes)
                        {
                            strError = "用户中断";
                            goto ERROR1;
                        }
                        if (result == DialogResult.No)
                        {
                            return(new NormalResult()); // 假装loop正常结束
                        }
                        stop.Continue();                // 继续循环
                    }


                    // 从ISO2709文件中读入一条MARC记录
                    // return:
                    //	-2	MARC格式错
                    //	-1	出错
                    //	0	正确
                    //	1	结束(当前返回的记录有效)
                    //	2	结束(当前返回的记录无效)
                    int nRet = MarcUtil.ReadMarcRecord(file,
                                                       encoding,
                                                       true, // bRemoveEndCrLf,
                                                       true, // bForce,
                                                       out string strMARC,
                                                       out strError);
                    if (nRet == -2 || nRet == -1)
                    {
                        DialogResult result = MessageBox.Show(this,
                                                              "读入MARC记录(" + nCount.ToString() + ")出错: " + strError + "\r\n\r\n确实要中断当前批处理操作?",
                                                              "ImportMarcForm",
                                                              MessageBoxButtons.YesNo,
                                                              MessageBoxIcon.Question,
                                                              MessageBoxDefaultButton.Button2);
                        if (result == DialogResult.Yes)
                        {
                            break;
                        }
                        else
                        {
                            strError = "读入MARC记录(" + nCount.ToString() + ")出错: " + strError;
                            GetErrorInfoForm().WriteHtml(strError + "\r\n");
                            continue;
                        }
                    }

                    if (nRet != 0 && nRet != 1)
                    {
                        return(new NormalResult());   // 结束
                    }
                    this.Invoke((Action)(() =>
                    {
                        this.progressBar_records.Value = (int)file.Position;
                    }));

                    if (range != null && range.IsInRange(i, true) == false)
                    {
                        stop.SetMessage("跳过第 " + (i + 1).ToString() + " 个 ISO2709 记录");
                        continue;
                    }
                    else
                    {
                        stop.SetMessage("正在获取第 " + (i + 1).ToString() + " 个 ISO2709 记录");
                    }

                    // 跳过太短的记录
                    if (string.IsNullOrEmpty(strMARC) == true ||
                        strMARC.Length <= 24)
                    {
                        continue;
                    }

                    if (this._openMarcFileDialog.Mode880 == true &&
                        (this._openMarcFileDialog.MarcSyntax == "usmarc" || this._openMarcFileDialog.MarcSyntax == "<自动>"))
                    {
                        MarcRecord temp = new MarcRecord(strMARC);
                        MarcQuery.ToParallel(temp);
                        strMARC = temp.Text;
                    }

                    // 处理
                    string strBiblioRecPath = strTargetDbName + "/?";

                    nRet = MarcUtil.Marc2Xml(strMARC,
                                             strBiblioSyntax,
                                             out XmlDocument domMarc,
                                             out strError);
                    if (nRet == -1)
                    {
                        goto ERROR1;
                    }
REDO:
                    long lRet = channel.SetBiblioInfo(
                        stop,
                        "new",
                        strBiblioRecPath,
                        "xml",
                        domMarc.DocumentElement.OuterXml,
                        null, // timestamp
                        "",
                        out string strOutputBiblioRecPath,
                        out byte[] baNewTimestamp,
                        out strError);
                    if (lRet == -1)
                    {
#if NO
                        strError = "创建书目记录 '" + strBiblioRecPath + "' 时出错: " + strError + "\r\n";
                        goto ERROR1;
#endif

                        // string strText = strError;
                        if (dont_display_dialog == false)
                        {
                            retry_result = (DialogResult)this.Invoke((Func <DialogResult>)(() =>
                            {
                                // return AutoCloseMessageBox.Show(this, strText + "\r\n\r\n(点右上角关闭按钮可以中断批处理)", 5000);
                                return(MessageDlg.Show(this,
                                                       strError + ", 是否重试?\r\n---\r\n\r\n[重试]重试; [跳过]跳过本条继续后面批处理; [中断]中断批处理",
                                                       "ImportMarcForm",
                                                       MessageBoxButtons.YesNoCancel,
                                                       MessageBoxDefaultButton.Button1,
                                                       ref dont_display_dialog,
                                                       new string[] { "重试", "跳过", "中断" },
                                                       "后面不再出现此对话框,按本次选择自动处理"));
                            }));
                        }

                        if (retry_result == System.Windows.Forms.DialogResult.Cancel)
                        {
                            goto ERROR1;
                        }
                        if (retry_result == DialogResult.Yes)
                        {
                            goto REDO;
                        }

                        // 在操作历史中显示出错信息
                    }

                    nCount++;
                }

                return(new NormalResult());
            }
            finally
            {
                EnableControls(true);

                this.ReturnChannel(channel);

                stop.EndLoop();
                stop.OnStop -= new StopEventHandler(this.DoStop);
                stop.Initial("");

                if (file != null)
                {
                    file.Close();
                }
            }
ERROR1:
            return(new NormalResult(-1, strError));
        }
Пример #2
0
        // 注意:上级函数RunScript()已经使用了BeginLoop()和EnableControls()
        // 对每个Iso2709Statis记录进行循环
        // return:
        //      0   普通返回
        //      1   要全部中断
        int DoLoop(out string strError)
        {
            strError = "";
            // int nRet = 0;
            // long lRet = 0;
            Encoding encoding = null;

            if (string.IsNullOrEmpty(this._openMarcFileDialog.EncodingName) == true)
            {
                strError = "尚未选定 ISO2709 文件的编码方式";
                return(-1);
            }

            if (StringUtil.IsNumber(this._openMarcFileDialog.EncodingName) == true)
            {
                encoding = Encoding.GetEncoding(Convert.ToInt32(this._openMarcFileDialog.EncodingName));
            }
            else
            {
                encoding = Encoding.GetEncoding(this._openMarcFileDialog.EncodingName);
            }

#if NO
            // 清除错误信息窗口中残余的内容
            if (this.ErrorInfoForm != null)
            {
                try
                {
                    this.ErrorInfoForm.HtmlString = "<pre>";
                }
                catch
                {
                }
            }
#endif
            ClearErrorInfoForm();

            string strInputFileName = "";

            try
            {
                strInputFileName = this._openMarcFileDialog.FileName;

                Stream file = null;

                try
                {
                    file = File.Open(strInputFileName,
                                     FileMode.Open,
                                     FileAccess.Read);
                }
                catch (Exception ex)
                {
                    strError = "打开文件 " + strInputFileName + " 失败: " + ex.Message;
                    return(-1);
                }

                this.progressBar_records.Minimum = 0;
                this.progressBar_records.Maximum = (int)file.Length;
                this.progressBar_records.Value   = 0;

                /*
                 * stop.OnStop += new StopEventHandler(this.DoStop);
                 * stop.Initial("正在获取ISO2709记录 ...");
                 * stop.BeginLoop();
                 *
                 * EnableControls(false);
                 * */

                try
                {
                    int nCount = 0;

                    for (int i = 0; ; i++)
                    {
                        Application.DoEvents(); // 出让界面控制权

                        if (stop != null)
                        {
                            if (stop.State != 0)
                            {
                                DialogResult result = MessageBox.Show(this,
                                                                      "准备中断。\r\n\r\n确实要中断全部操作? (Yes 全部中断;No 中断循环,但是继续收尾处理;Cancel 放弃中断,继续操作)",
                                                                      "Iso2709StatisForm",
                                                                      MessageBoxButtons.YesNoCancel,
                                                                      MessageBoxIcon.Question,
                                                                      MessageBoxDefaultButton.Button3);

                                if (result == DialogResult.Yes)
                                {
                                    strError = "用户中断";
                                    return(-1);
                                }
                                if (result == DialogResult.No)
                                {
                                    return(0);   // 假装loop正常结束
                                }
                                stop.Continue(); // 继续循环
                            }
                        }

                        // 从ISO2709文件中读入一条MARC记录
                        // return:
                        //	-2	MARC格式错
                        //	-1	出错
                        //	0	正确
                        //	1	结束(当前返回的记录有效)
                        //	2	结束(当前返回的记录无效)
                        int nRet = MarcUtil.ReadMarcRecord(file,
                                                           encoding,
                                                           true, // bRemoveEndCrLf,
                                                           true, // bForce,
                                                           out string strMARC,
                                                           out strError);
                        if (nRet == -2 || nRet == -1)
                        {
                            DialogResult result = MessageBox.Show(this,
                                                                  "读入MARC记录(" + nCount.ToString() + ")出错: " + strError + "\r\n\r\n确实要中断当前批处理操作?",
                                                                  "Iso2709StatisForm",
                                                                  MessageBoxButtons.YesNo,
                                                                  MessageBoxIcon.Question,
                                                                  MessageBoxDefaultButton.Button2);
                            if (result == DialogResult.Yes)
                            {
                                break;
                            }
                            else
                            {
                                strError = "读入MARC记录(" + nCount.ToString() + ")出错: " + strError;
                                GetErrorInfoForm().WriteHtml(strError + "\r\n");
                                continue;
                            }
                        }

                        if (nRet != 0 && nRet != 1)
                        {
                            return(0);   // 结束
                        }
                        stop.SetMessage("正在获取第 " + (i + 1).ToString() + " 个 ISO2709 记录");
                        this.progressBar_records.Value = (int)file.Position;

                        // 跳过太短的记录
                        if (string.IsNullOrEmpty(strMARC) == true ||
                            strMARC.Length <= 24)
                        {
                            continue;
                        }

                        if (this._openMarcFileDialog.Mode880 == true &&
                            (this._openMarcFileDialog.MarcSyntax == "usmarc" || this._openMarcFileDialog.MarcSyntax == "<自动>"))
                        {
                            MarcRecord temp = new MarcRecord(strMARC);
                            MarcQuery.ToParallel(temp);
                            strMARC = temp.Text;
                        }

                        // 触发Script中OnRecord()代码
                        if (objStatis != null)
                        {
                            objStatis.MARC = strMARC;
                            objStatis.CurrentRecordIndex = i;

                            StatisEventArgs args = new StatisEventArgs();
                            objStatis.OnRecord(this, args);
                            if (args.Continue == ContinueType.SkipAll)
                            {
                                return(1);
                            }
                            if (args.Continue == ContinueType.Error)
                            {
                                strError = args.ParamString;
                                return(-1);
                            }
                        }

                        nCount++;
                    }

                    /*
                     * Global.WriteHtml(this.webBrowser_batchAddItemPrice,
                     *  "处理结束。共增补价格字符串 " + nCount.ToString() + " 个。\r\n");
                     * */

                    return(0);
                }
                finally
                {
                    /*
                     * EnableControls(true);
                     *
                     * stop.EndLoop();
                     * stop.OnStop -= new StopEventHandler(this.DoStop);
                     * stop.Initial("");
                     * */

                    if (file != null)
                    {
                        file.Close();
                    }
                }
            }
            finally
            {
            }

            // return 0;
        }
Пример #3
0
        private void button_worToIso_convert_Click(object sender, EventArgs e)
        {
            string strError = "";
            int    nRet     = 0;

            if (string.IsNullOrEmpty(this.textBox_worToIso_worFilename.Text) == true)
            {
                strError = "尚未指定工作单文件名";
                goto ERROR1;
            }

            Encoding encoding = MarcUtil.GetEncoding(this.comboBox_worToIso_encoding.Text);

            if (encoding == null)
            {
                encoding = Encoding.GetEncoding(936);
            }

            Encoding preferredEncoding = Encoding.UTF8;

            OpenMarcFileDlg dlg = new OpenMarcFileDlg();

            GuiUtil.SetControlFont(dlg, this.Font);
            dlg.Text                  = "请指定目标 ISO2709 文件名";
            dlg.IsOutput              = true;
            dlg.FileName              = "";
            dlg.CrLf                  = false;
            dlg.AddG01Visible         = false;
            dlg.RemoveField998Visible = false;
            //dlg.RemoveField998 = m_mainForm.LastRemoveField998;
            dlg.EncodingListItems = Global.GetEncodingList(false);
            dlg.MarcSyntax        = "<自动>"; // strPreferedMarcSyntax;
            dlg.EnableMarcSyntax  = false;

            this.MainForm.AppInfo.LinkFormState(dlg, "OpenMarcFileDlg_forOutput_state");
            dlg.ShowDialog(this);
            if (dlg.DialogResult != DialogResult.OK)
            {
                return;
            }

            Encoding targetEncoding = null;

            nRet = Global.GetEncoding(dlg.EncodingName,
                                      out targetEncoding,
                                      out strError);
            if (nRet == -1)
            {
                goto ERROR1;
            }

            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注:(是)追加  (否)覆盖  (取消)放弃",
                                                      "UtilityForm",
                                                      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;
                }
            }


            EnableControls(false);

            stop.OnStop += new StopEventHandler(this.DoStop);
            stop.Initial("正在转换文件格式 ...");
            stop.BeginLoop();

            try
            {
                using (TextReader reader = new StreamReader(this.textBox_worToIso_worFilename.Text, encoding))
                    using (Stream target = File.Open(dlg.FileName,
                                                     FileMode.OpenOrCreate))
                    {
                        if (bAppend == false)
                        {
                            target.SetLength(0);
                        }
                        else
                        {
                            target.Seek(0, SeekOrigin.End);
                        }

                        for (int i = 0; ; i++)
                        {
                            stop.SetMessage("正在转换 " + (i + 1).ToString());

                            Application.DoEvents(); // 出让界面控制权

                            if (stop != null && stop.State != 0)
                            {
                                strError = "用户中断";
                                goto ERROR1;
                            }
                            string strMARC = "";
                            // return:
                            //	-2	MARC格式错
                            //	-1	出错
                            //	0	正确
                            //	1	结束(当前返回的记录有效)
                            //	2	结束(当前返回的记录无效)
                            nRet = MarcUtil.ReadWorksheetRecord(reader,
                                                                out strMARC,
                                                                out strError);
                            if (nRet == -1 || nRet == -2)
                            {
                                goto ERROR1;
                            }
                            if (nRet == 2)
                            {
                                break;
                            }

                            if (dlg.Mode880 == true && dlg.MarcSyntax == "usmarc")
                            {
                                MarcRecord record = new MarcRecord(strMARC);
                                MarcQuery.To880(record);
                                strMARC = record.Text;
                            }

                            byte[] baTarget = null;

                            // 将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,
                                dlg.MarcSyntax,
                                targetEncoding,
                                out baTarget,
                                out strError);
                            if (nRet == -1)
                            {
                                goto ERROR1;
                            }

                            target.Write(baTarget, 0,
                                         baTarget.Length);

                            if (dlg.CrLf == true)
                            {
                                byte[] baCrLf = targetEncoding.GetBytes("\r\n");
                                target.Write(baCrLf, 0,
                                             baCrLf.Length);
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                strError = "转换过程出现异常: " + ex.Message;
                goto ERROR1;
            }
            finally
            {
                stop.EndLoop();
                stop.OnStop -= new StopEventHandler(this.DoStop);
                stop.Initial("");

                EnableControls(true);
            }
            MessageBox.Show(this, "转换完成。记录已写入文件 " + dlg.FileName + " 中");
            return;

ERROR1:
            MessageBox.Show(this, strError);
        }