/// <summary>
        /// Allows the attachment of database records or other types of information which DO NOT require saving to the file system.
        /// Be aware the distinct displayText class expects the RelatedId and RelatedEnumId to form a unique key.
        /// </summary>
        /// <param name="formsRepo"> IFormsRepository necessary for database access.</param>
        /// <param name="RelatedId">Identifier used to associate a file with a record type.</param>
        /// <param name="fa">FileAttachment record to be recorded.</param>
        /// <returns></returns>
        public static int CreateDataAttachment(IFormsRepository formsRepo, int RelatedId, def_FileAttachment fa)
        {
            int fileId = -1;

            Debug.WriteLine("FileUploads CreateDataAttachment FileName: " + fa.FileName);

            if (!string.IsNullOrEmpty(fa.FilePath))
            {
                def_FileAttachment faQuery = formsRepo.GetFileAttachment(RelatedId, fa.RelatedEnumId, fa.FilePath);
                if (faQuery == null)
                {
                    fileId = formsRepo.AddFileAttachment(fa);
                }
                else
                {
                    formsRepo.UpdateFileAttachment(fa);

                    fileId = fa.FileId;
                }
            }
            else
            {
                Debug.WriteLine("FileUploads CreateAttachment Error: File not saved.");
            }

            return(fileId);
        }
        /// <summary>
        /// Adds a new record, or Updates an existing record, to the def_FileAttachment table, then saves the file in the file system.  Files in the same directory with the same name
        /// will be overwritten without warning.  Use of sub-directories can prevent this.
        /// </summary>
        /// <param name="formsRepo"> IFormsRepository necessary for database access.</param>
        /// <param name="fileStream">input stream containing the contents of the attachment file</param>
        /// <param name="originalFileName">the original filename of the attachment file</param>
        /// <param name="subDir">Optional superDirectory name, set to null to disable.</param>
        /// <param name="subDir">Optional subDirectory name, set to null to disable.</param>
        /// <param name="RelatedId">Identifier used to associate a file with a record type.</param>
        /// <param name="RelatedEnumId">Type of identifier used in RelatedId.</param>
        /// <param name="AttachTypeId">Indicates how the file was attached, which may represent the file itself or just a generic type.</param>
        /// <returns>Int value of the FileId</returns>
        public static int CreateAttachment(IFormsRepository formsRepo, Stream fileStream, string originalFileName, string superDir, string subDir, int RelatedId, int RelatedEnumId, int AttachTypeId)
        {
            int fileId = -1;

            if (fileStream != null && !String.IsNullOrEmpty(originalFileName.Trim()))
            {
                Debug.WriteLine("FileUploads CreateAttachment FileName: " + originalFileName);
                // Save the file to the file system.
                string fileName = SaveUpload(fileStream, Path.GetFileName(originalFileName), subDir, RelatedId, superDir);

                if (!String.IsNullOrEmpty(fileName))
                {
                    // Append the sub directory and the file name if sub directory has a value.
                    String subFileName = (!String.IsNullOrEmpty(subDir)) ? subDir + Path.DirectorySeparatorChar + fileName.Substring(fileName.LastIndexOf(Path.DirectorySeparatorChar) + 1)
                                                                                                                                        : fileName.Substring(fileName.LastIndexOf(Path.DirectorySeparatorChar) + 1);
                    def_FileAttachment fa = formsRepo.GetFileAttachment(RelatedId, RelatedEnumId, subFileName);
                    if (fa == null)
                    {
                        fa = new def_FileAttachment();
                        fa.EnterpriseId = SessionHelper.LoginStatus.EnterpriseID;
                        //fa.GroupId = SessionHelper.LoginStatus.GroupID;  //LoginStatus was returning an invalid number for GroupId
                        fa.UserId        = SessionHelper.LoginStatus.UserID;
                        fa.AttachTypeId  = AttachTypeId;
                        fa.RelatedId     = RelatedId;
                        fa.RelatedEnumId = RelatedEnumId;
                        fa.displayText   = fileName.Substring(fileName.LastIndexOf(Path.DirectorySeparatorChar) + 1);
                        fa.FilePath      = fileName;
                        fa.FileName      = subFileName;
                        fa.StatusFlag    = "A";
                        fa.CreatedDate   = DateTime.Now;
                        fa.CreatedBy     = SessionHelper.LoginStatus.UserID;

                        fileId = formsRepo.AddFileAttachment(fa);
                    }
                    else
                    {
                        fa.FilePath   = fileName;
                        fa.FileName   = subFileName;
                        fa.StatusFlag = "A";
                        formsRepo.UpdateFileAttachment(fa);

                        fileId = fa.FileId;
                    }
                }
                else
                {
                    Debug.WriteLine("FileUploads CreateAttachment Error: File not saved.");
                }
            }
            else
            {
                Debug.WriteLine("FileUploads CreateAttachment Error: File not found.");
            }

            return(fileId);
        }
