Example #1
0
        // 获得资源。写入文件的版本。特别适用于获得资源,也可用于获得主记录体。
        // parameters:
        //		fileTarget	文件。注意在调用函数前适当设置文件指针位置。函数只会在当前位置开始向后写,写入前不会主动改变文件指针。
        //		strStyleParam	一般设置为"content,data,metadata,timestamp,outputpath";
        //		input_timestamp	若!=null,则本函数会把第一个返回的timestamp和本参数内容比较,如果不相等,则报错
        // return:
        //		-1	出错。具体出错原因在this.ErrorCode中。this.ErrorInfo中有出错信息。
        //		0	成功
        public long GetRes(string strPath,
            Stream fileTarget,
			FlushOutput flushOutputMethod,
            DigitalPlatform.Stop stop,
            string strStyleParam,
            byte[] input_timestamp,
            out string strMetaData,
            out byte[] baOutputTimeStamp,
            out string strOutputPath,
            out string strError)
        {
            strError = "";
            baOutputTimeStamp = null;
            strMetaData = "";
            strOutputPath = "";

            this.ErrorCode = ChannelErrorCode.None;
            this.ErrorInfo = "";

            string strStyle = strStyleParam;

            if (StringUtil.IsInList("attachment", strStyle) == true)
            {
                Debug.Assert(false, "attachment style暂时不能使用");
            }


            // 检查参数
            if (StringUtil.IsInList("data", strStyle) == false)
            {
                if (fileTarget != null)
                {
                    strError = "strStyle参数中若不包含data风格,则无法获得数据...";
                    return -1;
                }
            }
            if (StringUtil.IsInList("data", strStyle) == true)
            {
                if (fileTarget == null)
                {
                    strError = "strStyle参数中若包含data风格,而fileTarget为null,会浪费通讯资源...";
                    return -1;
                }
            }

            bool bHasMetadataStyle = false;
            if (StringUtil.IsInList("metadata", strStyle) == true)
            {
                bHasMetadataStyle = true;
            }

            // string id = "";
            byte[] baContent = null;

            long lStart = 0;
            int nPerLength = -1;

            byte[] old_timestamp = null;
            byte[] timestamp = null;

            long lTotalLength = -1;

            for (; ; )
            {
                DoIdle(); // 出让控制权,避免CPU资源耗费过度

                if (stop != null && stop.State != 0)
                {
                    strError = "用户中断";
                    return -1;
                }

                REDO:
                try
                {

                    string strMessage = "";

                    string strPercent = "";
                    if (lTotalLength != -1)
                    {
                        double ratio = (double)lStart / (double)lTotalLength;
                        strPercent = String.Format("{0,3:N}", ratio * (double)100) + "%";
                    }

                    if (stop != null)
                    {
                        strMessage = "正在下载 " + Convert.ToString(lStart) + "-"
                            + (lTotalLength == -1 ? "?" : Convert.ToString(lTotalLength))
                            + " " + strPercent + " "
                            + strPath;
                        stop.SetMessage(strMessage);
                    }

                    IAsyncResult soapresult = this.ws.BeginGetRes(strPath,
                        fileTarget == null ? 0 : lStart,
                        fileTarget == null ? 0 : nPerLength,
                        strStyle,
                        null,
                        null);

                    for (; ; )
                    {

                        /*
                        try 
                        {
                            Application.DoEvents();	// 出让界面控制权
                        }
                        catch
                        {
                        }
					

                        // System.Threading.Thread.Sleep(10);	// 避免CPU资源过度耗费
                         */
                        DoIdle(); // 出让控制权,避免CPU资源耗费过度

                        bool bRet = soapresult.AsyncWaitHandle.WaitOne(100, false);
                        if (bRet == true)
                            break;
                    }
                    if (this.m_ws == null)
                    {
                        strError = "用户中断";
                        this.ErrorCode = ChannelErrorCode.RequestCanceled;
                        return -1;
                    }
                    // string strOutputResPath;
                    Result result = this.ws.EndGetRes(
                        out baContent,
                        // out id,
                        out strMetaData,
                        out strOutputPath,
                        out timestamp,soapresult);

                    // 即便不是返回-1,也可能有错误码和错误信息字符串
                    ConvertErrorCode(result);
                    strError = result.ErrorString;

                    if (result.Value == -1)
                    {
                        if (result.ErrorCode == ErrorCodeValue.NotLogin
                            && this.Container != null)
                        {
                            // return:
                            //		-1	error
                            //		0	login failed
                            //		1	login succeed
                            int nRet = this.UiLogin(strPath,
                                out strError);
                            if (nRet == -1 || nRet == 0)
                            {
                                return -1;
                            }

                            goto REDO;
                        }

                        /*
                        ConvertErrorCode(result);
                        strError = result.ErrorString;
                         */
                        return -1;
                    }

                    if (bHasMetadataStyle == true)
                    {
                        StringUtil.RemoveFromInList("metadata",
                            true,
                            ref strStyle);
                        bHasMetadataStyle = false;
                    }


                    lTotalLength = result.Value;


                    if (StringUtil.IsInList("timestamp", strStyle) == true
                        /*
                        && lTotalLength > 0
                         * */ )    // 2012/1/11
                    {
                        if (input_timestamp != null)
                        {
                            if (ByteArray.Compare(input_timestamp, timestamp) != 0)
                            {
                                strError = "下载过程中发现时间戳和input_timestamp参数中的时间戳不一致,下载失败 ...";
                                return -1;
                            }
                        }
                        if (old_timestamp != null)
                        {
                            if (ByteArray.Compare(old_timestamp, timestamp) != 0)
                            {
                                strError = "下载过程中发现时间戳变化,下载失败 ...";
                                return -1;
                            }
                        }
                    }

                    old_timestamp = timestamp;

                    if (fileTarget == null)
                        break;

                    // 写入文件
                    if (StringUtil.IsInList("attachment", strStyle) == true)
                    {
                        Debug.Assert(false, "attachment style暂时不能使用");
                        /*
						Attachment attachment = ws.ResponseSoapContext.Attachments[id];
						if (attachment == null)
						{
							strError = "id为 '" +id+ "' 的attachment在WebService响应中没有找到...";
							return -1;
						}
						StreamUtil.DumpStream(attachment.Stream, fileTarget);
						nStart += (int)attachment.Stream.Length;

						Debug.Assert(attachment.Stream.Length <= result.Value, "每次返回的包尺寸["+Convert.ToString(attachment.Stream.Length)+"]应当小于result.Value["+Convert.ToString(result.Value)+"]");
                         */

                    }
                    else
                    {
                        Debug.Assert(StringUtil.IsInList("content", strStyle) == true,
                            "不是attachment风格,就应是content风格");

                        Debug.Assert(baContent != null, "返回的baContent不能为null");
                        Debug.Assert(baContent.Length <= result.Value, "每次返回的包尺寸[" + Convert.ToString(baContent.Length) + "]应当小于result.Value[" + Convert.ToString(result.Value) + "]");

                        fileTarget.Write(baContent, 0, baContent.Length);
                        if (flushOutputMethod != null)
                        {
                            if (flushOutputMethod() == false)
                            {
                                strError = "FlushOutputMethod()用户中断";
                                return -1;
                            }
                        } 
                        lStart += baContent.Length;
                    }

                    if (lStart >= result.Value)
                        break;	// 结束

                } // end try


                catch (Exception ex)
                {
                    /*
                    strError = ConvertWebError(ex);
                    return -1;
                     * */
                    int nRet = ConvertWebError(ex, out strError);
                    if (nRet == 0)
                        return -1;
                    goto REDO;
                }

            } // end of for

            baOutputTimeStamp = timestamp;
            this.ClearRedoCount();
            return 0;
        }
