예제 #1
0
        private CommunicationPoolEntity GetMailContent(Mime message, int messageNumber)
        {
            CommunicationPoolEntity mail = null;

            if (message != null)
            {
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Mail Content").Add("Subject", message.MainEntity.Subject).Add("From", message.MainEntity.From)
                            .Add("MessageId", message.MainEntity.MessageID).Add("MessageNumber", messageNumber).Add("SendDateTime", message.MainEntity.Date).ToInputLogString());

                mail = new CommunicationPoolEntity();
                mail.SenderAddress = message.MainEntity.From.Mailboxes[0] != null ? message.MainEntity.From.Mailboxes[0].EmailAddress.NullSafeTrim() : "-";
                mail.Subject       = message.MainEntity.Subject.RemoveExtraSpaces();
                mail.Content       = ApplicationHelpers.StripHtmlTags(GetMailMessage(message));
                mail.PlainText     = ApplicationHelpers.RemoveHtmlTags(FindPlainTextInMessage(message));
                mail.MessageNumber = messageNumber;
                mail.SendDateTime  = message.MainEntity.Date;

                MimeEntity[] list = message.Attachments;
                foreach (MimeEntity part in list)
                {
                    byte[] bytes;
                    using (var memory = new MemoryStream())
                    {
                        part.DataToStream(memory);
                        bytes = StreamDataHelpers.ReadDataToBytes(memory);
                    }

                    IList <object> result     = StringHelpers.ConvertStringToList(part.ContentTypeString, ';');
                    var            attachment = new AttachmentEntity();
                    attachment.ContentType = (string)result[0];
                    attachment.ByteArray   = bytes;

                    if (!string.IsNullOrWhiteSpace(part.ContentDisposition_FileName))
                    {
                        attachment.Filename = part.ContentDisposition_FileName;
                    }
                    else
                    {
                        attachment.Filename = part.ContentType_Name;
                    }

                    mail.Attachments.Add(attachment);
                }

                Logger.Info(_logMsg.Clear().SetPrefixMsg("Mail Body").ToSuccessLogString());
            }
            else
            {
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Mail Content").Add("Error Message", "Message is null").ToFailLogString());
            }

            return(mail);
        }
