Exemple #1
0
        private static bool ExportBdwContactHeader(string filePath, string fileName, BdwContactEntity header, int totalRecord)
        {
            try
            {
                string targetFile = string.Format(CultureInfo.InvariantCulture, "{0}\\{1}", filePath, fileName);
                using (var sw = new StreamWriter(targetFile, false, Encoding.UTF8))
                {
                    // Header
                    sw.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}", header.DataType, header.DataDate, totalRecord.ToString(CultureInfo.InvariantCulture)));
                    //sw.Close();
                }
                return(true);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
            }

            return(false);
        }
Exemple #2
0
        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
            }
        }