Example #2
0
        // 下载资源,保存到备份文件
        public static int WriteResToBackupFile(
            IWin32Window owner,
            Stream outputfile,
            string strXmlRecPath,
            string[] res_ids,
            RmsChannel channel,
            DigitalPlatform.Stop stop,
            out string strError)
        {
            strError = "";


            long lRet;

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

                if (stop.State != 0)
                {
                    DialogResult result = MessageBox.Show(owner,
                        "确实要中断当前批处理操作?",
                        "dp2batch",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question,
                        MessageBoxDefaultButton.Button2);
                    if (result == DialogResult.Yes)
                    {
                        strError = "用户中断";
                        return -1;
                    }
                    else
                    {
                        stop.Continue();
                    }
                }

                string strID = res_ids[i].Trim();

                if (strID == "")
                    continue;

                string strResPath = strXmlRecPath + "/object/" + strID;

                string strMetaData;

                if (stop != null)
                    stop.SetMessage("正在下载 " + strResPath);

                long lResStart = 0;
                // 写res的头。
                // 如果不能预先确知整个res的长度,可以用随便一个lTotalLength值调用本函数,
                // 但是需要记忆下函数所返回的lStart,最后调用EndWriteResToBackupFile()。
                // 如果能预先确知整个res的长度,则最后不必调用EndWriteResToBackupFile()
                lRet = Backup.BeginWriteResToBackupFile(
                    outputfile,
                    0,	// 未知
                    out lResStart);

                byte[] baOutputTimeStamp = null;
                string strOutputPath;

            REDO_GETRES:
                lRet = channel.GetRes(strResPath,
                    (Stream)null,	// 故意不获取资源体
                    stop,
                    "metadata,timestamp,outputpath",
                    null,
                    out strMetaData,	// 但是要获得metadata
                    out baOutputTimeStamp,
                    out strOutputPath,
                    out strError);
                if (lRet == -1)
                {
                    // TODO: 允许重试
                    DialogResult redo_result = MessageBox.Show(owner,
                        "获取记录 '" + strResPath + "' 时出现错误: " + strError + "\r\n\r\n重试,还是中断当前批处理操作?\r\n(Retry 重试;Cancel 中断批处理)",
                        "dp2batch",
                        MessageBoxButtons.RetryCancel,
                        MessageBoxIcon.Question,
                        MessageBoxDefaultButton.Button1);
                    if (redo_result == DialogResult.Cancel)
                        return -1;
                    goto
                        REDO_GETRES;
                }

                byte[] timestamp = baOutputTimeStamp;

                ResPath respath = new ResPath();
                respath.Url = channel.Url;
                respath.Path = strOutputPath;	// strResPath;

                // strMetaData还要加入资源id?
                ExportUtil.ChangeMetaData(ref strMetaData,
                    strID,
                    null,
                    null,
                    null,
                    respath.FullPath,
                    ByteArray.GetHexTimeStampString(baOutputTimeStamp));


                lRet = Backup.WriteResMetadataToBackupFile(outputfile,
                    strMetaData);
                if (lRet == -1)
                    return -1;

                long lBodyStart = 0;
                // 写res body的头。
                // 如果不能预先确知body的长度,可以用随便一个lBodyLength值调用本函数,
                // 但是需要记忆下函数所返回的lBodyStart,最后调用EndWriteResBodyToBackupFile()。
                // 如果能预先确知body的长度,则最后不必调用EndWriteResBodyToBackupFile()
                lRet = Backup.BeginWriteResBodyToBackupFile(
                    outputfile,
                    0, // 未知
                    out lBodyStart);
                if (lRet == -1)
                    return -1;

                if (stop != null)
                    stop.SetMessage("正在下载 " + strResPath + " 的数据体");

            REDO_GETRES_1:
                lRet = channel.GetRes(strResPath,
                    outputfile,
                    stop,
                    "content,data,timestamp", //"content,data,timestamp"
                    timestamp,
                    out strMetaData,
                    out baOutputTimeStamp,
                    out strOutputPath,
                    out strError);
                if (lRet == -1)
                {
                    if (channel.ErrorCode == ChannelErrorCode.EmptyRecord)
                    {
                        // 空记录
                    }
                    else
                    {
                        // TODO: 允许重试
                        DialogResult redo_result = MessageBox.Show(owner,
                            "获取记录 '" + strResPath + "' 时出现错误: " + strError + "\r\n\r\n重试,还是中断当前批处理操作?\r\n(Retry 重试;Cancel 中断批处理)",
                            "dp2batch",
                            MessageBoxButtons.RetryCancel,
                            MessageBoxIcon.Question,
                            MessageBoxDefaultButton.Button1);
                        if (redo_result == DialogResult.Cancel)
                            return -1;
                        goto
                            REDO_GETRES_1;
                    }
                }

                long lBodyLength = outputfile.Position - lBodyStart - 8;
                // res body收尾
                lRet = Backup.EndWriteResBodyToBackupFile(
                    outputfile,
                    lBodyLength,
                    lBodyStart);
                if (lRet == -1)
                    return -1;

                long lTotalLength = outputfile.Position - lResStart - 8;
                lRet = Backup.EndWriteResToBackupFile(
                    outputfile,
                    lTotalLength,
                    lResStart);
                if (lRet == -1)
                    return -1;

            }

            /*
            if (stop != null)
                stop.SetMessage("保存资源到备份文件全部完成");
            */

            return 0;
        }
