Ejemplo n.º 1
0
        public bool Execute(ISessionContext context)
        {
            var _logger  = context.GetLogger();
            var _options = context.Options;

            IFileTransfer input               = null;
            string        lastFile            = null;
            int           filesCount          = 0;
            int           maxFilesCount       = Int32.MaxValue;
            var           fileTransferService = context.GetContainer().GetInstanceOf <IFileService> ();

            try
            {
                var searchPath = _options.Get("inputPath", "");
                if (String.IsNullOrEmpty(searchPath))
                {
                    throw new ArgumentNullException("inputPath");
                }

                var deleteSourceFile = _options.Get <bool>("deleteSourceFile", false);

                // prepare paths
                input = fileTransferService.Open(searchPath, _options);
                if (!input.IsOpened())
                {
                    throw new Exception("Invalid searchPath: " + searchPath);
                }

                maxFilesCount = _options.Get("maxFileCount", maxFilesCount);
                if (maxFilesCount <= 0)
                {
                    maxFilesCount = Int32.MaxValue;
                }

                var defaultEncoding = Encoding.GetEncoding(_options.Get("encoding", "ISO-8859-1"));

                // open connection
                string line;
                Layout layout = new Layout();

                foreach (var f in input.GetFileStreams())
                {
                    lastFile = f.FileName;
                    _logger.Info("File found: " + lastFile);
                    filesCount++;

                    // read file
                    using (var reader = new StreamReader(f.FileStream, defaultEncoding, true))
                    {
                        int n = 1;
                        while ((line = reader.ReadLine()) != null)
                        {
                            context.Emit(layout.Create()
                                         .Set("fileName", f.FileName)
                                         .Set("fileNumber", filesCount)
                                         .Set("filePath", searchPath)
                                         .Set("lineNumber", n++)
                                         .Set("line", line));
                        }
                    }

                    // If backup folder exists, move file
                    if (!String.IsNullOrWhiteSpace(_options.Get("backupLocation", "")))
                    {
                        // TODO: implement move operation if location are the same!
                        using (var backupLocation = fileTransferService.Open(_options.Get("backupLocation", ""), _options))
                        {
                            var destName = backupLocation.Details.GetDestinationPath(f.FileName);
                            backupLocation.SendFile(input.GetFileStream(f.FileName).FileStream, destName, true);
                            _logger.Info("Backup file created: " + destName);
                        }
                    }

                    // If DeleSource is set
                    if (deleteSourceFile)
                    {
                        input.RemoveFile(f.FileName);
                        _logger.Info("File deleted: " + f.FileName);
                    }

                    // limit
                    if (filesCount >= maxFilesCount)
                    {
                        break;
                    }
                }

                if (filesCount > 0)
                {
                    _logger.Success("Done");
                    return(true);
                }
                else
                {
                    _logger.Debug("No Files Found on: " + searchPath);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                context.Error = ex.Message;
                _logger.Error(ex);
                try
                {
                    if (lastFile != null && input != null && !String.IsNullOrEmpty(_options.Get("errorLocation", "")))
                    {
                        // move files
                        using (var parsedErrorLocation = fileTransferService.Open(_options.Get("errorLocation", ""), _options))
                        {
                            var destName = parsedErrorLocation.Details.GetDestinationPath(lastFile);
                            parsedErrorLocation.SendFile(input.GetFileStream(lastFile).FileStream, destName, true);
                        }
                    }
                }
                catch { }
                return(false);
            }
            finally
            {
                if (input != null)
                {
                    input.Dispose();
                }
            }
        }