public override void RemoveProject(Project project) { if (project == null) { throw new ArgumentNullException(); } IEnumerable <Document> documents; IEnumerable <Folder> folders; using (var dbContext = new sliceofpieEntities2()) { Project projectToGetFrom = dbContext.Projects.First(proj => project.Id == proj.Id); documents = projectToGetFrom.Documents.ToList(); folders = projectToGetFrom.Folders.ToList(); } foreach (Document d in documents) { RemoveDocument(d); } foreach (Folder f in folders) { RemoveFolder(f); } using (var dbContext = new sliceofpieEntities2()) { Project p = dbContext.Projects.First(proj => project.Id == proj.Id); ProjectUser pu = dbContext.ProjectUsers.First(user => project.Id == user.ProjectId); dbContext.ProjectUsers.DeleteObject(pu); dbContext.Projects.DeleteObject(p); dbContext.SaveChanges(); } }
/// <summary> /// Check whether the provided login details are a valid user. If the user does no exist, /// it is created and true is returned. If it does exist and the information is correct, /// true is returned. If it does exist and the information is INCORRECT, false is returned. /// </summary> /// <param name="userMail">User email to validate</param> /// <param name="password">Password corresponding to email</param> /// <returns>Whether the user is valid</returns> public bool ValidateLogin(string userMail, string password) { if (!(userMail.Length > 0 && password.Length > 0)) { throw new ArgumentException("Email or password cannot be blank"); } bool userValid; using (var dbContext = new sliceofpieEntities2()) { if (dbContext.Users.Count(dbUser => dbUser.Email.Equals(userMail)) > 0) //user exists { User u = dbContext.Users.First(dbUser => dbUser.Email.Equals(userMail)); if (u.Password.Equals(password)) { userValid = true; } else { userValid = false; } } else //create user { dbContext.Users.AddObject(new User() { Email = userMail, Password = password }); dbContext.SaveChanges(); userValid = true; } } return(userValid); }
public override void RemoveFolder(Folder folder) { if (folder == null) { throw new ArgumentNullException(); } IEnumerable <Document> documents; IEnumerable <Folder> folders; using (var dbContext = new sliceofpieEntities2()) { Folder folderToGetFrom = dbContext.Folders.First(fold => folder.Id == fold.Id); documents = folderToGetFrom.Documents.ToList(); folders = folderToGetFrom.Folders.ToList(); } foreach (Document d in documents) { RemoveDocument(d); } foreach (Folder f in folders) { RemoveFolder(f); } using (var dbContext = new sliceofpieEntities2()) { Folder f = dbContext.Folders.First(fold => fold.Id == folder.Id); dbContext.Folders.DeleteObject(f); dbContext.SaveChanges(); } }
public override Project AddProject(string title, string userMail, int id = 0, bool db = false) { if (title == null || userMail == null) throw new ArgumentNullException(); Project p = new Project() { Title = title }; using (var dbContext = new sliceofpieEntities2()) { //Insert project dbContext.Projects.AddObject(p); dbContext.SaveChanges(); } User u = new User() { Email = userMail }; ProjectUser pu = new ProjectUser() { UserEmail = userMail, ProjectId = p.Id }; using (var dbContext = new sliceofpieEntities2()) { //Insert projectUser if (dbContext.Users.Count(dbUser => dbUser.Email.Equals(u.Email)) < 1) throw new ArgumentException("No user with email " + u.Email + " exists!"); dbContext.ProjectUsers.AddObject(pu); dbContext.SaveChanges(); } return new Project() { Title = p.Title, Id = p.Id }; }
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); }
public override void SaveDocument(Document document) { using (var dbContext = new sliceofpieEntities2()) { Revision latestRevFromWeb = dbContext.Revisions.FirstOrDefault(rev => rev.DocumentId == document.Id); string merge; if (latestRevFromWeb != null) { merge = Merger.Merge(document.CurrentRevision, latestRevFromWeb.Content); //Merrrrrge } else { merge = document.CurrentRevision; } Document d = dbContext.Documents.First(doc => doc.Id == document.Id); Revision newRevision = new Revision() { Content = merge, ContentHash = merge.GetHashCode(), Timestamp = DateTime.Now, DocumentId = d.Id }; dbContext.Revisions.AddObject(newRevision); d.Revisions.Add(newRevision); d.CurrentRevision = merge; d.CurrentHash = merge.GetHashCode(); dbContext.SaveChanges(); } }
/// <summary> /// Check whether the provided login details are a valid user. If the user does no exist, /// it is created and true is returned. If it does exist and the information is correct, /// true is returned. If it does exist and the information is INCORRECT, false is returned. /// </summary> /// <param name="userMail">User email to validate</param> /// <param name="password">Password corresponding to email</param> /// <returns>Whether the user is valid</returns> public bool ValidateLogin(string userMail, string password) { if (!(userMail.Length > 0 && password.Length > 0)) { throw new ArgumentException("Email or password cannot be blank"); } bool userValid; using (var dbContext = new sliceofpieEntities2()) { if (dbContext.Users.Count(dbUser => dbUser.Email.Equals(userMail)) > 0) { //user exists User u = dbContext.Users.First(dbUser => dbUser.Email.Equals(userMail)); if (u.Password.Equals(password)) { userValid = true; } else { userValid = false; } } else { //create user dbContext.Users.AddObject(new User() { Email = userMail, Password = password }); dbContext.SaveChanges(); userValid = true; } } return userValid; }
public override Folder AddFolder(IItemContainer parent, string title, int id = 0, bool db = false) { if (title == null || parent == null) { throw new ArgumentNullException(); } Folder f = new Folder() { Title = title, Parent = parent }; if (parent is Folder) { f.FolderId = parent.Id; } else { f.ProjectId = parent.Id; } using (var dbContext = new sliceofpieEntities2()) { dbContext.Folders.AddObject(f); dbContext.SaveChanges(); } return(new Folder() { Title = title, Parent = parent, Id = f.Id, ProjectId = f.ProjectId, FolderId = f.FolderId }); }
public void ShareProject(int projectId, string userMail) { if (projectId == 0) { throw new ArgumentException("Project has to be synced, before it can be shared"); } if (userMail.Length < 1) { throw new ArgumentException("User email cannot be blank"); } userMail = userMail.Trim(); bool userExists = false; using (var dbContext = new sliceofpieEntities2()) { if (dbContext.Users.Count(dbUser => dbUser.Email.Equals(userMail)) > 0) { userExists = true; } } if (!userExists) { throw new ArgumentException("User does not exist"); } bool projectUserExists = true; using (var dbContext = new sliceofpieEntities2()) { var projectUsers = from projectUser in dbContext.ProjectUsers where projectUser.ProjectId == projectId && projectUser.UserEmail == userMail select projectUser; if (projectUsers.Count() == 0) { projectUserExists = false; } } if (projectUserExists) { throw new ArgumentException("User is already sharing this project"); } using (var dbContext = new sliceofpieEntities2()) { ProjectUser projectUser = new ProjectUser(); projectUser.ProjectId = projectId; projectUser.UserEmail = userMail; dbContext.ProjectUsers.AddObject(projectUser); dbContext.SaveChanges(); } }
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; }
public override void RemoveDocument(Document document) { if (document == null) { throw new ArgumentNullException(); } using (var dbContext = new sliceofpieEntities2()) { IEnumerable <Revision> revisions = dbContext.Documents.First(doc => doc.Id == document.Id).Revisions.ToList(); foreach (Revision r in revisions) { dbContext.Revisions.DeleteObject(r); } dbContext.SaveChanges(); } using (var dbContext = new sliceofpieEntities2()) { Document d = dbContext.Documents.First(doc => doc.Id == document.Id); dbContext.Documents.DeleteObject(d); dbContext.SaveChanges(); } }
public override Folder AddFolder(IItemContainer parent, string title, int id = 0, bool db = false) { if (title == null || parent == null) throw new ArgumentNullException(); Folder f = new Folder() { Title = title, Parent = parent }; if (parent is Folder) f.FolderId = parent.Id; else f.ProjectId = parent.Id; using (var dbContext = new sliceofpieEntities2()) { dbContext.Folders.AddObject(f); dbContext.SaveChanges(); } return new Folder() { Title = title, Parent = parent, Id = f.Id, ProjectId = f.ProjectId, FolderId = f.FolderId }; }
public override Project AddProject(string title, string userMail, int id = 0, bool db = false) { if (title == null || userMail == null) { throw new ArgumentNullException(); } Project p = new Project() { Title = title }; using (var dbContext = new sliceofpieEntities2()) { //Insert project dbContext.Projects.AddObject(p); dbContext.SaveChanges(); } User u = new User() { Email = userMail }; ProjectUser pu = new ProjectUser() { UserEmail = userMail, ProjectId = p.Id }; using (var dbContext = new sliceofpieEntities2()) { //Insert projectUser if (dbContext.Users.Count(dbUser => dbUser.Email.Equals(u.Email)) < 1) { throw new ArgumentException("No user with email " + u.Email + " exists!"); } dbContext.ProjectUsers.AddObject(pu); dbContext.SaveChanges(); } return(new Project() { Title = p.Title, Id = p.Id }); }
/// <summary> /// Upload all files and folders to db for specific user. /// </summary> /// <param name="email"></param> public void UploadStructure(string email) { // Projects string[] folders = Directory.GetDirectories(AppPath); foreach (string folderName in folders) { Project dbProject = null; using (var dbContext = new sliceofpieEntities2()) { string pathName = Path.GetFileName(folderName); string[] parts = pathName.Split('-'); int id = int.Parse(parts[0]); string title = pathName.Replace(parts[0] + "-", ""); var dbProjects = from dProject in dbContext.Projects where dProject.Id == id select dProject; if (id > 0 && dbProjects.Count() == 0) { if (Directory.Exists(Path.Combine(AppPath, Helper.GenerateName(id, title)))) { Directory.Move(Path.Combine(AppPath, Helper.GenerateName(id, title)), Path.Combine(AppPath, Helper.GenerateName(0, title))); } id = 0; } if (id > 0) { // Updating project dbProject = dbProjects.First(); dbProject.Title = title; } else { // Creating project dbProject = new Project { Title = title }; dbContext.Projects.AddObject(dbProject); } dbContext.SaveChanges(); } // Rename project directory string projectPath = Path.Combine(AppPath, Helper.GenerateName(dbProject.Id, dbProject.Title)); if (Directory.Exists(Path.Combine(AppPath, Helper.GenerateName(0, dbProject.Title)))) { Directory.Move(Path.Combine(AppPath, Helper.GenerateName(0, dbProject.Title)), projectPath); } // Create project user using (var dbContext = new sliceofpieEntities2()) { var dbProjectUsers = from dProjectUser in dbContext.ProjectUsers where dProjectUser.ProjectId == dbProject.Id && dProjectUser.UserEmail == email select dProjectUser; if (dbProjectUsers.Count() == 0) { dbContext.ProjectUsers.AddObject(new ProjectUser { ProjectId = dbProject.Id, UserEmail = email }); dbContext.SaveChanges(); } } // Upload folders and documents var path = Path.Combine(AppPath, Helper.GenerateName(dbProject.Id, dbProject.Title)); UploadFolders(path, dbProject.Id, Container.Project); UploadDocuments(path, dbProject.Id, Container.Project); } }
/// <summary> /// Upload folders to db for project or folder. /// </summary> /// <param name="parentPath"></param> /// <param name="parentId"></param> /// <param name="container"></param> public void UploadFolders(string parentPath, int parentId, Container container = Container.Folder) { string[] folders = Directory.GetDirectories(parentPath); foreach (string folderName in folders) { Folder dbFolder = null; using (var dbContext = new sliceofpieEntities2()) { string pathName = Path.GetFileName(folderName); string[] parts = pathName.Split('-'); int id = int.Parse(parts[0]); string title = pathName.Replace(parts[0] + "-", ""); var dbFolders = from dFolder in dbContext.Folders where dFolder.Id == id select dFolder; if (id > 0 && dbFolders.Count() == 0) { if (Directory.Exists(Path.Combine(parentPath, Helper.GenerateName(id, title)))) { Directory.Move(Path.Combine(parentPath, Helper.GenerateName(id, title)), Path.Combine(parentPath, Helper.GenerateName(0, title))); } id = 0; } if (id > 0) { // Updating folder dbFolder = dbFolders.First(); dbFolder.Title = title; if (container == Container.Project) { dbFolder.ProjectId = parentId; dbFolder.FolderId = null; } else { dbFolder.ProjectId = null; dbFolder.FolderId = parentId; } } else { // Creating folder dbFolder = new Folder { Title = title }; if (container == Container.Project) { dbFolder.ProjectId = parentId; dbFolder.FolderId = null; } else { dbFolder.ProjectId = null; dbFolder.FolderId = parentId; } dbContext.Folders.AddObject(dbFolder); } dbContext.SaveChanges(); } // Rename folder directory string folderPath = Path.Combine(parentPath, Helper.GenerateName(dbFolder.Id, dbFolder.Title)); if (Directory.Exists(Path.Combine(parentPath, Helper.GenerateName(0, dbFolder.Title)))) { Directory.Move(Path.Combine(parentPath, Helper.GenerateName(0, dbFolder.Title)), folderPath); } // Recursively UploadFolders(folderPath, dbFolder.Id); UploadDocuments(folderPath, dbFolder.Id); } }
/// <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(); } } } }
public override void RemoveFolder(Folder folder) { if (folder == null) throw new ArgumentNullException(); IEnumerable<Document> documents; IEnumerable<Folder> folders; using (var dbContext = new sliceofpieEntities2()) { Folder folderToGetFrom = dbContext.Folders.First(fold => folder.Id == fold.Id); documents = folderToGetFrom.Documents.ToList(); folders = folderToGetFrom.Folders.ToList(); } foreach (Document d in documents) { RemoveDocument(d); } foreach (Folder f in folders) { RemoveFolder(f); } using (var dbContext = new sliceofpieEntities2()) { Folder f = dbContext.Folders.First(fold => fold.Id == folder.Id); dbContext.Folders.DeleteObject(f); dbContext.SaveChanges(); } }
public override void RemoveProject(Project project) { if (project == null) throw new ArgumentNullException(); IEnumerable<Document> documents; IEnumerable<Folder> folders; using (var dbContext = new sliceofpieEntities2()) { Project projectToGetFrom = dbContext.Projects.First(proj => project.Id == proj.Id); documents = projectToGetFrom.Documents.ToList(); folders = projectToGetFrom.Folders.ToList(); } foreach (Document d in documents) { RemoveDocument(d); } foreach (Folder f in folders) { RemoveFolder(f); } using (var dbContext = new sliceofpieEntities2()) { Project p = dbContext.Projects.First(proj => project.Id == proj.Id); ProjectUser pu = dbContext.ProjectUsers.First(user => project.Id == user.ProjectId); dbContext.ProjectUsers.DeleteObject(pu); dbContext.Projects.DeleteObject(p); dbContext.SaveChanges(); } }
public override void SaveDocument(Document document) { using (var dbContext = new sliceofpieEntities2()) { Revision latestRevFromWeb = dbContext.Revisions.FirstOrDefault(rev => rev.DocumentId == document.Id); string merge; if (latestRevFromWeb != null) merge = Merger.Merge(document.CurrentRevision, latestRevFromWeb.Content); //Merrrrrge else merge = document.CurrentRevision; Document d = dbContext.Documents.First(doc => doc.Id == document.Id); Revision newRevision = new Revision() { Content = merge, ContentHash = merge.GetHashCode(), Timestamp = DateTime.Now, DocumentId = d.Id }; dbContext.Revisions.AddObject(newRevision); d.Revisions.Add(newRevision); d.CurrentRevision = merge; d.CurrentHash = merge.GetHashCode(); dbContext.SaveChanges(); } }
public override void RemoveDocument(Document document) { if (document == null) throw new ArgumentNullException(); using (var dbContext = new sliceofpieEntities2()) { IEnumerable<Revision> revisions = dbContext.Documents.First(doc => doc.Id == document.Id).Revisions.ToList(); foreach (Revision r in revisions) { dbContext.Revisions.DeleteObject(r); } dbContext.SaveChanges(); } using (var dbContext = new sliceofpieEntities2()) { Document d = dbContext.Documents.First(doc => doc.Id == document.Id); dbContext.Documents.DeleteObject(d); dbContext.SaveChanges(); } }