Example #3
0
        // 模拟创建检索点
        public long DoGetKeys(
            string strRecPath,
            string strXmlBody,
            string strLang,
            // string strStyle,
            ViewAccessPointForm dlg,
            DigitalPlatform.Stop stop,
            out string strError)
        {
            strError = "";

            if (strRecPath == "")
            {
                strError = "记录路径为空时无法模拟创建检索点";
                return -1;
            }

            KeyInfo[] keys = null;

            int nStart = 0;
            int nPerCount = -1;

            int nCount = 0;

            dlg.Clear();

            long lTotalCount = -1;
            for (; ; )
            {
                DoIdle(); // 出让控制权,避免CPU资源耗费过度

                if (stop != null)
                {
                    if (stop.State != 0)
                    {
                        strError = "用户中断";
                        return -1;
                    }
                }


                    REDO:
                try
                {
                    IAsyncResult soapresult = this.ws.BeginCreateKeys(
                        strXmlBody,
                        strRecPath,
                        nStart,
                        nPerCount,
                        strLang,
                        // strStyle,
                        null,
                        null);

                    for (; ; )
                    {
                        DoIdle(); // 出让控制权,避免CPU资源耗费过度
                        bool bRet = soapresult.AsyncWaitHandle.WaitOne(100, false);
                        if (bRet == true)
                            break;
                    }
                    if (this.m_ws == null)
                    {
                        strError = "用户中断";
                        this.ErrorCode = ChannelErrorCode.RequestCanceled;
                        return -1;
                    }
                    Result result = this.ws.EndCreateKeys(
                        out keys,soapresult);

                    if (result.Value == -1)
                    {
                        // 2011/4/21
                        if (result.ErrorCode == ErrorCodeValue.NotLogin
                            && this.Container != null)
                        {
                            // return:
                            //		-1	error
                            //		0	login failed
                            //		1	login succeed
                            int nRet = this.UiLogin("",
                                out strError);
                            if (nRet == -1 || nRet == 0)
                            {
                                return -1;
                            }

                            goto REDO;
                        }
                        ConvertErrorCode(result);
                        strError = result.ErrorString;
                        return -1;
                    }
                    else
                    {
                        Debug.Assert(keys != null, "WebService GetRecords() API record参数返回值不应为null");

                        lTotalCount = result.Value;
                    }

                    if (keys != null)
                    {
                        nStart += keys.Length;
                        nCount += keys.Length;
                    }

                    // 做事
                    for (int i = 0; i < keys.Length; i++)
                    {
                        DoIdle(); // 出让控制权,避免CPU资源耗费过度

                        if (stop != null)
                        {
                            if (stop.State != 0)
                            {
                                strError = "用户中断";
                                return -1;
                            }

                            stop.SetMessage("正在装入 " + Convert.ToString(nStart + i) + " / "
                                + ((lTotalCount == -1) ? "?" : Convert.ToString(lTotalCount)));
                        }



                        KeyInfo keyInfo = keys[i];

                        dlg.NewLine(keyInfo);
                    }

                    if (nCount >= result.Value)
                        break;
                }

                catch (Exception ex)
                {
                    /*
                    strError = ConvertWebError(ex);
                    return -1;
                     * */
                    int nRet = ConvertWebError(ex, out strError);
                    if (nRet == 0)
                        return -1;
                    goto REDO;
                }

            }

            this.ClearRedoCount();
            return 0;
        }
