示例#1
0
文件: MainForm.cs 项目: renyh1013/dp2
        // 上载一个item
        // parameter:
        //		strError: error info
        // return:
        //		-1	出错
        //		0	正常
        //		1	结束
        public int DoXmlItemUpload(
            bool bFastMode,
            string strXml,
            DbNameMap map,
            bool bSkip,
            string strCount,
            out string strError)
        {
            strError = "";
            int nRet = 0;
            // bool bRet = false;

            // MessageBox.Show(this, strXml);

            if (bSkip == true)
                return 0;

            XmlDocument dataDom = new XmlDocument();
            try
            {
                dataDom.LoadXml(strXml);
            }
            catch (Exception ex)
            {
                strError = "加载数据到dom出错!\r\n" + ex.Message;
                goto ERROR1;
            }

            XmlNode node = dataDom.DocumentElement;

            string strResPath = DomUtil.GetAttr(DpNs.dprms, node, "path");

            string strTargetPath = "";

            string strSourceDbPath = "";

            if (strResPath != "")
            {
                // 从map中查询覆盖还是追加?
                ResPath respath0 = new ResPath(strResPath);
                respath0.MakeDbName();
                strSourceDbPath = respath0.FullPath;
            }

        REDO:

            DbNameMapItem mapItem = null;


            mapItem = map.MatchItem(strSourceDbPath/*strResPath*/);
            if (mapItem != null)
                goto MAPITEMOK;

            if (mapItem == null)
            {

                if (strSourceDbPath/*strResPath*/ == "")
                {
                    string strText = "源数据文件中记录 " + Convert.ToString(this.m_nRecordCount) + " 没有来源数据库,对所有这样的数据,将作如何处理?";
                    WriteLog("打开对话框 '" + strText.Replace("\r\n", "\\n") + "'");
                    nRet = DbNameMapItemDlg.AskNullOriginBox(
                        this,
                        this.AppInfo,
                        strText,
                        this.SearchPanel,
                        map);
                    WriteLog("关闭对话框 '" + strText.Replace("\r\n", "\\n") + "'");

                    if (nRet == 0)
                    {
                        strError = "用户中断";
                        goto ERROR1;	// 中断整个处理
                    }

                    goto REDO;

                }
                else
                {
                    string strText = "源数据文件中记录 " + Convert.ToString(this.m_nRecordCount) + " 的来源数据库 '" + strSourceDbPath/*strResPath*/ + "' 没有找到对应的目标库, 对所有这样的数据,将作如何处理?";
                    WriteLog("打开对话框 '" + strText.Replace("\r\n", "\\n") + "'");
                    nRet = DbNameMapItemDlg.AskNotMatchOriginBox(
                        this,
                        this.AppInfo,
                        strText,
                        this.SearchPanel,
                        strSourceDbPath/*strResPath*/,
                        map);
                    WriteLog("关闭对话框 '" + strText.Replace("\r\n", "\\n") + "'");
                    if (nRet == 0)
                    {
                        strError = "用户中断";
                        goto ERROR1;	// 中断整个处理
                    }

                    goto REDO;
                }
            }

        MAPITEMOK:

            if (mapItem.Style == "skip")
                return 0;

            // 构造目标路径

            // 1)从源路径中提取id。源路径来自备份文件数据
            ResPath respath = new ResPath(strResPath);
            string strID = respath.GetRecordId();

            if (strID == null || strID == ""
                || (mapItem.Style == "append")
                )
            {
                strID = "?";	// 将来加一个对话框
            }

            // 2)用目标库路径构造完整的记录路径
            string strTargetFullPath = "";
            if (mapItem.Target == "*")
            {
                // 此时target为*, 需要从strResPath中获得库名

                if (strResPath == "")
                {
                    Debug.Assert(false, "不可能出现的情况");
                }

                respath = new ResPath(strResPath);
                respath.MakeDbName();
                strTargetFullPath = respath.FullPath;
            }
            else
            {
                strTargetFullPath = mapItem.Target;
            }

            respath = new ResPath(strTargetFullPath);


            // 需要检查目标库所允许的MARC格式
            if (CheckTargetDb != null)
            {
                CheckTargetDbEventArgs e = new CheckTargetDbEventArgs();
                e.DbFullPath = strTargetFullPath;
                e.CurrentMarcSyntax = this.CurMarcSyntax;
                this.CheckTargetDb(this, e);
                if (e.Cancel == true)
                {
                    if (e.ErrorInfo == "")
                        strError = "CheckTargetDb 事件导致中断";
                    else
                        strError = e.ErrorInfo;
                    return -1;
                }

            }


            strTargetPath = respath.Path + "/" + strID;
            // strRecordPath = strTargetPath;

            channel = this.Channels.GetChannel(respath.Url);

            string strTimeStamp = DomUtil.GetAttr(DpNs.dprms, node, "timestamp");

            byte[] timestamp = ByteArray.GetTimeStampByteArray(strTimeStamp);

            // 2012/5/29
            string strOutMarcSyntax = "";
            string strMARC = "";
            // 将MARCXML格式的xml记录转换为marc机内格式字符串
            // parameters:
            //		bWarning	==true, 警告后继续转换,不严格对待错误; = false, 非常严格对待错误,遇到错误后不继续转换
            //		strMarcSyntax	指示marc语法,如果=="",则自动识别
            //		strOutMarcSyntax	out参数,返回marc,如果strMarcSyntax == "",返回找到marc语法,否则返回与输入参数strMarcSyntax相同的值
            nRet = MarcUtil.Xml2Marc(strXml,
                false,
                "",
                out strOutMarcSyntax,
                out strMARC,
                out strError);
            /*
            if (nRet == -1)
                return -1;
             * */

            // 2012/5/30
            if (batchObj != null)
            {
                batchObj.MarcSyntax = strOutMarcSyntax;
                batchObj.MarcRecord = strMARC;
                batchObj.MarcRecordChanged = false;	// 为本轮Script运行准备初始状态
            }


            if (this.MarcFilter != null)
            {
                // 触发filter中的Record相关动作
                nRet = MarcFilter.DoRecord(
                    null,
                    batchObj.MarcRecord,
                    m_nRecordCount,
                    out strError);
                if (nRet == -1)
                    goto ERROR1;
            }

            // C#脚本 -- Inputing
            if (this.AssemblyMain != null)
            {
                // 这些变量要先初始化,因为filter代码可能用到这些Batch成员.
                batchObj.SkipInput = false;
                batchObj.XmlRecord = strXml;

                //batchObj.MarcSyntax = this.CurMarcSyntax;
                //batchObj.MarcRecord = strMarc;	// MARC记录体
                //batchObj.MarcRecordChanged = false;	// 为本轮Script运行准备初始状态


                batchObj.SearchPanel.ServerUrl = channel.Url;
                batchObj.ServerUrl = channel.Url;
                batchObj.RecPath = strTargetPath;	// 记录路径
                batchObj.RecIndex = m_nRecordCount;	// 当前记录在一批中的序号
                batchObj.TimeStamp = timestamp;


                BatchEventArgs args = new BatchEventArgs();

                batchObj.Inputing(this, args);
                if (args.Continue == ContinueType.SkipAll)
                {
                    strError = "脚本中断SkipAll";
                    goto END2;
                }

                if (batchObj.SkipInput == true)
                    return 0;	// 继续处理后面的
            }


            string strWarning = "";
            byte[] output_timestamp = null;
            string strOutputPath = "";

        REDOSAVE:
            if (stop != null)
            {
                if (strTargetPath.IndexOf("?") == -1)
                {
                    stop.SetMessage("正在上载 "
                        + strTargetPath + strWarning + " " + strCount);
                }
            }


            // 保存Xml记录
            long lRet = channel.DoSaveTextRes(strTargetPath,
                strXml,
                false,	// bIncludePreamble
                    bFastMode == true ? "fastmode" : "",//strStyle,
                timestamp,
                out output_timestamp,
                out strOutputPath,
                out strError);

            if (lRet == -1)
            {
                if (stop != null)
                    stop.Continue();

                if (channel.ErrorCode == ChannelErrorCode.TimestampMismatch)
                {
                    string strDisplayRecPath = strOutputPath;
                    if (string.IsNullOrEmpty(strDisplayRecPath) == true)
                        strDisplayRecPath = strTargetPath;

                    if (this.bNotAskTimestampMismatchWhenOverwrite == true)
                    {
                        timestamp = new byte[output_timestamp.Length];
                        Array.Copy(output_timestamp, 0, timestamp, 0, output_timestamp.Length);
                        strWarning = " (时间戳不匹配, 自动重试)";
                        goto REDOSAVE;
                    }

                    string strText = "上载 '" + strDisplayRecPath
                        + " 时发现时间戳不匹配。详细情况如下:\r\n---\r\n"
                        + strError + "\r\n---\r\n\r\n是否以新时间戳强行上载?\r\n注:(是)强行上载 (否)忽略当前记录或资源上载,但继续后面的处理 (取消)中断整个批处理";
                    WriteLog("打开对话框 '" + strText.Replace("\r\n", "\\n") + "'");
                    DialogResult result = MessageDlg.Show(this,
                        strText,
                        "dp2batch",
                        MessageBoxButtons.YesNoCancel,
                        MessageBoxDefaultButton.Button1,
                        ref this.bNotAskTimestampMismatchWhenOverwrite);
                    WriteLog("关闭对话框 '" + strText.Replace("\r\n", "\\n") + "'");
                    if (result == DialogResult.Yes)
                    {
                        timestamp = new byte[output_timestamp.Length];
                        Array.Copy(output_timestamp, 0, timestamp, 0, output_timestamp.Length);
                        strWarning = " (时间戳不匹配, 应用户要求重试)";
                        goto REDOSAVE;
                    }

                    if (result == DialogResult.No)
                    {
                        return 0;	// 继续作后面的资源
                    }

                    if (result == DialogResult.Cancel)
                    {
                        strError = "用户中断";
                        goto ERROR1;	// 中断整个处理
                    }
                }

                // 询问是否重试
                {
                    string strText = "上载 '" + strTargetPath
                        + " 时发生错误。详细情况如下:\r\n---\r\n"
                        + strError + "\r\n---\r\n\r\n是否重试?\r\n注:(是)重试 (否)不重试,但继续后面的处理 (取消)中断整个批处理";
                    WriteLog("打开对话框 '" + strText.Replace("\r\n", "\\n") + "'");

                    DialogResult result1 = MessageBox.Show(this,
                        strText,
                        "dp2batch",
                        MessageBoxButtons.YesNoCancel,
                        MessageBoxIcon.Question,
                        MessageBoxDefaultButton.Button1);
                    WriteLog("关闭对话框 '" + strText.Replace("\r\n", "\\n") + "'");
                    if (result1 == DialogResult.Yes)
                        goto REDOSAVE;
                    if (result1 == DialogResult.No)
                        return 0;	// 继续作后面的资源
                }

                goto ERROR1;
            }

            // C#脚本 -- Inputed()
            if (this.AssemblyMain != null)
            {
                // 大部分变量保留刚才Inputing()时的原样,只修改部分

                batchObj.RecPath = strOutputPath;	// 记录路径
                batchObj.TimeStamp = output_timestamp;

                BatchEventArgs args = new BatchEventArgs();

                batchObj.Inputed(this, args);
                /*
                if (args.Continue == ContinueType.SkipMiddle)
                {
                    strError = "脚本中断SkipMiddle";
                    goto END1;
                }
                if (args.Continue == ContinueType.SkipBeginMiddle)
                {
                    strError = "脚本中断SkipBeginMiddle";
                    goto END1;
                }
                */
                if (args.Continue == ContinueType.SkipAll)
                {
                    strError = "脚本中断SkipAll";
                    goto END1;
                }
            }

            this.m_nRecordCount++;

            if (stop != null)
            {
                stop.SetMessage("已上载成功 '"
                    + strOutputPath + "' " + strCount);
            }


            // strRecordPath = strOutputPath;

            return 0;
        END1:
        END2:

        ERROR1:
            return -1;
        }
