public JsonNetResult CreateModal(SourceAuthorViewModel vm) { IList <SourceAuthor> existing = this.sourcePermissionTasks.GetSourceAuthor(vm.Author); if (existing != null && existing.Count > 0) { ModelState.AddModelError("Author", "Author name already exists."); } if (ModelState.IsValid) { SourceAuthor entity = new SourceAuthor(); entity.Author = vm.Author; entity = this.sourcePermissionTasks.SaveSourceAuthor(entity); return(JsonNet(new { Id = entity.Id, Name = entity.Author, WasSuccessful = true })); } else { return(JsonNet(this.GetErrorsForJson())); } }
public JsonNetResult Name(int id) { SourceAuthor a = this.sourcePermissionTasks.GetSourceAuthor(id); if (a != null) { return(JsonNet(new { Id = id, Name = a.Author })); } else { return(JsonNet(string.Empty)); } }
public void SaveSourceAuthors(string authors, Source source) { string[] authorsId = authors.Split(','); foreach (var authorName in authorsId) { var author = _db.Authors.FirstOrDefault(a => a.Name == authorName); if (author != null) { SourceAuthor check = _db.SourceAuthors.FirstOrDefault(s => s.SourceId == source.Id && s.AuthorId == author.Id); if (check == null) { SourceAuthor sourceAuthor = new SourceAuthor() { SourceId = source.Id, AuthorId = author.Id }; _db.SourceAuthors.Add(sourceAuthor); _db.SaveChanges(); } } } }
public SourceAuthor SaveSourceAuthor(SourceAuthor author) { return(this.sourceAuthorRepo.SaveOrUpdate(author)); }
protected FeedingSource ProcessUpload(MultipleUploadViewModel vm, int index) { // we use this flag instead of ModelState.IsValid because we need to reset this value for every file; ModelState.IsValid is a single flag for all uploaded files. bool hasError = false; using (Stream fileStream = vm.FileData[index].InputStream) { // Progressively check for duplicates, most reliable check first. Only return one duplicate validation error if any. // check Source hash duplicate string hash = BitConverter.ToString(MD5.Create().ComputeHash(fileStream)).Replace("-", ""); IList <SourceDTO> dtos = this.sourceTasks.GetSources(hash); if (dtos != null && dtos.Count > 0) { hasError = true; ModelState.AddModelError( "FileData", "An identical source (<a href='" + Url.Action("Index", "Sources", new { area = "Profiling" }) + "#info/" + dtos.First().SourceID + "' target='_blank'>" + dtos.First().SourceName + "</a>) exists already with Source ID of " + dtos.First().SourceID + "." ); } else { // check Source name duplicate Source s = this.sourceTasks.GetSource(vm.FileData[index].FileName); if (s != null) { hasError = true; ModelState.AddModelError( "FileData", "<a href='" + Url.Action("Index", "Sources", new { area = "Profiling" }) + "#info/" + s.Id + "' target='_blank'>" + s.SourceName + "</a> exists already with Source ID of " + s.Id + ". If you're sure you have a different file, rename it before uploading." ); } else { // check FeedingSource name duplicate FeedingSource fs = this.feedingSourceTasks.GetFeedingSource(vm.FileData[index].FileName); if (fs != null) { hasError = true; ModelState.AddModelError( "FileData", "<a href='" + Url.Action("Details", "Feeding", new { area = "Sources", id = fs.Id }) + "' target='_blank'>" + vm.FileData[index].FileName + "</a> was already uploaded; delete it or change your file name." ); } } } AdminUser user = this.userTasks.GetAdminUser(User.Identity.Name); if (user == null) { hasError = true; ModelState.AddModelError("UploadedBy", "Logged-in user doesn't appear to be exist."); } if (vm.IsReadOnly && string.IsNullOrEmpty(vm.UploadNotes)) { hasError = true; ModelState.AddModelError("UploadNotes", "Read-only sources should have a justification in the notes."); } if (!hasError) { FeedingSource fs = new FeedingSource(); fs.Name = Path.GetFileName(vm.FileData[index].FileName); fs.Restricted = vm.Restricted; fs.IsReadOnly = vm.IsReadOnly; fs.IsPublic = vm.IsPublic; fs.FileModifiedDateTime = vm.FileModifiedDateTime != null && vm.FileModifiedDateTime[index] != null ? vm.FileModifiedDateTime[index] : DateTime.Now; fs.UploadedBy = user; fs.UploadDate = DateTime.Now; fs.UploadNotes = vm.UploadNotes; if (!string.IsNullOrEmpty(vm.AuthorIds)) { string[] ids = vm.AuthorIds.Split(','); foreach (string id in ids) { int result; if (int.TryParse(id, out result)) { SourceAuthor a = this.sourcePermissionTasks.GetSourceAuthor(result); if (a != null) { fs.SourceAuthors.Add(a); } } } } if (!string.IsNullOrEmpty(vm.OwnerIds)) { string[] ids = vm.OwnerIds.Split(','); foreach (string id in ids) { int result; if (int.TryParse(id, out result)) { SourceOwningEntity e = this.sourcePermissionTasks.GetSourceOwningEntity(result); if (e != null) { fs.SourceOwningEntities.Add(e); } } } } fileStream.Position = 0; // we read the stream earlier when computing hash MemoryStream memoryStream = fileStream as MemoryStream; if (memoryStream == null) { memoryStream = new MemoryStream(); fileStream.CopyTo(memoryStream); } fs.FileData = memoryStream.ToArray(); return(fs); } return(null); } }
public ActionResult Approve(FeedingSourceViewModel vm) { if (vm != null && vm.Id > 0) { AdminUser user = this.userTasks.GetAdminUser(User.Identity.Name); if (user != null) { FeedingSource fs = this.feedingSourceTasks.GetFeedingSource(vm.Id); if (fs != null) { if (!((PrfPrincipal)User).CanAccess(fs)) { return(new HttpUnauthorizedResult()); } if (fs.UploadedBy != user) { fs.ApprovedBy = user; fs.ApprovedDate = DateTime.Now; fs.Restricted = vm.Restricted; fs.IsReadOnly = vm.IsReadOnly; fs.IsPublic = vm.IsPublic; fs.UploadNotes = vm.UploadNotes; if (!string.IsNullOrEmpty(vm.AuthorIds)) { fs.SourceAuthors.Clear(); string[] ids = vm.AuthorIds.Split(','); foreach (string id in ids) { int result; if (int.TryParse(id, out result)) { SourceAuthor a = this.sourcePermissionTasks.GetSourceAuthor(result); if (a != null) { fs.SourceAuthors.Add(a); } } } } if (!string.IsNullOrEmpty(vm.OwnerIds)) { fs.SourceOwningEntities.Clear(); string[] ids = vm.OwnerIds.Split(','); foreach (string id in ids) { int result; if (int.TryParse(id, out result)) { SourceOwningEntity e = this.sourcePermissionTasks.GetSourceOwningEntity(result); if (e != null) { fs.SourceOwningEntities.Add(e); } } } } fs = this.feedingSourceTasks.SaveFeedingSource(fs); Source source = this.feedingSourceTasks.FeedSource(fs.Id); this.emailTasks.SendFeedingSourceApprovedEmail(fs); return(RedirectToAction("Index")); } else { ModelState.AddModelError("ApprovedBy", "User cannot approve a source they uploaded themselves."); } } else { ModelState.AddModelError("Id", "Feeding source doesn't exist."); } } else { ModelState.AddModelError("ApprovedBy", "Logged-in user doesn't exist."); } } else { ModelState.AddModelError("Id", "No Id was sent."); } return(Approve(vm.Id)); }
public JsonNetResult Edit(SourceViewModel vm) { if (ModelState.IsValid) { Source s = this.sourceTasks.GetSource(vm.Id); s.FullReference = vm.FullReference; s.Notes = vm.Notes; s.IsRestricted = vm.IsRestricted; s.IsReadOnly = vm.IsReadOnly; s.IsPublic = vm.IsPublic; s.Archive = vm.Archive; s.SourceAuthors.Clear(); if (!string.IsNullOrEmpty(vm.SourceAuthorIds)) { string[] ids = vm.SourceAuthorIds.Split(','); foreach (string id in ids) { int result; if (int.TryParse(id, out result)) { SourceAuthor a = this.sourcePermissionTasks.GetSourceAuthor(result); if (a != null) { s.SourceAuthors.Add(a); } } } } s.SourceOwningEntities.Clear(); if (!string.IsNullOrEmpty(vm.SourceOwningEntityIds)) { string[] ids = vm.SourceOwningEntityIds.Split(','); foreach (string id in ids) { int result; if (int.TryParse(id, out result)) { SourceOwningEntity e = this.sourcePermissionTasks.GetSourceOwningEntity(result); if (e != null) { s.SourceOwningEntities.Add(e); } } } } s = this.sourceTasks.SaveSource(s); // queue indexing BackgroundJob.Enqueue <ISourceTasks>(x => x.IndexSourceQueueable(s.Id, s.HasUploadedBy() ? s.GetUploadedBy().UserID : string.Empty, s.SourceAuthors.Select(y => y.Author).ToList(), s.SourceOwningEntities.Select(y => y.Name).ToList(), s.JhroCase != null ? s.JhroCase.CaseNumber : string.Empty, this.sourceTasks.GetSourceDTO(s.Id).FileSize) ); return(JsonNet(string.Empty)); } return(JsonNet(this.GetErrorsForJson())); }