Example #4
0
        // 2009/7/19
        // 获取检索命中结果
        // 获得某一列信息的版本
        public long DoGetSearchResultOneColumn(
            string strResultSetName,
            long lStart,
            long lMax,
            string strLang,
            DigitalPlatform.Stop stop,
            int nColumn,
            out List<string> aLine,
            out string strError)
        {
            strError = "";
            aLine = new List<string>();

            Record[] records = null;

            long nPerCount = lMax;	// -1;

            int nCount = 0;

            long lTotalCount = -1;
            for (; ; )
            {
                DoIdle(); // 出让控制权,避免CPU资源耗费过度

                if (stop != null)
                {
                    if (stop.State != 0)
                    {
                        strError = "用户中断";
                        return -1;
                    }
                }


                    REDO:
                try
                {
                    IAsyncResult soapresult = this.ws.BeginGetRecords(
                        strResultSetName,
                        lStart,
                        nPerCount,
                        strLang,
                        "id,cols",	// 不要cols
                        null,
                        null);

                    for (; ; )
                    {
                        DoIdle(); // 出让控制权,避免CPU资源耗费过度
                        bool bRet = soapresult.AsyncWaitHandle.WaitOne(100, false);
                        if (bRet == true)
                            break;
                    }
                    if (this.m_ws == null)
                    {
                        strError = "用户中断";
                        this.ErrorCode = ChannelErrorCode.RequestCanceled;
                        return -1;
                    }
                    Result result = this.ws.EndGetRecords(
                        out records,soapresult);

                    if (result.Value == -1)
                    {
                        // 2011/4/21
                        if (result.ErrorCode == ErrorCodeValue.NotLogin
                            && this.Container != null)
                        {
                            // return:
                            //		-1	error
                            //		0	login failed
                            //		1	login succeed
                            int nRet = this.UiLogin("",
                                out strError);
                            if (nRet == -1 || nRet == 0)
                            {
                                return -1;
                            }

                            goto REDO;
                        }
                        ConvertErrorCode(result);
                        strError = result.ErrorString;
                        return -1;
                    }
                    else
                    {
                        Debug.Assert(records != null, "WebService GetRecords() API record参数返回值不应为null");

                        lTotalCount = result.Value;
                        if (lMax != -1)
                            lTotalCount = Math.Min(lTotalCount, lMax);
                    }

                    if (records != null)
                    {
                        lStart += records.Length;
                        nCount += records.Length;
                        nPerCount = lTotalCount - lStart;
                    }

                    // 做事
                    for (int i = 0; i < records.Length; i++)
                    {
                        DoIdle(); // 出让控制权,避免CPU资源耗费过度

                        if (stop != null)
                        {
                            if (stop.State != 0)
                            {
                                strError = "用户中断";
                                return -1;
                            }

                            stop.SetMessage("正在装入 " + Convert.ToString(lStart + i) + " / "
                                + ((lTotalCount == -1) ? "?" : Convert.ToString(lTotalCount)));
                        }



                        Record record = records[i];
                        aLine.Add(record.Cols[nColumn]);
                    }

                    if (nCount >= result.Value || nCount >= lTotalCount)
                        break;

                }

                catch (Exception ex)
                {
                    /*
                    strError = ConvertWebError(ex);
                    return -1;
                     * */
                    int nRet = ConvertWebError(ex, out strError);
                    if (nRet == 0)
                        return -1;

                    // 2013/2/11
                    if (this.ErrorCode == ChannelErrorCode.QuotaExceeded)
                    {
                        if (nPerCount > 1 || nPerCount == -1)
                            nPerCount = 1;   // 修改为最小数量重做一次
                        else
                            return -1;
                    }

                    goto REDO;
                }
            }

            this.ClearRedoCount();
            return 0;
        }