Example #3
0
        private List <CaFullReportRow> BuildHistoryRowsForBaseRow(CaFullReportRow baseRow, vFormResultUser thisRecord, IEnumerable <vFormResultUser> allRecords)
        {
            IQueryable <def_StatusLog> statusLogs = formsRepo
                                                    .GetStatusLogsForFormResultId(thisRecord.formResultId)
                                                    .Where(sl => sl.statusDetailIdTo.HasValue);
            List <CaFullReportRow> result = new List <CaFullReportRow>();

            foreach (def_StatusLog sl in statusLogs)
            {
                def_FileAttachment snapshot = formsRepo.GetFileAttachment(sl.statusLogId, 2);
                result.Add(new CaFullReportRow(formsRepo, allRecords, baseRow.formIdentifier, thisRecord, sl.statusLogDate, true, snapshot));
            }
            return(result);
        }
        public static bool DeleteFile(IFormsRepository formsRepo, int FileId)
        {
            // Pull the database record and set the Status Flag to 'D'.
            bool success = false;

            try {
                def_FileAttachment fa = formsRepo.GetFileAttachment(FileId);
                fa.StatusFlag = "D";
                success       = formsRepo.UpdateFileAttachment(fa);
            }
            catch (Exception excptn) {
                Debug.WriteLine("FileUploads RetrieveFile Error: " + excptn.Message);
            }

            return(success);
        }
        public bool UploadFile(FormCollection formCollection)
        {
            bool edit       = UAS_Business_Functions.hasPermission(UAS.Business.PermissionConstants.EDIT, UAS.Business.PermissionConstants.ASSMNTS);
            bool editlocked = UAS_Business_Functions.hasPermission(UAS.Business.PermissionConstants.EDIT_LOCKED, UAS.Business.PermissionConstants.ASSMNTS);
            int  fileId     = -1;

            if (edit || editlocked)
            {
                int RelatedEnumId = formsRepo.GetRelatedEnumIdByEnumDescription("formResultId");
                int AttachTypeId  = formsRepo.GetAttachTypeIdByAttachDescription("Generic Upload");

                int formResultId = Convert.ToInt32(formCollection["formResultId"]);
                System.Web.HttpPostedFileWrapper file = (System.Web.HttpPostedFileWrapper)Request.Files["file" + formResultId.ToString()];
                def_FormResults fr = formsRepo.GetFormResultById(formResultId);

                DateTime now    = DateTime.Now;
                string   date   = now.Year.ToString() + ((now.Month < 10) ? "0" : "") + now.Month.ToString() + ((now.Day < 10) ? "0" : "") + now.Day.ToString();
                string   time   = ((now.Hour < 10) ? "0" : "") + now.Hour.ToString() + ((now.Minute < 10) ? "0" : "") + now.Minute.ToString() + ((now.Second < 10) ? "0" : "") + now.Second.ToString();
                string   subDir = date + Path.DirectorySeparatorChar + time;

                fileId = FileUploads.CreateAttachment(formsRepo, file, null, subDir, formResultId, RelatedEnumId, AttachTypeId);

                // check if the file uploaded has a duplicated displayText for this assessment.  i.e., a logical overwrite.
                if (fileId > -1)
                {
                    def_FileAttachment       fa    = formsRepo.GetFileAttachment(fileId);
                    Dictionary <int, string> texts = FileUploads.RetrieveFileDisplayTextsByRelatedId(formsRepo, formResultId, RelatedEnumId, "A", AttachTypeId);

                    foreach (int k in texts.Keys)
                    {
                        if (texts[k].Equals(fa.displayText) && k != fa.FileId)
                        {
                            FileUploads.DeleteFile(formsRepo, k);
                        }
                    }
                }
            }

            if (fileId > -1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
 public FullReportRow(
     IFormsRepository formsRepo,
     IEnumerable <vFormResultUser> allVfrus,
     string formIdentifier,
     vFormResultUser vfru,
     DateTime?statusChangeDate,
     bool isHistorical = false,
     def_FileAttachment historicalSnapshot = null)
 {
     this.formsRepo          = formsRepo;
     this.allVfrus           = allVfrus;
     this.formIdentifier     = formIdentifier;
     this.vfru               = vfru;
     this.statusChangeDate   = statusChangeDate;
     this.isHistorical       = isHistorical;
     this.historicalSnapshot = historicalSnapshot;
 }
        public static FileContentResult RetrieveFile(IFormsRepository formsRepo, int FileId)
        {
            try {
                def_FileAttachment fa = formsRepo.GetFileAttachment(FileId);

                FileContentResult result = new FileContentResult(System.IO.File.ReadAllBytes(fa.FilePath),
                                                                 System.Net.Mime.MediaTypeNames.Application.Octet);
                result.FileDownloadName = System.IO.Path.GetFileName(fa.FilePath);

                return(result);
            }
            catch (Exception excptn) {
                Debug.WriteLine("FileUploads RetrieveFile Error: " + excptn.Message);

                return(new FileContentResult(Encoding.ASCII.GetBytes(excptn.Message), "text/html"));
            }
        }
Example #8
0
        public ActionResult RetrieveMagi(int index)
        {
            //Use the index and the current formResultId to pull the fileAttachment record to find the formResultId of the MAGI form.
            int fileId        = -1;
            int RelatedEnumId = formsRepo.GetRelatedEnumIdByEnumDescription("formResultId");
            int AttachTypeId  = formsRepo.GetAttachTypeIdByAttachDescription("Def Form");

            string attachedForm = findMagi(index, RelatedEnumId, AttachTypeId);

            if (String.IsNullOrEmpty(attachedForm))
            {
                def_FormResults fr = formsRepo.GetFormResultById(SessionHelper.SessionForm.formResultId);

                def_FormResults magi = CreateMagiForm(fr.subject);
                attachedForm = magi.formResultId.ToString();

                def_FileAttachment fa = new def_FileAttachment()
                {
                    EnterpriseId  = SessionHelper.LoginStatus.EnterpriseID,
                    GroupId       = SessionHelper.LoginStatus.GroupID,
                    UserId        = fr.subject,
                    AttachTypeId  = AttachTypeId,
                    RelatedId     = fr.formResultId,
                    RelatedEnumId = RelatedEnumId,
                    displayText   = index + "/" + attachedForm,
                    FilePath      = index + "/" + attachedForm,
                    FileName      = attachedForm,
                    StatusFlag    = "A",
                    CreatedDate   = DateTime.Now,
                    CreatedBy     = SessionHelper.LoginStatus.UserID
                };
                FileUploads.CreateDataAttachment(formsRepo, SessionHelper.SessionForm.formResultId, fa);
            }

            // Save the current form data as a session variable
            SessionForm sf = new SessionForm();

            sf.formId       = SessionHelper.SessionForm.formId;
            sf.formResultId = SessionHelper.SessionForm.formResultId;
            sf.partId       = SessionHelper.SessionForm.partId;
            sf.sectionId    = SessionHelper.SessionForm.sectionId;
            sf.itemId       = SessionHelper.SessionForm.itemId;
            Session.Add("ParentFormData", sf);

            return(RedirectToAction("ToTemplate", "Adap", new { formResultId = attachedForm }));
        }
Example #9
0
        public bool UpdateFileAttachment(def_FileAttachment fa)
        {
            bool success = false;

            try
            {
                db.def_FileAttachment.Attach(fa);

                db.Entry(fa).State = EntityState.Modified;
                Save();
                success = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("* * *  FormsRepository  UpdateFileAttachment exception: " + ex.Message);
            }

            return(success);
        }
        /// <summary>
        /// Overloaded method allowing the creation of the File Attachment record in a higher level method.  The FilePath is the only field updated within this method.
        /// </summary>
        /// <param name="formsRepo"> IFormsRepository necessary for database access.</param>
        /// <param name="file">File being uploaded.</param>
        /// <param name="subDir">Optional superDirectory name, set to null to disable.</param>
        /// <param name="subDir">Optional subDirectory name, set to null to disable.</param>
        /// <param name="RelatedId">Identifier used to associate a file with a record type.</param>
        /// <param name="fa">FileAttachment record to be recorded.</param>
        /// <returns>Int value of the FileId</returns>
        public static int CreateAttachment(IFormsRepository formsRepo, System.Web.HttpPostedFileWrapper file, string superDir, string subDir, int RelatedId, def_FileAttachment fa)
        {
            int fileId = -1;

            if (file != null && !String.IsNullOrEmpty(file.FileName.Trim()))
            {
                Debug.WriteLine("FileUploads CreateAttachment FileName: " + file.FileName);
                // Save the file to the file system.
                string fileName = SaveUpload(file.InputStream, Path.GetFileName(file.FileName), subDir, RelatedId, superDir);

                if (!string.IsNullOrEmpty(fileName))
                {
                    def_FileAttachment faQuery = formsRepo.GetFileAttachment(RelatedId, fa.RelatedEnumId, fileName);
                    if (faQuery == null)
                    {
                        fa.FilePath = fileName;
                        fileId      = formsRepo.AddFileAttachment(fa);
                    }
                    else
                    {
                        fa.FilePath = fileName;
                        formsRepo.UpdateFileAttachment(fa);

                        fileId = fa.FileId;
                    }
                }
                else
                {
                    Debug.WriteLine("FileUploads CreateAttachment Error: File not saved.");
                }
            }
            else
            {
                Debug.WriteLine("FileUploads CreateAttachment Error: File not found.");
            }

            return(fileId);
        }
Example #11
0
 public int AddFileAttachment(def_FileAttachment fa)
 {
     db.def_FileAttachment.Add(fa);
     db.SaveChanges();
     return(fa.FileId);
 }