コード例 #1
0
ファイル: ProcessingEngine.cs プロジェクト: tjakopovic/Rhetos
        public ProcessingResult ExecuteInner(IList <ICommandInfo> commands)
        {
            var authorizationMessage = _authorizationManager.Authorize(commands);

            if (!String.IsNullOrEmpty(authorizationMessage))
            {
                return new ProcessingResult
                       {
                           UserMessage   = authorizationMessage,
                           SystemMessage = authorizationMessage,
                           Success       = false
                       }
            }
            ;

            var commandResults = new List <CommandResult>();

            try
            {
                foreach (var commandInfo in commands)
                {
                    _logger.Trace("Executing command {0}: {1}.", commandInfo.GetType().Name, commandInfo);

                    var implementations = _commandRepository.GetImplementations(commandInfo.GetType());

                    if (implementations.Count() == 0)
                    {
                        throw new FrameworkException(string.Format(CultureInfo.InvariantCulture,
                                                                   "Cannot execute command \"{0}\". There are no command implementations loaded that implement the command.", commandInfo));
                    }

                    if (implementations.Count() > 1)
                    {
                        throw new FrameworkException(string.Format(CultureInfo.InvariantCulture,
                                                                   "Cannot execute command \"{0}\". It has more than one implementation registered: {1}.", commandInfo, String.Join(", ", implementations.Select(i => i.GetType().Name))));
                    }

                    var commandImplementation = implementations.Single();
                    _logger.Trace("Executing implementation {0}.", commandImplementation.GetType().Name);

                    var commandObserversForThisCommand = _commandObservers.GetImplementations(commandInfo.GetType());
                    var stopwatch = Stopwatch.StartNew();

                    foreach (var commandObeserver in commandObserversForThisCommand)
                    {
                        commandObeserver.BeforeExecute(commandInfo);
                        _performanceLogger.Write(stopwatch, () => "ProcessingEngine: CommandObeserver.BeforeExecute " + commandObeserver.GetType().FullName);
                    }

                    var commandResult = commandImplementation.Execute(commandInfo);

                    _performanceLogger.Write(stopwatch, () => "ProcessingEngine: Command executed (" + commandInfo.GetType().FullName + ").");
                    _logger.Trace("Execution result message: {0}", commandResult.Message);

                    if (commandResult.Success)
                    {
                        foreach (var commandObeserver in commandObserversForThisCommand)
                        {
                            commandObeserver.AfterExecute(commandInfo, commandResult);
                            _performanceLogger.Write(stopwatch, () => "ProcessingEngine: CommandObeserver.AfterExecute " + commandObeserver.GetType().FullName);
                        }
                    }

                    commandResults.Add(commandResult);

                    if (!commandResult.Success)
                    {
                        _persistenceTransaction.DiscardChanges();

                        var systemMessage = String.Format(CultureInfo.InvariantCulture, "Command failed. {0} {1} {2}", commandInfo.GetType().Name, commandInfo, commandImplementation.GetType().Name);
                        return(LogAndReturnError(commandResults, systemMessage + " " + commandResult.Message, systemMessage, commandResult.Message));
                    }
                }

                return(new ProcessingResult
                {
                    CommandResults = commandResults.ToArray(),
                    Success = true,
                    SystemMessage = null
                });
            }
            catch (Exception ex)
            {
                _persistenceTransaction.DiscardChanges();

                if (ex is TargetInvocationException && ex.InnerException is RhetosException)
                {
                    _logger.Trace(() => "Unwrapping exception: " + ex.ToString());
                    ex = ex.InnerException;
                }

                string userMessage   = null;
                string systemMessage = null;

                var sqlException = SqlUtility.InterpretSqlException(ex);
                if (sqlException != null)
                {
                    ex = sqlException;
                }

                if (ex is UserException)
                {
                    userMessage   = ex.Message;
                    systemMessage = (ex as UserException).SystemMessage;
                }
                else if (ex is ClientException)
                {
                    userMessage   = _clientExceptionUserMessage;
                    systemMessage = ex.Message;
                }
                else
                {
                    userMessage   = null;
                    systemMessage = "Internal server error occurred (" + ex.GetType().Name + "). See RhetosServer.log for more information.";
                }

                return(LogAndReturnError(
                           commandResults,
                           "Command execution error: ",
                           systemMessage,
                           userMessage,
                           ex));
            }
        }