/// <summary>
        /// Uploads file to blob store and FTP site as needed.
        /// </summary>
        /// <param name="content">
        /// The content of the file to upload.
        /// </param>
        /// <returns>
        /// The task to upload the file where needed.
        /// </returns>
        public async Task UploadRebateFile(string content)
        {
            string fileName = String.Concat("Rebate", DateTime.Now.ToString("yyyyMMddHmmss"), ".txt");
            MasterCardRebateBlobClient blobClient = MasterCardBlobClientFactory.MasterCardRebateBlobClient(Log);

            // Upload file to the blob store and MasterCard.
            byte[] contentBytes = Encoding.ASCII.GetBytes(content);
            using (MemoryStream memoryStream = new MemoryStream(contentBytes))
            {
                // Upload the file to the blob store.
                memoryStream.Position = 0;
                await blobClient.UploadAsync(memoryStream, fileName).ConfigureAwait(false);

                Log.Verbose("Uploaded file {0} to blob store.", fileName);

                // Upload the file to MasterCard.
                IFtpClient ftpClient = MasterCardFtpClientFactory.RebateFtpClient(Log);
                memoryStream.Position = 0;
                await ftpClient.UploadFileAsync(fileName, memoryStream);

                Log.Verbose("Uploaded file {0} to MasterCard.", fileName);
            }

            // Mark the file copy in the blob store as having been uploaded to MasterCard.
            await blobClient.MarkAsCompleteAsync(fileName).ConfigureAwait(false);

            Log.Verbose("File {0} marked as uploaded.", fileName);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Process MasterCard rebate confirmation file job execution.
        /// </summary>
        /// <param name="details">
        /// Details of the job to be executed.
        /// </param>
        /// <param name="log">
        /// Log within which to log status of job processing.
        /// </param>
        /// <returns>
        /// A task to execute the  job.
        /// </returns>
        /// <remarks>
        /// Once complete, this job will schedule a corresponding MasterCardProcessRebateConfirmationJob.
        /// </remarks>
        public async Task Execute(ScheduledJobDetails details,
                                  CommerceLog log)
        {
            Log = log;
            Log.Verbose("Starting execution of job.\r\nDetails {0}", details);

            MasterCardRebateConfirmationBlobClient blobClient = MasterCardBlobClientFactory.MasterCardRebateConfirmationBlobClient(log);

            // Download files from MasterCard and upload them to the blob store.
            IFtpClient ftpClient = MasterCardFtpClientFactory.RebateConfirmationFtpClient(Log);

            string[] files = await ftpClient.DirectoryListAsync();

            if (files != null)
            {
                foreach (string fileName in files)
                {
                    using (MemoryStream memStream = new MemoryStream())
                    {
                        // Download the file from MasterCard.
                        await ftpClient.DownloadFileAsync(fileName, memStream).ConfigureAwait(false);

                        // Upload the file to the blob store.
                        memStream.Position = 0;
                        await blobClient.UploadAsync(memStream, fileName).ConfigureAwait(false);
                    }
                }
            }

            // Process all pending rebate confirmation files in the blob store.
            ICollection <string> fileNames = blobClient.RetrieveNamesOfPendingFiles();

            if (fileNames != null)
            {
                foreach (string fileName in fileNames)
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        // Download the file from the blob store.
                        memoryStream.Position = 0;
                        await blobClient.DownloadAsync(memoryStream, fileName).ConfigureAwait(false);

                        // Process the file.
                        memoryStream.Position = 0;
                        ISettlementFileProcessor rebateConfirmationProcessor = MasterCardFileProcessorFactory.MasterCardRebateConfirmationProcessor(memoryStream, fileName);
                        await rebateConfirmationProcessor.Process().ConfigureAwait(false);
                    }

                    // Mark the file as having been processed.
                    await blobClient.MarkAsCompleteAsync(fileName).ConfigureAwait(false);
                }
            }

            Log.Verbose("Execution of job {0} complete ", details.JobId);
        }