Ejemplo n.º 1
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(ISerialization <TInput> input, ISerialization <TOutput> output, TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var serviceType = TypeResolver.Resolve(argument.Name);

            if (serviceType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find service: {0}".With(argument.Name),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            var serviceInterface =
                serviceType.GetInterfaces()
                .FirstOrDefault(it => it.IsGenericType && typeof(IServerService <,>) == it.GetGenericTypeDefinition());

            if (serviceInterface == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Object: {0} is not a valid service.".With(argument.Name),
                         "{0} must implement {1} to be executed as a service call".With(argument.Name, typeof(IServerService <,>).FullName)));
            }

            if (!Permissions.CanAccess(serviceType))
            {
                return
                    (CommandResult <TOutput> .Return(
                         HttpStatusCode.Forbidden,
                         default(TOutput),
                         "You don't have permission to access: {0}.",
                         argument.Name));
            }

            try
            {
                var commandType = typeof(ExecuteServiceCommand <,>).MakeGenericType(serviceInterface.GetGenericArguments());
                var command     = Activator.CreateInstance(commandType) as IExecuteServiceCommand;
                var result      = command.Execute(input, output, Locator, serviceType, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Created, result, "Service executed"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 2
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(ISerialization <TInput> input, ISerialization <TOutput> output, TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }

            try
            {
                var table = PopulateTable(input, output, Locator, DomainModel, either.Argument, Permissions);
                if (either.Argument.UseDataTable)
                {
                    return(CommandResult <TOutput> .Return(HttpStatusCode.Created, output.Serialize(table), "Data analyzed"));
                }
                var result = ConvertTable.Convert(output, table);
                return(CommandResult <TOutput> .Return(HttpStatusCode.Created, result, "Data analyzed"));
            }
            catch (SecurityException ex)
            {
                return(CommandResult <TOutput> .Return(HttpStatusCode.Forbidden, default(TOutput), ex.Message));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 3
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(ISerialization <TInput> input, ISerialization <TOutput> output, TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var rootType = DomainModel.Find(argument.Name);

            if (rootType == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Couldn't find aggregate root type {0}.".With(argument.Name),
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!typeof(IAggregateRoot).IsAssignableFrom(rootType))
            {
                return(CommandResult <TOutput> .Fail(@"Specified type ({0}) is not an aggregate root. 
Please check your arguments.".With(argument.Name), null));
            }

            if (!Permissions.CanAccess(rootType))
            {
                return
                    (CommandResult <TOutput> .Return(
                         HttpStatusCode.Forbidden,
                         default(TOutput),
                         "You don't have permission to access: {0}.",
                         argument.Name));
            }

            if (argument.Data == null)
            {
                return(CommandResult <TOutput> .Fail("Data to create not specified.", @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output, rootType))));
            }

            try
            {
                var commandType = typeof(CreateCommand <>).MakeGenericType(rootType);
                var command     = Activator.CreateInstance(commandType) as ICreateCommand;
                var result      = command.Create(input, output, Locator, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Created, result, "Object created"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output, rootType))));
            }
        }
Ejemplo n.º 4
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var eventType = DomainModel.Find(argument.Name);

            if (eventType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find event type {0}.".With(argument.Name),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
            if (!typeof(IEvent).IsAssignableFrom(eventType))
            {
                return(CommandResult <TOutput> .Fail(@"Specified type ({0}) is not an event. 
Please check your arguments.".With(argument.Name), null));
            }
            if (!Permissions.CanAccess(eventType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.Name));
            }
            try
            {
                ISubmitCommand command;
                if (!Cache.TryGetValue(eventType, out command))
                {
                    var commandType = typeof(SubmitEventCommand <>).MakeGenericType(eventType);
                    command = Activator.CreateInstance(commandType) as ISubmitCommand;
                    var newCache = new Dictionary <Type, ISubmitCommand>(Cache);
                    newCache[eventType] = command;
                    Cache = newCache;
                }
                var result = command.Submit(input, output, locator, argument.ReturnInstance ?? false, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Created, result, "Event stored"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 5
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(ISerialization <TInput> input, ISerialization <TOutput> output, TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var objectType = DomainModel.Find(argument.Name);

            if (objectType == null)
            {
                return(CommandResult <TOutput> .Fail("Couldn't find domain object type {0}.".With(argument.Name), null));
            }

            if (!Permissions.CanAccess(objectType))
            {
                return
                    (CommandResult <TOutput> .Return(
                         HttpStatusCode.Forbidden,
                         default(TOutput),
                         "You don't have permission to access: {0}.",
                         argument.Name));
            }

            if (argument.Uri == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Uri to read not specified.",
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output, objectType))));
            }

            try
            {
                var commandType = typeof(FindCommand <>).MakeGenericType(objectType);
                var command     = Activator.CreateInstance(commandType) as IFindCommand;
                var result      = command.Find(output, Locator, Permissions, argument.Uri);

                return(result != null
                                        ? CommandResult <TOutput> .Success(result, "Object found")
                                        : CommandResult <TOutput> .Return(
                           HttpStatusCode.NotFound,
                           result,
                           "Can't find {0} with Uri: {1}.".With(objectType.FullName, argument.Uri)));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output, objectType))));
            }
        }
