public void OfflineSynchronizationTest() { //creation of documents and user for testing User user1 = engine.userhandler.GetUser("mrT","1234"); Document document1 = new Document(user1, "tmp", "There.html", Permission.Permissions.Edit); Document document2 = new Document(user1, "tmp", "There.html", Permission.Permissions.Edit); Document document3 = new Document(user1, "tmp", "There.html", Permission.Permissions.Edit); Document document4 = new Document(user1, "tmp", "There.html", Permission.Permissions.Edit); Document document5 = new Document(user1, "tmp", "There.html", Permission.Permissions.Edit); Document document6 = new Document(user1, "tmp", "There.html", Permission.Permissions.Edit); document2.lastChanged = new DateTime(2012, 12, 5); document3.lastChanged = new DateTime(2012, 12, 5); document4.lastChanged = new DateTime(2012, 12, 2); document5.lastChanged = new DateTime(2012, 12, 7); List<Document> userLocalDocuments = new List<Document>(); List<Document> userServerDocuments = new List<Document>(); userLocalDocuments.Add(document1); //not on server userLocalDocuments.Add(document2); //most recent version userLocalDocuments.Add(document3); //oldest version userServerDocuments.Add(document4); //oldest version userServerDocuments.Add(document5); //most recent version userServerDocuments.Add(document6); //not local List<Document> testSet = engine.userhandler.docHandler.OfflineSynchronization(userLocalDocuments, userServerDocuments); Assert.AreEqual(4,testSet.Count); Assert.AreEqual(true, testSet.Contains(document1)); Assert.AreEqual(true, testSet.Contains(document2)); Assert.AreEqual(true, testSet.Contains(document5)); Assert.AreEqual(true, testSet.Contains(document6)); }
/// <summary> /// Add document to system. If db is set to true, only create file. /// </summary> /// <param name="parent"></param> /// <param name="title"></param> /// <param name="db"></param> /// <returns></returns> public override Document AddDocument(IItemContainer parent, string title, string revision = "", int id = 0, bool db = false) { if (!db) title = GetAvailableName(title, id, parent.GetPath(), ".txt"); string documentPath = Path.Combine(parent.GetPath(), Helper.GenerateName(id, title)); documentPath += ".txt"; try { FileStream fileStream = File.Create(documentPath); StreamWriter streamWriter = new StreamWriter(fileStream); string[] lines = revision.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); foreach (string line in lines) { streamWriter.WriteLine(line); } streamWriter.Flush(); streamWriter.Close(); fileStream.Close(); } catch (IOException e) { // Should not be accesible Console.WriteLine(e.Message); } if (db) return null; Document document = new Document(); document.Title = title; document.Parent = parent; parent.Documents.Add(document); return document; }
public static void MyClassInitialize(TestContext testContext) { // Test doc 0 string docText = "This my new and fabulous blog where i would love to write about my dog called Fido!\n" + "Fido is a really nice dog which sadly only has 3 legs, because one time where i was really angry, and i had this saw, well never mind.\n" + "I love my dog above anything else in this world, and i thought i would dedicate this document to him!\n" + "This is Fido: <img src=\"Fido.gif\" alt=\"My Dog\">"; testDoc = new Document(docText, "Fido", new User("Karsten")); testDoc.Path = "root/cuteanimalsxoxo"; // Test doc 1 string docText1 = "New text\n" + "more text"; testDoc1 = new Document(docText1, "Herp", new User("Kewin")); testDoc1.Path = "root"; // Test doc 2 string docText2 = "Interesting facts about crocodiles: \n" + "They bite hard: \n"+ "and thats it."; testDoc2= new Document(docText2, "Crocoman", new User("Karsten")); testDoc2.Path = "root/cuteanimalsxoxo/reptiles"; testproject = new Project("Projekt", new User("Kewin"), new List<User>(){new User("Karsten") }); testproject2 = new Project("Projekt2", new User("Karsten"), new List<User>() { new User("Kewin") }); }
public static void SaveDocument(Project proj, Document doc, User user, bool fromServer = false) { // If the document exists in the storage, merge old version with newer verion. // Otherwise just save it to the storage. Document docInStorage = Storage.ReadFromFile(proj.Id, doc.Id); if (docInStorage == null) Storage.WriteToFile(proj, doc, fromServer); else { docInStorage.MergeWith(doc, user); Storage.WriteToFile(proj, docInStorage, fromServer); } }
public ActionResult CreateCompleted(Document newDocument) { //Get parent IItemContainer parent; if (newDocument.ProjectId != null) { parent = controller.GetProjectDirectly((int)newDocument.ProjectId); } else { parent = controller.GetFolderDirectly((int)newDocument.FolderId); } Document result = controller.CreateDocument(newDocument.Title, User.Identity.Name, parent); return RedirectToAction("Edit", result); }
public static Document Merge(Document current, Document old) { //Null checks if (current == null || current.CurrentRevision == null) { if (old == null || old.CurrentRevision == null) return null; return old; } if (old == null || old.CurrentRevision == null) { return current; } String[] arrC = current.CurrentRevision.Split('\n'); String[] arrO = old.CurrentRevision.Split('\n'); return Merge(arrC, arrO); }
/// <summary> /// Used as a demonstration only. /// </summary> /// <param name="args"></param> public static void Main(String[] args) { Document o = new Document { CurrentRevision = @"DRACULA CHAPTER I JONATHAN BARKER'S JOURNAL (Kept in shorthand.) 3 May. Bistritz. Left Munich at 8:35 p. M., on ist May, ar- riving at Vienna early next morning; should have arrived at 6:46, but train was an hour late. Buda-Pesth seems a wonderful IMMAH FIRING MAH LAZOR place, from the glimpse which I got of it from the train and the little I could walk through the streets. I feared to go very far from the station, as we had arrived late and would start as near were leaving the West and entering the East; the most western of splendid bridges over the Danube, which is here of noble width and depth, took us among the traditions of Turkish rule. " }; Document c = new Document(); c.CurrentRevision = @"DRACULA CHAPTER I JONATHAN BARKER'S JOURNAL (Kept in shorthand.) 3 May. Bistritz. Left Munich at 8:35 p. M., on ist May, ar- riving at Vienna early next morning; should have arrived at 6:46, but train wars an raptors, raptors everywhere! Buda-Pesth seems a wonderful place, from the glimpse which I got of it from the train and the little I could walk through the streets. I feared to go very far from the station, as we had arrived late and would start as near the correct time as possible. The impression I had was that we were leaving the West and entering the East; the most western and depth, took us among the traditions of Turkish rule. "; Document merged = Merger.Merge(c, o); Console.WriteLine("Merge result:"); PrintDoc(merged); Console.WriteLine("done"); Console.ReadLine(); }
public override Document AddDocument(IItemContainer parent, string title, string revision = "", int id = 0, bool db = false) { if (parent == null || title == null) throw new ArgumentNullException(); Document d = new Document() { Title = title, Parent = parent, CurrentRevision = revision, CurrentHash = revision.GetHashCode() }; if(parent is Project) d.ProjectId = parent.Id; else d.FolderId = parent.Id; using(var dbContext = new sliceofpieEntities2()) { dbContext.Documents.AddObject(d); dbContext.SaveChanges(); } return d; }
/// <summary> /// Checks the database for already existing document /// </summary> /// <param name="user">document's owner</param> /// <param name="doc">document instance</param> /// <returns>Wether the document exists or not. /// True = it exists /// False= it does not exist</returns> public bool CheckForDocument(User user, Document doc) { string query = "SELECT * FROM document WHERE owner='" + user.username + "' AND id='" + doc.documentId + "'"; List<int> counterList = new List<int>(); MySqlDataReader reader = ExecuteReader(query); while (reader.Read()) { counterList.Add((int)reader["id"]); } reader.Close(); CloseConnection(); if (counterList.Count > 0) return true; else return false; }
/// <summary> /// Deletes a document from the system /// </summary> /// <param name="doc">The document to delete</param> public void DeleteDocument(Document doc) { string path = doc.path; // Delete the file if (File.Exists(path)) { try { File.Delete(path); } catch (IOException e) { Console.WriteLine(e.StackTrace); } } // Delete the entry from the database. dbCon.DeleteDocumentByID(doc.documentId); }
// Creates and returns a document in it's complete version from a file on the file system. public static Document CreateDocumentFromFile(string id, string text, string title, bool modified, bool deleted, User owner, List<Picture> pictures, string path, Document.DocumentLog log) { Document doc = new Document(); doc.id = id; doc.text = text; doc.title = title; doc.owner = owner; doc.path = path; doc.log = log; doc.images = pictures; doc.modified = modified; doc.deleted = deleted; return doc; }
/// <summary> /// Save document to file. Take CurrentRevision from Document and overwrite existing file. /// </summary> /// <param name="document"></param> public override void SaveDocument(Document document) { if (!File.Exists(document.GetPath())) { throw new ArgumentException("File does not exist (" + document.GetPath() + ")"); } FileStream fileStream = new FileStream(document.GetPath(), FileMode.Create, FileAccess.Write); StreamWriter streamWriter = new StreamWriter(fileStream); string[] lines = document.CurrentRevision.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); streamWriter.WriteLine(document.CurrentHash); foreach (string line in lines) { streamWriter.WriteLine(line); } streamWriter.Flush(); streamWriter.Close(); }
/// <summary> /// Upload documents to db for project or folder. /// </summary> /// <param name="parentPath"></param> /// <param name="parentId"></param> /// <param name="container"></param> public void UploadDocuments(string parentPath, int parentId, Container container = Container.Folder) { string[] files = Directory.GetFiles(parentPath); foreach (string fileName in files) { Document dbDocument = null; using (var dbContext = new sliceofpieEntities2()) { string pathName = Path.GetFileName(fileName); string[] parts = pathName.Split('-'); int id = int.Parse(parts[0]); string title = pathName.Replace(parts[0] + "-", "").Replace(".txt", ""); int hash = "".GetHashCode(); string revision = ""; bool isRevision = false; FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader streamReader = new StreamReader(fileStream); string line; int i = 0; while ((line = streamReader.ReadLine()) != null) { if (i == 0) { if (line.Length > 0) { if (line.Substring(0, 3).Equals("rev")) { isRevision = true; line = line.Substring(3); } hash = int.Parse(line); } } else { revision += line + "\n"; } i++; } revision = revision.Trim(); streamReader.Close(); var dbDocuments = from dDocument in dbContext.Documents where dDocument.Id == id select dDocument; if (id > 0 && dbDocuments.Count() == 0) { if (Directory.Exists(Path.Combine(parentPath, Helper.GenerateName(id, title)) + ".txt")) { Directory.Move(Path.Combine(parentPath, Helper.GenerateName(id, title)) + ".txt", Path.Combine(parentPath, Helper.GenerateName(0, title)) + ".txt"); } id = 0; } if (id > 0) { // Updating document dbDocument = dbDocuments.First(); dbDocument.Title = title; if (container == Container.Project) { dbDocument.ProjectId = parentId; dbDocument.FolderId = null; } else { dbDocument.ProjectId = null; dbDocument.FolderId = parentId; } dbDocument.IsMerged = isRevision; if (dbDocument.CurrentHash == hash) { dbDocument.CurrentRevision = revision; dbDocument.CurrentHash = revision.GetHashCode(); UpdateHash(fileName, revision.GetHashCode()); } else if (revision.GetHashCode() == hash) { UpdateHash(fileName, (int)dbDocument.CurrentHash); } else { // Handle merge (and conflicts) string merge = Merger.Merge(revision, dbDocument.CurrentRevision); FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); StreamWriter streamWriter = new StreamWriter(fs); string[] lines = merge.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); streamWriter.WriteLine("rev" + dbDocument.CurrentHash); foreach (string l in lines) { streamWriter.WriteLine(l); } streamWriter.Flush(); streamWriter.Close(); } } else { // Creating document dbDocument = new Document { Title = title, CurrentRevision = revision, CurrentHash = revision.GetHashCode() }; if (container == Container.Project) { dbDocument.ProjectId = parentId; dbDocument.FolderId = null; } else { dbDocument.ProjectId = null; dbDocument.FolderId = parentId; } UpdateHash(fileName, revision.GetHashCode()); dbContext.Documents.AddObject(dbDocument); } dbContext.SaveChanges(); } // Rename document file if (File.Exists(Path.Combine(parentPath, Helper.GenerateName(0, dbDocument.Title + ".txt")))) { File.Move(Path.Combine(parentPath, Helper.GenerateName(0, dbDocument.Title + ".txt")), Path.Combine(parentPath, Helper.GenerateName(dbDocument.Id, dbDocument.Title + ".txt"))); } // Create revision using (var dbContext = new sliceofpieEntities2()) { var dbRevisions = from dRevision in dbContext.Revisions where dRevision.DocumentId == dbDocument.Id && dRevision.ContentHash == dbDocument.CurrentHash select dRevision; if (dbRevisions.Count() == 0) { dbContext.Revisions.AddObject(new Revision { DocumentId = dbDocument.Id, Content = dbDocument.CurrentRevision, ContentHash = dbDocument.CurrentHash, Timestamp = DateTime.Now }); dbContext.SaveChanges(); } } } }
/// <summary> /// Adds a document to the users list of documents. /// </summary> /// <param name="doc">The document to be added.</param> private void AddDocToList(User user, Document doc) { user.documents.Add(doc); }
/// <summary> /// Rename file both in file system and internal system. /// </summary> /// <param name="document"></param> /// <param name="title"></param> public void RenameDocument(Document document, string title) { string documentPath = Path.Combine(document.Parent.GetPath(), Helper.GenerateName(document.Id, GetAvailableName(title, document.Id, document.Parent.GetPath(), ".txt"))) + ".txt"; try { File.Move(document.GetPath(), documentPath); } catch (IOException e) { // Should not be accesible Console.WriteLine(e.Message); } document.Title = title; }
/// <summary> /// Remove a document from the system. /// </summary> /// <param name="d">Document to remove</param> /// <seealso cref="BeginRemoveDocument"/> public void RemoveDocument(Document d) { fileModel.RemoveDocument(d); }
/// <summary> /// Allows the owner of the document to share the /// document with other users. /// </summary> /// <param name="file">File path of file</param> /// <param name="perm">Enumerated permition</param> /// <param name="users">List of users to share with.</param> public void ShareDocument(User currentUser, Document doc, Permission.Permissions perm , string filename, params User[] users) { Document sharedDocument = new Document(doc.owner, doc.path + "/" + filename, perm); string pathToFile = "root/" + currentUser.username + "/" + filename; //Document sharedDoc = new Document(currentUser, doc.content, pathToFile, Permission.Permissions.Edit); foreach (User i in users) { //int someID = dbCon.GetDocument(currentUser.username, "root/myuser/" + filename); int id = dbCon.GetDocument(currentUser.username, pathToFile); dbCon.InsertUserDocument(i, id, Permission.Permissions.Edit); dbCon.InsertUserDocument(currentUser, id, Permission.Permissions.Edit); //dbCon.InsertUserDocument(i, 243, Permission.Permissions.Edit); if (i.documents.Contains(doc) || i.documents.Contains(sharedDocument)) Console.WriteLine("Document does already exist"); else if (!dbCon.CheckForUserDocument(i, doc)) { //int newID = dbCon.GetDocument(currentUser.username, doc.path); //dbCon.InsertUserDocument(i, newID, Permission.Permissions.Edit); }//SaveDocument(currentUser, doc, doc.path + "/" + filename); //Console.WriteLine(i.documents.Contains(sharedDocument)); } }
// This functions takes a newer version of this document, and merges it with this one // acording to "Simple Merge Policy" given in slice-of-pie.pdf. // It also updated all other fields in the document according to the new version // and generates a log based on the changes. // MergeWith returns a bool as well, it returns false if the ID of the updated // document is not the same as this documents ID, otherwise it returns true public bool MergeWith(Document doc, User user) { if (this.id != doc.id) return false; List<string> changes = new List<string>(); // Create original and latest arrays. ( step 1 ) string[] original = this.GetTextAsArray(); string[] latest = doc.GetTextAsArray(); // Create merged array ( made as a list instead ). ( step 2 ) List<string> merged = new List<string>(); // Definer o and n index, which point to lines in the original and latest arrays. ( step 3 ) int o = 0; int n = 0; bool done = false; Console.Out.WriteLine("Started merge."); // While loop that continues untill the end of both versions of the document have been reached. while (!done) { Console.Out.WriteLine("New round in while loop"); // All remaining lines in latest are new. ( step 4 ) if ((o == original.Length) && (n != latest.Length)) { Console.Out.WriteLine("Step 4"); for (int N=n; N < latest.Length; N++) { changes.Add("+L"+N+": "+latest[N]); merged.Add(latest[N]); n++; } } // All remaining lines in original have been removed. ( step 5 ) else if ((o != original.Length) && (n == latest.Length)) { Console.Out.WriteLine("Step 5"); for (int O=o; O < original.Length; O++) { changes.Add("-L"+O+": "+original[O]); } o = original.Length; } // No changes have occured in this line ( step 6 ) else if (String.Compare(original[o], latest[n]) == 0) { Console.Out.WriteLine("Step 6"); merged.Add(original[o]); n++; o++; } else if (String.Compare(original[o], latest[n]) != 0) { List<string> temp = new List<string>(); bool found = false; int t = 0; for (int N = n; N < latest.Length; N++) { temp.Add(latest[N]); if (String.Compare(original[o], latest[N]) == 0) { found = true; t = N; break; } } // Line has been removed ( Step 7.b ) if (found == false) { Console.Out.WriteLine("Step 7.b"); changes.Add("-L"+o+": "+original[o]); o++; } // All lines in between have been added ( step 7.c ) else { Console.Out.WriteLine("Step 7.c"); int counter = 0; foreach (string s in temp) { if (!(counter == temp.Count-1)) changes.Add("+L"+n+": "+latest[n]); merged.Add(s); n++; counter++; } o++; n = t + 1; } } if (((o == original.Length) && (n == latest.Length))) { done = true; } } Console.Out.WriteLine("Merge complete"); StringBuilder newTextBuilder = new StringBuilder(); for (int i = 0; i < merged.Count; i++) { if (!(i == merged.Count-1)) newTextBuilder.AppendFormat(merged[i] + "\n"); else newTextBuilder.AppendFormat(merged[i]); } text = newTextBuilder.ToString(); // Check and see if the document has been marked for deletion this.deleted = doc.deleted; // A log that will document what changes have been made to the document. List<string> changeLog = new List<string>(); bool titleChanged = false; bool pathChanged = false; bool textChanged = (!(changes.Count==0)); bool picturesAdded = false; bool picturesRemoved = false; List<Picture> imagesAdded = new List<Picture>(); List<Picture> imagesRemoved = new List<Picture>(); // Has title been changed? if (String.Compare(doc.Title, this.Title) != 0) { changeLog.Add("Title has been changed from '" + this.Title + "' to '" + doc.Title + "'"); this.title = doc.Title; titleChanged = true; } // Has path been changed? if (String.Compare(doc.Path, this.Path) != 0) { changeLog.Add("Path has been changed from '" + this.Path + "' to '" + doc.Path + "'"); this.path = doc.Path; pathChanged = true; } // Are there any new pictures? foreach (Picture pic in doc.Images) { if (!(this.Images.Contains(pic))) { picturesAdded = true; imagesAdded.Add(pic); } } // Are any of the old pictures removed? foreach (Picture pic in this.Images) { if (!(doc.Images.Contains(pic))) { picturesRemoved = true; imagesRemoved.Add(pic); } } foreach (Picture pic in imagesRemoved) { images.Remove(pic); pic.Image.Dispose(); } foreach (Picture pic in imagesAdded) { images.Add(pic); } // If there were pictures added, add it to the changelog. if (picturesAdded) { StringBuilder imageLineBuilder = new StringBuilder(); for (int i = 0; i < imagesAdded.Count; i++) { if (i == imagesAdded.Count - 1) imageLineBuilder.AppendFormat(imagesAdded[i].Id); else imageLineBuilder.AppendFormat(imagesAdded[i].Id + ","); } changeLog.Add("Following pictures were added: " + imageLineBuilder.ToString()); } // If there were pictures removed, add it to the changelog. if (picturesRemoved) { StringBuilder imageLineBuilder = new StringBuilder(); for (int i = 0; i < imagesRemoved.Count; i++) { if (i == imagesRemoved.Count - 1) imageLineBuilder.AppendFormat(imagesRemoved[i].Id); else imageLineBuilder.AppendFormat(imagesRemoved[i].Id + ", "); } changeLog.Add("Following pictures were removed: " + imageLineBuilder.ToString()); } // Finally, add changes to the text to the changelog. if (textChanged) changeLog.Add("Changes to the documents text:"); foreach (string change in changes) { changeLog.Add(change); } string titleString = ""; string pathString = ""; string textString = ""; string pictureString = ""; string masterString = "Changed the document's: "; if (titleChanged) titleString = "Title. "; if (textChanged) textString = "Text. "; if (pathChanged) pathString = "Path. "; if ((picturesAdded || picturesRemoved)&&textChanged) pictureString = " And changed attached pictures"; else if ((picturesAdded || picturesRemoved) && textChanged == false) { masterString = ""; pictureString = "Changed the attached pictures"; } this.Log.AddEntry(new DocumentLog.Entry(user, masterString + titleString + textString + pathString + pictureString, changeLog)); return true; }
/// <summary> /// Start saving a document asynchronously in accordance with the Asynchronous Programming Model. /// </summary> /// <param name="d">Document to save</param> /// <param name="callback">Callback called when document saving finishes</param> /// <param name="state">State object, passed to callback</param> /// <returns>IAsyncResult for EndSaveDocument</returns> /// <seealso cref="EndSaveDocument"/> public IAsyncResult BeginSaveDocument(Document d, AsyncCallback callback, object state) { AsyncResultNoResult<Document> ar = new AsyncResultNoResult<Document>(callback, state, d); ThreadPool.QueueUserWorkItem(SaveDocumentAsyncHelper, ar); return ar; }
/// <summary> /// Compares a single document with all documents in a list /// </summary> /// <param name="d">document to be compared</param> /// <param name="toList">list of documents the "d" document will be compared to</param> /// <returns>if it finds a document in "toList" that matches the ID of "d", /// it returns the newest of those 2 documents. If "d" does not exist in "toList" it returns null /// to let it's caller know that the document does not exist in the "toList"</returns> private Document CheckDocumentInList(Document d, List<Document> toList) { foreach (Document i in toList) { try { if (d.documentId == i.documentId) { //Document tmp = FindNewestDocument(i, d); //if (toList.Contains(i)) toList.Remove(i); //if (toList.Contains(d)) toList.Remove(d); Document tmp = MergeDocuments(i, d); toList.Add(tmp); return tmp; } } catch (NullReferenceException ex) { Console.WriteLine("Something weird happend, Please try again"); } } return null; }
private static Document Merge(String[] curArr, String[] oldArr) { int curLast = curArr.Length - 1; int oldLast = oldArr.Length - 1; int maxLen = curArr.Length > oldArr.Length ? curArr.Length : oldArr.Length; String[] merged = new String[maxLen]; int o = 0, n = 0, m = 0; //actual algorithm, 'steps' are according to the describtion from the assignment https://docs.google.com/viewer?url=https%3A%2F%2Fblog.itu.dk%2FBDSA-E2012%2Ffiles%2F2012%2F11%2Fslice-of-pie.pdf while (o <= oldLast || n <= curLast) { System.Diagnostics.Debug.WriteLine("step"); //Step 4 if (o == oldLast + 1 && n <= curLast) { System.Diagnostics.Debug.WriteLine("step 4"); //copy the last [length of curArr] - n lines from curArr of merged Array.Copy(curArr, n, merged, m, (curLast - n) + 1); n = curLast + 1; } else if (n == curLast + 1) { //step 5 System.Diagnostics.Debug.WriteLine("step 5"); o = oldLast + 1; } else if (curArr[n].Equals(oldArr[o])) { //step 6 System.Diagnostics.Debug.WriteLine("step 6"); merged[m++] = oldArr[o++]; ++n; } else if (!curArr[n].Equals(oldArr[o])) { //step 7 System.Diagnostics.Debug.WriteLine("step 7"); int t = -1; //step 7.a for (int i = n + 1; i <= curLast; i++) { if (curArr[i].Equals(oldArr[o])) { t = i; break; } } //step 7.b + 7.c if (t == -1) { System.Diagnostics.Debug.WriteLine("step 7.b"); ++o; } else { System.Diagnostics.Debug.WriteLine("step 7.c"); Array.Copy(curArr, n, merged, m, (t - n) + 1); n = t + 1; m = n; ++o; //not necessary, but it lets us skip a round (without this, the next run would end in step 7.b) } } PrintArray(merged); } Document ret = new Document(); for (int i = 0; i < merged.Length; i++) { if (i == merged.Length - 1) { ret.CurrentRevision += merged[i]; } else { ret.CurrentRevision += merged[i] + "\n"; } } return ret; }
public static void PrintDoc(Document d) { PrintArray(d.CurrentRevision.Split('\n')); }
/// <summary> /// Creates a new document object. /// </summary> /// <param name="owner">Owner of the document.</param> /// <param name="id">Id of the document.</param> /// <param name="content">Content of the document file.</param> /// <returns>The new document object.</returns> private Document NewDocObject(User owner, int id, string content, string path) { Document doc = new Document(owner, id, content, path); return doc; }
/// <summary> /// Compares two documents and finds the one with the newest DateTime Object /// </summary> /// <param name="doc1">First Document</param> /// <param name="doc2">Second Document</param> /// <returns>The document with the newest DateTime object</returns> private Document FindNewestDocument(Document doc1, Document doc2) { if (doc1.lastChanged.CompareTo(doc2.lastChanged) < 0) return doc2; else if (doc1.lastChanged.CompareTo(doc2.lastChanged) > 0) return doc1; else return doc1; }
/// <summary> /// Start downloading revisions asynchronously in accordance with the Asynchronous Programming Model. /// </summary> /// <param name="d">Document whose revisions to get</param> /// <param name="callback">Callback called when download of revisions finishes</param> /// <param name="state">State object, passed to callback</param> /// <returns>IAsyncResult for EndDownloadRevisions</returns> /// <seealso cref="EndDownloadRevisions"/> public IAsyncResult BeginDownloadRevisions(Document d, AsyncCallback callback, object state) { AsyncResult <IEnumerable<Revision>, Document> ar = new AsyncResult<IEnumerable<Revision>, Document>(callback, state, d); ThreadPool.QueueUserWorkItem(DownloadRevisionsAsyncHelper, ar); return ar; }
/// <summary> /// Download revisions for a document. /// </summary> /// <param name="document"></param> public override IEnumerable<Revision> DownloadRevisions(Document document) { if (document.Id == 0) { throw new ArgumentException("Document has to be synced, before being able to retrieve revisions"); } List<Revision> documentRevisions = new List<Revision>(); using (var dbContext = new sliceofpieEntities2()) { var revisions = from revision in dbContext.Revisions where revision.DocumentId == document.Id orderby revision.Timestamp descending select revision; foreach (Revision revision in revisions) { documentRevisions.Add(revision); } } foreach (Revision revision in documentRevisions) { yield return revision; } }
/// <summary> /// Download revisions for a specific Document and add them to internal collection. /// </summary> /// <param name="d"></param> public IEnumerable<Revision> DownloadRevisions(Document d) { foreach (Revision r in fileModel.DownloadRevisions(d)) { yield return r; } }
/// <summary> /// Find all documents in project/folder in file system and create them in internal system. /// </summary> /// <param name="parent"></param> public void FindDocuments(IItemContainer parent) { string[] documentPaths = Directory.GetFiles(parent.GetPath()); foreach (string documentName in documentPaths) { string pathName = Path.GetFileName(documentName); string[] parts = pathName.Split('-'); int id = int.Parse(parts[0]); bool isRevision = false; int hash = "".GetHashCode(); string revision = ""; FileStream fileStream = new FileStream(documentName, FileMode.Open, FileAccess.Read); StreamReader streamReader = new StreamReader(fileStream); string line; int i = 0; while ((line = streamReader.ReadLine()) != null) { if (i == 0) { if (line.Length > 0) { if (line.Substring(0, 3).Equals("rev")) { isRevision = true; line = line.Substring(3); } hash = int.Parse(line); } } else { revision += line + "\n"; } i++; } streamReader.Close(); fileStream.Close(); Document document = new Document { Id = id, Title = pathName.Replace(parts[0] + "-", "").Replace(".txt", ""), Parent = parent, CurrentRevision = revision, CurrentHash = (id == 0 ? revision.GetHashCode() : hash), IsMerged = isRevision }; parent.Documents.Add(document); } }
/// <summary> /// Save a document /// </summary> /// <param name="document">Document to save</param> public void SaveDocument(Document document) { fileModel.SaveDocument(document); }
public override void RemoveDocument(Document document) { if (!File.Exists(document.GetPath())) { throw new ArgumentException("File does not exist (" + document.GetPath() + ")"); } File.Delete(document.GetPath()); document.Parent.Documents.Remove(document); }