예제 #1
0
        /// <summary>
        /// Syncs a document with the server.
        /// </summary>
        /// <param name="editorId">The id of the user who's submitting his work</param>
        /// <param name="documentId">The id of the document</param>
        /// <param name="filepath">The path to where the file lies on the client</param>
        /// <param name="fileContent">The xaml content of the document the user is syncing</param>
        /// <param name="title">The title of the document</param>
        /// <param name="pureContent">The "pure" content of the document. One line per index in the array</param>
        /// <returns>Null if there's no mergeconflict.
        /// If there is a mergeconflict the returned is like this:
        /// Array[0] = the merged document
        /// Array[1] = insertions, same length as Array[0]
        /// Array[2] = deletions, same length as Array[3]
        /// Array[3] = the original document (server version)</returns>
        public String[][] SyncDocument(int editorId, int documentId, String filepath, String fileContent, String title, String pureContent)
        {
            String[] latestAsArray = pureContent.Split(new String[] { "\r\n", "\n" }, StringSplitOptions.None);

            PersistentStorage ps = PersistentStorage.GetInstance();

            //Document found with the given id
            if (GetDocumentById(documentId) != null)
            {
                //Check if the document has any revisions
                bool hasRevisions = ps.DocumentHasRevision(documentId);

                if (!hasRevisions)
                {
                    //No conflict
                    return(ps.SyncNoConflict(editorId, documentId, filepath, fileContent));
                }

                //Get the latest documentrevision by the user
                Documentrevision latestUserDocumentRevision = ps.GetLatestDocumentRevisionByUserId(editorId, documentId);
                //Get the latest documentrevision on the server
                Documentrevision latestServerDocumentRevision = ps.GetLatestDocumentRevisions(documentId)[0];
                //Get the content of the latest documentrevision by the user
                String latestUserDocumentContent = ps.GetDocumentRevisionContent(latestUserDocumentRevision);
                //Get the content of the latest documentrevision on the server
                String latestServerDocumentContent = ps.GetDocumentRevisionContent(latestServerDocumentRevision);

                //Check if the two contents are equal. If they are equal, there's no conflict
                if (latestUserDocumentContent == latestServerDocumentContent)
                {
                    //No conflict
                    return(ps.SyncNoConflict(editorId, documentId, filepath, fileContent));
                }
                else
                {
                    //Conflict
                    return(Model.GetInstance().SyncConflict(documentId, latestAsArray));
                }
            }
            else
            {
                //No document found with the given id.
                ps.AddDocumentWithUserDocument(title, editorId, filepath, fileContent);
                return(null);
            }
        }