Example #5
0
        // 获取浏览记录
        public long DoBrowse(
            BrowseList listView,
            string strLang,
            DigitalPlatform.Stop stop,
            string strResultSetName,
            string strOutputStyle,
            out string strError)
        {
            strError = "";

            Record[] records = null;

            long nStart = 0;
            long nPerCount = -1;

            int nCount = 0;

            bool bOutputKeyID = StringUtil.IsInList("keyid", strOutputStyle);

            long lTotalCount = -1;
            for (; ; )
            {
                DoIdle(); // 出让控制权,避免CPU资源耗费过度

                if (stop != null)
                {
                    if (stop.State != 0)
                    {
                        strError = "用户中断";
                        return -1;
                    }
                }

                nPerCount = -1; // 2013/2/12
            REDO:
                try
                {
                    IAsyncResult soapresult = this.ws.BeginGetRecords(
                        strResultSetName,
                        nStart,
                        nPerCount,
                        strLang,
                        strOutputStyle, //"id,cols",
                        null,
                        null);

                    for (; ; )
                    {
                        DoIdle(); // 出让控制权,避免CPU资源耗费过度
                        bool bRet = soapresult.AsyncWaitHandle.WaitOne(100, false);
                        if (bRet == true)
                            break;
                    }
                    if (this.m_ws == null)
                    {
                        strError = "用户中断";
                        this.ErrorCode = ChannelErrorCode.RequestCanceled;
                        return -1;
                    }
                    Result result = this.ws.EndGetRecords(
                        out records,soapresult);

                    if (result.Value == -1)
                    {
                        // 2011/4/21
                        if (result.ErrorCode == ErrorCodeValue.NotLogin
                            && this.Container != null)
                        {
                            // return:
                            //		-1	error
                            //		0	login failed
                            //		1	login succeed
                            int nRet = this.UiLogin("",
                                out strError);
                            if (nRet == -1 || nRet == 0)
                            {
                                return -1;
                            }

                            goto REDO;
                        }

                        ConvertErrorCode(result);
                        strError = result.ErrorString;
                        return -1;
                    }
                    else
                    {
                        Debug.Assert(records != null, "WebService GetRecords() API record参数返回值不应为null");

                        lTotalCount = result.Value;

                        if (nStart == 0 && stop != null)
                            stop.SetProgressRange(0, lTotalCount);
                    }

                    if (records != null)
                    {
                        nCount += records.Length;
                    }

                    listView.BeginUpdate();
                    try
                    {
                        // 做事
                        for (int i = 0; i < records.Length; i++)
                        {
                            DoIdle(); // 出让控制权,避免CPU资源耗费过度

                            if (stop != null)
                            {
                                if (stop.State != 0)
                                {
                                    strError = "用户中断";
                                    return -1;
                                }

                                stop.SetMessage("正在装入 " + Convert.ToString(nStart + i) + " / "
                                    + ((lTotalCount == -1) ? "?" : Convert.ToString(lTotalCount)));
                            }

                            Record record = records[i];

                            string[] cols = null;
                            if (bOutputKeyID == true)
                            {
                                cols = new string[(record.Cols == null ? 0 : record.Cols.Length) + 1];
                                cols[0] = BuildDisplayKeyString(record.Keys);
                                if (cols.Length > 1)
                                    Array.Copy(record.Cols, 0, cols, 1, cols.Length - 1);
                            }
                            else
                                cols = record.Cols;

                            listView.NewLine(record.Path + " @" + this.Url,
                                cols);
                            // record.ID 放入第一列
                            // record.Cols 放入其他列(如果为keyid方式,key在这一群的第一列)
                        }

                        if (stop != null)
                            stop.SetProgressValue(nStart + records.Length);
                    }
                    finally
                    {
                        listView.EndUpdate();
                    }

                    if (nCount >= result.Value)
                        break;

                    if (records != null)
                    {
                        nStart += records.Length;
                    }

                }
                catch (Exception ex)
                {
                    /*
                    strError = ConvertWebError(ex);
                    return -1;
                     * */
                    int nRet = ConvertWebError(ex, out strError);
                    if (nRet == 0)
                        return -1;
                    // 2013/2/11
                    if (this.ErrorCode == ChannelErrorCode.QuotaExceeded)
                    {
                        if (nPerCount > 1 || nPerCount == -1)
                            nPerCount = 1;   // 修改为最小数量重做一次
                        else
                            return -1;
                    }
                    goto REDO;
                }
            }

            this.ClearRedoCount();
            if (stop != null)
                stop.HideProgress();
            return 0;
        }
