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 } }
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 { } }
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 } }
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 } }