コード例 #1
0
ファイル: Watchers.cs プロジェクト: eyedia/idpe
 protected void InvokeFileDownloaded(FileSystemWatcherEventArgs e)
 {
     if (FileDownloaded != null)
     {
         FileDownloaded(e);
     }
 }
コード例 #2
0
        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();
            }
        }
コード例 #3
0
        public JobProcessorFileHandler(FileSystemWatcherEventArgs e)
        {
            InputFileName       = e.FileName;
            InputFileExtension  = e.FileExtension;
            RenamedToIdentifier = e.RenamedToIdentifier;
            int dataSourceId = 0;

            int.TryParse(e.ApplicationParameters["DataSourceId"].ToString(), out dataSourceId);
            if (dataSourceId == 0)
            {
                throw new Exception("The data source id was not found!");
            }
            DataSourceId = dataSourceId;
            ProcessingBy = "Deb";
        }
コード例 #4
0
        private bool DataSourceIsDisabled(int dataSourceId, FileSystemWatcherEventArgs e)
        {
            DataSource dataSource = new DataSource(dataSourceId, string.Empty);

            if (dataSource.IsActive == false)
            {
                #region Getting Ignored Folder

                string ignoredFolder = Path.Combine(IdpeConfigurationSection.CurrentConfig.LocalFileWatcher.DirectoryIgnored, dataSourceId.ToString());
                ignoredFolder = Path.Combine(ignoredFolder, DateTime.Now.ToDBDateFormat());
                if (!Directory.Exists(ignoredFolder))
                {
                    Directory.CreateDirectory(ignoredFolder);
                }

                #endregion Getting Ignored Folder

                #region Getting Ignored File
                string ignoredFile = Path.Combine(ignoredFolder, Path.GetFileName(e.FileName));

                if (File.Exists(ignoredFile))
                {
                    string ignoredFileBUName = Path.Combine(ignoredFolder, string.Format("{0}_{1}", ShortGuid.NewGuid(), Path.GetFileName(e.FileName)));
                    FileCopy(ignoredFile, ignoredFileBUName, true); //backup existing
                }

                #endregion Getting Ignored File

                new FileUtility().FileCopy(e.FileName, ignoredFile, true);
                ExtensionMethods.TraceInformation("Pullers - A file from {0} was ignored as the datasource is disabled. The file can be found at {1}",
                                                  dataSource.Name, ignoredFile);

                #region Sending Email

                string emailBody = string.Format("A file from <b>{0}</b> was ignored as the datasource is disabled. The file can be found at <b>{1}</b>",
                                                 dataSource.Name, ignoredFile);
                emailBody = PostMan.__warningStartTag + emailBody + PostMan.__warningEndTag;
                new PostMan(dataSource, false).Send(emailBody, "File Ignored");

                #endregion Sending Email

                return(true);
            }
            return(false);
        }
