public SearchResultLoader(RmsChannel channel, DigitalPlatform.Stop stop, string resultsetName, string formatList, string lang = "zh") { this.Channel = channel; this.Stop = stop; this.ResultSetName = resultsetName; this.FormatList = formatList; this.Lang = lang; }
// 保存记录到模板配置文件 // parameters: public void SaveToTemplate() { // 选择目标数据库 OpenResDlg dlg = new OpenResDlg(); dlg.Font = GuiUtil.GetDefaultFont(); dlg.Text = "请选择目标数据库"; dlg.EnabledIndices = new int[] { ResTree.RESTYPE_DB }; dlg.ap = this.MainForm.AppInfo; dlg.ApCfgTitle = "detailform_openresdlg"; dlg.Path = textBox_recPath.Text; dlg.Initial( MainForm.Servers, this.Channels); // dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; // 下载模板配置文件 ResPath respath = new ResPath(dlg.Path); string strError; string strContent; byte[] baTimeStamp = null; string strMetaData; string strOutputPath; string strCfgFilePath = respath.Path + "/cfgs/template"; long lRet = 0; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载文件" + strCfgFilePath); stop.BeginLoop(); lRet = channel.GetRes( MainForm.cfgCache, strCfgFilePath, out strContent, out strMetaData, out baTimeStamp, out strOutputPath, out strError); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { this.TimeStamp = null; MessageBox.Show(this, strError); return; } } finally { channel = channelSave; } SelectRecordTemplateDlg tempdlg = new SelectRecordTemplateDlg(); tempdlg.Font = GuiUtil.GetDefaultFont(); int nRet = tempdlg.Initial(strContent, out strError); if (nRet == -1) goto ERROR1; tempdlg.Text = "请选择要修改的模板记录"; tempdlg.CheckNameExist = false; // 按OK按钮时不警告"名字不存在",这样允许新建一个模板 tempdlg.ap = this.MainForm.AppInfo; tempdlg.ApCfgTitle = "detailform_selecttemplatedlg"; tempdlg.ShowDialog(this); if (tempdlg.DialogResult != DialogResult.OK) return; string strXmlBody = ""; bool bHasUploadedFile = false; nRet = GetXmlRecord(out strXmlBody, out bHasUploadedFile, out strError); if (nRet == -1) goto ERROR1; // 修改配置文件内容 if (tempdlg.textBox_name.Text != "") { // 替换或者追加一个记录 nRet = tempdlg.ReplaceRecord(tempdlg.textBox_name.Text, strXmlBody, out strError); if (nRet == -1) { goto ERROR1; } } if (tempdlg.Changed == false) // 没有必要保存回去 return; string strOutputXml = tempdlg.OutputXml; // 使用Channel channelSave = channel; // 重新获得一次channel, 是因为前面GetXmlRecord()函数有可能摧毁这个变量 channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { // 存回配置文件 stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在保存配置文件 " + strCfgFilePath); stop.BeginLoop(); byte [] baOutputTimeStamp = null; // string strOutputPath = ""; EnableControlsInLoading(true); lRet = channel.DoSaveTextRes(strCfgFilePath, strOutputXml, true, // bInlucdePreamble "", // style baTimeStamp, out baOutputTimeStamp, out strOutputPath, out strError); EnableControlsInLoading(false); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { strError = "保存配置文件"+ strCfgFilePath +"失败,原因: "+strError; goto ERROR1; } } finally { channel = channelSave; } MessageBox.Show(this, "修改模板配置文件成功。"); return; ERROR1: MessageBox.Show(this, strError); }
/// <summary> /// 检索获得若干路径 /// </summary> /// <param name="channel"></param> /// <param name="strDbName"></param> /// <param name="strFrom"></param> /// <param name="strKey"></param> /// <param name="nMax"></param> /// <param name="paths"></param> /// <param name="strError"></param> /// <returns>-1 error;0 not found;>=1 found</returns> static int SearchPath(RmsChannel channel, string strDbName, string strFrom, string strKey, long nMax, out string[] paths, out string strError) { paths = null; strError = ""; // 2007/4/5 改造 加上了 GetXmlStringSimple() string strQueryXml = "<target list='" + StringUtil.GetXmlStringSimple(strDbName + ":" + strFrom) // 2007/9/14 + "'><item><word>" + StringUtil.GetXmlStringSimple(strKey) + "</word><match>exact</match><relation>=</relation><dataType>string</dataType><maxCount>" + Convert.ToString(nMax) + "</maxCount></item><lang>zh</lang></target>"; long lRet = channel.DoSearch(strQueryXml, "default", "", // strOuputStyle out strError); if (lRet == -1) { strError = "检索库 '" + strDbName + "/" + strFrom + "' 时出错: " + strError; return -1; } if (lRet == 0) { return 0; // 没有找到 } if (lRet > 1) { strError = "以Key '" + strKey + "' 检索库 '" + strDbName + "' 时命中 " + Convert.ToString(lRet) + " 条,属于不正常情况。请修改库 '" + strDbName + "' 中相应记录,确保同一Key只有一条对应的记录。"; return -1; } List<string> aPath = null; lRet = channel.DoGetSearchResult( "default", 0, -1, "zh", null, // this.stop, out aPath, out strError); if (lRet == -1) { strError = "检索库 '" + strDbName + "' 获取检索结果时出错: " + strError; return -1; } paths = new string[aPath.Count]; for (int i = 0; i < aPath.Count; i++) { paths[i] = (string)aPath[i]; } return paths.Length; }
// 删除所有用到的内核数据库 // 专门开发给安装程序卸载时候使用 public static int DeleteAllDatabase( RmsChannel channel, XmlDocument cfg_dom, out string strError) { strError = ""; string strTempError = ""; long lRet = 0; // 大书目库 XmlNodeList nodes = cfg_dom.DocumentElement.SelectNodes("itemdbgroup/database"); for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes[i]; // 实体库 string strEntityDbName = DomUtil.GetAttr(node, "name"); if (String.IsNullOrEmpty(strEntityDbName) == false) { lRet = channel.DoDeleteDB(strEntityDbName, out strTempError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) { strError += "删除实体库 '" + strEntityDbName + "' 内数据时候发生错误:" + strTempError + "; "; } } // 订购库 string strOrderDbName = DomUtil.GetAttr(node, "orderDbName"); if (String.IsNullOrEmpty(strOrderDbName) == false) { lRet = channel.DoDeleteDB(strOrderDbName, out strTempError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) { strError += "删除订购库 '" + strOrderDbName + "' 内数据时候发生错误:" + strTempError + "; "; } } // 期库 string strIssueDbName = DomUtil.GetAttr(node, "issueDbName"); if (String.IsNullOrEmpty(strIssueDbName) == false) { lRet = channel.DoDeleteDB(strIssueDbName, out strTempError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) { strError += "删除期库 '" + strIssueDbName + "' 内数据时候发生错误:" + strTempError + "; "; } } // 小书目库 string strBiblioDbName = DomUtil.GetAttr(node, "biblioDbName"); if (String.IsNullOrEmpty(strBiblioDbName) == false) { lRet = channel.DoDeleteDB(strBiblioDbName, out strTempError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) { strError += "删除小书目库 '" + strBiblioDbName + "' 内数据时候发生错误:" + strTempError + "; "; } } } // 读者库 nodes = cfg_dom.DocumentElement.SelectNodes("readerdbgroup/database"); for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes[i]; string strDbName = DomUtil.GetAttr(node, "name"); if (String.IsNullOrEmpty(strDbName) == false) { lRet = channel.DoDeleteDB(strDbName, out strTempError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) { strError += "删除读者库 '" + strDbName + "' 内数据时候发生错误:" + strTempError + "; "; } } } // 预约到书队列库 XmlNode arrived_node = cfg_dom.DocumentElement.SelectSingleNode("arrived"); if (arrived_node != null) { string strArrivedDbName = DomUtil.GetAttr(arrived_node, "dbname"); if (String.IsNullOrEmpty(strArrivedDbName) == false) { lRet = channel.DoDeleteDB(strArrivedDbName, out strTempError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) { strError += "删除预约到书库 '" + strArrivedDbName + "' 内数据时候发生错误:" + strTempError + "; "; } } } // 违约金库 XmlNode amerce_node = cfg_dom.DocumentElement.SelectSingleNode("amerce"); if (amerce_node != null) { string strAmerceDbName = DomUtil.GetAttr(amerce_node, "dbname"); if (String.IsNullOrEmpty(strAmerceDbName) == false) { lRet = channel.DoDeleteDB(strAmerceDbName, out strTempError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) { strError += "删除违约金库 '" + strAmerceDbName + "' 内数据时候发生错误:" + strTempError + "; "; } } } // 发票库 XmlNode invoice_node = cfg_dom.DocumentElement.SelectSingleNode("invoice"); if (invoice_node != null) { string strInvoiceDbName = DomUtil.GetAttr(amerce_node, "dbname"); if (String.IsNullOrEmpty(strInvoiceDbName) == false) { lRet = channel.DoDeleteDB(strInvoiceDbName, out strTempError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) { strError += "删除发票库 '" + strInvoiceDbName + "' 内数据时候发生错误:" + strTempError + "; "; } } } // 消息库 XmlNode message_node = cfg_dom.DocumentElement.SelectSingleNode("message"); if (message_node != null) { string strMessageDbName = DomUtil.GetAttr(message_node, "dbname"); if (String.IsNullOrEmpty(strMessageDbName) == false) { lRet = channel.DoDeleteDB(strMessageDbName, out strTempError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) { strError += "删除消息库 '" + strMessageDbName + "' 内数据时候发生错误:" + strTempError + "; "; } } } // 实用库 nodes = cfg_dom.DocumentElement.SelectNodes("utilDb/database"); for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes[i]; string strDbName = DomUtil.GetAttr(node, "name"); string strType = DomUtil.GetAttr(node, "type"); if (String.IsNullOrEmpty(strDbName) == false) { lRet = channel.DoDeleteDB(strDbName, out strTempError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) { strError += "删除类型为 " + strType + " 的实用库 '" + strDbName + "' 内数据时发生错误:" + strTempError + "; "; } } } if (String.IsNullOrEmpty(strError) == false) return -1; return 0; }
// 在服务器端创建对象 // parameters: // strStyle 风格。当创建目录的时候,为"createdir",否则为空 // return: // -1 错误 // 1 以及存在同名对象 // 0 正常返回 int NewServerSideObject( RmsChannel channel, string strPath, string strStyle, Stream stream, byte[] baTimeStamp, out string strError) { strError = ""; byte[] baOutputTimestamp = null; string strOutputPath = ""; string strRange = ""; if (stream != null && stream.Length != 0) { Debug.Assert(stream.Length != 0, "test"); strRange = "0-" + Convert.ToString(stream.Length - 1); } long lRet = channel.DoSaveResObject(strPath, stream, (stream != null && stream.Length != 0) ? stream.Length : 0, strStyle, "", // strMetadata, strRange, true, baTimeStamp, // timestamp, out baOutputTimestamp, out strOutputPath, out strError); if (lRet == -1) { if (channel.ErrorCode == ChannelErrorCode.AlreadyExist) { return 1; // 已经存在同名同类型对象 } strError = "写入 '" + strPath + "' 发生错误: " + strError; return -1; } return 0; }
// 根据数据库模板的定义,刷新一个已经存在的数据库的定义 // return: // -1 // 0 keys定义没有更新 // 1 keys定义更新了 int RefreshDatabase(RmsChannel channel, string strTemplateDir, string strDatabaseName, string strIncludeFilenames, string strExcludeFilenames, out string strError) { strError = ""; int nRet = 0; strIncludeFilenames = strIncludeFilenames.ToLower(); strExcludeFilenames = strExcludeFilenames.ToLower(); bool bKeysChanged = false; DirectoryInfo di = new DirectoryInfo(strTemplateDir); FileInfo[] fis = di.GetFiles(); // 创建所有文件对象 for (int i = 0; i < fis.Length; i++) { string strName = fis[i].Name; if (strName == "." || strName == "..") continue; if (FileUtil.IsBackupFile(strName) == true) continue; /* if (strName.ToLower() == "keys" || strName.ToLower() == "browse") continue; * */ // 如果Include和exclude里面都有一个文件名,优先依exclude(排除) if (StringUtil.IsInList(strName, strExcludeFilenames) == true) continue; if (strIncludeFilenames != "*") { if (StringUtil.IsInList(strName, strIncludeFilenames) == false) continue; } string strFullPath = fis[i].FullName; nRet = ConvertGb2312TextfileToUtf8(strFullPath, out strError); if (nRet == -1) return -1; string strExistContent = ""; string strNewContent = ""; Stream new_stream = new FileStream(strFullPath, FileMode.Open); { StreamReader sr = new StreamReader(new_stream, Encoding.UTF8); strNewContent = ConvertCrLf(sr.ReadToEnd()); } new_stream.Seek(0, SeekOrigin.Begin); try { string strPath = strDatabaseName + "/cfgs/" + strName; // 获取已有的配置文件对象 byte[] timestamp = null; string strOutputPath = ""; string strMetaData = ""; string strStyle = "content,data,metadata,timestamp,outputpath"; MemoryStream exist_stream = new MemoryStream(); try { long lRet = channel.GetRes( strPath, exist_stream, null, // stop, strStyle, null, // byte [] input_timestamp, out strMetaData, out timestamp, out strOutputPath, out strError); if (lRet == -1) { // 配置文件不存在,怎么返回错误码的? if (channel.ErrorCode == ChannelErrorCode.NotFound) { timestamp = null; goto DO_CREATE; } return -1; } exist_stream.Seek(0, SeekOrigin.Begin); { StreamReader sr = new StreamReader(exist_stream, Encoding.UTF8); strExistContent = ConvertCrLf(sr.ReadToEnd()); } } finally { if (exist_stream != null) exist_stream.Close(); } // 比较本地的和服务器的有无区别,无区别就不要上载了 if (strExistContent == strNewContent) { continue; } DO_CREATE: // 在服务器端创建对象 // parameters: // strStyle 风格。当创建目录的时候,为"createdir",否则为空 // return: // -1 错误 // 1 已经存在同名对象 // 0 正常返回 nRet = NewServerSideObject( channel, strPath, "", new_stream, timestamp, out strError); if (nRet == -1) return -1; if (nRet == 1) { strError = "NewServerSideObject()发现已经存在同名对象: " + strError; return -1; } if (strName.ToLower() == "keys") bKeysChanged = true; } finally { new_stream.Close(); } } if (bKeysChanged == true) { // 对数据库及时调用刷新keys表的API long lRet = channel.DoRefreshDB( "begin", strDatabaseName, false, out strError); if (lRet == -1) { strError = "数据库 '" + strDatabaseName + "' 的定义已经被成功刷新,但在刷新内核Keys表操作时失败: " + strError; return -1; } return 1; } return 0; }
// 包装后的版本 int ChangeDbName( RmsChannel channel, string strOldDbName, string strNewDbName, out string strError) { return ChangeDbName( channel, strOldDbName, strNewDbName, () => { }, out strError); }
// 检索尾号记录的路径和记录体 // return: // -1 error(注:检索命中多条情况被当作错误返回) // 0 not found // 1 found public int SearchOneClassTailNumberPathAndRecord( // RmsChannelCollection Channels, RmsChannel channel, string strArrangeGroupName, string strClass, out string strPath, out string strXml, out byte[] timestamp, out string strError) { strError = ""; strPath = ""; strXml = ""; timestamp = null; if (strClass == "") { strError = "尚未指定分类号"; return -1; } if (strArrangeGroupName == "") { strError = "尚未指定排架体系名"; return -1; } string strZhongcihaoDbName = GetTailDbName(strArrangeGroupName); if (String.IsNullOrEmpty(strZhongcihaoDbName) == true) { strError = "无法通过排架体系名 '" + strArrangeGroupName + "' 获得种次号库名"; return -1; } string strQueryXml = "<target list='" + StringUtil.GetXmlStringSimple(strZhongcihaoDbName + ":" + "分类号") // 2007/9/14 + "'><item><word>" + StringUtil.GetXmlStringSimple(strClass) + "</word><match>exact</match><relation>=</relation><dataType>string</dataType><maxCount>-1</maxCount></item><lang>zh</lang></target>"; List<string> aPath = null; // 获得通用记录 // 本函数可获得超过1条以上的路径 // return: // -1 error // 0 not found // 1 命中1条 // >1 命中多于1条 int nRet = GetRecXml( // Channels, channel, strQueryXml, out strXml, 2, out aPath, out timestamp, out strError); if (nRet == -1) { strError = "检索库 " + strZhongcihaoDbName + " 时出错: " + strError; return -1; } if (nRet == 0) { return 0; // 没有找到 } if (nRet > 1) { strError = "以分类号'" + strClass + "'检索库 " + strZhongcihaoDbName + " 时命中 " + Convert.ToString(nRet) + " 条,无法取得尾号。请修改库 '" + strZhongcihaoDbName + "' 中相应记录,确保同一类目只有一条对应的记录。"; return -1; } Debug.Assert(aPath.Count >= 1, ""); strPath = aPath[0]; return 1; }
// 自动加工数据 public void AutoGenerate() { // 库名部分路径 ResPath respath = new ResPath(textBox_recPath.Text); respath.MakeDbName(); string strError; string strCode; string strRef; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { string strCfgPath = respath.Path + "/cfgs/autoGenerate.cs"; string strCfgRefPath = respath.Path + "/cfgs/autoGenerate.cs.ref"; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载文件" + strCfgPath); stop.BeginLoop(); byte[] baTimeStamp = null; string strMetaData; string strOutputPath; long lRet = channel.GetRes( MainForm.cfgCache, strCfgPath, out strCode, out strMetaData, out baTimeStamp, out strOutputPath, out strError); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { MessageBox.Show(this, strError); return; } stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载文件" + strCfgRefPath); stop.BeginLoop(); lRet = channel.GetRes( MainForm.cfgCache, strCfgRefPath, out strRef, out strMetaData, out baTimeStamp, out strOutputPath, out strError); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { MessageBox.Show(this, strError); return; } } finally { channel = channelSave; } // 执行代码 int nRet = RunScript(strCode, strRef, out strError); if (nRet == -1) { MessageBox.Show(this, strError); return; } }
// 保存到备份格式 public void SaveToBackup(string strOutputFileName) { string strError; // 询问文件名 if (strOutputFileName == null) { SaveFileDialog dlg = new SaveFileDialog(); dlg.Title = "请指定要保存的备份文件名"; dlg.CreatePrompt = false; dlg.OverwritePrompt = false; dlg.FileName = MainForm.UsedBackupFileName; // "*.dp2bak"; dlg.InitialDirectory = Environment.CurrentDirectory; dlg.Filter = "backup files (*.dp2bak)|*.dp2bak|All files (*.*)|*.*" ; dlg.RestoreDirectory = true; if(dlg.ShowDialog() != DialogResult.OK) return; strOutputFileName = dlg.FileName; MainForm.UsedBackupFileName = strOutputFileName; // 记忆下来 } bool bOverwrite = false; if (File.Exists(strOutputFileName) == true) { OverwriteOrAppendBackupFileDlg dlg = new OverwriteOrAppendBackupFileDlg(); dlg.Font = GuiUtil.GetDefaultFont(); dlg.Text = "文件已经存在,是否覆盖?"; dlg.Message = "文件 " + strOutputFileName + " 已经存在,是追加还是覆盖?"; dlg.ShowDialog(this); if (dlg.DialogResult == DialogResult.Yes) { // 追加 bOverwrite = false; } else if (dlg.DialogResult == DialogResult.No) { // 覆盖 bOverwrite = true; } return; // 放弃 } // 打开文件 FileStream fileTarget = File.Open( strOutputFileName, FileMode.OpenOrCreate, // 原来是Open,后来修改为OpenOrCreate。这样对临时文件被系统管理员手动意外删除(但是xml文件中仍然记载了任务)的情况能够适应。否则会抛出FileNotFoundException异常 FileAccess.Write, FileShare.ReadWrite); if (bOverwrite == true) fileTarget.SetLength(0); try { fileTarget.Seek(0, SeekOrigin.End); // 具有追加的能力 long lStart = fileTarget.Position; // 记忆起始位置 byte [] length = new byte[8]; fileTarget.Write(length, 0, 8); // 临时写点数据,占据记录总长度位置 bool bHasUploadedFile = false; // 获得Xml记录 string strXmlBody; int nRet = GetXmlRecord(out strXmlBody, out bHasUploadedFile, out strError); if (nRet == -1) { fileTarget.SetLength(lStart); // 把本次追加写入的全部去掉 goto ERROR1; } ResPath respath = new ResPath(textBox_recPath.Text); // 向backup文件中保存第一个 res ExportUtil.ChangeMetaData(ref this.m_strMetaData, // ResFileList null, null, null, null, respath.FullPath, ByteArray.GetHexTimeStampString(this.TimeStamp)); // 任延华加 2005/6/11 long lRet = Backup.WriteFirstResToBackupFile( fileTarget, this.m_strMetaData, strXmlBody); // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在保存记录 " + respath.FullPath + "到备份文件 " + strOutputFileName); stop.BeginLoop(); EnableControlsInLoading(true); nRet = this.listView_resFiles.DoSaveResToBackupFile( fileTarget, respath.Path, channel, stop, out strError); EnableControlsInLoading(false); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (nRet == -1) { fileTarget.SetLength(lStart); // 把本次追加写入的全部去掉 strError = "保存记录失败,原因: "+ strError; goto ERROR1; } } finally { channel = channelSave; } // 写入总长度 long lTotalLength = fileTarget.Position - lStart - 8; byte[] data = BitConverter.GetBytes(lTotalLength); fileTarget.Seek(lStart, SeekOrigin.Begin); fileTarget.Write(data, 0, 8); } finally { fileTarget.Close(); fileTarget = null; } string strText = ""; if (bOverwrite == false) strText = "追加保存备份文件完成..."; else strText = "覆盖保存备份文件完成 ..."; MessageBox.Show(this, strText); return; ERROR1: MessageBox.Show(this, strError); }
// 回调函数 int DownloadOneFileMetaData(string strID, out string strResultXml, out byte[] timestamp, out string strError) { timestamp = null; strError = ""; ResPath respath = new ResPath(textBox_recPath.Text); string strResPath = respath.Path + "/object/" + strID; strResPath = strResPath.Replace(":", "/"); // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载资源文件的元数据 " + strResPath); stop.BeginLoop(); byte [] baOutputTimeStamp = null; string strOutputPath = ""; EnableControlsInLoading(true); // 只得到metadata long lRet = channel.GetRes(strResPath, (Stream)null, stop, "metadata,timestamp,outputpath", null, out strResultXml, out baOutputTimeStamp, out strOutputPath, out strError); EnableControlsInLoading(false); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { MessageBox.Show(this, "下载资源文件元数据失败,原因: "+strError); goto ERROR1; } timestamp = baOutputTimeStamp; } finally { channel = channelSave; } return 0; ERROR1: return -1; }
int DownloadOneFile(string strID, out string strError) { strError = ""; ResPath respath = new ResPath(textBox_recPath.Text); string strResPath = respath.Path + "/object/" + strID; strResPath = strResPath.Replace(":", "/"); string strLocalPath = this.listView_resFiles.GetLocalFileName(strID); SaveFileDialog dlg = new SaveFileDialog(); dlg.Title = "请指定要保存的本地文件名"; dlg.CreatePrompt = false; dlg.FileName = strLocalPath == "" ? strID + ".res" : strLocalPath; dlg.InitialDirectory = Environment.CurrentDirectory; // dlg.Filter = "projects files (outer*.xml)|outer*.xml|All files (*.*)|*.*" ; dlg.RestoreDirectory = true ; if(dlg.ShowDialog() != DialogResult.OK) { strError = "放弃"; return -1; } // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载资源文件 " + strResPath); stop.BeginLoop(); byte [] baOutputTimeStamp = null; EnableControlsInLoading(true); string strMetaData; string strOutputPath = ""; long lRet = channel.GetRes(strResPath, dlg.FileName, stop, out strMetaData, out baOutputTimeStamp, out strOutputPath, out strError); EnableControlsInLoading(false); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { MessageBox.Show(this, "下载资源文件失败,原因: "+strError); goto ERROR1; } } finally { channel = channelSave; } return 0; ERROR1: return -1; }
// 观察检索点 // parameters: // strRecordPath 记录路径。如果==null,表示直接用textBox_recPath中当前的内容作为路径 public void ViewAccessPoint(string strRecordPath) { if (strRecordPath == null || strRecordPath == "") strRecordPath = textBox_recPath.Text; if (strRecordPath == "") { MessageBox.Show(this, "必须指定好路径后, 才能模拟创建检索点"); return; } ResPath respath = new ResPath(strRecordPath); string strError; string strXml = ""; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { bool bHasUploadedFile = false; int nRet = GetXmlRecord(out strXml, out bHasUploadedFile, out strError); if (nRet == -1) { MessageBox.Show(this, strError); return; } } finally { channel = channelSave; } ViewAccessPointForm accessPointWindow = MainForm.TopViewAccessPointForm; if (accessPointWindow == null) { accessPointWindow = new ViewAccessPointForm(); // accessPointWindow.StartPosition = FormStartPosition.CenterScreen; accessPointWindow.Show(); accessPointWindow.MdiParent = MainForm; // MDI子窗口 } else accessPointWindow.Activate(); /* else { accessPointWindow.Focus(); if (accessPointWindow.WindowState == FormWindowState.Minimized) { accessPointWindow.WindowState = FormWindowState.Normal; } } */ /* if (accessPointWindow.Visible == false) { try // Close()过的窗口 { accessPointWindow.Show(); } catch (System.ObjectDisposedException) { accessPointWindow = new ViewAccessPointForm(); accessPointWindow.StartPosition = FormStartPosition.CenterScreen; accessPointWindow.Show(); } } */ // 使用Channel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在获取检索点 " + respath.FullPath); stop.BeginLoop(); EnableControlsInLoading(true); long lRet = channel.DoGetKeys( respath.Path, strXml, "zh", // "", accessPointWindow, stop, out strError); EnableControlsInLoading(false); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { MessageBox.Show(this, "获取检索点失败,原因: "+strError); return; } // 设置标题 // ResPath respath = new ResPath(this.textBox_recPath.Text); accessPointWindow.Text = "观察检索点: " + respath.ReverseFullPath; } finally { channel = channelSave; } }
// 删除记录 // parameters: // strRecordPath 记录路径。如果==null,表示直接用textBox_recPath中当前的内容作为路径 public void DeleteRecord(string strRecordPath) { if (strRecordPath != null) textBox_recPath.Text = strRecordPath; if (textBox_recPath.Text == "") { MessageBox.Show(this, "路径不能为空"); return; } ResPath respath = new ResPath(textBox_recPath.Text); Uri uri = null; try { uri = new Uri(respath.Url); } catch (Exception ex) { MessageBox.Show(this, "路径错误: " + ex.Message); return; } // 保存到文件 if (uri.IsFile) { MessageBox.Show(this, "暂时不支持删除文件"); return; } string strText = "你确实要删除位于服务器 '"+respath.Url+"' 上的记录 '"+respath.Path + "' 吗?"; DialogResult msgResult = MessageBox.Show(this, strText, "dp2rms", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (msgResult != DialogResult.OK) { MessageBox.Show(this, "删除操作被放弃..."); return; } string strError; byte [] baOutputTimeStamp = null; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在删除记录 " + respath.FullPath); stop.BeginLoop(); EnableControlsInLoading(true); long lRet = channel.DoDeleteRes(respath.Path, this.TimeStamp, out baOutputTimeStamp, out strError); EnableControlsInLoading(false); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { MessageBox.Show(this, "删除记录 '"+respath.Path+"' 失败,原因: "+strError); return; } } finally { channel = channelSave; } // 如果删除成功,原来时间戳遗留在this.TimeStamp中,也无害 MessageBox.Show(this, "删除记录 '" + respath.FullPath + "' 成功。"); }
// 保存记录 // parameters: // strRecordPath 记录路径。如果==null,表示直接用textBox_recPath中当前的内容作为路径 public void SaveRecord(string strRecordPath) { if (strRecordPath != null) textBox_recPath.Text = strRecordPath; if (textBox_recPath.Text == "") { MessageBox.Show(this, "路径不能为空"); return; } ResPath respath = new ResPath(textBox_recPath.Text); Uri uri = null; try { uri = new Uri(respath.Url); } catch (Exception ex) { MessageBox.Show(this, "路径错误: " + ex.Message); return; } // 保存到文件 if (uri.IsFile) { MessageBox.Show(this, "暂时不支持保存到文件"); return; } string strError; string strXml = ""; bool bHasUploadedFile = false; int nRet = GetXmlRecord(out strXml, out bHasUploadedFile, out strError); if (nRet == -1) { MessageBox.Show(this, strError); return; } byte [] baOutputTimeStamp = null; string strOutputPath = ""; long lRet = 0; int nUploadCount = 0; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在保存记录 " + respath.FullPath); stop.BeginLoop(); EnableControlsInLoading(true); //string strTemp = ByteArray.GetHexTimeStampString(this.TimeStamp); if (String.IsNullOrEmpty(this.strDatabaseOriginPath) == false && bHasUploadedFile == true && respath.FullPath != this.strDatabaseOriginPath) { ResPath respath_old = new ResPath(this.strDatabaseOriginPath); if (respath_old.Url != respath.Url) { MessageBox.Show(this, "目前暂不支持跨服务器情况下的资源复制。本记录中原有的已上载资源,在另存到目标库的时丢失(为空),请注意保存完后手动上载。"); goto SKIPCOPYRECORD; } // 复制记录 // return: // -1 出错。错误信息在strError中 // 0或者其他 成功 nRet = channel.DoCopyRecord(respath_old.Path, respath.Path, false, // bool bDeleteOriginRecord, out baOutputTimeStamp, out strOutputPath, out strError); if (nRet == -1) { MessageBox.Show(this, "复制资源时发生错误: " + strError); } else { // 为继续保存最新XML记录作准备 respath.Path = strOutputPath; // ?形式路径其实已经确定 this.TimeStamp = baOutputTimeStamp; } } SKIPCOPYRECORD: lRet = channel.DoSaveTextRes(respath.Path, strXml, false, // bInlucdePreamble "", // style this.TimeStamp, out baOutputTimeStamp, out strOutputPath, out strError); EnableControlsInLoading(false); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { MessageBox.Show(this, "保存记录失败,原因: "+strError); return; } // this.TimeStamp = baOutputTimeStamp; respath.Path = strOutputPath; textBox_recPath.Text = respath.FullPath; //// this.strDatabaseOriginPath = respath.Url + "?" + strOutputPath; // 保存从数据库中来的原始path stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在保存资源 " + respath.FullPath); stop.BeginLoop(); EnableControlsInLoading(true); Debug.Assert(channel != null, ""); // 保存对象资源,循环对象列表就可以了 nUploadCount = this.listView_resFiles.DoUpload( respath.Path, channel, stop, out strError); EnableControlsInLoading(false); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); } finally { channel = channelSave; } if (nUploadCount == -1) { MessageBox.Show(this, "XML记录保存成功, 但保存资源失败,原因: "+strError); return; } if (nUploadCount > 0) { // 使用Channel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); // 需要重新获得时间戳 string strStyle = "timestamp,metadata"; // withresmetadata string strMetaData = ""; string strContent = ""; try { lRet = channel.GetRes(respath.Path, strStyle, out strContent, out strMetaData, out baOutputTimeStamp, out strOutputPath, out strError); if (lRet == -1) { MessageBox.Show(this, "重新获得时间戳 '" + respath.FullPath + "' 失败。原因 : " + strError); return; } } finally { channel = channelSave; } this.TimeStamp = baOutputTimeStamp; // 设置时间戳很重要。即便xml不合法,也应设置好时间戳,否则窗口无法进行正常删除。 this.m_strMetaData = strMetaData; // 记忆XML记录的元数据 } this.Changed = false; MessageBox.Show(this, "保存记录 '" + respath.FullPath + "' 成功。"); }
private void button_OK_Click(object sender, System.EventArgs e) { string strMetaData = ""; // TODO: 一旦遇到问题,可以放开注释试验 // this.textBox_content.Text = RemoveSingle0a(this.textBox_content.Text); if (this.Obj != null) { if (this.IsText == true) { byte[] baContent = StringUtil.GetUtf8Bytes(this.textBox_content.Text, true); this.Obj.Content = baContent; } else { if (this.Stream != null) { this.Obj.Content = new byte[this.Stream.Length]; this.Stream.Seek(0, SeekOrigin.Begin); this.Stream.Read(this.Obj.Content, 0, (int)this.Stream.Length); } else { this.Obj.Content = null; } } StringUtil.ChangeMetaData(ref strMetaData, null, // string strID, this.LocalPath, // string strLocalPath, this.Mime, // string strMimeType, null, // string strLastModified, null, // string strPath, null); // string strTimestamp) this.Obj.Metadata = strMetaData; this.Obj.Changed = true; this.DialogResult = DialogResult.OK; this.Close(); return; } this.channel = Channels.GetChannel(this.ServerUrl); Debug.Assert(channel != null, "Channels.GetChannel() 异常"); DigitalPlatform.Stop stop = null; if (stopManager != null) { stop = new DigitalPlatform.Stop(); stop.Register(this.stopManager, true); // 和容器关联 stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在保存配置文件: " + this.Path); stop.BeginLoop(); } if (this.IsText == true) { // 更新stream对象内容 byte[] baContent = StringUtil.GetUtf8Bytes(this.textBox_content.Text, true); this.Stream = new MemoryStream(baContent); // !!! 什么时候释放? /* this.Stream.SetLength(0); StreamWriter sw = new StreamWriter(this.Stream, Encoding.UTF8); sw.Write(this.textBox_content.Text); */ } if (this.Stream != null) this.Stream.Seek(0, SeekOrigin.Begin); // 保存配置文件 string strError = ""; byte[] baOutputTimestamp = null; string strOutputPath = ""; string strStyle = ""; if (this.checkBox_autoCreate.Checked == true) { if (strStyle != "") strStyle += ","; strStyle += "autocreatedir"; } StringUtil.ChangeMetaData(ref strMetaData, null, // string strID, this.LocalPath, // string strLocalPath, this.Mime, // string strMimeType, null, // string strLastModified, null, // string strPath, null); // string strTimestamp) string strRange = ""; if (this.Stream != null && this.Stream.Length != 0) { Debug.Assert(this.Stream.Length != 0, "test"); strRange = "0-" + Convert.ToString(this.Stream.Length - 1); } long lRet = channel.DoSaveResObject(this.Path, this.Stream, (this.Stream != null && this.Stream.Length != 0) ? this.Stream.Length : 0, strStyle, strMetaData, // strMetadata, strRange, true, this.TimeStamp, // timestamp, out baOutputTimestamp, out strOutputPath, out strError); /* // 保存配置文件 byte[] baOutputTimeStamp = null; string strOutputPath = ""; string strError = ""; long lRet = channel.DoSaveTextRes(this.Path, this.textBox_content.Text, true, // bInlucdePreamble "", // style this.TimeStamp, out baOutputTimeStamp, out strOutputPath, out strError); */ if (stopManager != null) { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); } if (lRet == -1) { MessageBox.Show(this, strError); goto FINISH; } this.TimeStamp = baOutputTimestamp; MessageBox.Show(this, "配置文件 '" + this.Path + "' 保存成功"); ///////////// FINISH: if (stopManager != null && stop != null) { stop.Unregister(); // 和容器关联 stop = null; } this.channel = null; this.DialogResult = DialogResult.OK; this.Close(); }
// 递归 public int Fill(TreeNode node) { TreeNodeCollection children = null; if (node == null) { children = this.Nodes; } else { children = node.Nodes; } int i; // 填充根 if (node == null) { children.Clear(); TreeNode nodeNew = new TreeNode(this.ServerUrl, ResTree.RESTYPE_SERVER, ResTree.RESTYPE_SERVER); ResTree.SetLoading(nodeNew); NodeInfo nodeinfo = new NodeInfo(); nodeinfo.TreeNode = nodeNew; nodeinfo.Expandable = true; nodeinfo.DefElement = GetDefElementString(nodeNew.ImageIndex); nodeinfo.NodeState |= NodeState.Object; nodeNew.Tag = nodeinfo; if (EnabledIndices != null && StringUtil.IsInList(nodeNew.ImageIndex, EnabledIndices) == false) nodeNew.ForeColor = ControlPaint.LightLight(nodeNew.ForeColor); children.Add(nodeNew); return 0; } // 根以下的节点类型 ResPath respath = new ResPath(node); string strPath = respath.Path; //if (node != null) // strPath = TreeViewUtil.GetPath(node); this.channel = Channels.GetChannel(this.ServerUrl); Debug.Assert(channel != null, "Channels.GetChannel() 异常"); ResInfoItem [] items = null; string strError = ""; DigitalPlatform.Stop stop = null; if (stopManager != null) { stop = new DigitalPlatform.Stop(); stop.Register(this.stopManager, true); // 和容器关联 stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在列目录: " + this.ServerUrl + "?" + strPath); stop.BeginLoop(); } long lRet = channel.DoDir(strPath, this.Lang, null, // 不需要列出全部语言的名字 out items, out strError); if (stopManager != null) { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.Unregister(); // 和容器关联 } this.channel = null; if (lRet == -1) { try { MessageBox.Show(this, "Channel::DoDir() Error: " + strError); } catch { // this可能已经不存在 return -1; } if (node != null) { ResTree.SetLoading(node); // 出错的善后处理,重新出现+号 node.Collapse(); } return -1; } if (items != null) { children.Clear(); for(i=0;i<items.Length;i++) { // 忽略from类型节点 if (items[i].Type == ResTree.RESTYPE_FROM) continue; TreeNode nodeNew = new TreeNode(items[i].Name, items[i].Type, items[i].Type); NodeInfo nodeinfo = new NodeInfo(); nodeinfo.TreeNode = nodeNew; nodeinfo.Expandable = items[i].HasChildren; nodeinfo.DefElement = GetDefElementString(nodeNew.ImageIndex); nodeinfo.NodeState |= NodeState.Object; nodeinfo.Style = items[i].Style; nodeNew.Tag = nodeinfo; if (items[i].HasChildren) ResTree.SetLoading(nodeNew); if (EnabledIndices != null && StringUtil.IsInList(nodeNew.ImageIndex, EnabledIndices) == false) nodeNew.ForeColor = ControlPaint.LightLight(nodeNew.ForeColor); children.Add(nodeNew); } } return 0; }
// 获得一个汉字的拼音 // 所获得的拼音, 是一个分号间隔的字符串, 表示对应于这个汉字的多音 // return: // -1 error // 1 found // 0 not found int GetOnePinyin(string strOneHanzi, out string strPinyin, out string strError) { strPinyin = ""; strError = ""; // 拼音库路径 string strPinyinDbPath = MainForm.AppInfo.GetString("pinyin", "pinyin_db_path", ""); if (String.IsNullOrEmpty(strPinyinDbPath) == true) { strError = "拼音库路径尚未配置。请先用菜单“帮助/系统参数设置”功能配置适当的拼音库路径。"; return -1; } ResPath respath = new ResPath(strPinyinDbPath); string strDbName = respath.Path; // 2007/4/5 改造 加上了 GetXmlStringSimple() string strQueryXml = "<target list='" + strDbName + ":" + "汉字'><item><word>" + StringUtil.GetXmlStringSimple(strOneHanzi) + "</word><match>exact</match><relation>=</relation><dataType>string</dataType><maxCount>10</maxCount></item><lang>chi</lang></target>"; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在检索拼音 '" + strOneHanzi + "'"); stop.BeginLoop(); try { long nRet = channel.DoSearch(strQueryXml, "default", out strError); if (nRet == -1) { strError = "检索拼音库时出错: " + strError; return -1; } if (nRet == 0) return 0; // not found List<string> aPath = null; nRet = channel.DoGetSearchResult( "default", 1, this.Lang, stop, out aPath, out strError); if (nRet == -1) { strError = "检索拼音库获取检索结果时出错: " + strError; return -1; } if (aPath.Count == 0) { strError = "检索拼音库获取的检索结果为空"; return -1; } string strStyle = "content,data"; string strContent; string strMetaData; byte[] baTimeStamp; string strOutputPath; nRet = channel.GetRes((string)aPath[0], strStyle, // this.eventClose, out strContent, out strMetaData, out baTimeStamp, out strOutputPath, out strError); if (nRet == -1) { strError = "获取拼音记录体时出错: " + strError; return -1; } // 取出拼音字符串 XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strContent); } catch (Exception ex) { strError = "汉字 '" + strOneHanzi + "' 所获取的拼音记录 " + strContent + " XML数据装载出错: " + ex.Message; return -1; } strPinyin = DomUtil.GetAttr(dom.DocumentElement, "p"); return 1; } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); } } finally { channel = channelSave; } }
// 删除一个数据库,并删除library.xml中相关OPAC检索库定义 // 如果数据库不存在会当作出错-1来报错 int DeleteDatabase(RmsChannel channel, string strDbName, out string strError) { strError = ""; long lRet = channel.DoDeleteDB(strDbName, out strError); if (lRet == -1 && channel.ErrorCode != ChannelErrorCode.NotFound) return -1; // 删除一个数据库在OPAC可检索库中的定义 // return: // -1 error // 0 not change // 1 changed int nRet = RemoveOpacDatabaseDef( channel.Container, strDbName, out strError); if (nRet == -1) { this.Changed = true; return -1; } return 0; }
// return: // -1 出错 // 0 没有找到 // 1 找到 int GetMarcDefCfgFile(string strUrl, string strDbName, out Stream s, out string strError) { strError = ""; s = null; if (String.IsNullOrEmpty(strUrl) == true) { /* strError = "URL为空"; goto ERROR1; */ return 0; } string strPath = strDbName + "/cfgs/marcdef"; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(strUrl); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { string strContent; // string strError; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载文件" + strPath); stop.BeginLoop(); byte[] baTimeStamp = null; string strMetaData; string strOutputPath; long lRet = channel.GetRes( MainForm.cfgCache, strPath, out strContent, out strMetaData, out baTimeStamp, out strOutputPath, out strError); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { if (channel.ErrorCode == ChannelErrorCode.NotFound) return 0; strError = "获得配置文件 '" +strPath+ "' 时出错:" + strError; goto ERROR1; } else { byte [] baContent = StringUtil.GetUtf8Bytes(strContent, true); MemoryStream stream = new MemoryStream(baContent); s = stream; } } finally { channel = channelSave; } return 1; ERROR1: return -1; }
// parameters: // change_complte 数据名修改成功后的收尾工作 int ChangeDbName( RmsChannel channel, string strOldDbName, string strNewDbName, Action change_complete, out string strError) { strError = ""; // TODO: 要对 strNewDbName 进行查重,看看是不是已经有同名的数据库存在了 // 另外 DoSetDBInfo() API 是否负责查重? List<string[]> log_names = new List<string[]>(); string[] one = new string[2]; one[0] = strNewDbName; one[1] = "zh"; log_names.Add(one); // 修改数据库信息 // parameters: // logicNames 逻辑库名。ArrayList。每个元素为一个string[2]类型。其中第一个字符串为名字,第二个为语言代码 // return: // -1 出错 // 0 成功(基于WebService接口CreateDb的返回值) long lRet = channel.DoSetDBInfo( strOldDbName, log_names, null, // string strType, null, // string strSqlDbName, null, // string strKeysDef, null, // string strBrowseDef, out strError); if (lRet == -1) return -1; // 数据名修改成功后的收尾工作 change_complete(); // 修改一个数据库在OPAC可检索库中的定义的名字 // return: // -1 error // 0 not change // 1 changed int nRet = RenameOpacDatabaseDef( channel.Container, strOldDbName, strNewDbName, out strError); if (nRet == -1) return -1; return 0; }
int m_nInGetCfgFile = 0; // 防止GetCfgFile()函数重入 2008/3/6 // marc编辑窗要从外部获得配置文件内容 private void MarcEditor_GetConfigFile(object sender, DigitalPlatform.Marc.GetConfigFileEventArgs e) { if (m_nInGetCfgFile > 0) { e.ErrorInfo = "MarcEditor_GetConfigFile() 重入了"; return; } if (String.IsNullOrEmpty(textBox_recPath.Text)) { e.ErrorInfo = "URL为空"; return; } // 下载配置文件 ResPath respath = new ResPath(textBox_recPath.Text); string strCfgFileName = e.Path; int nRet = strCfgFileName.IndexOf("#"); if (nRet != -1) { strCfgFileName = strCfgFileName.Substring(0, nRet); } string strPath = ResPath.GetDbName(respath.Path) + "/cfgs/" + strCfgFileName; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); m_nInGetCfgFile++; try { string strContent; string strError; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载文件" + strPath); stop.BeginLoop(); byte[] baTimeStamp = null; string strMetaData; string strOutputPath; long lRet = channel.GetRes( MainForm.cfgCache, strPath, out strContent, out strMetaData, out baTimeStamp, out strOutputPath, out strError); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { if (channel.ErrorCode == ChannelErrorCode.NotFound) { e.ErrorInfo = ""; return; } e.ErrorInfo = "获得配置文件 '" + strPath + "' 时出错:" + strError; return; } else { byte[] baContent = StringUtil.GetUtf8Bytes(strContent, true); MemoryStream stream = new MemoryStream(baContent); e.Stream = stream; } } finally { channel = channelSave; m_nInGetCfgFile--; } }
// 根据数据库模板的定义,创建一个数据库 int CreateDatabase(RmsChannel channel, string strTemplateDir, string strDatabaseName, out string strError) { strError = ""; int nRet = 0; List<string[]> logicNames = new List<string[]>(); string[] cols = new string[2]; cols[1] = "zh"; cols[0] = strDatabaseName; logicNames.Add(cols); string strKeysDefFileName = PathUtil.MergePath(strTemplateDir, "keys"); string strBrowseDefFileName = PathUtil.MergePath(strTemplateDir, "browse"); nRet = ConvertGb2312TextfileToUtf8(strKeysDefFileName, out strError); if (nRet == -1) return -1; nRet = ConvertGb2312TextfileToUtf8(strBrowseDefFileName, out strError); if (nRet == -1) return -1; string strKeysDef = ""; string strBrowseDef = ""; StreamReader sr = null; try { sr = new StreamReader(strKeysDefFileName, Encoding.UTF8); strKeysDef = sr.ReadToEnd(); sr.Close(); } catch (Exception ex) { strError = "装载文件 " + strKeysDefFileName + " 时发生错误: " + ex.Message; return -1; } try { sr = new StreamReader(strBrowseDefFileName, Encoding.UTF8); strBrowseDef = sr.ReadToEnd(); sr.Close(); } catch (Exception ex) { strError = "装载文件 " + strBrowseDefFileName + " 时发生错误: " + ex.Message; return -1; } long lRet = channel.DoCreateDB(logicNames, "", // strType, "", // strSqlDbName, strKeysDef, strBrowseDef, out strError); if (lRet == -1) { strError = "创建数据库 " + strDatabaseName + " 时发生错误: " + strError; return -1; } lRet = channel.DoInitialDB(strDatabaseName, out strError); if (lRet == -1) { strError = "初始化数据库 " + strDatabaseName + " 时发生错误: " + strError; return -1; } // 增补其他数据从属对象 /* List<string> subdirs = new List<string>(); // 创建所有目录对象 GetSubdirs(strTemplateDir, ref subdirs); for (int i = 0; i < subdirs.Count; i++) { string strDiskPath = subdirs[i]; // 反过来推算为逻辑路径 // 或者预先在获得的数组中就存放为部分(逻辑)路径? string strPath = ""; // 在服务器端创建对象 // parameters: // strStyle 风格。当创建目录的时候,为"createdir",否则为空 // return: // -1 错误 // 1 以及存在同名对象 // 0 正常返回 nRet = NewServerSideObject( channel, strPath, "createdir", null, null, out strError); if (nRet == -1) return -1; } // 列出每个目录中的文件,并在服务器端创建之 // 注意模板目录下的文件,被当作cfgs中的文件来创建 * */ DirectoryInfo di = new DirectoryInfo(strTemplateDir); FileInfo[] fis = di.GetFiles(); // 创建所有文件对象 for (int i = 0; i < fis.Length; i++) { string strName = fis[i].Name; if (strName == "." || strName == "..") continue; if (strName.ToLower() == "keys" || strName.ToLower() == "browse") continue; string strFullPath = fis[i].FullName; nRet = ConvertGb2312TextfileToUtf8(strFullPath, out strError); if (nRet == -1) return -1; Stream s = new FileStream(strFullPath, FileMode.Open); try { string strPath = strDatabaseName + "/cfgs/" + strName; // 在服务器端创建对象 // parameters: // strStyle 风格。当创建目录的时候,为"createdir",否则为空 // return: // -1 错误 // 1 以及存在同名对象 // 0 正常返回 nRet = NewServerSideObject( channel, strPath, "", s, null, out strError); if (nRet == -1) return -1; } finally { s.Close(); } } return 0; }
private void MarcEditor_GetConfigDom(object sender, GetConfigDomEventArgs e) { if (String.IsNullOrEmpty(textBox_recPath.Text) == true) { e.ErrorInfo = "记录路径为空,无法获得配置文件 '" + e.Path + "'"; return; } ResPath respath = new ResPath(textBox_recPath.Text); // 得到干净的文件名 string strCfgFileName = e.Path; int nRet = strCfgFileName.IndexOf("#"); if (nRet != -1) { strCfgFileName = strCfgFileName.Substring(0, nRet); } string strPath = ResPath.GetDbName(respath.Path) + "/cfgs/" + strCfgFileName; // 在cache中寻找 e.XmlDocument = this.MainForm.DomCache.FindObject(strPath); if (e.XmlDocument != null) return; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); m_nInGetCfgFile++; try { string strContent; string strError; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载文件" + strPath); stop.BeginLoop(); byte[] baTimeStamp = null; string strMetaData; string strOutputPath; long lRet = channel.GetRes( MainForm.cfgCache, strPath, out strContent, out strMetaData, out baTimeStamp, out strOutputPath, out strError); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { if (channel.ErrorCode == ChannelErrorCode.NotFound) { e.ErrorInfo = ""; return; } e.ErrorInfo = "获得配置文件 '" + strPath + "' 时出错:" + strError; return; } XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strContent); } catch (Exception ex) { e.ErrorInfo = "配置文件 '" + strPath + "' 装入XMLDUM时出错: " + ex.Message; return; } e.XmlDocument = dom; this.MainForm.DomCache.SetObject(strPath, dom); // 保存到缓存 } finally { channel = channelSave; m_nInGetCfgFile--; } }
// 数据库是否已经存在? // return: // -1 error // 0 not exist // 1 exist // 2 其他类型的同名对象已经存在 int IsDatabaseExist( RmsChannel channel, string strDatabaseName, out string strError) { strError = ""; // 看看数据库是否已经存在 ResInfoItem[] items = null; long lRet = channel.DoDir("", "zh", "", // style out items, out strError); if (lRet == -1) { strError = "列服务器 " + channel.Url + " 下全部数据库目录的时候出错: " + strError; return -1; } for (int i = 0; i < items.Length; i++) { if (items[i].Name == strDatabaseName) { if (items[i].Type == ResTree.RESTYPE_DB) { strError = "数据库 " + strDatabaseName + " 已经存在。"; return 1; } else { strError = "和数据库 " + strDatabaseName + " 同名的非数据库类型对象已经存在。"; return 2; } } } return 0; }
public void LoadTemplate() { if (this.Changed == true) { DialogResult result = MessageBox.Show(this, "装载模板前,发现当前窗口中已有内容修改后未来得及保存。是否要继续装载模板到窗口中(这样将丢失先前修改的内容)?\r\n\r\n(是)继续装载模板 (否)不装载模板", "dp2rms", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result != DialogResult.Yes) { MessageBox.Show(this, "装载模板操作被放弃..."); return; } } OpenResDlg dlg = new OpenResDlg(); dlg.Font = GuiUtil.GetDefaultFont(); dlg.Text = "请选择目标数据库"; dlg.EnabledIndices = new int[] { ResTree.RESTYPE_DB }; dlg.ap = this.MainForm.AppInfo; dlg.ApCfgTitle = "detailform_openresdlg"; dlg.Path = textBox_recPath.Text; dlg.Initial( MainForm.Servers, this.Channels); // dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return; textBox_recPath.Text = dlg.Path + "/?"; // 为了追加保存 // 下载配置文件 ResPath respath = new ResPath(dlg.Path); // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { string strContent; string strError; string strCfgFilePath = respath.Path + "/cfgs/template"; stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载文件" + strCfgFilePath); stop.BeginLoop(); byte[] baTimeStamp = null; string strMetaData; string strOutputPath; long lRet = channel.GetRes( MainForm.cfgCache, strCfgFilePath, // this.eventClose, out strContent, out strMetaData, out baTimeStamp, out strOutputPath, out strError); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); if (lRet == -1) { this.TimeStamp = null; MessageBox.Show(this, strError); return; } else { // MessageBox.Show(this, strContent); SelectRecordTemplateDlg tempdlg = new SelectRecordTemplateDlg(); tempdlg.Font = GuiUtil.GetDefaultFont(); int nRet = tempdlg.Initial(strContent, out strError); if (nRet == -1) { MessageBox.Show(this, "装载配置文件 '" + strCfgFilePath + "' 发生错误: " + strError); return; } tempdlg.ap = this.MainForm.AppInfo; tempdlg.ApCfgTitle = "detailform_selecttemplatedlg"; tempdlg.ShowDialog(this); if (tempdlg.DialogResult != DialogResult.OK) return; this.TimeStamp = null; this.m_strMetaData = ""; // 记忆XML记录的元数据 this.strDatabaseOriginPath = ""; // 保存从数据库中来的原始path nRet = this.SetRecordToControls(tempdlg.SelectedRecordXml, out strError); if (nRet == -1) { MessageBox.Show(this, strError); return; } this.TimeStamp = baTimeStamp; this.Text = respath.ReverseFullPath; } } finally { channel = channelSave; } }
private void button_OK_Click(object sender, System.EventArgs e) { if (textBox_url.Text == "") { MessageBox.Show(this, "尚未指定服务器URL..."); return; } if (textBox_newPassword.Text != textBox_newPasswordConfirm.Text) { MessageBox.Show(this, "新密码和确认密码不一致,请重新输入..."); return; } if (textBox_userName.Text == "") { MessageBox.Show(this, "尚未输入用户名。"); return; } channel = Channels.GetChannel(textBox_url.Text); Debug.Assert(channel != null, "Channels.GetChannel 异常"); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在修改密码..."); stop.BeginLoop(); int nRet; string strError; EnableControls(false); button_Cancel.Text = "中断"; nRet = channel.ChangePassword( textBox_userName.Text, textBox_oldPassword.Text, textBox_newPassword.Text, checkBox_manager.Checked, out strError); EnableControls(true); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); button_Cancel.Enabled = true; // 因为Cancel按钮还有退出对话框的功能 button_Cancel.Text = "取消"; if (nRet == -1) goto ERROR1; channel = null; MessageBox.Show(this, "密码修改成功。"); this.DialogResult = DialogResult.OK; this.Close(); return; ERROR1: button_Cancel.Enabled = true; button_Cancel.Text = "取消"; channel = null; MessageBox.Show(this, "修改密码失败,原因:" + strError); }
// 装载记录 // 把strRecordPath表示的记录装载到窗口中,并且在窗口第一行 // 路径内容设置好 // parameters: // strRecordPath 记录路径。如果==null,表示直接用textBox_recPath中当前的内容作为路径 // strExtStyle 如果为null,表示获取strRecordPath或textbox表示的记录。如果为"next"或"prev", // 则表示取其后或前一条记录 // return: // -2 放弃 // -1 出错 // 0 正常 // 1 到头或者到尾 public int LoadRecord(string strRecordPath, string strExtStyle, out string strError) { strError = ""; EnableControlsInLoading(true); try { if (this.Changed == true) { DialogResult result = MessageBox.Show(this, "装载新内容前, 发现当前窗口中已有内容修改后未来得及保存。是否要继续装载新内容到窗口中(这样将丢失先前修改过的内容)?\r\n\r\n(是)继续装载新内容 (否)不装载新内容", "dp2rms", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result != DialogResult.Yes) { strError = "装载新内容操作被放弃..."; return -2; } } if (strRecordPath != null) textBox_recPath.Text = strRecordPath; ResPath respath = new ResPath(textBox_recPath.Text); this.Text = respath.ReverseFullPath; string strContent; string strMetaData; // string strError; byte [] baTimeStamp = null; string strOutputPath; // 使用Channel RmsChannel channelSave = channel; channel = Channels.GetChannel(respath.Url); Debug.Assert(channel != null, "Channels.GetChannel 异常"); try { string strStyle = "content,data,metadata,timestamp,outputpath,withresmetadata"; // if (strExtStyle != null && strExtStyle != "") { strStyle += "," + strExtStyle; } stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在装载记录" + respath.FullPath); stop.BeginLoop(); long lRet = channel.GetRes(respath.Path, strStyle, // this.eventClose, out strContent, out strMetaData, out baTimeStamp, out strOutputPath, out strError); stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); this.TimeStamp = baTimeStamp; // 设置时间戳很重要。即便xml不合法,也应设置好时间戳,否则窗口无法进行正常删除。 this.strDatabaseOriginPath = respath.Url+"?"+strOutputPath; // 保存从数据库中来的原始path if (lRet == -1) { if (channel.ErrorCode == ChannelErrorCode.NotFoundSubRes) { // 下级资源不存在, 警告一下就行了 MessageBox.Show(this, strError); goto CONTINUELOAD; } else if (channel.ErrorCode == ChannelErrorCode.NotFound) { if (strExtStyle == "prev") strError = "到头"; else if (strExtStyle == "next") strError = "到尾"; return 1; } else { // this.TimeStamp = null; strError = "从路径 '"+respath.Path+"' 获取记录时出错: " + strError; return -1; } } } finally { channel = channelSave; } CONTINUELOAD: respath.Path = strOutputPath; textBox_recPath.Text = respath.FullPath; //string strTemp = ByteArray.GetHexTimeStampString(baTimeStamp); this.m_strMetaData = strMetaData; // 记忆XML记录的元数据 int nRet = SetRecordToControls(strContent, out strError); if (nRet == -1) return -1; return 0; } finally { EnableControlsInLoading(false); } }
// 获得一行信息 int GetLineInfo( RmsChannel channel, string strPath, out string strSender, out string strRecipient, out string strSubject, out string strDate, out string strSize, out bool bTouched, out string strError) { strSender = ""; strRecipient = ""; strSubject = ""; strDate = ""; strSize = ""; bTouched = false; strError = ""; // 将种记录数据从XML格式转换为HTML格式 string strMetaData = ""; byte[] timestamp = null; string strXml = ""; string strOutputPath = ""; long lRet = channel.GetRes(strPath, out strXml, out strMetaData, out timestamp, out strOutputPath, out strError); if (lRet == -1) { // text-level: 内部错误 strError = "获得消息记录 '" + strPath + "' 时出错: " + strError; return -1; } XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strXml); } catch (Exception ex) { // text-level: 内部错误 strError = "装载XML记录进入DOM时出错: " + ex.Message; return -1; } strSender = DomUtil.GetElementText(dom.DocumentElement, "sender"); strRecipient = DomUtil.GetElementText(dom.DocumentElement, "recipient"); strSubject = DomUtil.GetElementText(dom.DocumentElement, "subject"); strDate = DomUtil.GetElementText(dom.DocumentElement, "date"); strDate = DateTimeUtil.LocalTime(strDate); strSize = DomUtil.GetElementText(dom.DocumentElement, "size"); string strTouched = DomUtil.GetElementText(dom.DocumentElement, "touched"); if (strTouched == "1") bTouched = true; else bTouched = false; return 0; }
private void CfgFileEditDlg_Load(object sender, System.EventArgs e) { button_export.Enabled = false; MemoryStream stream = null; string strMetaData; string strError = ""; string strMime = ""; Hashtable values = null; if (Obj != null) { if (this.Obj.Content != null) { stream = new MemoryStream(this.Obj.Content); this.Stream = stream; this.Stream.Seek(0, SeekOrigin.Begin); button_export.Enabled = true; } this.TimeStamp = this.Obj.TimeStamp; strMetaData = this.Obj.Metadata; // 观察mime // 取metadata values = StringUtil.ParseMetaDataXml(strMetaData, out strError); if (values == null) { MessageBox.Show(this, strError); return; } strMime = (string)values["mimetype"]; if (strMime == null || strMime == "") strMime = "text"; this.Mime = strMime; this.LocalPath = (string)values["localpath"]; if (this.LocalPath == null) this.LocalPath = ""; this.textBox_content.Text = ""; // string strFirstPart = StringUtil.GetFirstPartPath(ref strMime); if (this.IsText == true) { if (this.Stream != null) { this.Stream.Seek(0, SeekOrigin.Begin); using (StreamReader sr = new StreamReader(this.Stream, Encoding.UTF8)) { this.textBox_content.Text = ConvertCrLf(sr.ReadToEnd()); } } } else { } ////// button_OK.Enabled = false; this.textBox_content.SelectionStart = 0; this.textBox_content.SelectionLength = 0; return; } this.channel = Channels.GetChannel(this.ServerUrl); Debug.Assert(channel != null, "Channels.GetChannel() 异常"); DigitalPlatform.Stop stop = null; if (stopManager != null) { stop = new DigitalPlatform.Stop(); stop.Register(this.stopManager, true); // 和容器关联 stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在下载配置文件: " + this.Path); stop.BeginLoop(); } // string strContent = ""; byte[] baTimeStamp = null; string strOutputPath; string strStyle = "content,data,metadata,timestamp,outputpath"; // string strStyle = "attachment,data,metadata,timestamp,outputpath"; stream = new MemoryStream(); long lRet = channel.GetRes( this.Path, stream, stop, // stop, strStyle, null, // byte [] input_timestamp, out strMetaData, out baTimeStamp, out strOutputPath, out strError); /* long lRet = channel.GetRes(( this.Path, out strContent, out strMetaData, out baTimeStamp, out strOutputPath, out strError); */ if (stopManager != null) { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); } if (lRet == -1) { MessageBox.Show(this, strError); goto FINISH; } this.Stream = stream; this.Stream.Seek(0, SeekOrigin.Begin); button_export.Enabled = true; this.TimeStamp = baTimeStamp; // 观察mime // 取metadata values = StringUtil.ParseMetaDataXml(strMetaData, out strError); if (values == null) { MessageBox.Show(this, strError); goto FINISH; } strMime = (string)values["mimetype"]; if (strMime == null || strMime == "") strMime = "text"; this.Mime = strMime; this.LocalPath = (string)values["localpath"]; if (this.LocalPath == null) this.LocalPath = ""; // string strFirstPart = StringUtil.GetFirstPartPath(ref strMime); if (this.IsText == true) { this.Stream.Seek(0, SeekOrigin.Begin); using (StreamReader sr = new StreamReader(this.Stream, Encoding.UTF8)) { this.textBox_content.Text = ConvertCrLf(sr.ReadToEnd()); } // 注意,此后 this.Stream 被关闭 } else { //this.textBox_content.Text = "<二进制内容无法直接编辑>"; //this.textBox_content.ReadOnly = true; //this.button_format.Enabled = false; } ////// button_OK.Enabled = false; FINISH: if (stopManager != null && stop != null) { stop.Unregister(); // 和容器关联 stop = null; } this.channel = null; this.textBox_content.SelectionStart = 0; this.textBox_content.SelectionLength = 0; }