Example #1
0
 public int GetNextAttachmentSeq()
 {
     _commonDataAccess = new CommonDataAccess(_context);
     return(_commonDataAccess.GetNextAttachmentSeq());
 }
Example #2
0
        private void SaveAttachments(int newsId, NewsEntity newsEnity, IEnumerable <AttachmentEntity> attachments)
        {
            foreach (AttachmentEntity attachment in attachments)
            {
                TB_T_NEWS_ATTACHMENT dbAttach = null;
                var isIDNull = attachment.AttachmentId == null || attachment.AttachmentId == 0 ? true : false;

                if (isIDNull)
                {
                    dbAttach                 = new TB_T_NEWS_ATTACHMENT();
                    dbAttach.NEWS_ID         = newsId;
                    dbAttach.ATTACHMENT_NAME = attachment.Name;
                    dbAttach.ATTACHMENT_DESC = attachment.Description;
                    dbAttach.CREATE_DATE     = DateTime.Now;
                    dbAttach.EXPIRY_DATE     = attachment.ExpiryDate;

                    // New file
                    var tempFile = attachment.TempPath;
                    _commonDataAccess = new CommonDataAccess(_context);
                    int    nextSeq     = _commonDataAccess.GetNextAttachmentSeq();
                    string fileNameUrl = ApplicationHelpers.GenerateFileName(newsEnity.DocumentFolder, attachment.FileExtension, nextSeq);

                    var targetFile = string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", newsEnity.DocumentFolder, fileNameUrl);

                    if (StreamDataHelpers.TryToCopy(tempFile, targetFile))
                    {
                        // Save new file
                        dbAttach.FILE_NAME    = attachment.Filename;
                        dbAttach.URL          = fileNameUrl;
                        dbAttach.CONTENT_TYPE = attachment.ContentType;
                        dbAttach.FILE_SIZE    = attachment.FileSize;

                        _context.TB_T_NEWS_ATTACHMENT.Add(dbAttach);
                    }
                }

                if (!isIDNull && attachment.IsDelete == true)
                {
                    // Delete AttachmentType
                    var listType = _context.TB_T_ATTACHMENT_TYPE.Where(x => x.NEWS_ATTACHMENT_ID == attachment.AttachmentId);
                    _context.TB_T_ATTACHMENT_TYPE.RemoveRange(listType);

                    // Delete News Attachment
                    dbAttach = _context.TB_T_NEWS_ATTACHMENT.FirstOrDefault(x => x.NEWS_ATTACHMENT_ID == attachment.AttachmentId);
                    var prevFile = dbAttach.URL; // for delete file
                    _context.TB_T_NEWS_ATTACHMENT.Remove(dbAttach);

                    StreamDataHelpers.TryToDelete(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", newsEnity.DocumentFolder, prevFile));
                }

                if (!isIDNull && attachment.IsDelete == false)
                {
                    // Get previous path file
                    dbAttach = _context.TB_T_NEWS_ATTACHMENT.FirstOrDefault(x => x.NEWS_ATTACHMENT_ID == attachment.AttachmentId);
                    dbAttach.ATTACHMENT_NAME = attachment.Name;
                    dbAttach.ATTACHMENT_DESC = attachment.Description;
                    //dbAttach.CREATE_DATE = DateTime.Now;
                    dbAttach.EXPIRY_DATE = attachment.ExpiryDate;

                    if (!string.IsNullOrWhiteSpace(attachment.TempPath))
                    {
                        var prevFile = dbAttach.URL;

                        // New file
                        var tempFile = attachment.TempPath;
                        _commonDataAccess = new CommonDataAccess(_context);
                        int    nextSeq     = _commonDataAccess.GetNextAttachmentSeq();
                        string fileNameUrl = ApplicationHelpers.GenerateFileName(newsEnity.DocumentFolder, attachment.FileExtension, nextSeq);

                        var targetFile = string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", newsEnity.DocumentFolder, fileNameUrl);

                        if (StreamDataHelpers.TryToCopy(tempFile, targetFile))
                        {
                            if (StreamDataHelpers.TryToDelete(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", newsEnity.DocumentFolder, prevFile)))
                            {
                                // Save new file
                                dbAttach.FILE_NAME    = attachment.Filename;
                                dbAttach.URL          = fileNameUrl;
                                dbAttach.CONTENT_TYPE = attachment.ContentType;
                                dbAttach.FILE_SIZE    = attachment.FileSize;
                            }
                        }
                    }

                    SetEntryStateModified(dbAttach);
                }

                this.Save();

                if (attachment.IsDelete == false && attachment.AttachTypeList != null && attachment.AttachTypeList.Count > 0)
                {
                    this.SaveAttachTypes(dbAttach.NEWS_ATTACHMENT_ID, attachment.AttachTypeList);
                }
            }
        }