예제 #2
0
        public bool SaveConfigureUrl(ConfigureUrlEntity configUrlEntity)
        {
            var today = DateTime.Now;

            try
            {
                using (var transaction = _context.Database.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    _context.Configuration.AutoDetectChangesEnabled = false;

                    try
                    {
                        TB_M_CONFIG_URL dbConfigUrl = null;

                        if (configUrlEntity.ConfigureUrlId == 0)
                        {
                            dbConfigUrl = new TB_M_CONFIG_URL();
                            dbConfigUrl.CONFIG_URL_ID = configUrlEntity.ConfigureUrlId;
                            dbConfigUrl.CONFIG_NAME   = configUrlEntity.SystemName;
                            dbConfigUrl.CONFIG_URL    = configUrlEntity.Url;
                            dbConfigUrl.IMAGE         = configUrlEntity.ImageFile; //path
                            dbConfigUrl.STATUS        = configUrlEntity.Status;
                            dbConfigUrl.CREATE_USER   = configUrlEntity.CreateUser.UserId;
                            dbConfigUrl.UPDATE_USER   = configUrlEntity.UpdateUser.UserId;
                            dbConfigUrl.CREATE_DATE   = today;
                            dbConfigUrl.UPDATE_DATE   = today;
                            dbConfigUrl.MENU_ID       = configUrlEntity.Menu.MenuId;
                            dbConfigUrl.FONT_NAME     = configUrlEntity.FontName;

                            _context.TB_M_CONFIG_URL.Add(dbConfigUrl);
                            this.Save();

                            // Save Config Role
                            if (configUrlEntity.Roles != null && configUrlEntity.Roles.Count > 0)
                            {
                                this.SaveConfigurationRole(dbConfigUrl.CONFIG_URL_ID, configUrlEntity);
                            }
                        }
                        else
                        {
                            //Edit
                            dbConfigUrl =
                                _context.TB_M_CONFIG_URL.FirstOrDefault(
                                    x => x.CONFIG_URL_ID == configUrlEntity.ConfigureUrlId);
                            if (dbConfigUrl != null)
                            {
                                if (!string.IsNullOrWhiteSpace(configUrlEntity.ImageFile))
                                {
                                    var prevFile =
                                        StreamDataHelpers.GetApplicationPath(string.Format(CultureInfo.InvariantCulture, "{0}{1}",
                                                                                           Constants.ConfigUrlPath,
                                                                                           dbConfigUrl.IMAGE));

                                    if (StreamDataHelpers.TryToDelete(prevFile))
                                    {
                                        // Save new file
                                        dbConfigUrl.IMAGE = configUrlEntity.ImageFile;
                                    }
                                    else
                                    {
                                        var newFile =
                                            StreamDataHelpers.GetApplicationPath(string.Format(CultureInfo.InvariantCulture, "{0}{1}",
                                                                                               Constants.ConfigUrlPath, configUrlEntity.ImageFile));
                                        StreamDataHelpers.TryToDelete(newFile);
                                        throw new CustomException(string.Format(CultureInfo.InvariantCulture, "Could not delete existing file : {0}",
                                                                                prevFile));
                                    }
                                }

                                dbConfigUrl.CONFIG_NAME = configUrlEntity.SystemName;
                                dbConfigUrl.CONFIG_URL  = configUrlEntity.Url;
                                dbConfigUrl.STATUS      = configUrlEntity.Status;
                                dbConfigUrl.UPDATE_USER = configUrlEntity.UpdateUser.UserId;
                                dbConfigUrl.UPDATE_DATE = today;
                                dbConfigUrl.MENU_ID     = configUrlEntity.Menu.MenuId;
                                dbConfigUrl.FONT_NAME   = configUrlEntity.FontName;

                                SetEntryStateModified(dbConfigUrl);
                                this.Save();

                                if (configUrlEntity.Roles != null && configUrlEntity.Roles.Count > 0)
                                {
                                    this.SaveConfigurationRole(dbConfigUrl.CONFIG_URL_ID, configUrlEntity);
                                }
                            }
                            else
                            {
                                Logger.ErrorFormat("CONFIG_URL_ID: {0} does not exist",
                                                   configUrlEntity.ConfigureUrlId);
                            }
                        }

                        transaction.Commit();
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        Logger.Error("Exception occur:\n", ex);
                    }
                    finally
                    {
                        _context.Configuration.AutoDetectChangesEnabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
            }

            return(false);
        }
예제 #3
0
파일: HpFacade.cs 프로젝트: KKPBank/CSM
        public List <HpActivityEntity> ReadFileHpActivity(string filePath, string fiPrefix, ref int numOfActivity, ref string fiActivity, ref bool isValidFile, ref string msgValidateFileError)
        {
            try
            {
                _hpDataAccess = new HpDataAccess(_context);
                IEnumerable <string> files = Directory.EnumerateFiles(filePath, string.Format(CultureInfo.InvariantCulture, "{0}*.txt", fiPrefix)); // lazy file system lookup

                if (files.Any())
                {
                    fiActivity = Path.GetFileName(files.First());
                    IEnumerable <string[]> results = StreamDataHelpers.ReadCsv(files.First());

                    #region "Validate file format"

                    bool isValidFormat = false;

                    int           inx = 1;
                    List <string> lstLengthNotMatch = new List <string>();
                    foreach (var source in results)
                    {
                        if (source.Length != Constants.ImportHp.LengthOfDetail)
                        {
                            lstLengthNotMatch.Add(string.Format(CultureInfo.InvariantCulture, "{0}", inx.ToString(CultureInfo.InvariantCulture)));
                        }

                        inx++;
                    }

                    if (lstLengthNotMatch.Count > 0 && lstLengthNotMatch.Count <= 5000)
                    {
                        Logger.DebugFormat("File:{0} Invalid format @line[{1}]", fiActivity, string.Join(",", lstLengthNotMatch.ToArray()));
                    }
                    else if (lstLengthNotMatch.Count > 0)
                    {
                        Logger.DebugFormat("File:{0} Invalid format {1} records", fiActivity,
                                           lstLengthNotMatch.Count.ToString(CultureInfo.InvariantCulture));
                    }

                    isValidFormat = (lstLengthNotMatch.Count == 0);

                    if (isValidFormat == false)
                    {
                        msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File name : {0}  is invalid file format.", fiActivity);
                    }


                    if (isValidFormat == false)
                    {
                        // Move File
                        string pathError = this.GetParameter(Constants.ParameterName.HPPathError);
                        StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiActivity), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", pathError, fiActivity));

                        isValidFile = false; // ref value
                        goto Outer;
                    }

                    #endregion

                    var rx = new Regex(@".*SR;.*;\s*Status\s*:.*;\s*Contact\s*Mobile\s*#", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    List <HpActivityEntity> hpActivity = (from source in results
                                                          where !rx.IsMatch(source[5].NullSafeTrim())
                                                          select new HpActivityEntity
                    {
                        Channel = source[0].NullSafeTrim(),
                        Type = source[1].NullSafeTrim(),
                        Area = source[2].NullSafeTrim(),
                        Status = source[3].NullSafeTrim(),
                        Description = source[4].NullSafeTrim(),
                        Comment = source[5].NullSafeTrim(),
                        AssetInfo = source[6].NullSafeTrim(),
                        ContactInfo = source[7].NullSafeTrim(),
                        Ano = source[8].NullSafeTrim(),
                        CallId = source[9].NullSafeTrim(),
                        ContactName = source[10].NullSafeTrim(),
                        ContactLastName = source[11].NullSafeTrim(),
                        ContactPhone = source[12].NullSafeTrim(),
                        DoneFlg = source[13].NullSafeTrim(),
                        CreateDate = source[14].NullSafeTrim(),
                        CreateBy = source[15].NullSafeTrim(),
                        StartDate = source[16].NullSafeTrim(),
                        EndDate = source[17].NullSafeTrim(),
                        OwnerLogin = source[18].NullSafeTrim(),
                        OwnerPerId = source[19].NullSafeTrim(),
                        UpdateDate = source[20].NullSafeTrim(),
                        UpdateBy = source[21].NullSafeTrim(),
                        SrNo = source[22].NullSafeTrim(),
                        CallFlg = source[23].NullSafeTrim(),
                        EnqFlg = source[24].NullSafeTrim(),
                        LocEnqFlg = source[25].NullSafeTrim(),
                        DocReqFlg = source[26].NullSafeTrim(),
                        PriIssuedFlg = source[27].NullSafeTrim(),
                        AssetInspectFlg = source[28].NullSafeTrim(),
                        PlanstartDate = source[29].NullSafeTrim(),
                        ContactFax = source[30].NullSafeTrim(),
                        ContactEmail = source[31].NullSafeTrim()
                    }).ToList();

                    #region "Validate MaxLength"

                    ValidationContext vc = null;
                    int inxErr           = 1;
                    var lstErrMaxLength  = new List <string>();
                    foreach (HpActivityEntity hp in hpActivity)
                    {
                        vc = new ValidationContext(hp, null, null);
                        var  validationResults = new List <ValidationResult>();
                        bool valid             = Validator.TryValidateObject(hp, vc, validationResults, true);
                        if (!valid)
                        {
                            hp.Error =
                                validationResults.Select(x => x.ErrorMessage)
                                .Aggregate((i, j) => i + Environment.NewLine + j);

                            lstErrMaxLength.Add(string.Format(CultureInfo.InvariantCulture, "{0}", inxErr.ToString(CultureInfo.InvariantCulture)));
                        }

                        inxErr++;
                    }

                    if (lstErrMaxLength.Count > 0)
                    {
                        Logger.DebugFormat("File:{0} Invalid MaxLength @line[{1}]", fiActivity, string.Join(",", lstErrMaxLength.ToArray()));
                    }

                    if (hpActivity.Count(x => !string.IsNullOrWhiteSpace(x.Error)) > 0)
                    {
                        // Move File
                        string pathError = this.GetParameter(Constants.ParameterName.HPPathError);
                        StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiActivity), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", pathError, fiActivity));

                        //Logger.DebugFormat("File:{0} Invalid MaxLength", fiActivity);
                        isValidFile          = false; // ref value
                        msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File name : {0}  is invalid maxLength.", fiActivity);
                        goto Outer;
                    }

                    #endregion

                    numOfActivity = hpActivity.Count;
                    return(hpActivity);
                }
                else
                {
                    isValidFile          = false;
                    msgValidateFileError = "File not found.";
                }

Outer:

                return(null);
            }
            catch (IOException ex)
            {
                Logger.Error("Exception occur:\n", ex);
                throw new CustomException("{0}: {1}", fiPrefix, ex.Message);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
                throw;
            }
            finally
            {
                #region "Move file to PathSource"

                if (!string.IsNullOrEmpty(fiActivity))
                {
                    string hpPathSource = this.GetParameter(Constants.ParameterName.HpPathSource);
                    StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiActivity), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", hpPathSource, fiActivity));
                    StreamDataHelpers.TryToDelete(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiActivity));
                }

                #endregion
            }
        }
