예제 #1
0
        void submit_button_Click(object sender, EventArgs e)
        {
            string strError = "";

            TextBox         textbox = (TextBox)this.FindControl("displayName");
            OpacApplication app     = (OpacApplication)this.Page.Application["app"];

            if (app == null)
            {
                strError = "app == null";
                goto ERROR1;
            }
            SessionInfo sessioninfo = (SessionInfo)this.Page.Session["sessioninfo"];

            if (sessioninfo == null)
            {
                strError = "sessioninfo == null";
                goto ERROR1;
            }

            int  nRedoCount   = 0;
            bool bUploadSaved = false;

REDO:
            XmlDocument readerdom = null;
            // 获得当前session中已经登录的读者记录DOM
            // return:
            //      -2  当前登录的用户不是reader类型
            //      -1  出错
            //      0   尚未登录
            //      1   成功
            int nRet = sessioninfo.GetLoginReaderDom(
                out readerdom,
                out strError);

            if (nRet == -1 || nRet == -2)
            {
                goto ERROR1;
            }

            if (nRet == 0)
            {
                goto ERROR1;
            }

            bool       bXmlRecordChanged = false;
            FileUpload upload            = (FileUpload)this.FindControl("upload");

            string strOldDisplayName = DomUtil.GetElementText(readerdom.DocumentElement,
                                                              "displayName");

            if (textbox.Text == strOldDisplayName &&
                upload.HasFile == false)
            {
                strError = "显示名没有发生修改并且也没有新上传头像,放弃保存";
                goto ERROR1;
            }

            if (strOldDisplayName != textbox.Text)
            {
                DomUtil.SetElementText(readerdom.DocumentElement,
                                       "displayName",
                                       textbox.Text);
                bXmlRecordChanged = true;
            }


            // 如果有上传头像
            if (upload.HasFile == true &&
                bUploadSaved == false)
            {
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
                nsmgr.AddNamespace("dprms", DpNs.dprms);

                // 全部<dprms:file>元素
                XmlNodeList nodes = sessioninfo.ReaderInfo.ReaderDom
                                    .DocumentElement.SelectNodes("//dprms:file[@usage='photo']", nsmgr);
                string strResTimeStamp = "";
                string strFileID       = "0";
                if (nodes.Count > 0)
                {
                    strFileID = DomUtil.GetAttr(nodes[0], "id");
                    if (String.IsNullOrEmpty(strFileID) == true)
                    {
                        strFileID = FindNewFileID(sessioninfo.ReaderInfo.ReaderDom);
                    }
                    strResTimeStamp = DomUtil.GetAttr(nodes[0], "__timestamp");
                }
                else
                {
                    strFileID = FindNewFileID(sessioninfo.ReaderInfo.ReaderDom);

                    XmlNode node = sessioninfo.ReaderInfo.ReaderDom.CreateElement(
                        "dprms:file", DpNs.dprms);
                    sessioninfo.ReaderInfo.ReaderDom.DocumentElement.AppendChild(node);

                    DomUtil.SetAttr(node, "id", strFileID);
                    DomUtil.SetAttr(node, "usage", "photo");
                    bXmlRecordChanged = true;
                }

                if (bXmlRecordChanged == true)
                {
                    sessioninfo.SetLoginReaderDomChanged();
                    // 先要保存一次XML记录,这样才有<dprms:file>元素,为上载资源作好准备
                    // return:
                    //      -2  时间戳冲突
                    //      -1  error
                    //      0   没有必要保存(changed标志为false)
                    //      1   成功保存
                    nRet = sessioninfo.SaveLoginReaderDom(
                        out strError);
                    if (nRet == -1)
                    {
                        goto ERROR1;
                    }
                    if (nRet == -2)
                    {
                        if (nRedoCount < 10)
                        {
                            nRedoCount++;
                            sessioninfo.ReaderInfo.ReaderDom = null;   // 强迫重新读入
                            goto REDO;
                        }
                        goto ERROR1;
                    }

                    bXmlRecordChanged = false;
                }


                // Stream stream = upload.FileContent;


                // 保存资源
                // 采用了代理帐户
                // return:
                //		-1	error
                //		0	发现上载的文件其实为空,不必保存了
                //		1	已经保存
                nRet = app.SaveUploadFile(
                    this.Page,
                    sessioninfo.ReaderInfo.ReaderDomPath,
                    strFileID,
                    strResTimeStamp,
                    upload.PostedFile,
                    64,
                    64,
                    out strError);
                if (nRet == -1)
                {
                    goto ERROR1;
                }

                bUploadSaved = true;

                sessioninfo.ReaderInfo.ReaderDom = null;    // 迫使重新装载
                // 因为上载后时间戳改变,需要重新获取
                nRet = sessioninfo.GetLoginReaderDom(
                    out readerdom,
                    out strError);
                if (nRet == -1 || nRet == -2)
                {
                    goto ERROR1;
                }
            }

            // 为显示名的修改而保存
            if (bXmlRecordChanged == true)
            {
                sessioninfo.SetLoginReaderDomChanged();


                // return:
                //      -2  时间戳冲突
                //      -1  error
                //      0   没有必要保存(changed标志为false)
                //      1   成功保存
                nRet = sessioninfo.SaveLoginReaderDom(
                    out strError);
                if (nRet == -1)
                {
                    goto ERROR1;
                }
                if (nRet == -2)
                {
                    if (nRedoCount < 10)
                    {
                        nRedoCount++;
                        sessioninfo.ReaderInfo.ReaderDom = null;   // 强迫重新读入
                        goto REDO;
                    }
                    goto ERROR1;
                }

                bXmlRecordChanged = false;
            }

            // 刷新显示
            return;

ERROR1:
            this.SetDebugInfo("errorinfo", strError);
        }