コード例 #5
0
        internal void FileDownloaded(FileSystemWatcherEventArgs e)
        {
            JobProcessorFileHandler jobProcessorFile = new JobProcessorFileHandler(e);

            jobProcessorFile.PrepareInput();

            if (!jobProcessorFile.IsRequestFromWCF)
            {
                ExtensionMethods.TraceInformation("Pullers - Got a new file for {0} - '{1}'", jobProcessorFile.DataSourceId, e.FileName);
            }
            else
            {
                ExtensionMethods.TraceInformation("Pullers - Got a new WCF request for {0}, JobId = '{1}'", jobProcessorFile.DataSourceId, jobProcessorFile.JobId);
            }

            if (DataSourceIsDisabled(jobProcessorFile.DataSourceId, e))
            {
                return;
            }

            StringBuilder result       = new StringBuilder();
            JobProcessor  jobProcessor = new JobProcessor();

            if (jobProcessorFile.IsRequestFromWCF)
            {
                jobProcessor.IsWCFRequest = true;
                result = jobProcessor.ProcessJob(jobProcessorFile.DataSourceId, string.Empty, jobProcessorFile.ProcessingBy,
                                                 jobProcessorFile.InputFileNameOnly, jobProcessorFile.JobId, jobProcessorFile.WithError, jobProcessorFile.WithWarning);
            }
            else if ((jobProcessorFile.InputFileExtension.ToLower() == ".zip") || (jobProcessorFile.InputFileExtension.ToLower() == ".rar") ||
                     (jobProcessorFile.InputFileExtension.ToLower() == ".tar"))
            {
                jobProcessorFile.PrepareInputZip();
                result.AppendLine(jobProcessorFile.ZipUniuqeId);
            }
            else if ((jobProcessorFile.InputFileExtension.ToLower() == ".xls") || (jobProcessorFile.InputFileExtension.ToLower() == ".xlsx"))
            {
                result = jobProcessor.ProcessSpreadSheet(jobProcessorFile.DataSourceId, string.Empty, jobProcessorFile.ProcessingBy, e.FileName);
            }
            else if (jobProcessorFile.InputFileExtension.ToLower() == ".edi")
            {
                new EDIX12FileWatcher(jobProcessorFile.DataSourceId, e.FileName).Process();
                ExtensionMethods.TraceInformation("Pullers - {0} successfully processed. Output file was {1}",
                                                  e.FileName, jobProcessorFile.OutputFileName);
                if (File.Exists(jobProcessorFile.OutputFileName))
                {
                    InvokeFileProcessed(jobProcessorFile.DataSourceId, jobProcessor.JobId,
                                        jobProcessorFile.Keys, jobProcessorFile.OutputFileName,
                                        jobProcessorFile.ActualOutputFolder, jobProcessorFile.ZipUniuqeId);
                }

                jobProcessor.Dispose();
                return;
            }
            else
            {
                result = jobProcessor.ProcessJob(jobProcessorFile.DataSourceId, string.Empty,
                                                 jobProcessorFile.ProcessingBy, e.FileName, string.Empty, jobProcessorFile.WithError, jobProcessorFile.WithWarning);
            }
            #region Commented2

            /*
             * if (File.Exists(jobProcessorFile.OutputFileName))
             * {
             *  string buName = Path.Combine(jobProcessorFile.OutputFolder, string.Format("{0}_{1}", e.RenamedToIdentifier, Path.GetFileName(jobProcessorFile.OutputFileName)));
             *  FileCopy(jobProcessorFile.OutputFileName, buName, true); //backup existing
             * }
             *
             * if (((jobProcessorFile.InputFileExtension.ToLower() == ".zip")
             || (jobProcessorFile.InputFileExtension.ToLower() == ".rar") || (jobProcessorFile.InputFileExtension.ToLower() == ".tar"))
             || && (jobProcessorFile.Keys.GetKeyValue(SreKeyTypes.ZipDoNotCreateAcknoledgementInOutputFolder).ParseBool()))
             ||{
             || ExtensionMethods.TraceInformation("Pullers - The data source '{0}' has been configured as not to copy zip acknoledgement file. File will not be created!",
             ||     jobProcessorFile.DataSourceId);
             || return;
             ||}
             ||
             ||if (result.Length > 0)
             ||{
             || using (StreamWriter tw = new StreamWriter(jobProcessorFile.OutputFileName))
             || {
             ||     tw.Write(result);
             ||     tw.Close();
             || }
             ||
             || ExtensionMethods.TraceInformation("{0} successfully processed. Output file was {1}", e.FileName, outputFileName);
             || InvokeFileProcessed(dataSourceId, jobProcessor.JobId, keys, outputFileName, actualOutputFolder, zipUniuqeId);
             ||}
             ||else
             ||{
             || ExtensionMethods.TraceInformation("Pullers - Failed to process '{0}', empty data came from output writer! Check log for more details.", e.FileName);
             ||}
             */
            #endregion Commented2

            if (jobProcessorFile.PrepareOutput(result))
            {
                InvokeFileProcessed(jobProcessorFile.DataSourceId, jobProcessor.JobId, jobProcessorFile.Keys,
                                    jobProcessorFile.OutputFileName, jobProcessorFile.ActualOutputFolder, jobProcessorFile.ZipUniuqeId);
            }

            jobProcessor.Dispose();
        }
コード例 #6
0
        /// <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();
                }
            }
        }