Example #6
0
		// 上载资源
		// return:
		//		-1	error
		//		>=0 实际上载的资源对象数
		public int DoUpload(
			string strXmlRecPath,
			RmsChannel channel,
			DigitalPlatform.Stop stop,
			out string strError)
		{
			strError = "";
            
			int nUploadCount = 0;

            string strLastModifyTime = DateTime.UtcNow.ToString("u");

			bNotAskTimestampMismatchWhenOverwrite = false;

			for(int i=0;i<this.Items.Count;i++)
			{
				string strState = ListViewUtil.GetItemText(this.Items[i], COLUMN_STATE);
				string strID = ListViewUtil.GetItemText(this.Items[i], COLUMN_ID);
				string strResPath = strXmlRecPath + "/object/" + ListViewUtil.GetItemText(this.Items[i], COLUMN_ID);
				string strLocalFileName = ListViewUtil.GetItemText(this.Items[i], COLUMN_LOCALPATH);
				string strMime =  ListViewUtil.GetItemText(this.Items[i], COLUMN_MIME);
				string strTimeStamp =  ListViewUtil.GetItemText(this.Items[i], COLUMN_TIMESTAMP);

				if (IsNewFileState(strState) == false)
					continue;

				// 检测文件尺寸
				FileInfo fi = new FileInfo(strLocalFileName);


				if (fi.Exists == false) 
				{
					strError = "文件 '" + strLocalFileName + "' 不存在...";
					return -1;
				}

				string[] ranges = null;

				if (fi.Length == 0)	
				{ // 空文件
					ranges = new string[1];
					ranges[0] = "";
				}
				else 
				{
					string strRange = "";
					strRange = "0-" + Convert.ToString(fi.Length-1);

					// 按照100K作为一个chunk
					ranges = RangeList.ChunkRange(strRange,
						100*1024);
				}

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

				nUploadCount ++;

			REDOWHOLESAVE:
				string strWarning = "";


				for(int j=0;j<ranges.Length;j++) 
				{
				REDOSINGLESAVE:

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

					if (stop.State != 0)
					{
						strError = "用户中断";
						goto ERROR1;
					}

					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)fi.Length;
						strPercent = String.Format("{0,3:N}",ratio * (double)100) + "%";
					}

					if (stop != null)
						stop.SetMessage("正在上载 " + ranges[j] + "/"
							+ Convert.ToString(fi.Length)
							+ " " + strPercent + " " + strLocalFileName + strWarning + strWaiting);

					/*
					if (stop != null)
						stop.SetMessage("正在上载 " + ranges[j] + "/" + Convert.ToString(fi.Length) + " " + strLocalFileName);
					*/

					long lRet = channel.DoSaveResObject(strResPath,
						strLocalFileName,
						strLocalFileName,
						strMime,
                        strLastModifyTime,
						ranges[j],
						j == ranges.Length - 1 ? true : false,	// 最尾一次操作,提醒底层注意设置特殊的WebService API超时时间
						timestamp,
						out output_timestamp,
						out strError);
					timestamp = output_timestamp;
					ListViewUtil.ChangeItemText(this.Items[i], COLUMN_TIMESTAMP, ByteArray.GetHexTimeStampString(timestamp));

					strWarning = "";

					if (lRet == -1) 
					{
						if (channel.ErrorCode == ChannelErrorCode.TimestampMismatch)
						{

							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, 
								"上载 '" + strLocalFileName + "' (片断:" + ranges[j] + "/总尺寸:"+Convert.ToString(fi.Length)
								+") 时发现时间戳不匹配。详细情况如下:\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) 
							{
								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;
							}

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

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

						goto ERROR1;
					}
					//timestamp = output_timestamp;

				}


				DigitalPlatform.Xml.ElementItem item = GetFileItem(strID);

				if (item != null) 
				{
					item.SetAttrValue("__state", this.ServerFileState);
				}
				else 
				{
					Debug.Assert(false, "xmleditor中居然不存在id为[" + strID + "]的<dprms:file>元素");
				}


			}

			END1:
			if (stop != null)
				stop.SetMessage("上载资源全部完成");

			return nUploadCount;
			ERROR1:
				return -1;
		}