Ejemplo n.º 6
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(ISerialization <TInput> input, ISerialization <TOutput> output, TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var reportType = DomainModel.Find(argument.ReportName);

            if (reportType == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Couldn't find report type {0}.".With(argument.ReportName),
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            var ri = reportType.GetInterfaces().FirstOrDefault(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IReport <>));

            if (ri == null)
            {
                return(CommandResult <TOutput> .Fail(@"Specified type ({0}) is not an report. 
Please check your arguments.".With(argument.ReportName), null));
            }

            if (!Permissions.CanAccess(reportType))
            {
                return
                    (CommandResult <TOutput> .Return(
                         HttpStatusCode.Forbidden,
                         default(TOutput),
                         "You don't have permission to access: {0}.",
                         argument.ReportName));
            }

            try
            {
                var commandType = typeof(PopulateReportCommand <,>).MakeGenericType(reportType, ri.GetGenericArguments()[0]);
                var command     = Activator.CreateInstance(commandType) as IPopulateReport;
                var result      = command.Populate(input, output, Locator, argument.Data);

                return(CommandResult <TOutput> .Success(result, "Report populated"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 7
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var eventType = DomainModel.Find(argument.Name);

            if (eventType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find event type {0}.".With(argument.Name),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
            if (!Permissions.CanAccess(eventType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.Name));
            }
            try
            {
                IQueueCommand command;
                if (!Cache.TryGetValue(eventType, out command))
                {
                    var commandType = typeof(QueueEventCommand <>).MakeGenericType(eventType);
                    command = Activator.CreateInstance(commandType) as IQueueCommand;
                    var newCache = new Dictionary <Type, IQueueCommand>(Cache);
                    newCache[eventType] = command;
                    Cache = newCache;
                }
                command.Queue(input, locator, DataContext, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Accepted, default(TOutput), "Event queued"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 8
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var eventType = DomainModel.Find(argument.Name);

            if (eventType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find event type {0}.".With(argument.Name),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            var aggregateInterface = eventType.GetInterfaces().FirstOrDefault(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IDomainEvent <>));
            var aggregateType      = aggregateInterface != null?aggregateInterface.GetGenericArguments()[0] : null;

            if (aggregateType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "{0} does not implement IDomainEvent<TAggregate>.".With(eventType.FullName),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!Permissions.CanAccess(eventType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(eventType.FullName));
            }
            if (!Permissions.CanAccess(aggregateType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(aggregateType.FullName));
            }
            try
            {
                IQueueCommand command;
                if (!Cache.TryGetValue(eventType, out command))
                {
                    var commandType = typeof(QueueEventCommand <,>).MakeGenericType(eventType, aggregateType);
                    command = Activator.CreateInstance(commandType) as IQueueCommand;
                    var newCache = new Dictionary <Type, IQueueCommand>(Cache);
                    newCache[eventType] = command;
                    Cache = newCache;
                }
                var result = command.Queue(input, output, locator, argument.Uri, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Accepted, result, "Event queued"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 9
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var documentType = DomainModel.FindNested(argument.CubeName, argument.TemplaterName);

            if (documentType == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Couldn't find Templater type {0} for {1}.".With(argument.TemplaterName, argument.CubeName),
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!Permissions.CanAccess(documentType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden("{0} in {1}.".With(argument.TemplaterName, argument.CubeName)));
            }
            if (!typeof(IDocumentReport <DataTable>).IsAssignableFrom(documentType))
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Templater type {0} for {1} is not IDocumentReport<DataTable>. Check {0}.".With(
                             documentType.FullName,
                             argument.CubeName),
                         null));
            }

            IDocumentReport <DataTable> report;

            try
            {
                report = locator.Resolve <IDocumentReport <DataTable> >(documentType);
            }
            catch (Exception ex)
            {
                return(CommandResult <TOutput> .Fail(
                           @"Can't create document report. Is report {0} registered in system?".With(documentType.FullName),
                           ex.GetDetailedExplanation()));
            }
            try
            {
                var table  = AnalyzeOlapCube.PopulateTable(input, output, locator, DomainModel, argument, principal, Permissions);
                var result = report.Create(table);
                return(CommandResult <TOutput> .Return(HttpStatusCode.Created, Serialize(output, result), "Report created"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 10
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(ISerialization <TInput> input, ISerialization <TOutput> output, TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var rootType = DomainModel.Find(argument.Name);

            if (rootType == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Couldn't find domain object type {0}.".With(argument.Name),
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!typeof(IObjectHistory).IsAssignableFrom(rootType))
            {
                return(CommandResult <TOutput> .Fail(@"Specified type ({0}) does not support history tracking. 
Please check your arguments.".With(argument.Name), null));
            }

            if (!Permissions.CanAccess(rootType))
            {
                return
                    (CommandResult <TOutput> .Return(
                         HttpStatusCode.Forbidden,
                         default(TOutput),
                         "You don't have permission to access: {0}.",
                         argument.Name));
            }

            if (argument.Uri == null || argument.Uri.Length == 0)
            {
                return(CommandResult <TOutput> .Fail(
                           "Uri not specified.",
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            try
            {
                var commandType = typeof(FindCommand <>).MakeGenericType(rootType);
                var command     = Activator.CreateInstance(commandType) as IFindCommand;
                var result      = command.Find(output, Locator, argument.Uri);

                return(CommandResult <TOutput> .Success(result.Result, "Found {0} item(s)", result.Count));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 11
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(ISerialization <TInput> input, ISerialization <TOutput> output, TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            Type domainType = DomainModel.Find(argument.DomainObjectName);

            if (domainType == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Couldn't find domain object type {0}.".With(argument.DomainObjectName),
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!Permissions.CanAccess(domainType))
            {
                return
                    (CommandResult <TOutput> .Return(
                         HttpStatusCode.Forbidden,
                         default(TOutput),
                         "You don't have permission to access: {0}.",
                         argument.DomainObjectName));
            }

            Type validationType = DomainModel.FindNested(domainType.FullName, argument.ValidationName);

            if (validationType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find validation {0} for {1}".With(
                             argument.ValidationName,
                             argument.DomainObjectName),
                         null));
            }

            if (!Permissions.CanAccess(validationType))
            {
                return
                    (CommandResult <TOutput> .Return(
                         HttpStatusCode.Forbidden,
                         default(TOutput),
                         "You don't have permission to access: {0}.",
                         argument.ValidationName));
            }

            Type bindingType = string.IsNullOrWhiteSpace(argument.BindingObjectName) ? null : DomainModel.Find(argument.BindingObjectName);

            try
            {
                var commandType = typeof(FindInvalidDataCommand <>).MakeGenericType(domainType);
                var command     = Activator.CreateInstance(commandType) as IFindInvalidDataCommand;
                var result      = command.FindInvalid(input, output, DomainModel, Locator, validationType, bindingType, argument.Specification);
                return(CommandResult <TOutput> .Success(result.Result, "Found {0} item(s)", result.Count));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 12
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var reportType = DomainModel.Find(argument.ReportName);

            if (reportType == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Couldn't find report type {0}.".With(argument.ReportName),
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            var ri = reportType.GetInterfaces().FirstOrDefault(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IReport <>));

            if (ri == null)
            {
                return(CommandResult <TOutput> .Fail(@"Specified type ({0}) is not an report. 
Please check your arguments.".With(argument.ReportName), null));
            }

            if (!Permissions.CanAccess(reportType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.ReportName));
            }
            var documentType = DomainModel.FindNested(argument.ReportName, argument.TemplaterName);

            if (documentType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find Templater type {0} for {1}.".With(argument.TemplaterName, argument.ReportName),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!Permissions.CanAccess(documentType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden("{0} in {1}.".With(argument.TemplaterName, argument.ReportName)));
            }
            if (!typeof(IDocumentReport <>).MakeGenericType(reportType).IsAssignableFrom(documentType))
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Templater type {0} for {1} is not IDocumentReport<{1}>. Check {0}.".With(
                             documentType.FullName,
                             reportType.FullName),
                         null));
            }

            try
            {
                var commandType = typeof(GenerateReportCommand <,>).MakeGenericType(reportType, ri.GetGenericArguments()[0]);
                var command     = Activator.CreateInstance(commandType) as IGenerateReport;
                var result      = command.Convert(input, locator, documentType, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Created, Serialize(output, result), "Report created"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 13
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var objectType = DomainModel.Find(argument.Name);

            if (objectType == null)
            {
                return(CommandResult <TOutput> .Fail("Couldn't find domain object type {0}.".With(argument.Name), null));
            }

            if (!Permissions.CanAccess(objectType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.Name));
            }
            if (argument.Uri == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Uri to read not specified.",
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output, objectType))));
            }

            try
            {
                IFindCommand command;
                if (!Cache.TryGetValue(objectType, out command))
                {
                    var commandType = typeof(FindCommand <>).MakeGenericType(objectType);
                    command = Activator.CreateInstance(commandType) as IFindCommand;
                    var newCache = new Dictionary <Type, IFindCommand>(Cache);
                    newCache[objectType] = command;
                    Cache = newCache;
                }
                var result = command.Find(output, locator, Permissions, principal, argument.Uri);

                return(result != null
                                        ? CommandResult <TOutput> .Success(result, "Object found")
                                        : CommandResult <TOutput> .Return(
                           HttpStatusCode.NotFound,
                           result,
                           "Can't find {0} with Uri: {1}.".With(objectType.FullName, argument.Uri)));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output, objectType))));
            }
        }
Ejemplo n.º 14
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(ISerialization <TInput> input, ISerialization <TOutput> output, TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var eventType = DomainModel.Find(argument.Name);

            if (eventType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find event type {0}.".With(argument.Name),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            var aggregateInterface = eventType.GetInterfaces().FirstOrDefault(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IDomainEvent <>));
            var aggregateType      = aggregateInterface != null?aggregateInterface.GetGenericArguments()[0] : null;

            if (aggregateType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "{0} does not implement IDomainEvent<TAggregate>.".With(eventType.FullName),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!Permissions.CanAccess(eventType))
            {
                return
                    (CommandResult <TOutput> .Return(
                         HttpStatusCode.Forbidden,
                         default(TOutput),
                         "You don't have permission to access: {0}.",
                         eventType.FullName));
            }

            if (!Permissions.CanAccess(aggregateType))
            {
                return
                    (CommandResult <TOutput> .Return(
                         HttpStatusCode.Forbidden,
                         default(TOutput),
                         "You don't have permission to access: {0}.",
                         aggregateType.FullName));
            }

            try
            {
                var commandType = typeof(SubmitEventCommand <,>).MakeGenericType(eventType, aggregateType);
                var command     = Activator.CreateInstance(commandType) as ISubmitEventCommand;
                var result      = command.Submit(input, output, Locator, EventStore, argument.Uri, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Created, result, "Event applied"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Ejemplo n.º 15
0
            public CommandResult <TOutput> Submit <TInput, TOutput>(
                ISerialization <TInput> input,
                ISerialization <TOutput> output,
                IServiceProvider locator,
                bool returnInstance,
                TInput data)
            {
                TEvent domainEvent;

                try
                {
                    domainEvent = data != null?input.Deserialize <TInput, TEvent>(data, locator) : Activator.CreateInstance <TEvent>();
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Error deserializing domain event.", ex);
                }
                var    domainStore = locator.Resolve <IEventStore>();
                string uri;

                try
                {
                    uri = domainStore.Submit(domainEvent);
                }
                catch (SecurityException) { throw; }
                catch (Exception ex)
                {
                    throw new ArgumentException(
                              ex.Message,
                              data == null
                                                        ? new FrameworkException("Error while submitting event: {0}. Data not sent.".With(ex.Message), ex)
                                                        : new FrameworkException(@"Error while submitting event: {0}. Sent data: 
{1}".With(ex.Message, input.Serialize(domainEvent)), ex));
                }
                try
                {
                    var command = domainEvent as ICommand;
                    Dictionary <string, List <string> > errors;
                    if (command != null)
                    {
                        errors = command.GetValidationErrors();
                        if (errors.Count != 0)
                        {
                            return(CommandResult <TOutput> .Return(
                                       HttpStatusCode.BadRequest,
                                       output.Serialize(errors),
                                       "Validation errors"));
                        }
                        return(CommandResult <TOutput> .Return(
                                   HttpStatusCode.OK,
                                   returnInstance?output.Serialize(domainEvent) : output.Serialize(uri),
                                   "Command processed"));
                    }
                    return(CommandResult <TOutput> .Return(
                               HttpStatusCode.Created,
                               returnInstance?output.Serialize(domainEvent) : output.Serialize(uri),
                               "Event stored"));
                }
                catch (Exception ex)
                {
                    throw new ArgumentException(
                              ex.Message,
                              new FrameworkException(@"Error serializing result: " + ex.Message, ex));
                }
            }