public HttpResponseMessage GetDataFromSmtp(string lstDataFileIds, string ConnectionString) { DataFileImporter objDataFileImporter = new DataFileImporter(); try { string connectionstring = Regex.Replace(ConnectionString.Trim(), @"\t|\n|\r", ""); using (ThomsonDbContext db = new ThomsonDbContext(ConnectionString)) { foreach (var item in lstDataFileIds.Split(',')) { int idItem = Convert.ToInt32(item); string pathName = db.DataFiles.Where(m => m.Id == idItem).FirstOrDefault().FileName; string fileName = Path.GetFileName(pathName); objDataFileImporter.SaveDataFile(pathName, fileName, ConnectionString); retMessage = "successfully Saved"; } } } catch (Exception ex) { retMessage = ex.InnerException.Message; throw ex; } return(new HttpResponseMessage() { Content = new StringContent(retMessage) }); }
public HttpResponseMessage ClearFileData(string lstDataFileIds, string ConnectionString) { DataFileImporter objDataFileImporter = new DataFileImporter(); try { string connectionstring = Regex.Replace(ConnectionString.Trim(), @"\t|\n|\r", ""); using (ThomsonDbContext db = new ThomsonDbContext(ConnectionString)) { foreach (var item in lstDataFileIds.Split(',')) { int idItem = Convert.ToInt32(item); DataFile objdataFile = db.DataFiles.Find(idItem); db.DataFiles.Remove(objdataFile); db.SaveChanges(); retMessage = "successfully Deleted"; } } } catch (Exception ex) { retMessage = ex.InnerException.Message; throw ex; } return(new HttpResponseMessage() { Content = new StringContent(retMessage) }); }
public HttpResponseMessage LoadSftpData(string strDateFormat, string ConnectionString) { try { DataFileImporter objDataFileImporter = new DataFileImporter(); List <DataFile> lstDataFile = new List <DataFile>(); ConnectionString = Regex.Replace(ConnectionString.Trim(), @"\t|\n|\r", ""); using (ThomsonDbContext db = new ThomsonDbContext(ConnectionString)) { Session session = new Session(); string path = db.Parameters.FirstOrDefault(m => m.Name == "RemotePath").Value; string localFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/ZipCsvs"); SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = ConfigurationManager.AppSettings["HostName"], UserName = ConfigurationManager.AppSettings["UserName"], Password = ConfigurationManager.AppSettings["Password"], }; sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = true; session.Open(sessionOptions); string remoteFilePath = db.Parameters.FirstOrDefault(m => m.Name == "RemotePath").Value + "/" + strDateFormat + "/" + "cleaned"; if (session.FileExists(remoteFilePath)) { //Fetch all the Files from SFTP as per configuration structure setting in Config var SftpFiles = session.EnumerateRemoteFiles(remoteFilePath, "", EnumerationOptions.None).Where(m => m.Name.Contains(ConfigurationManager.AppSettings["csvFileNameFormat1"].ToString()) || m.Name.Contains(ConfigurationManager.AppSettings["csvFileNameFormat2"].ToString()) || m.Name.Contains(ConfigurationManager.AppSettings["csvFileNameFormat3"].ToString())); foreach (var item1 in SftpFiles) { //Check the DataFile exist or not in database for duplicate if (!db.DataFiles.Any(m => (m.FileName == item1.FullName) || (m.FileName == item1.Name))) { DataFile objDataFile = new DataFile(); objDataFile.FileName = item1.FullName; objDataFile.DateOfFile = Convert.ToDateTime(objDataFileImporter.GetDateFromString(item1.Name)); objDataFile.IsInSftp = true; lstDataFile.Add(objDataFile); } } } if (lstDataFile.Count > 0) { //Save the changes for the new collection of DataFiles db.DataFiles.AddRange(lstDataFile); db.SaveChanges(); } retMessage = "Sucessfully Imported"; } } catch (Exception ex) { retMessage = ex.Message; throw ex; } return(new HttpResponseMessage() { Content = new StringContent(retMessage) }); }
protected override void Seed(WeatherDB context) { #region Import fields StationFileImporter stationFileImporter = new StationFileImporter(@"..\..\..\import.csv"); DataFileImporter dataFileImporter = new DataFileImporter(@"..\..\..\import.csv"); List <IDuplicateDeleter> duplicateDeleters = new List <IDuplicateDeleter>() { stationFileImporter, dataFileImporter }; List <ICollectionImporter> collectionImporters = new List <ICollectionImporter>() { stationFileImporter, dataFileImporter }; #endregion #region Export fields DataWeatherExporter dataWeatherExporter = new DataWeatherExporter(dataFileImporter.Datas); StationWeatherExporter stationWeatherExporter = new StationWeatherExporter(stationFileImporter.Stations); List <WeatherExporter> weatherExporters = new List <WeatherExporter> { stationWeatherExporter, dataWeatherExporter }; #endregion #region Import //imports from file to collections foreach (var item in collectionImporters) { item.CollectionImport(); } //deletes duplicate from collections foreach (var item in duplicateDeleters) { item.DeleteDuplicates(); } #endregion #region Export //adds collections to Db foreach (var item in weatherExporters) { item.DbExport(); } #endregion base.Seed(context); }