// 2017/3/8 // 判断路径最后一级是否为问号或者空,也就是追加的方式 public static bool IsAppendRecPath(string strBiblioRecPath) { if (string.IsNullOrEmpty(strBiblioRecPath)) { return(false); } string strTargetRecId = ResPath.GetRecordId(strBiblioRecPath); if (strTargetRecId == "?" || String.IsNullOrEmpty(strTargetRecId) == true) { return(true); } return(false); }
// 2017/3/8 // 规范追加形态的路径为 "中文图书/?" public static bool CannoicalizeAppendRecPath(ref string strBiblioRecPath) { if (string.IsNullOrEmpty(strBiblioRecPath)) { return(false); } string strTargetRecId = ResPath.GetRecordId(strBiblioRecPath); if (strTargetRecId == "?") { return(false); } if (String.IsNullOrEmpty(strTargetRecId) == true) { strBiblioRecPath = ResPath.GetDbName(strBiblioRecPath) + "/?"; return(true); } return(false); }
// 上载一个res // parameter: // inputfile: 源流 // bIsFirstRes: 是否是第一个资源(xml) // strError: error info // return: // -2 片断中发现时间戳不匹配。本函数调主可重上载整个资源 // -1 error // 0 successed public int DoResUpload( ref RmsChannel channel, ref string strRecordPath, Stream inputfile, ref DbNameMap map, bool bIsFirstRes, string strCount, out string strError) { strError = ""; int nRet; long lBodyStart = 0; long lBodyLength = 0; // 1. 从输入流中得到strMetadata,与body(body放到一个临时文件里) string strMetaDataXml = ""; nRet = GetResInfo(inputfile, bIsFirstRes, out strMetaDataXml, out lBodyStart, out lBodyLength, out strError); if (nRet == -1) goto ERROR1; if (lBodyLength == 0) return 0; // 空包不需上载 // 2.为上载做准备 XmlDocument metadataDom = new XmlDocument(); try { metadataDom.LoadXml(strMetaDataXml); } catch (Exception ex) { strError = "加载元数据到dom出错!\r\n" + ex.Message; goto ERROR1; } XmlNode node = metadataDom.DocumentElement; string strResPath = DomUtil.GetAttr(node, "path"); string strTargetPath = ""; if (bIsFirstRes == true) // 第一个资源 { // 从map中查询覆盖还是追加? ResPath respath = new ResPath(strResPath); respath.MakeDbName(); REDO: DbNameMapItem mapItem = (DbNameMapItem)map["*"]; if (mapItem != null) { } else { mapItem = (DbNameMapItem)map[respath.FullPath.ToUpper()]; } if (mapItem == null) { OriginNotFoundDlg dlg = new OriginNotFoundDlg(); MainForm.SetControlFont(dlg, this.DefaultFont); dlg.Message = "数据中声明的数据库路径 '" + respath.FullPath + "' 在覆盖关系对照表中没有找到, 请选择覆盖方式: "; dlg.Origin = respath.FullPath.ToUpper(); dlg.Servers = this.Servers; dlg.Channels = this.Channels; dlg.Map = map; dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) { strError = "用户中断..."; goto ERROR1; } map = dlg.Map; goto REDO; } if (mapItem.Style == "skip") return 0; // 构造目标路径 // 1)从源路径中提取id。源路径来自备份文件数据 respath = new ResPath(strResPath); string strID = respath.GetRecordId(); if (strID == null || strID == "" || (mapItem.Style == "append") ) { strID = "?"; // 将来加一个对话框 } // 2)用目标库路径构造完整的记录路径 string strTargetFullPath = ""; if (mapItem.Target == "*") { respath = new ResPath(strResPath); respath.MakeDbName(); strTargetFullPath = respath.FullPath; } else { strTargetFullPath = mapItem.Target; } respath = new ResPath(strTargetFullPath); strTargetPath = respath.Path + "/" + strID; strRecordPath = strTargetPath; channel = this.Channels.GetChannel(respath.Url); } else // 第二个以后的资源 { if (channel == null) { strError = "当bIsFirstRes==false时,参数channel不应为null..."; goto ERROR1; } ResPath respath = new ResPath(strResPath); string strObjectId = respath.GetObjectId(); if (strObjectId == null || strObjectId == "") { strError = "object id为空..."; goto ERROR1; } strTargetPath = strRecordPath + "/object/" + strObjectId; if (strRecordPath == "") { strError = "strRecordPath参数值为空..."; goto ERROR1; } } // string strLocalPath = DomUtil.GetAttr(node,"localpath"); // string strMimeType = DomUtil.GetAttr(node,"mimetype"); string strTimeStamp = DomUtil.GetAttr(node, "timestamp"); // 注意,strLocalPath并不是要上载的body文件,它只用来作元数据\ // body文件为strBodyTempFileName // 3.将body文件拆分成片断进行上载 string[] ranges = null; if (lBodyLength == 0) { // 空文件 ranges = new string[1]; ranges[0] = ""; } else { string strRange = ""; strRange = "0-" + Convert.ToString(lBodyLength - 1); // 按照100K作为一个chunk ranges = RangeList.ChunkRange(strRange, 100 * 1024 ); } byte[] timestamp = ByteArray.GetTimeStampByteArray(strTimeStamp); byte[] output_timestamp = null; REDOWHOLESAVE: string strOutputPath = ""; string strWarning = ""; for (int j = 0; j < ranges.Length; j++) { REDOSINGLESAVE: Application.DoEvents(); // 出让界面控制权 if (stop.State != 0) { DialogResult result = MessageBox.Show(this, "确实要中断当前批处理操作?", "dp2batch", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result == DialogResult.Yes) { strError = "用户中断"; goto ERROR1; } else { stop.Continue(); } } string strWaiting = ""; if (j == ranges.Length - 1) strWaiting = " 请耐心等待..."; string strPercent = ""; RangeList rl = new RangeList(ranges[j]); if (rl.Count >= 1) { double ratio = (double)((RangeItem)rl[0]).lStart / (double)lBodyLength; strPercent = String.Format("{0,3:N}", ratio * (double)100) + "%"; } if (stop != null) stop.SetMessage("正在上载 " + ranges[j] + "/" + Convert.ToString(lBodyLength) + " " + strPercent + " " + strTargetPath + strWarning + strWaiting + " " + strCount); inputfile.Seek(lBodyStart, SeekOrigin.Begin); long lRet = channel.DoSaveResObject(strTargetPath, inputfile, lBodyLength, "", // style strMetaDataXml, ranges[j], j == ranges.Length - 1 ? true : false, // 最尾一次操作,提醒底层注意设置特殊的WebService API超时时间 timestamp, out output_timestamp, out strOutputPath, out strError); // progressBar_main.Value = (int)((inputfile.Position)/ProgressRatio); stop.SetProgressValue(inputfile.Position); strWarning = ""; if (lRet == -1) { 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 = " (时间戳不匹配, 自动重试)"; if (ranges.Length == 1 || j == 0) goto REDOSINGLESAVE; goto REDOWHOLESAVE; } DialogResult result = MessageDlg.Show(this, "上载 '" + strDisplayRecPath + "' (片断:" + ranges[j] + "/总尺寸:" + Convert.ToString(lBodyLength) + ") 时发现时间戳不匹配。详细情况如下:\r\n---\r\n" + strError + "\r\n---\r\n\r\n是否以新时间戳强行上载?\r\n注:(是)强行上载 (否)忽略当前记录或资源上载,但继续后面的处理 (取消)中断整个批处理", "dp2batch", MessageBoxButtons.YesNoCancel, MessageBoxDefaultButton.Button1, ref this.bNotAskTimestampMismatchWhenOverwrite); if (result == DialogResult.Yes) { if (output_timestamp != null) { timestamp = new byte[output_timestamp.Length]; Array.Copy(output_timestamp, 0, timestamp, 0, output_timestamp.Length); } else { timestamp = output_timestamp; } strWarning = " (时间戳不匹配, 应用户要求重试)"; if (ranges.Length == 1 || j == 0) goto REDOSINGLESAVE; goto REDOWHOLESAVE; } if (result == DialogResult.No) { return 0; // 继续作后面的资源 } if (result == DialogResult.Cancel) { strError = "用户中断"; goto ERROR1; // 中断整个处理 } } goto ERROR1; } timestamp = output_timestamp; } // 考虑到保存第一个资源的时候,id可能为“?”,因此需要得到实际的id值 if (bIsFirstRes) strRecordPath = strOutputPath; return 0; ERROR1: return -1; }
// 上载一个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; }
// 根据原始路径准备即将写入的路径 // return: // -1 出错 // 0 用户放弃 // 1 成功 public static int PrepareOverwritePath( ServerCollection Servers, RmsChannelCollection Channels, IWin32Window owner, ref DbNameMap map, ref string strLongPath, out string strError) { strError = ""; // 从map中查询覆盖还是追加? ResPath respath = new ResPath(strLongPath); respath.MakeDbName(); REDO: DbNameMapItem mapItem = (DbNameMapItem)map["*"]; if (mapItem != null) { } else { mapItem = (DbNameMapItem)map[respath.FullPath.ToUpper()]; } if (mapItem == null) { OriginNotFoundDlg dlg = new OriginNotFoundDlg(); Font font = GuiUtil.GetDefaultFont(); if (font != null) dlg.Font = font; dlg.Message = "数据中声明的数据库路径 '" +respath.FullPath+ "' 在覆盖关系对照表中没有找到, 请选择覆盖方式: " ; dlg.Origin = respath.FullPath.ToUpper(); dlg.Servers = Servers; dlg.Channels = Channels; dlg.Map = map; dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(owner); if (dlg.DialogResult != DialogResult.OK) { strError = "用户中断..."; return 0; } map = dlg.Map; goto REDO; } if (mapItem.Style == "skip") return 0; // 构造目标路径 // 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; }
// 根据原始路径准备即将写入的路径 // 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; }