示例#2
0
        // 根据原始路径准备即将写入的路径
        // parameters:
        //      strSelectedLongPath 用户选定的默认的目标库长路径。将优先出现在询问对话框的目标中
        // return:
        //      -1  出错
        //      0   用户放弃
        //      1   成功
        //      2   要跳过本条
        public static int PrepareOverwritePath(
            IWin32Window owner,
            ServerCollection Servers,
            RmsChannelCollection Channels,
            ApplicationInfo AppInfo,
            long lIndex,
            string strSelectedLongPath,
            ref DbNameMap map,
            ref string strLongPath,
            out string strError)
        {
            strError = "";
            int nRet = 0;

            ResPath respath = new ResPath(strLongPath);
            respath.MakeDbName();
            string strSourceDbPath = respath.FullPath;

        REDO:

            DbNameMapItem mapItem = null;

            mapItem = map.MatchItem(strSourceDbPath);
            if (mapItem != null)
                goto MAPITEMOK;

            if (mapItem == null)
            {

                if (string.IsNullOrEmpty(strSourceDbPath) == true)
                {
                    string strText = "源数据文件中记录 " + lIndex.ToString() + " 没有来源数据库。\r\n请问对所有这样的数据,将作如何处理?";
                    // WriteLog("打开对话框 '" + strText.Replace("\r\n", "\\n") + "'");
                    nRet = DbNameMapItemDlg.AskNullOriginBox(
                        owner,
                        AppInfo,
                        Servers,
                        Channels,
                        strText,
                        strSelectedLongPath,
                        map);
                    // WriteLog("关闭对话框 '" + strText.Replace("\r\n", "\\n") + "'");

                    if (nRet == 0)
                    {
                        strError = "用户中断";
                        return 0;	// 中断整个处理
                    }

                    goto REDO;

                }
                else
                {
                    string strText = "源数据文件中记录 " + lIndex.ToString() + " 的来源数据库 '" + strSourceDbPath + "' 没有明确的对应规则。\r\n请问对所有这样的数据,将作如何处理?";    // 没有找到对应的目标库
                    // WriteLog("打开对话框 '" + strText.Replace("\r\n", "\\n") + "'");
                    nRet = DbNameMapItemDlg.AskNotMatchOriginBox(
                        owner,
                        AppInfo,
                        Servers,
                        Channels,
                        strText,
                        strSelectedLongPath,
                        strSourceDbPath/*strResPath*/,
                        map);
                    // WriteLog("关闭对话框 '" + strText.Replace("\r\n", "\\n") + "'");
                    if (nRet == 0)
                    {
                        strError = "用户中断";
                        return 0;	// 中断整个处理
                    }

                    goto REDO;
                }
            }

        MAPITEMOK:

            if (mapItem.Style == "skip")
                return 2;

            // 构造目标路径

            // 1)从源路径中提取id。源路径来自备份文件数据
            respath = new ResPath(strLongPath);
            string strID = respath.GetRecordId();

            if (string.IsNullOrEmpty(strID) == true
                || mapItem.Style == "append")
            {
                strID = "?";	// 将来加一个对话框
            }

            // 2)用目标库路径构造完整的记录路径
            string strTargetFullPath = "";
            if (mapItem.Target == "*")
            {
                respath = new ResPath(strLongPath);
                respath.MakeDbName();
                strTargetFullPath = respath.FullPath;
            }
            else
            {
                strTargetFullPath = mapItem.Target;
            }

            respath = new ResPath(strTargetFullPath);
            respath.Path = respath.Path + "/" + strID;
            strLongPath = respath.FullPath;

            return 1;
        }