コード例 #1
0
        /// <summary>
        /// Synchronizes a specific document to push changes to the server. If another user made changes meanwhile, a conflict arises and a difference view is displayed.
        /// </summary>
        /// <param name="document">The document content from the UI</param>
        public void SyncDocument(FlowDocument document)
        {
            if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
            {
                //Metadata
                //0: docid -> docid 11
                //1: userid -> userid 11
                //2: timestamp -> timestamp 12-12-2012 12:18:19
                object[] metadata   = LocalPersistenceHandler.RetrieveMetadataFromFile(session.CurrentDocumentPath);
                int      documentID = (int)metadata[0];
                DateTime baseDocumentCreationTime = (DateTime)metadata[2];
                //int folderID = (int)metadata[3];

                StringBuilder sb             = new StringBuilder();
                string        metadataString = Metadata.GenerateMetadataString(documentID, session.UserID, DateTime.UtcNow);//, folderID);
                sb.Append(metadataString);
                sb.AppendLine();
                //generate xaml for the document
                String xaml = System.Windows.Markup.XamlWriter.Save(document);
                sb.Append(xaml);

                //get the text from the document
                String content = new TextRange(document.ContentStart, document.ContentEnd).Text;

                String[][] responseArrays = null;
                //connect to the websevice
                using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client())
                {
                    //push the current document
                    responseArrays = proxy.SyncDocument(session.UserID, documentID, session.CurrentDocumentPath, sb.ToString(), session.CurrentDocumentTitle, content);
                }

                if (responseArrays == null) //if there is no conflict
                {
                    if (documentID == 0)
                    {
                        //connect to the websevice
                        using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client())
                        {
                            documentID = proxy.GetDocumentId(session.UserID, session.CurrentDocumentTitle);
                        }
                    }
                    //save document with new metadata - basedocument
                    localPersistence.SaveDocumentToFile(document, Metadata.ReplaceDocumentIDInMetadata(metadataString, documentID));
                    session.CurrentDocumentID = documentID;
                }
                else //if there is a conflict
                {
                    gui.SetupMergeView(responseArrays);
                }
            }
            else
            {
                //No web connection
            }
        }