Example #7
0
		// 下载资源,保存到备份文件
		public int DoSaveResToBackupFile(
			Stream outputfile,
			string strXmlRecPath,
			RmsChannel channel,
			DigitalPlatform.Stop stop,
			out string strError)
		{
			strError = "";

			string strTempFileName = Path.GetTempFileName();
			try 
			{
				long lRet;

				for(int i=0;i<this.Items.Count;i++)
				{
					string strState = ListViewUtil.GetItemText(this.Items[i], COLUMN_STATE);
					string strID = ListViewUtil.GetItemText(this.Items[i], COLUMN_ID);
					string strResPath = strXmlRecPath + "/object/" + ListViewUtil.GetItemText(this.Items[i], COLUMN_ID);
					string strLocalFileName = ListViewUtil.GetItemText(this.Items[i], COLUMN_LOCALPATH);
					string strMime =  ListViewUtil.GetItemText(this.Items[i], COLUMN_MIME);

					string strMetaData;

					// 服务器端文件
					if (IsNewFileState(strState) == false) 
					{
						if (stop != null)
							stop.SetMessage("正在下载 " + strResPath);

						byte [] baOutputTimeStamp = null;
						string strOutputPath;

						lRet = channel.GetRes(strResPath,
							strTempFileName,
							stop,
							out strMetaData,
							out baOutputTimeStamp,
							out strOutputPath,
							out strError);
						if (lRet == -1)
							return -1;

						ResPath respath = new ResPath();
						respath.Url = channel.Url;
						respath.Path = strResPath;

						// strMetaData还要加入资源id?
						ExportUtil.ChangeMetaData(ref strMetaData,
							strID,
							null,
							null,
							null,
							respath.FullPath,
							ByteArray.GetHexTimeStampString(baOutputTimeStamp));


						lRet = Backup.WriteOtherResToBackupFile(outputfile,
							strMetaData,
							strTempFileName);

					}
					else // 本地新文件
					{
						if (stop != null)
							stop.SetMessage("正在复制 " + strLocalFileName);

						// strMetaData = "<file mimetype='"+ strMime+"' localpath='"+strLocalPath+"' id='"+strID+"'></file>";

						ResPath respath = new ResPath();
						respath.Url = channel.Url;
						respath.Path = strResPath;

						strMetaData = "";
						FileInfo fi = new FileInfo(strLocalFileName);
						ExportUtil.ChangeMetaData(ref strMetaData,
							strID,
							strLocalFileName,
							strMime,
							fi.LastWriteTimeUtc.ToString(),
							respath.FullPath,
							"");

						lRet = Backup.WriteOtherResToBackupFile(outputfile,
							strMetaData,
							strLocalFileName);

					}

				}

				if (stop != null)
					stop.SetMessage("保存资源到备份文件全部完成");

			}
			finally 
			{

				if (strTempFileName != "")
					File.Delete(strTempFileName);
			}

			return 0;
		}