/// <method> /// Add file path information /// </method> public bool AddFilePath(PathModal path) { string query = string.Format("INSERT INTO tblPath(Path,CreatedDate) VALUES (@Path , @CreatedDate)"); SqlParameter[] sqlParameters = new SqlParameter[2]; sqlParameters[0] = new SqlParameter("@Path", SqlDbType.VarChar); sqlParameters[0].Value = path.PathName; sqlParameters[1] = new SqlParameter("@CreatedDate", SqlDbType.DateTime); sqlParameters[1].Value = path.CreatedDate; return(con.executeInsertQuery(query, sqlParameters)); }
/// <method> /// Get file path /// </method> public PathModal GetFilePath(string path) { DataTable pathDetail = _fileProcessDL.GetFilePath(path); PathModal currentPathInfo = (from DataRow row in pathDetail.Rows select new PathModal { PathID = Convert.ToInt32(row["PathID"]), PathName = row["Path"].ToString(), CreatedDate = Convert.ToDateTime(row["CreatedDate"]) }).FirstOrDefault(); return(currentPathInfo); }
/// <method> /// Read file information /// </method> public void ReadFileInformation(string path, int pathId = 0, bool isPathExist = false) { var xmlFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".xml")); var imageFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".jpeg") || s.EndsWith(".gif") || s.EndsWith(".pdf")); var zipFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".zip")); /* Insert input path into table */ PathModal addPath = new PathModal() { PathName = path.Trim(), CreatedDate = DateTime.Now }; if (!isPathExist) { bool pathResult = _fileProcessDL.AddFilePath(addPath); pathId = GetFilePath(path).PathID; } List <FileInformationModal> fileInfoList = new List <FileInformationModal>(); List <FileInformationModal> finalList = new List <FileInformationModal>(); /* Get XMl files information */ foreach (string xmlFile in xmlFiles) { string dpsiValue = GetDPSIValue(xmlFile); FileInformationModal addFileInfo = new FileInformationModal() { DPSI = dpsiValue, PathID = pathId, XMLFileCount = 1 // Handling with single file }; fileInfoList.Add(addFileInfo); } /*Get other files information like image, pdf and etc */ foreach (string imgFile in imageFiles) { string dpsiValue = GetDPSIValue(imgFile); FileInformationModal addFileInfo = new FileInformationModal() { DPSI = dpsiValue, PathID = pathId, ImageCount = 1 // Handling with single file }; fileInfoList.Add(addFileInfo); } /*Get zip files information */ foreach (string zipFile in zipFiles) { string dpsiValue = GetDPSIValue(zipFile); int xmlCount = 0; int otherCount = 0; using (ZipArchive archive = ZipFile.OpenRead(zipFile)) { foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.FullName.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)) { xmlCount++; } else { otherCount++; } } FileInformationModal addFileInfo = new FileInformationModal() { DPSI = dpsiValue, PathID = pathId, XMLFileCount = xmlCount, ImageCount = otherCount }; fileInfoList.Add(addFileInfo); } } finalList = (from rec in fileInfoList group rec by rec.DPSI into g select new FileInformationModal { PathID = g.Select(x => x.PathID).First(), DPSI = g.Key, XMLFileCount = g.Sum(x => x.XMLFileCount), ImageCount = g.Sum(x => x.ImageCount) }).ToList(); List <FileInformationModal> existFileInfoList = GetFileInformation(); /* Insert or update fileinfo based on availability in database */ foreach (FileInformationModal info in finalList) { var existFileInfo = existFileInfoList.Where(x => x.DPSI == info.DPSI && x.PathID == pathId).FirstOrDefault(); if (existFileInfo == null) { info.CreatedDate = DateTime.Now; info.UpdatedDate = DateTime.Now; var fileInfoResult = _fileProcessDL.AddFileInformation(info); } else { info.XMLFileCount += existFileInfo.XMLFileCount; info.ImageCount += existFileInfo.ImageCount; info.UpdatedDate = DateTime.Now; var fileInfoResult = _fileProcessDL.UpdateFileInformation(info); } } }
static void Main(string[] args) { try { FileProcess process = new FileProcess(); FileProcessBL processBL = new FileProcessBL(); PathModal getFilePath = new PathModal(); bool hasPathEntered = false; while (!hasPathEntered) { Console.WriteLine("Input File Path :"); string path = Console.ReadLine(); hasPathEntered = true; getFilePath = processBL.GetFilePath(path); if (getFilePath == null) { processBL.ReadFileInformation(path); getFilePath = processBL.GetFilePath(path); } else { Console.WriteLine("Given path is already exist. Do you want to \n1.Added \n2.Re-Created \n3.Re-Enter path \n4.Exit"); Console.WriteLine("Please type number :"); bool isValidInput = true; while (isValidInput) { isValidInput = false; string responseNo = Console.ReadLine(); switch (responseNo) { case "1": processBL.ReadFileInformation(getFilePath.PathName, getFilePath.PathID, true); break; case "2": processBL.DeleteFileInformationByPath(getFilePath.PathID); processBL.ReadFileInformation(getFilePath.PathName, getFilePath.PathID, true); break; case "3": hasPathEntered = false; break; case "4": return; default: Console.WriteLine("Please choose valid number."); isValidInput = true; break; } } } } List <FileInformationModal> fileInfo = processBL.GetFileInformationbyPath(getFilePath.PathID); process.DisplayFileInformation(fileInfo); Console.WriteLine("Generating report ..."); process.GenerateReport(fileInfo); Console.WriteLine("Process Completed."); Console.ReadLine(); } catch (Exception exception) { Console.WriteLine("The Error Message : " + exception.Message); } }