예제 #4
0
파일: AFSFacade.cs 프로젝트: KKPBank/CSM
        public List <SaleZoneEntity> ReadFileSaleZone(string filePath, string fiPrefix, ref int numOfSaleZones, ref string fiSaleZone, ref bool isValidFile, ref string msgValidateFileError)
        {
            try
            {
                _afsDataAccess = new AFSDataAccess(_context);
                IEnumerable <string> files = Directory.EnumerateFiles(filePath, string.Format(CultureInfo.InvariantCulture, "{0}*.txt", fiPrefix)); // lazy file system lookup

                if (files.Any())
                {
                    fiSaleZone = Path.GetFileName(files.First());
                    IEnumerable <string[]> results = StreamDataHelpers.ReadPipe(files.First());

                    #region "Validate file format"

                    bool isValidFormat = false;

                    int           inx = 1;
                    List <string> lstLengthNotMatch = new List <string>();
                    foreach (var source in results.Skip(1))
                    {
                        if (source.Length != Constants.ImportAfs.LengthOfSaleZone)
                        {
                            lstLengthNotMatch.Add(string.Format(CultureInfo.InvariantCulture, "{0}", inx.ToString(CultureInfo.InvariantCulture)));
                        }

                        inx++;
                    }

                    if (lstLengthNotMatch.Count > 0 && lstLengthNotMatch.Count <= 5000)
                    {
                        Logger.DebugFormat("File:{0} Invalid format @line[{1}]", fiSaleZone, string.Join(",", lstLengthNotMatch.ToArray()));
                    }

                    isValidFormat = (lstLengthNotMatch.Count == 0);

                    if (isValidFormat == false)
                    {
                        msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File name : {0}  is invalid file format.", fiSaleZone);
                    }


                    if (isValidFormat == false)
                    {
                        // Move File
                        string pathError = this.GetParameter(Constants.ParameterName.AFSPathError);
                        StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiSaleZone), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", pathError, fiSaleZone));

                        isValidFile = false; // ref value
                        goto Outer;
                    }

                    #endregion


                    List <SaleZoneEntity> saleZones = (from source in results
                                                       select new SaleZoneEntity
                    {
                        District = source[0].ToString(),
                        Province = source[1].ToString(),
                        SaleName = source[2].ToString(),
                        PhoneNo = source[3].ToString(),
                        EmployeeNo = source[4].ToString(),
                        MobileNo = source[5].ToString(),
                        Email = source[6].ToString(),
                        EmployeeId = _afsDataAccess.GetUserIdByEmployeeCode(source[4].ToString())
                    }).ToList();

                    #region "Validate MaxLength"

                    ValidationContext vc          = null;
                    int           inxErr          = 2;
                    List <string> lstErrMaxLength = new List <string>();
                    foreach (SaleZoneEntity obj in saleZones)
                    {
                        vc = new ValidationContext(obj, null, null);
                        var  validationResults = new List <ValidationResult>();
                        bool valid             = Validator.TryValidateObject(obj, vc, validationResults, true);
                        if (!valid)
                        {
                            obj.Error =
                                validationResults.Select(x => x.ErrorMessage)
                                .Aggregate((i, j) => i + Environment.NewLine + j);

                            lstErrMaxLength.Add(string.Format(CultureInfo.InvariantCulture, "{0}", inxErr.ToString(CultureInfo.InvariantCulture)));
                        }

                        inxErr++;
                    }

                    if (lstErrMaxLength.Count > 0)
                    {
                        Logger.DebugFormat("File:{0} Invalid MaxLength @line[{1}]", fiSaleZone, string.Join(",", lstErrMaxLength.ToArray()));
                    }

                    if (saleZones.Count(x => !string.IsNullOrWhiteSpace(x.Error)) > 0)
                    {
                        // Move File
                        string pathError = this.GetParameter(Constants.ParameterName.AFSPathError);
                        StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiSaleZone), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", pathError, fiSaleZone));

                        Logger.DebugFormat("File:{0} Invalid MaxLength", fiSaleZone);
                        isValidFile          = false; // ref value
                        msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File name : {0}  is invalid file format.", fiSaleZone);
                        goto Outer;
                    }

                    #endregion

                    numOfSaleZones = saleZones.Count;
                    return(saleZones);
                }

                fiSaleZone = string.Empty;
                return(_afsDataAccess.GetSaleZones());

