/// <summary> /// Get a specific Document /// </summary> /// <param name="id">Document Id</param> /// <param name="authorized">bool to check if user should be able to retrieve HIDDEN documents</param> /// <returns></returns> public tbl_Document SelectById(string id, bool authorized) { int docId; tbl_Document document = null; try { docId = Int32.Parse(id); if (authorized == true) { document = _db.tbl_Document.AsNoTracking().SingleOrDefault(p => p.Document_ID == docId); } else { document = _db.tbl_Document.AsNoTracking().SingleOrDefault(p => p.Document_ID == docId && p.Active_IND == true); } //if document exists and ArchiveFile is null, it will look into the purged WAS db instead. if (document.ArchivedFile == null) { this._db = new WASEntities("name=WASArchiveEntities"); if (authorized == true) { document = _db.tbl_Document.AsNoTracking().SingleOrDefault(p => p.Document_ID == docId); } else { document = _db.tbl_Document.AsNoTracking().SingleOrDefault(p => p.Document_ID == docId && p.Active_IND == true); } //Because this is a rare occurance, I would rather blindly search through other db's than change my model to bring in the repo value //if more than one repo is used, we will have to create a repo attribute on the document model and bring tbl_Document.Repository_ID over to check and find } return(document); } catch (FormatException e) { FormatException exception = new FormatException("Document Id must be a positive integer", e); exception.HelpLink = "Please check over the provided information and try again."; exception.Data["Document ID"] = id; throw exception; } catch (InvalidOperationException e) { InvalidOperationException exception = new InvalidOperationException("There was an issue connecting to the database", e); exception.HelpLink = "Please contact Support through ServiceNow."; throw exception; } catch (Exception e) { //maybe write more here throw new Exception("There seems to be an issue.", e); } }
public string SaveUpdateDocument(HttpPostedFileBase fb, DocumentModel model) { string filePath = ""; string fileName = ""; string sysFileName = ""; string msg = ""; ShomaRMEntities db = new ShomaRMEntities(); if (fb != null && fb.ContentLength > 0) { filePath = HttpContext.Current.Server.MapPath("~/Content/assets/img/Document/"); fileName = fb.FileName; sysFileName = DateTime.Now.ToFileTime().ToString() + Path.GetExtension(fb.FileName); fb.SaveAs(filePath + "//" + sysFileName); if (!string.IsNullOrWhiteSpace(fb.FileName)) { string afileName = HttpContext.Current.Server.MapPath("~/Content/assets/img/Document/") + "/" + sysFileName; } } int userid = ShomaGroupWebSession.CurrentUser != null ? ShomaGroupWebSession.CurrentUser.UserID : 0; if (model.DocID == 0) { var saveDocument = new tbl_Document() { TenantID = model.TenantID, DocumentName = sysFileName, DocumentType = model.DocumentType, DocumentNumber = model.DocumentNumber, UploadBy = userid, UploadDate = DateTime.Now.Date }; db.tbl_Document.Add(saveDocument); db.SaveChanges(); msg = "Document Save Successfully"; } else { var GetDocumentData = db.tbl_Document.Where(p => p.DocID == model.DocID).FirstOrDefault(); if (GetDocumentData != null) { GetDocumentData.TenantID = model.TenantID; GetDocumentData.DocumentName = sysFileName; GetDocumentData.DocumentType = model.DocumentType; GetDocumentData.DocumentNumber = model.DocumentNumber; GetDocumentData.UploadBy = userid; GetDocumentData.UploadDate = DateTime.Now.Date; db.SaveChanges(); msg = "Document Updated Successfully"; } } db.Dispose(); return(msg); }
/// <summary> /// Downloads one file and puts it into a zip file /// </summary> /// <param name="zipArchive">The zipfile to put the file into</param> /// <param name="file">the file to download</param> private void DownloadFile(ZipArchive zipArchive, tbl_Document file) { if (file.ArchivedFile != null) { //according to Ramin, creation of an ArchivedFile and Submitting an ArchivedFile are different steps, so there could be 'dirty' records/documents in WAS db that has no ArchivedFile Fields records var zipEntry = zipArchive.CreateEntry(file.Document_ID.ToString() + "." + file.FileExtension); //creates a unit of space for the individual file to be placed in using (var entryStream = zipEntry.Open()) { using (var tmpMemory = new MemoryStream(file.ArchivedFile)) { tmpMemory.CopyTo(entryStream); //copies the data into the unit space } } } }
/// <summary> /// Updates the entity model with the changes, but does not save. /// </summary> /// <param name="publicVM">The publicVm instance</param> /// <param name="db">the database</param> /// <returns>(make use of this)</returns> public bool UpdateChanges(PublicVM publicVM, WASEntities db) { tbl_Document modDoc = SelectById(publicVM.Document_ID.ToString(), true); //maybe more elegant way to do this modDoc.Issue_DT = publicVM.IssueDate; modDoc.Description = publicVM.Description; modDoc.Method = publicVM.Method; modDoc.Originator = publicVM.Originator; modDoc.Reason = publicVM.Reason; modDoc.Recipient = publicVM.Recipient; modDoc.Active_IND = publicVM.Hidden; db.Entry(modDoc).State = System.Data.Entity.EntityState.Modified; //tags the row as one thats modifed and needs to be saved //should probably do some conditional check here to allow user to know if it actually got modified return(true); }
public tbl_Document SelectById(string id, bool authorized) { int docId = 0; tbl_Document document = null; try { docId = Int32.Parse(id); if (authorized == true) { document = _db.tbl_Document.AsNoTracking().SingleOrDefault(p => p.Document_ID == docId); } else { document = _db.tbl_Document.AsNoTracking().SingleOrDefault(p => p.Document_ID == docId && p.Active_IND == true); } //if document exists and ArchiveFile is null, it will look into the purged WAS db instead. if (document.ArchivedFile == null) { this._db = new WASEntities("name=WASArchiveEntities"); if (authorized == true) { document = _db.tbl_Document.AsNoTracking().SingleOrDefault(p => p.Document_ID == docId && p.Active_IND == true); } else { document = _db.tbl_Document.AsNoTracking().SingleOrDefault(p => p.Document_ID == docId && p.Active_IND == false); } //Because this is a rare occurance, I would rather blindly search through other db's than change my model to bring in the repo value //if more than one repo is used, we will have to create a repo attribute on the document model and bring tbl_Document.Repository_ID over to check and find } } catch { return(null); } return(document); }
public ActionResult Edit([Bind(Prefix = "publicId")] string Folder_ID, List <PublicVM> updatedEditList) { if (ModelState.IsValid) { foreach (PublicVM pvm in updatedEditList) { tbl_Document modDoc = documentRepository.SelectById(pvm.Document_ID.ToString(), true); modDoc.Issue_DT = pvm.IssueDate; modDoc.Description = pvm.Description; modDoc.Method = pvm.Method; modDoc.Originator = pvm.Originator; modDoc.Reason = pvm.Reason; modDoc.Recipient = pvm.Recipient; modDoc.Active_IND = pvm.Hidden; _db.Entry(modDoc).State = System.Data.Entity.EntityState.Modified; } _db.SaveChanges(); } return(RedirectToAction("Index", "Folder", new { ClientId = TempData["Client_Id"], Role = "Admin" })); }
public void Update(tbl_Document doc) { _db.Entry(doc).State = System.Data.Entity.EntityState.Modified; }
public bool SaveChanges(tbl_Document doc) { //dont think im using this method, maybe others below too return(true); }
public ActionResult FileDisplay([Bind(Prefix = "documentId")] string id) { string MimeType = null; //only admins can see hidden file contents tbl_Document file = (User.IsInRole("IT-ops") ? documentRepository.SelectById(id, true) : documentRepository.SelectById(id, false)); if (file == null) { NoResultException exception = new NoResultException("The document is not available or does not exist."); exception.HelpLink = "Please check over the provided information and try again."; exception.Data["Document ID"] = id; throw exception; } else if (file.ArchivedFile == null) { NullReferenceException exception = new NullReferenceException("The file appears to be missing or corrupt."); exception.HelpLink = "Please contact ServiceNow if additional help is needed."; throw exception; } else if (file.ArchivedFile.Length < 100) { //rare occation of when there is a file, but possibly corrupted //better to use IOException, but seems like a waste to bring in a whole new package for this Exception exception = new Exception("The file was unable to be open."); exception.HelpLink = "Please contact Support at ServiceNow"; exception.Data["Document ID"] = id; throw exception; } else { //maybe can do this better than comparing strings switch (file.FileExtension.ToLower().Trim()) { case "pdf": MimeType = "application/pdf"; break; case "gif": MimeType = "image/gif"; break; case "jpg": MimeType = "image/jpeg"; break; case "msg": MimeType = "application/vnd.outlook"; //HttpContext.Response.AddHeader("Content-Disposition", "Attachment"); break; case "ppt": MimeType = "application/vnd.ms-powerpoint"; HttpContext.Response.AddHeader("Content-Disposition", "Attachment"); break; case "xls": MimeType = "application/vnd.ms-excel"; HttpContext.Response.AddHeader("Content-Disposition", "Attachment"); break; case "csv": MimeType = "application/vnd.ms-excel"; HttpContext.Response.AddHeader("Content-Disposition", "Attachment"); break; case "xlsx": MimeType = "application/vnd.ms-excel.12"; HttpContext.Response.AddHeader("Content-Disposition", "Attachment"); break; case "doc": case "dot": MimeType = "application/msword"; HttpContext.Response.AddHeader("Content-Disposition", "Attachment"); break; case "docx": MimeType = "application/vnd.ms-word.document.12"; HttpContext.Response.AddHeader("Content-Disposition", "Attachment"); break; default: MimeType = "text/html"; break; } } return(File(file.ArchivedFile, MimeType)); }
// Get: File public ActionResult FileDisplay([Bind(Prefix = "documentId")] string id) { tbl_Document file = (User.IsInRole("IT-ops") ? documentRepository.SelectById(id, true) : documentRepository.SelectById(id, false)); string MimeType = null; if (file == null) { ViewData["repositoryRequestDocId"] = id; TempData.Clear(); TempData["error_info"] = "The document is not available or does not exist."; //maybe seperate this later with a check for tbl_doc.Active_IND TempData["importance"] = false; return(RedirectToAction("Index", "ErrorHandler", null)); } if (file.ArchivedFile.Length < 100) { //rare occation of when there is a file, but possibly corrupted TempData["error_info"] = "The file was unable to be open."; TempData["importance"] = true; return(RedirectToAction("Index", "ErrorHandler", null)); } switch (file.FileExtension.ToLower().Trim()) { case "pdf": MimeType = "application/pdf"; break; case "gif": MimeType = "image/gif"; break; case "jpg": MimeType = "image/jpeg"; break; case "msg": MimeType = "application/vnd.outlook"; break; case "ppt": MimeType = "application/vnd.ms-powerpoint"; break; case "xls": case "csv": MimeType = "application/vnd.ms-excel"; break; case "xlsx": MimeType = "application/vnd.ms-excel.12"; break; case "doc": case "dot": MimeType = "application/msword"; break; case "docx": MimeType = "application/vnd.ms-word.document.12"; break; default: MimeType = "text/html"; break; } if (file.ArchivedFile == null) { ViewData["repositoryRequestDocId"] = id; return(PartialView("_FileDisplay")); } return(File(file.ArchivedFile, MimeType)); }
/// <summary> /// Add document into database /// </summary> /// <param name="doc"></param> private int AddDocument(Document doc) { var context = new dbDataContext(); var document = new tbl_Document { DocumentTypeValue = doc.DocumentTypeValue, FileName = doc.FileName, RefId = doc.RefId, DocumentPath = GetDocumentLocation(doc.DocumentTypeValue, doc.RefId, doc.SubRefId, true) + doc.FileName, CreatedDate = DateTime.Now, SubRefId = doc.SubRefId, DocGuid = Guid.NewGuid().ToString(), UploadedBy = doc.UploadedBy }; context.tbl_Documents.InsertOnSubmit(document); context.SubmitChanges(); return document.DocumentId; }
partial void Deletetbl_Document(tbl_Document instance);
partial void Updatetbl_Document(tbl_Document instance);
partial void Inserttbl_Document(tbl_Document instance);