int GetBrowseCols(List <string> pathlist, List <ListViewItem> itemlist, out string strError) { strError = ""; // 获得server url if (String.IsNullOrEmpty(this.LibraryServerName) == true) { strError = "尚未指定服务器名"; return(-1); } dp2Server server = this.MainForm.Servers.GetServerByName(this.LibraryServerName); if (server == null) { strError = "服务器名为 '" + this.LibraryServerName + "' 的服务器不存在..."; return(-1); } string strServerUrl = server.Url; this.Channel = this.Channels.GetChannel(strServerUrl); EnableControls(false); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在填充浏览列 ..."); stop.BeginLoop(); this.Update(); this.MainForm.Update(); try { int nStart = 0; int nCount = 0; for (; ;) { nCount = pathlist.Count - nStart; if (nCount > 100) { nCount = 100; } if (nCount <= 0) { break; } Application.DoEvents(); // 出让界面控制权 if (stop != null) { if (stop.State != 0) { strError = "用户中断"; return(-1); } } stop.SetMessage("正在装入浏览信息 " + (nStart + 1).ToString() + " - " + (nStart + nCount).ToString()); string[] paths = new string[nCount]; pathlist.CopyTo(nStart, paths, 0, nCount); DigitalPlatform.LibraryClient.localhost.Record[] searchresults = null; long lRet = this.Channel.GetBrowseRecords( this.stop, paths, "id,cols", out searchresults, out strError); if (lRet == -1) { return(-1); } if (searchresults == null || searchresults.Length == 0) { strError = "searchresults == null || searchresults.Length == 0"; return(-1); } for (int i = 0; i < searchresults.Length; i++) { DigitalPlatform.LibraryClient.localhost.Record record = searchresults[i]; ListViewUtil.EnsureColumns(this.listView_browse, 2 + (record.Cols == null ? 0 : record.Cols.Length), 200); ListViewItem item = itemlist[nStart + i]; item.Text = record.Path; if (record.Cols != null) { for (int j = 0; j < record.Cols.Length; j++) { item.SubItems.Add(record.Cols[j]); } } } nStart += searchresults.Length; } } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); EnableControls(true); } return(0); }
// 初始化一个读者库的指纹缓存 // return: // -1 出错 // >=0 实际发送给接口程序的事项数目 static int BuildOneDbCache( LibraryChannel channel, string strDir, string strReaderDbName, CancellationToken token, out string strError) { strError = ""; int nRet = 0; DpResultSet resultset = null; bool bCreate = false; Hashtable timestamp_table = new Hashtable(); // recpath --> fingerprint timestamp ShowMessage(strReaderDbName); // 结果集文件名 string strResultsetFilename = Path.Combine(strDir, strReaderDbName); if (File.Exists(strResultsetFilename) == false) { resultset = new DpResultSet(false, false); resultset.Create(strResultsetFilename, strResultsetFilename + ".index"); bCreate = true; } else { bCreate = false; } // *** 第一阶段, 创建新的结果集文件;或者获取全部读者记录中的指纹时间戳 bool bDone = false; // 创建情形下 是否完成了写入操作 try { /* * long lRet = Channel.SearchReader(stop, * strReaderDbName, * "1-9999999999", * -1, * "__id", * "left", * this.Lang, * null, // strResultSetName * "", // strOutputStyle * out strError); */ long lRet = channel.SearchReader(null, // stop, strReaderDbName, "", -1, "指纹时间戳", "left", ClientInfo.Lang, null, // strResultSetName "", // strOutputStyle out strError); if (lRet == -1) { if (channel.ErrorCode == ErrorCode.AccessDenied) { strError = "用户 " + channel.UserName + " 权限不足: " + strError; } return(-1); } if (lRet == 0) { // TODO: 这时候如果以前有结果集文件还会残留,但不会影响功能正确性,可以改进为把残留的结果集文件删除 return(0); } long lHitCount = lRet; ResultSetLoader loader = new ResultSetLoader(channel, null, null, bCreate == true ? "id,cols,format:cfgs/browse_fingerprint" : "id,cols,format:cfgs/browse_fingerprinttimestamp", ClientInfo.Lang); loader.Prompt += Loader_Prompt; foreach (DigitalPlatform.LibraryClient.localhost.Record record in loader) { token.ThrowIfCancellationRequested(); ShowMessage("正在处理读者记录 " + record.Path); if (bCreate == true) { if (record.Cols == null || record.Cols.Length < 3) { strError = "record.Cols error ... 有可能是因为读者库缺乏配置文件 cfgs/browse_fingerprint"; return(-1); } if (string.IsNullOrEmpty(record.Cols[0]) == true) { continue; // 读者记录中没有指纹信息 } DpRecord item = new DpRecord(record.Path); // timestamp | barcode | fingerprint item.BrowseText = record.Cols[0] + "|" + record.Cols[1] + "|" + record.Cols[2]; resultset.Add(item); } else { if (record.Cols == null || record.Cols.Length < 1) { strError = "record.Cols error ... 有可能是因为读者库缺乏配置文件 cfgs/browse_fingerprinttimestamp"; return(-1); } if (record.Cols.Length < 2) { strError = "record.Cols error ... 需要刷新配置文件 cfgs/browse_fingerprinttimestamp 到最新版本"; return(-1); } if (string.IsNullOrEmpty(record.Cols[0]) == true) { continue; // 读者记录中没有指纹信息 } // 记载时间戳 // timestamp | barcode timestamp_table[record.Path] = record.Cols[0] + "|" + record.Cols[1]; } } #if NO // stop.SetProgressRange(0, lHitCount); long lStart = 0; long lCount = lHitCount; DigitalPlatform.LibraryClient.localhost.Record[] searchresults = null; // 装入浏览格式 for (; ;) { token.ThrowIfCancellationRequested(); lRet = channel.GetSearchResult( null, null, // strResultSetName lStart, lCount, bCreate == true ? "id,cols,format:cfgs/browse_fingerprint" : "id,cols,format:cfgs/browse_fingerprinttimestamp", ClientInfo.Lang, out searchresults, out strError); if (lRet == -1) { return(-1); } if (lRet == 0) { strError = "GetSearchResult() return 0"; return(-1); } Debug.Assert(searchresults != null, ""); Debug.Assert(searchresults.Length > 0, ""); for (int i = 0; i < searchresults.Length; i++) { DigitalPlatform.LibraryClient.localhost.Record record = searchresults[i]; if (bCreate == true) { if (record.Cols == null || record.Cols.Length < 3) { strError = "record.Cols error ... 有可能是因为读者库缺乏配置文件 cfgs/browse_fingerprint"; return(-1); } if (string.IsNullOrEmpty(record.Cols[0]) == true) { continue; // 读者记录中没有指纹信息 } DpRecord item = new DpRecord(record.Path); // timestamp | barcode | fingerprint item.BrowseText = record.Cols[0] + "|" + record.Cols[1] + "|" + record.Cols[2]; resultset.Add(item); } else { if (record.Cols == null || record.Cols.Length < 1) { strError = "record.Cols error ... 有可能是因为读者库缺乏配置文件 cfgs/browse_fingerprinttimestamp"; return(-1); } if (record.Cols.Length < 2) { strError = "record.Cols error ... 需要刷新配置文件 cfgs/browse_fingerprinttimestamp 到最新版本"; return(-1); } if (string.IsNullOrEmpty(record.Cols[0]) == true) { continue; // 读者记录中没有指纹信息 } // 记载时间戳 // timestamp | barcode timestamp_table[record.Path] = record.Cols[0] + "|" + record.Cols[1]; } } lStart += searchresults.Length; lCount -= searchresults.Length; //stop.SetMessage(strReaderDbName + " 包含记录 " + lHitCount.ToString() + " 条,已装入 " + lStart.ToString() + " 条"); if (lStart >= lHitCount || lCount <= 0) { break; } //stop.SetProgressValue(lStart); } #endif if (bCreate == true) { bDone = true; } if (bCreate == true) { // return: // -2 remoting服务器连接失败。驱动程序尚未启动 // -1 出错 // >=0 实际发送给接口程序的事项数目 nRet = CreateFingerprintCache(resultset, out strError); if (nRet == -1 || nRet == -2) { return(-1); } return(nRet); } } finally { if (bCreate == true) { Debug.Assert(resultset != null, ""); if (bDone == true) { resultset.Detach(out string strTemp1, out string strTemp2); } else { // 否则文件会被删除 resultset.Close(); } } } // 比对时间戳,更新结果集文件 Hashtable update_table = new Hashtable(); // 需要更新的事项。recpath --> 1 resultset = new DpResultSet(false, false); resultset.Attach(strResultsetFilename, strResultsetFilename + ".index"); try { long nCount = resultset.Count; for (long i = 0; i < nCount; i++) { token.ThrowIfCancellationRequested(); DpRecord record = resultset[i]; string strRecPath = record.ID; ShowMessage("比对 " + strRecPath); // timestamp | barcode string strNewTimestamp = (string)timestamp_table[strRecPath]; if (strNewTimestamp == null) { // 最新状态下,读者记录已经不存在,需要从结果集中删除 resultset.RemoveAt((int)i); i--; nCount--; continue; } // 拆分出证条码号 2013/1/28 string strNewBarcode = ""; nRet = strNewTimestamp.IndexOf("|"); if (nRet != -1) { strNewBarcode = strNewTimestamp.Substring(nRet + 1); strNewTimestamp = strNewTimestamp.Substring(0, nRet); } // 最新读者记录中已经没有指纹信息。例如读者记录中的指纹元素被删除了 if (string.IsNullOrEmpty(strNewTimestamp) == true) { // 删除现有事项 resultset.RemoveAt((int)i); i--; nCount--; timestamp_table.Remove(strRecPath); continue; } // 取得结果集文件中的原有时间戳字符串 string strText = record.BrowseText; // timestamp | barcode | fingerprint nRet = strText.IndexOf("|"); if (nRet == -1) { strError = "browsetext 错误,没有 '|' 字符"; return(-1); } string strOldTimestamp = strText.Substring(0, nRet); // timestamp | barcode | fingerprint string strOldBarcode = strText.Substring(nRet + 1); nRet = strOldBarcode.IndexOf("|"); if (nRet != -1) { strOldBarcode = strOldBarcode.Substring(0, nRet); } // 时间戳发生变化,需要更新事项 if (strNewTimestamp != strOldTimestamp || strNewBarcode != strOldBarcode) { // 如果证条码号为空,无法建立对照关系,要跳过 if (string.IsNullOrEmpty(strNewBarcode) == false) { update_table[strRecPath] = 1; } // 删除现有事项 resultset.RemoveAt((int)i); i--; nCount--; } timestamp_table.Remove(strRecPath); } // 循环结束后,timestamp_table中剩余的是当前结果集文件中没有包含的那些读者记录路径 if (update_table.Count > 0) { // 获取指纹信息,追加到结果集文件的尾部 // parameters: // update_table key为读者记录路径 AppendFingerprintInfo( channel, resultset, update_table, token); } // 如果服务器端新增了指纹信息,需要获取后追加到结果集文件尾部 if (timestamp_table.Count > 0) { // 获取指纹信息,追加到结果集文件的尾部 // parameters: // update_table key为读者记录路径 AppendFingerprintInfo( channel, resultset, timestamp_table, token); } // return: // -2 remoting服务器连接失败。驱动程序尚未启动 // -1 出错 // >=0 实际发送给接口程序的事项数目 nRet = CreateFingerprintCache(resultset, out strError); if (nRet == -1 || nRet == -2) { return(-1); } return(nRet); } finally { resultset.Detach(out string strTemp1, out string strTemp2); } }