Outer:
                return(null);
            }
            finally
            {
                #region "Move file to PathSource"

                if (!string.IsNullOrEmpty(fiSaleZone))
                {
                    string afsPathSource = this.GetParameter(Constants.ParameterName.AfsPathSource);
                    StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiSaleZone), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", afsPathSource, fiSaleZone));
                    StreamDataHelpers.TryToDelete(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiSaleZone));
                }

                #endregion
            }
        }
예제 #5
0
파일: AFSFacade.cs 프로젝트: KKPBank/CSM
        public List <PropertyEntity> ReadFileProperty(string filePath, string fiPrefix, ref int numOfProp, ref string fiProp, ref bool isValidFile, ref string msgValidateFileError, bool isValidFileSaleZone)
        {
            try
            {
                IEnumerable <string> files = Directory.EnumerateFiles(filePath, string.Format(CultureInfo.InvariantCulture, "{0}*.txt", fiPrefix)); // lazy file system lookup

                if (files.Any())
                {
                    fiProp = Path.GetFileName(files.First());

                    IEnumerable <string[]> results = StreamDataHelpers.ReadPipe(files.First());

                    #region "Validate file format"

                    bool isValidFormat = false;

                    int           inx = 1;
                    List <string> lstLengthNotMatch = new List <string>();
                    foreach (var source in results.Skip(1))
                    {
                        if (source.Length != Constants.ImportAfs.LengthOfProperty)
                        {
                            lstLengthNotMatch.Add(string.Format(CultureInfo.InvariantCulture, "{0}", inx.ToString(CultureInfo.InvariantCulture)));
                        }

                        inx++;
                    }

                    if (lstLengthNotMatch.Count > 0 && lstLengthNotMatch.Count <= 5000)
                    {
                        Logger.DebugFormat("File:{0} Invalid format @line[{1}]", fiProp, string.Join(",", lstLengthNotMatch.ToArray()));
                    }

                    isValidFormat = (lstLengthNotMatch.Count == 0);

                    if (isValidFormat == false)
                    {
                        msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File name : {0}  is invalid file format.", fiProp);
                    }


                    if (isValidFormat == false)
                    {
                        // Move File
                        string pathError = this.GetParameter(Constants.ParameterName.AFSPathError);
                        StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiProp), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", pathError, fiProp));

                        isValidFile = false; // ref value
                        goto Outer;
                    }

                    #endregion

                    List <PropertyEntity> properties = (from source in results.Skip(1)
                                                        select new PropertyEntity
                    {
                        RowId = Convert.ToInt32(source[0], CultureInfo.InvariantCulture),
                        IfRowStat = source[1].ToString(),
                        IfRowBatchNum = source[2].ToString(),
                        AssetNum = source[3].ToString(),
                        AssetType = source[4].ToString(),
                        AssetTradeInType = source[5].ToString(),
                        AssetStatus = source[6].ToString(),
                        AssetDesc = source[7].ToString(),
                        AssetName = source[8].ToString(),
                        AssetComments = source[9].ToString(),
                        AssetRefNo1 = source[10].ToString(),
                        AssetLot = source[11].ToString(),
                        AssetPurch = source[12].ToString()
                    }).ToList();


                    #region "Validate MaxLength"

                    ValidationContext vc          = null;
                    int           inxErr          = 2;
                    List <string> lstErrMaxLength = new List <string>();
                    foreach (PropertyEntity obj in properties)
                    {
                        vc = new ValidationContext(obj, null, null);
                        var  validationResults = new List <ValidationResult>();
                        bool valid             = Validator.TryValidateObject(obj, vc, validationResults, true);
                        if (!valid)
                        {
                            obj.Error =
                                validationResults.Select(x => x.ErrorMessage)
                                .Aggregate((i, j) => i + Environment.NewLine + j);

                            lstErrMaxLength.Add(string.Format(CultureInfo.InvariantCulture, "{0}({1})", inxErr.ToString(CultureInfo.InvariantCulture), obj.Error));
                        }

                        inxErr++;
                    }

                    if (lstErrMaxLength.Count > 0)
                    {
                        Logger.DebugFormat("File:{0} Invalid MaxLength @line[{1}]", fiProp, string.Join(",", lstErrMaxLength.ToArray()));
                    }

                    if (properties.Count(x => !string.IsNullOrWhiteSpace(x.Error)) > 0)
                    {
                        // Move File
                        string pathError = this.GetParameter(Constants.ParameterName.AFSPathError);
                        StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiProp), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", pathError, fiProp));

                        Logger.DebugFormat("File:{0} Invalid MaxLength", fiProp);
                        isValidFile          = false; // ref value
                        msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File name : {0}  is invalid file format.", fiProp);
                        goto Outer;
                    }

                    #endregion

                    if (isValidFileSaleZone == false && isValidFile == true)
                    {
                        #region "Move File to pathError"

                        // Move File
                        string pathError = this.GetParameter(Constants.ParameterName.AFSPathError);
                        StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiProp), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", pathError, fiProp));
                        goto Outer;

                        #endregion
                    }

                    numOfProp = properties.Count;
                    return(properties);
                }
                else
                {
                    isValidFile          = false;
                    msgValidateFileError = "File not found.";
                }

