예제 #1
0
        internal CommandOutput ExecuteCommand(ServiceRequestContext context)
        {
            var userTokenFromCache = CurrentHostContext.Default.Cache.Get<AuthenticationOutput>(context.Token);

            CommandOutput commandOutput = null;
            try
            {
                var input = new CommandInput(new CommandArgs(context.Command));
                input.Add("Request.SessionId", context.SessionId);
                if (input.Keyword == null)
                    throw new InvalidDataException("Could not parse the command");

                var firstKeyword = input.Keyword.ToLower();
                if (_commandFactory.Commands.Any(x => IsCommand(firstKeyword, x, userTokenFromCache)))
                {
                    var commandObject =
                        _commandFactory.Commands.FirstOrDefault(x => IsCommand(firstKeyword, x, userTokenFromCache));

                    if (userTokenFromCache != null) // enrich with UserId
                        input.AssociateUserId(userTokenFromCache.UserId.GetValueOrDefault(-1));

                    if (RequiresCaching(commandObject))
                    {
                        // cache it
                        commandOutput = CurrentHostContext.Default.Cache.Get(context.Command, () =>
                            ExecuteCommandInternal(input, commandObject));
                    }
                    else
                    {
                        commandOutput = ExecuteCommandInternal(input, commandObject);
                    }

                    AuditExecution(userTokenFromCache, input, commandObject);

                    // Execute all subcommands
                    commandOutput.SubCommands.ForEach(x => ExecuteCommand(new ServiceRequestContext
                    {
                        Command = x,
                        SessionId = context.SessionId,
                        Token = context.Token
                    }));
                }
                else
                {
                    commandOutput = _helpCommand.OnExecute(input);
                }
            }
            catch (DbEntityValidationException e)
            {
                var builder = new StringBuilder();

                foreach (var eve in e.EntityValidationErrors)
                {
                    builder.AppendFormat("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State).AppendLine();
                    foreach (var ve in eve.ValidationErrors)
                    {
                        builder.AppendFormat("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage).AppendLine();
                    }
                }

                CurrentHostContext.Default.Log.Error(builder.ToString());
                commandOutput = new CommandOutput
                {
                    MessageType = CommandOutput.DisplayMessageType.Error,
                    DisplayMessage = "Errors: " + builder
                };
            }
            catch (Exception ex)
            {
                CurrentHostContext.Default.Log.Error(ex.GetDetails());
                commandOutput = new CommandOutput
                {
                    MessageType = CommandOutput.DisplayMessageType.Error,
                    DisplayMessage = "Errors: " + ex.GetDetails()
                };
            }

            return commandOutput;
        }
예제 #2
0
 internal string[] GetCommandsStartingWith(ServiceRequestContext context)
 {
     string lowerCommand = context.Command.ToLowerInvariant();
     return
         _commandFactory.Commands.Where(x => x.Command.ToLowerInvariant().StartsWith(lowerCommand))
             .Select(x => x.Keyword)
             .OrderBy(x => x.Length)
             .ToArray();
 }