public void Process(string fileFullName) { string fileName = Path.GetFileName(fileFullName); //move file string moveTo = Path.Combine(LocalFileSystemFolderArchive, fileName); Directory.CreateDirectory(Path.GetDirectoryName(moveTo)); string renamedToIdentifier = Guid.NewGuid().ToString(); if (File.Exists(moveTo)) { string moveToBUName = Path.Combine(LocalFileSystemFolderArchive, string.Format("{0}_{1}", renamedToIdentifier, fileName)); new FileUtility().FileCopy(moveTo, moveToBUName, true); //backup existing } new FileUtility().FileCopy(fileFullName, moveTo, true); //move file if (this.DataSourceParameters == null) { this.DataSourceParameters = new Dictionary <string, object>(); } DataSourceParameters.Clear(); DataSourceParameters.Add("DataSourceId", this.DataSourceId); FileSystemWatcherEventArgs e = new FileSystemWatcherEventArgs(DataSourceParameters, moveTo, renamedToIdentifier); try { //if (File.Exists(moveTo)) //{ // using (StreamReader sr = new StreamReader(moveTo)) // { // e.FileContent = sr.ReadToEnd(); // sr.Close(); // } // InvokeFileDownloaded(e); //} InvokeFileDownloaded(e); } catch (BusinessException ex) { ExtensionMethods.TraceError(ex.ToString()); Trace.Flush(); } catch (Exception ex) { ExtensionMethods.TraceError("An unknown error occurred!. {0}. {1} This error needs immediate attention", ex.ToString(), Environment.NewLine + Environment.NewLine); Trace.Flush(); } }
//private List<SreKey> Keys //{ // get // { // List<SreKey> keys = Cache.Instance.Bag[DataSourceId + ".keys"] as List<SreKey>; // if (keys == null) // { // keys = new Manager().GetApplicationKeys(DataSourceId, true); // Cache.Instance.Bag.Add(DataSourceId + ".keys", keys); // } // return keys; // } //} #endregion Properties /// <summary> /// Keeps pulling from database (MS SQL, MS SQL CE, Oracle) /// </summary> /// <param name="dataSource">The data source object</param> public SqlWatcher(SreDataSource dataSource) { this.DataSource = new DataSource(dataSource.Id, string.Empty); //DataSourceName = dataSource.Name; ProcessingBy = dataSource.ProcessingBy; if (string.IsNullOrEmpty(ProcessingBy)) { ProcessingBy = dataSource.Name; } string strinterval = DataSource.Keys.GetKeyValue(SreKeyTypes.SqlWatchInterval); int interval = 0; int.TryParse(strinterval, out interval); if (interval <= 0) { interval = 2; } this.Interval = interval; string appPullFolder = SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryPull + "\\" + dataSource.Id; string appOutputFolder = SreConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryOutput + "\\" + dataSource.Id + "\\" + DateTime.Now.ToString("yyyyMMdd"); this.DataSourceParameters = new Dictionary <string, object>(); DataSourceParameters.Add("DataSourceId", dataSource.Id); DataSourceParameters.Add("PullFolder", appPullFolder); DataSourceParameters.Add("OutputFolder", appOutputFolder); DataSourceParameters.Add("ProcessingBy", dataSource.ProcessingBy); this.ReturnType = DataSource.Keys.GetKeyValue(SreKeyTypes.PullSqlReturnType); if (this.ReturnType == "I") { this.InterfaceName = DataSource.Keys.GetKeyValue(SreKeyTypes.PullSqlInterfaceName); if ((!string.IsNullOrEmpty(this.InterfaceName)) && (Type.GetType(this.InterfaceName) != null)) { object objInputFileGenerator = Activator.CreateInstance(Type.GetType(this.InterfaceName), this); this.InputFileGenerator = (InputFileGenerator)objInputFileGenerator; } } }
/// <summary> /// Processes a file by (1) moving file to archive folder, and then (2) invoking FileDownloaded event /// </summary> /// <param name="fileFullName"></param> /// <param name="fileName"></param> /// <param name="dataSourceId"></param> /// <param name="handleArchive"></param> public void Process(string fileFullName, string fileName, int dataSourceId, bool handleArchive = true) { if (fileName.Contains("\\")) { fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); } if (dataSourceId == 0) { int tempInt = 0; int.TryParse(Directory.GetParent(fileFullName).Name, out tempInt); if (tempInt == 0) { throw new Exception(string.Format("File '{0}' dropped on wrong location!", fileFullName)); } dataSourceId = tempInt; } lock (_lock) { string moveTo = fileFullName; string renamedToIdentifier = Guid.NewGuid().ToString(); if (handleArchive) { string archiveLoc = ArchiveLocation + "\\" + dataSourceId + "\\" + DateTime.Now.ToString("yyyyMMdd"); //move file moveTo = Path.Combine(archiveLoc, fileName); Directory.CreateDirectory(Path.GetDirectoryName(moveTo)); if (File.Exists(moveTo)) { string moveToBUName = Path.Combine(archiveLoc, string.Format("{0}_{1}", renamedToIdentifier, fileName)); FileCopy(moveTo, moveToBUName, true); //backup existing } FileCopy(fileFullName, moveTo, true); //move file } if (this.DataSourceParameters == null) { this.DataSourceParameters = new Dictionary <string, object>(); } DataSourceParameters.Clear(); DataSourceParameters.Add("DataSourceId", dataSourceId); FileSystemWatcherEventArgs e = new FileSystemWatcherEventArgs(DataSourceParameters, moveTo, renamedToIdentifier); try { InvokeFileDownloaded(e); } catch (BusinessException ex) { ExtensionMethods.TraceError(ex.ToString()); Trace.Flush(); } catch (Exception ex) { ExtensionMethods.TraceError("An unknown error occurred!. {0}. {1} This error needs immediate attention", ex.ToString(), Environment.NewLine + Environment.NewLine); Trace.Flush(); } } }