Outer:
                return(null);
            }
            finally
            {
                #region "Move file to PathSource"

                if (!string.IsNullOrEmpty(fiProp))
                {
                    string afsPathSource = this.GetParameter(Constants.ParameterName.AfsPathSource);
                    StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiProp), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", afsPathSource, fiProp));
                    StreamDataHelpers.TryToDelete(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiProp));
                }

                #endregion
            }
        }
예제 #6
0
        public bool ReadHRFile(out string readFile, out string msgValidateFileError, out List <HRIEmployeeEntity> hriEmpls)
        {
            readFile             = string.Empty;
            msgValidateFileError = string.Empty;
            hriEmpls             = null;
            try
            {
                IEnumerable <string> files = Directory.EnumerateFiles(localPath, $"{filePrefix}*.txt"); // lazy file system lookup
                if (files.Any())
                {
                    FileInfo fi = new FileInfo(files.First());

                    readFile = fi.Name;

                    IEnumerable <string[]> results = StreamDataHelpers.ReadPipe(fi.FullName);

                    #region "Validate file format"

                    bool isValidFormat = false;

                    // Validate Header
                    string[] header = results.FirstOrDefault();
                    if (header.Length != Constants.ImportHRI.ColumnCount)
                    {
                        msgValidateFileError = $"File {fi.Name} : columns length of header mismatch.";
                        Logger.Debug(msgValidateFileError);
                    }
                    else
                    {
                        isValidFormat = true;
                        int lineNo = 1;
                        foreach (var line in results.Skip(1))
                        {
                            lineNo++;
                            if (line.Length != Constants.ImportHRI.ColumnCount)
                            {
                                isValidFormat        = false;
                                msgValidateFileError = $"File {fi.Name} : columns length of data row {lineNo} mismatch.";
                                Logger.Debug(msgValidateFileError);
                            }
                        }
                    }

                    if (isValidFormat == false)
                    {
                        // Move File to Error
                        if (StreamDataHelpers.TryToCopy(fi.FullName, string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", errorPath, fi.Name)))
                        {
                            StreamDataHelpers.TryToDelete(fi.FullName);
                        }
                        goto Outer;
                    }

                    #endregion

                    var empls = (from src in results.Skip(1)
                                 select new HRIEmployeeEntity
                    {
                        Branch = src[0].NullSafeTrim(),
                        BranchDesc = src[1].NullSafeTrim(),
                        EmployeeId = src[2].NullSafeTrim(),
                        TitleId = src[3].NullSafeTrim(),
                        Title = src[4].NullSafeTrim(),
                        FName = src[5].NullSafeTrim(),
                        LName = src[6].NullSafeTrim(),
                        Nickname = src[7].NullSafeTrim(),
                        FullNameEng = src[8].NullSafeTrim(),
                        ETitle = src[9].NullSafeTrim(),
                        EFName = src[10].NullSafeTrim(),
                        ELName = src[11].NullSafeTrim(),
                        Sex = src[12].NullSafeTrim(),
                        BirthDay = src[13].NullSafeTrim(),
                        EmpType = src[14].NullSafeTrim(),
                        EmpTypeDesc = src[15].NullSafeTrim(),
                        Position = src[16].NullSafeTrim(),
                        PositionDesc = src[17].NullSafeTrim(),
                        BU1 = src[18].NullSafeTrim(),
                        BU1Desc = src[19].NullSafeTrim(),
                        BU2 = src[20].NullSafeTrim(),
                        BU2Desc = src[21].NullSafeTrim(),
                        BU3 = src[22].NullSafeTrim(),
                        BU3Desc = src[23].NullSafeTrim(),
                        BU4 = src[24].NullSafeTrim(),
                        BU4Desc = src[25].NullSafeTrim(),
                        Job = src[26].NullSafeTrim(),
                        JobPosition = src[27].NullSafeTrim(),
                        StartDate = src[28].NullSafeTrim(),
                        FirstHireDate = src[29].NullSafeTrim(),
                        ResignDate = src[30].NullSafeTrim(),
                        Status = src[31].NullSafeTrim(),
                        EmpStatus = src[32].NullSafeTrim(),
                        Email = src[33].NullSafeTrim(),
                        NotesAddress = src[34].NullSafeTrim(),
                        WorkArea = src[35].NullSafeTrim(),
                        WorkAreaDesc = src[36].NullSafeTrim(),
                        CostCenter = src[37].NullSafeTrim(),
                        CostCenterDesc = src[38].NullSafeTrim(),
                        TelExt = src[39].NullSafeTrim(),
                        Boss = src[40].NullSafeTrim(),
                        BossName = src[41].NullSafeTrim(),
                        Assessor1 = src[42].NullSafeTrim(),
                        Assessor1Name = src[43].NullSafeTrim(),
                        Assessor2 = src[44].NullSafeTrim(),
                        Assessor2Name = src[45].NullSafeTrim(),
                        Assessor3 = src[46].NullSafeTrim(),
                        Assessor3Name = src[47].NullSafeTrim(),
                        TelNo = src[48].NullSafeTrim(),
                        MobileNo = src[49].NullSafeTrim(),
                        ADUser = src[50].NullSafeTrim(),
                        OfficerId = src[51].NullSafeTrim(),
                        OfficerDesc = src[52].NullSafeTrim(),
                        AdditionJob = src[53].NullSafeTrim(),
                        UnitBoss = src[54].NullSafeTrim(),
                        UnitBossName = src[55].NullSafeTrim(),
                        IDNO = src[56].NullSafeTrim()
                    });

                    #region "Validate MaxLength"

                    ValidationContext vc = null;
                    int inxErr           = 2;
                    var lstErrMaxLength  = new List <string>();
                    foreach (HRIEmployeeEntity empl in empls)
                    {
                        vc = new ValidationContext(empl, null, null);
                        var  validationResults = new List <ValidationResult>();
                        bool valid             = Validator.TryValidateObject(empl, vc, validationResults, true);
                        if (!valid)
                        {
                            empl.Error = validationResults.Select(x => x.ErrorMessage)
                                         .Aggregate((result, item) => result + ", " + item);
                            lstErrMaxLength.Add($"@Line {inxErr}: {empl.Error}");
                        }
                        inxErr++;
                    }
                    if (lstErrMaxLength.Count > 0)
                    {
                        Logger.Debug($"File:{fi.Name} Invalid MaxLength \n{string.Join("\n", lstErrMaxLength.ToArray())}");
                    }
                    if (empls.Count(x => !string.IsNullOrWhiteSpace(x.Error)) > 0)
                    {
                        // Move File to Error
                        if (StreamDataHelpers.TryToCopy(fi.FullName, string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", errorPath, fi.Name)))
                        {
                            StreamDataHelpers.TryToDelete(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", localPath, fi.Name));
                        }
                        msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File name : {0}  is invalid maxLength.", fi.Name);
                        goto Outer;
                    }
                    #endregion

                    hriEmpls = empls.ToList();

                    #region "Move file to PathSource"
                    if (fi.Exists)
                    {
                        StreamDataHelpers.TryToCopy(fi.FullName, string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", sourcePath, fi.Name));
                        StreamDataHelpers.TryToDelete(fi.FullName);
                    }
                    #endregion

                    return(true);
                }
                else
                {
                    msgValidateFileError = " File not found.";
                }
Outer:
                return(false);
            }
            catch (IOException ex)
            {
                Logger.Error("Exception occur:\n", ex);
                throw new CustomException("file {0}: {1}", readFile, ex.Message);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
                throw;
            }
            finally
            { }
        }
예제 #7
0
        public bool DownloadFiles()
        {
            try
            {
                // Delete exist files
                IEnumerable <string> localFiles = Directory.EnumerateFiles(localPath, $"{filePrefix}*.txt"); // lazy file system lookup

                foreach (var localFile in localFiles)
                {
                    if (StreamDataHelpers.TryToDelete(localFile))
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Delete exist local file").Add("FileName", localFile).ToSuccessLogString());
                    }
                    else
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Delete exist local file").Add("FileName", localFile).ToFailLogString());
                    }
                }

                Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").Add("LocalPath", localPath).ToInputLogString());

                bool isFileFound;

                using (var sftp = new SftpClient(host, port, username, password))
                {
                    sftp.Connect();
                    sftp.ChangeDirectory(remoteDirectory);

                    var files = sftp.ListDirectory(remoteDirectory)
                                .Where(x => x.Name.ToUpperInvariant().StartsWith(filePrefix.ToUpperInvariant()));

                    sftpFileList.Clear();
                    sftpFileList.AddRange(files);

                    isFileFound = sftpFileList.Count > 0;

                    if (isFileFound)
                    {
                        foreach (var file in sftpFileList)
                        {
                            Logger.Info(_logMsg.Clear().SetPrefixMsg("Download File").Add("FileName", file.FullName).ToInputLogString());
                            using (var fileStream = File.OpenWrite(Path.Combine(localPath, file.Name)))
                            {
                                sftp.DownloadFile(file.FullName, fileStream);
                            }
                            Logger.Info(_logMsg.Clear().SetPrefixMsg("Download File").ToSuccessLogString());
                        }
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").ToSuccessLogString());
                    }
                    else
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").Add("Error Message", "File Not Found").ToFailLogString());
                    }
                    sftp.Disconnect();
                }
                return(isFileFound);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").Add("Error Message", ex.Message).ToInputLogString());
            }

            return(false);
        }
예제 #8
0
파일: BdwFacade.cs 프로젝트: KKPBank/CSM
        public List <BdwContactEntity> ReadFileBdwContact(string filePath, string fiPrefix, ref int numOfBdw, ref string fiBdw, ref bool isValidFile, ref string msgValidateFileError)
        {
            bool hasFile = true;

            try
            {
                IEnumerable <string> files = Directory.EnumerateFiles(filePath, string.Format(CultureInfo.InvariantCulture, "{0}*.txt", fiPrefix)); // lazy file system lookup

                if (files.Any())
                {
                    fiBdw = Path.GetFileName(files.First());

                    string endFile = fiBdw.Replace(".txt", ".end").Replace(".TXT", ".end");

                    if (File.Exists(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, endFile)) == false)
                    {
                        hasFile              = false;
                        isValidFile          = false; // ref value
                        msgValidateFileError = " File not found.";
                        goto Outer;
                    }

                    IEnumerable <string[]> results = StreamDataHelpers.ReadPipe(files.First());

                    #region "Validate file format"

                    bool isValidFormat = false;

                    // Validate Header
                    string[] header = results.FirstOrDefault();
                    if (header.Length == Constants.ImportBDWContact.LengthOfHeader)
                    {
                        if (header[0] == Constants.ImportBDWContact.DataTypeHeader)
                        {
                            isValidFormat = true;
                        }
                        else
                        {
                            msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File:{0} dataType of header mismatch", fiBdw);
                            Logger.DebugFormat("File:{0} dataType of header mismatch", fiBdw);
                        }
                    }
                    else
                    {
                        msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File:{0} length of header mismatch", fiBdw);
                        Logger.DebugFormat("File:{0} length of header mismatch", fiBdw);
                    }

                    // Check TotalRecord
                    if (isValidFormat)
                    {
                        int?totalRecord = header[Constants.ImportBDWContact.LengthOfHeader - 1].ToNullable <int>();
                        int cntDetail   = results.Skip(1).Count();

                        if (totalRecord != cntDetail)
                        {
                            isValidFormat        = false;
                            msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File:{0} TotalRecord mismatch", fiBdw);
                            Logger.DebugFormat("File:{0} TotalRecord mismatch", fiBdw);
                        }
                    }

                    if (isValidFormat)
                    {
                        int inx = 2;
                        var lstLengthNotMatch = new List <string>();
                        foreach (var source in results.Skip(1))
                        {
                            if (source.Length != Constants.ImportBDWContact.LengthOfDetail || source[0].NullSafeTrim() != Constants.ImportBDWContact.DataTypeDetail)
                            {
                                lstLengthNotMatch.Add(string.Format(CultureInfo.InvariantCulture, "{0}", inx.ToString(CultureInfo.InvariantCulture)));
                            }

                            inx++;
                        }

                        if (lstLengthNotMatch.Count > 0 && lstLengthNotMatch.Count <= 5000)
                        {
                            Logger.DebugFormat("File:{0} Invalid format @line[{1}]", fiBdw, string.Join(",", lstLengthNotMatch.ToArray()));
                        }
                        else if (lstLengthNotMatch.Count > 0)
                        {
                            Logger.DebugFormat("File:{0} Invalid format {1} records", fiBdw,
                                               lstLengthNotMatch.Count.ToString(CultureInfo.InvariantCulture));
                        }

                        isValidFormat = (lstLengthNotMatch.Count == 0);

                        if (isValidFormat == false)
                        {
                            msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File name : {0}  is invalid file format.", fiBdw);
                        }
                    }

                    if (isValidFormat == false)
                    {
                        // Move File
                        string bdwExportPath = this.GetParameter(Constants.ParameterName.BDWPathError);
                        StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiBdw), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", bdwExportPath, fiBdw));

                        isValidFile = false; // ref value
                        goto Outer;
                    }

                    #endregion

                    BdwContactEntity headerContact = (from source in results
                                                      where source[0].NullSafeTrim() == Constants.ImportBDWContact.DataTypeHeader
                                                      select new BdwContactEntity
                    {
                        DataType = source[0].NullSafeTrim(),
                        DataDate = source[1].NullSafeTrim(),
                        DataSource = source[2].NullSafeTrim()
                    }).FirstOrDefault();

                    List <BdwContactEntity> deatilContacts = (from source in results.Skip(1)
                                                              select new BdwContactEntity
                    {
                        DataType = source[0].NullSafeTrim(),
                        DataDate = source[1].NullSafeTrim(),
                        DataSource = source[2].NullSafeTrim(),
                        CardTypeCode = source[3].NullSafeTrim(),
                        CardNo = source[4].NullSafeTrim(),
                        TitileTh = source[5].NullSafeTrim(),
                        NameTh = source[6].NullSafeTrim(),
                        SurnameTh = source[7].NullSafeTrim(),
                        TitileEn = source[8].NullSafeTrim(),
                        NameEn = source[9].NullSafeTrim(),
                        SurnameEn = source[10].NullSafeTrim(),
                        AccountNo = source[11].NullSafeTrim(),
                        LoanMain = source[12].NullSafeTrim(),
                        ProductGroup = source[13].NullSafeTrim(),
                        Product = source[14].NullSafeTrim(),
                        Campaign = source[15].NullSafeTrim(),
                        AccountStatus = source[16].NullSafeTrim(),
                        Relationship = source[17].NullSafeTrim(),
                        Phone = source[18].NullSafeTrim()
                    }).ToList();

                    #region "Validate MaxLength"

                    ValidationContext vc = null;
                    int inxErr           = 2;
                    var lstErrMaxLength  = new List <string>();
                    foreach (BdwContactEntity bdwContact in deatilContacts)
                    {
                        vc = new ValidationContext(bdwContact, null, null);
                        var  validationResults = new List <ValidationResult>();
                        bool valid             = Validator.TryValidateObject(bdwContact, vc, validationResults, true);
                        if (!valid)
                        {
                            bdwContact.Error =
                                validationResults.Select(x => x.ErrorMessage)
                                .Aggregate((i, j) => i + ", " + j);

                            lstErrMaxLength.Add(string.Format(CultureInfo.InvariantCulture, "@Line {0}: {1}", inxErr.ToString(CultureInfo.InvariantCulture), bdwContact.Error));
                        }

                        inxErr++;
                    }

                    if (lstErrMaxLength.Count > 0)
                    {
                        Logger.DebugFormat("File:{0} Invalid MaxLength \n{1}", fiBdw, string.Join("\n", lstErrMaxLength.ToArray()));
                    }

                    if (deatilContacts.Count(x => !string.IsNullOrWhiteSpace(x.Error)) > 0)
                    {
                        // Move File
                        string bdwExportPath = this.GetParameter(Constants.ParameterName.BDWPathError);
                        StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiBdw), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", bdwExportPath, fiBdw));

                        //Logger.DebugFormat("File:{0} Invalid MaxLength", fiBdw);
                        isValidFile          = false; // ref value
                        msgValidateFileError = string.Format(CultureInfo.InvariantCulture, " File name : {0}  is invalid maxLength.", fiBdw);
                        goto Outer;
                    }

                    #endregion

                    var bdwContacts = new List <BdwContactEntity>();
                    bdwContacts.Add(headerContact);
                    bdwContacts.AddRange(deatilContacts);

                    numOfBdw = deatilContacts.Count;
                    return(bdwContacts);
                }
                else
                {
                    isValidFile          = false; // ref value
                    msgValidateFileError = " File not found.";
                }

Outer:
                return(null);
            }
            catch (IOException ex)
            {
                Logger.Error("Exception occur:\n", ex);
                throw new CustomException("{0}: {1}", fiPrefix, ex.Message);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
                throw;
            }
            finally
            {
                #region "Move file to PathSource"

                if (!string.IsNullOrEmpty(fiBdw) && hasFile)
                {
                    string bdwExportSource = this.GetParameter(Constants.ParameterName.BdwPathSource);
                    StreamDataHelpers.TryToCopy(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiBdw), string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", bdwExportSource, fiBdw));
                    StreamDataHelpers.TryToDelete(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fiBdw));
                    string endFile = fiBdw.Replace(".txt", ".end").Replace(".TXT", ".end");
                    StreamDataHelpers.TryToDelete(string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, endFile));
                }

                #endregion
            }
        }
