private async Task <QueueIntegrationImportCommandResult> HandleWithErrorLogging(
            QueueIntegrationImportCommand command,
            CancellationToken cancellationToken)
        {
            try
            {
                var commandResult = await _handler
                                    .Handle(command, cancellationToken)
                                    .ConfigureAwait(Await.Default);

                return(commandResult);
            }
            catch (Exception ex)
            {
                const string commandName = nameof(QueueIntegrationImportCommand);

                _logger.ErrorFormat(
                    ex,
                    "Error executing command {0} :\r\n{1}",
                    commandName,
                    _logger.Serialize(command));

                throw;
            }
        }
 /// <summary>
 /// Handle the command
 /// </summary>
 /// <param name="command">The command</param>
 /// <param name="cancellationToken">The cancellation token</param>
 /// <returns>The command result</returns>
 public Task <QueueIntegrationImportCommandResult> Handle(
     QueueIntegrationImportCommand command,
     CancellationToken cancellationToken)
 {
     if (_logger.IsErrorEnabled)
     {
         return(HandleWithErrorLogging(command, cancellationToken));
     }
     else
     {
         return(_handler.Handle(command, cancellationToken));
     }
 }
        /// <inheritdoc/>
        public Task <QueueIntegrationImportCommandResult> Handle(
            QueueIntegrationImportCommand command,
            CancellationToken cancellationToken)
        {
            Argument.NotNull(command, nameof(command));

            if (command != null)
            {
                _validator.ValidateObject(command);
            }

            return(_handler.Handle(command, cancellationToken));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Handle the command
        /// </summary>
        /// <param name="command">The command</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The command result</returns>
        public async Task <QueueIntegrationImportCommandResult> Handle(
            QueueIntegrationImportCommand command,
            CancellationToken cancellationToken)
        {
            Argument.NotNull(command, nameof(command));

            var commandResult = await _handler
                                .Handle(command, cancellationToken)
                                .ConfigureAwait(Await.Default);

            await _dataContext
            .SaveChanges(cancellationToken)
            .ConfigureAwait(Await.Default);

            return(commandResult);
        }
        private async Task <QueueIntegrationImportCommandResult> HandleWithDebug(
            QueueIntegrationImportCommand command,
            CancellationToken cancellationToken)
        {
            const string commandName = nameof(QueueIntegrationImportCommand);

            _logger.DebugFormat(
                "Executing command {0} :\r\n{1}",
                commandName,
                _logger.Serialize(command));

            var commandResult = await _handler
                                .Handle(command, cancellationToken)
                                .ConfigureAwait(Await.Default);

            _logger.DebugFormat(
                "Executed command {0} :\r\n{1}",
                commandName,
                _logger.Serialize(commandResult));

            return(commandResult);
        }
        /// <summary>
        /// Handle the command
        /// </summary>
        /// <param name="command">The command</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The command result</returns>
        public async Task <QueueIntegrationImportCommandResult> Handle(
            QueueIntegrationImportCommand command,
            CancellationToken cancellationToken)
        {
            Argument.NotNull(command, nameof(command));

            try
            {
                var commandResult = await _handler
                                    .Handle(command, cancellationToken)
                                    .ConfigureAwait(Await.Default);

                return(commandResult);
            }
            catch (Exception)
            {
                await _dataContext
                .Rollback(cancellationToken)
                .ConfigureAwait(Await.Default);

                throw;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Handle the command
        /// </summary>
        /// <param name="command">The command</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The command result</returns>
        public async Task <QueueIntegrationImportCommandResult> Handle(
            QueueIntegrationImportCommand command,
            CancellationToken cancellationToken)
        {
            Argument.NotNull(command, nameof(command));

            var commandResult = new QueueIntegrationImportCommandResult();

            if (_dataContext.IntegrationImports.TryGetValue(command.IntegrationId, out var integrationImport) == false)
            {
                integrationImport = new IntegrationImport
                {
                    IntegrationId = command.IntegrationId,
                };

                _dataContext.IntegrationImports.Add(command.IntegrationId, integrationImport);
            }

            var fileInfo = _fileInfoFactory.FromFileName(command.File);

            if (fileInfo.Exists)
            {
                var versionId = _guidProvider.NewGuid();

                string extension = fileInfo.Extension;

                var now = _dateTimeProvider.UtcNow;

                string queueFolder = _configuration.Value.QueueFolder;

                string orignalFileName = fileInfo.Name;

                var fileDate = new DateTimeOffset(fileInfo.LastWriteTimeUtc, TimeSpan.Zero);

                string fileName = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}_{1:yyyyMMddHHmmssfff}_{2}{3}",
                    command.IntegrationId,
                    now,
                    versionId,
                    extension);

                var filePath = _path.Combine(queueFolder, fileName);

                long size = fileInfo.Length;

                if (command.MoveFile == true)
                {
                    fileInfo.MoveTo(filePath);

                    var queueItem = new IntegrationImportQueueItem
                    {
                        IntegrationId = command.IntegrationId,
                        File          = filePath,
                        FileName      = orignalFileName,
                        FileSize      = size,
                        FileDate      = fileDate,
                        Hash          = null,
                        FileVersionId = versionId,
                    };

                    _dataContext.IntegrationImportQueue.Add(queueItem);
                }
                else
                {
                    using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        var query = new HashFileQuery
                        {
                            File = stream
                        };

                        var queryResult = await _queryHandler
                                          .Handle(query, cancellationToken)
                                          .ConfigureAwait(Await.Default);

                        _ = stream.Seek(0, SeekOrigin.Begin);

                        if (string.Equals(integrationImport.Hash, queryResult.Hash, StringComparison.Ordinal) == false)
                        {
                            integrationImport.Hash = queryResult.Hash;
                            integrationImport.Size = size;

                            var writeCommand = new WriteFileFromStreamCommand
                            {
                                Path     = queueFolder,
                                FileName = fileName,
                                File     = stream,
                                Size     = size
                            };

                            var writeCommandResult = await _commandHandler
                                                     .Handle(writeCommand, cancellationToken)
                                                     .ConfigureAwait(Await.Default);

                            switch (writeCommandResult.Result)
                            {
                            case WriteFileFromStreamCommandResultKind.Success:
                                break;

                            default:
                                throw UnexpectedEnumValueException.Create(writeCommandResult.Result);
                            }

                            string hash = queryResult.Hash;

                            var queueItem = new IntegrationImportQueueItem
                            {
                                IntegrationId = command.IntegrationId,
                                File          = filePath,
                                FileName      = orignalFileName,
                                FileSize      = size,
                                FileDate      = fileDate,
                                Hash          = hash,
                                FileVersionId = versionId,
                            };

                            _dataContext.IntegrationImportQueue.Add(queueItem);
                        }
                    }
                }
            }

            return(commandResult);
        }