/// <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(); }
/// <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(); } } } }