예제 #9
0
파일: BdwFacade.cs 프로젝트: KKPBank/CSM
        /// <summary>
        /// This will list the contents of the current directory.
        /// </summary>
        public bool DownloadFilesViaFTP(string localPath, string fiPrefix)
        {
            try
            {
                // Delete exist files
                IEnumerable <string> localFiles = Directory.EnumerateFiles(localPath, string.Format(CultureInfo.InvariantCulture, "{0}*.*", fiPrefix)); // lazy file system lookup

                foreach (var localFile in localFiles)
                {
                    if (StreamDataHelpers.TryToDelete(localFile))
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Delete exist local file").Add("FileName", localFile).ToSuccessLogString());
                    }
                    else
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Delete exist local file").Add("FileName", localFile).ToFailLogString());
                    }
                }

                Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").Add("LocalPath", localPath).ToInputLogString());

                bool   isFileFound;
                string host            = WebConfig.GetBDWSshServer();
                int    port            = WebConfig.GetBDWSshPort();
                string username        = WebConfig.GetBDWSshUsername();
                string password        = WebConfig.GetBDWSshPassword();
                string remoteDirectory = WebConfig.GetBDWSshRemoteDir(); // . always refers to the current directory.

                using (var sftp = new SftpClient(host, port, username, password))
                {
                    sftp.Connect();
                    var files = sftp.ListDirectory(remoteDirectory).Where(x => x.Name.ToUpperInvariant().Contains(fiPrefix.ToUpperInvariant()));
                    var txt   = files.Where(x => Path.GetExtension(x.Name).ToUpperInvariant() == ".TXT");
                    var end   = files.Where(x => Path.GetExtension(x.Name).ToUpperInvariant() == ".END");

                    _downloadList.Clear();

                    foreach (var x in txt)
                    {
                        var y = end.Where(e => Path.GetFileNameWithoutExtension(e.Name).ToUpperInvariant() == Path.GetFileNameWithoutExtension(x.Name).ToUpperInvariant()).FirstOrDefault();
                        if (y != null)
                        {
                            _downloadList.Add(x);
                            _downloadList.Add(y);
                        }
                    }

                    isFileFound = _downloadList.Count > 0;

                    if (isFileFound)
                    {
                        // Download file to local via SFTP
                        foreach (var file in _downloadList)
                        {
                            DownloadFile(sftp, file, localPath);
                        }

                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").ToSuccessLogString());
                    }
                    else
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").Add("Error Message", "File Not Found").ToFailLogString());
                    }

                    sftp.Disconnect();
                }
                return(isFileFound);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").Add("Error Message", ex.Message).ToInputLogString());
            }

            return(false);
        }
예제 #10
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);
                }
            }
        }