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); } try { var result = FindAndReturn(locator, input, output, principal, either.Argument); 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)))); } }
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)))); } }
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)))); } }
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 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.FullName, principal)) { return(CommandResult <TOutput> .Forbidden(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)))); } }
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); } try { var command = PrepareCommand(principal, either.Argument.Name, either.Argument.SpecificationName); var exists = command.CheckExists(input, locator, either.Argument.Specification); return(CommandResult <TOutput> .Success(output.Serialize(exists), exists.ToString())); } catch (ArgumentException ex) { return(CommandResult <TOutput> .Fail( ex.Message, ex.GetDetailedExplanation() + @" Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output)))); } }
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); } try { var result = Execute <TInput>(locator, principal, input, either.Argument); return(CommandResult <TOutput> .Success(Serialize(output, result), "Document created")); } catch (FileNotFoundException fnf) { return(CommandResult <TOutput> .Fail(fnf.Message, null)); } catch (ArgumentException ex) { return(CommandResult <TOutput> .Fail( ex.Message, ex.GetDetailedExplanation() + @" Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output)))); } }
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 commands = Locator.Resolve <IEnumerable <IServerCommandDescription <TInput> > >(); var results = Locator.Resolve <IEnumerable <ICommandResultDescription <TOutput> > >(); var resultCommand = results.FirstOrDefault(it => it.RequestID == argument.ResultID); if (resultCommand == null) { return(CommandResult <TOutput> .Fail("Couldn't find executed command {0}".With(argument.ResultID), null)); } var inputCommand = commands.FirstOrDefault(it => it.RequestID == argument.InputID); if (inputCommand == null) { return(CommandResult <TOutput> .Fail("Couldn't find command description {0}".With(argument.InputID), null)); } var transformation = input.Deserialize <TInput, ITransformation <TOutput, TInput> >(argument.Transformation); inputCommand.Data = transformation.Transform(output, input, resultCommand.Result.Data); return(CommandResult <TOutput> .Success(default(TOutput), "Data propagated")); }
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); } if (either.Argument.Uri == null) { return(CommandResult <TOutput> .Fail("Uri not provided.", @"Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output)))); } try { var arg = new GetDomainObject.Argument { Name = either.Argument.Name, Uri = new[] { either.Argument.Uri } }; var result = GetDomainObject.GetData(locator, arg, principal); return(CommandResult <TOutput> .Success(output.Serialize(result.Length == 1), result.Length == 1? "Found" : "Not found")); } catch (ArgumentException ex) { return(CommandResult <TOutput> .Fail( ex.Message, ex.GetDetailedExplanation() + @" Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output)))); } }
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)))); } }
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)))); } }
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)))); } }
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)))); } }
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)))); } }
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)))); } }
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 rootType = DomainModel.Find(argument.RootName); if (rootType == null) { return(CommandResult <TOutput> .Fail("Couldn't find root type: {0}".With(argument.RootName), @"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.RootName), null)); } if (!Permissions.CanAccess(rootType.FullName, principal)) { return(CommandResult <TOutput> .Forbidden(argument.RootName)); } if (argument.ToInsert == null && argument.ToUpdate == null && argument.ToDelete == null) { return(CommandResult <TOutput> .Fail("Data to persist not specified.", @"Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output, rootType)))); } try { IPersistCommand command; if (!Cache.TryGetValue(rootType, out command)) { var commandType = typeof(PersistAggregateRootCommand <>).MakeGenericType(rootType); command = Activator.CreateInstance(commandType) as IPersistCommand; var newCache = new Dictionary <Type, IPersistCommand>(Cache); newCache[rootType] = command; Cache = newCache; } var uris = command.Persist(input, locator, argument.ToInsert, argument.ToUpdate, argument.ToDelete); return(CommandResult <TOutput> .Success(output.Serialize(uris), "Data persisted")); } catch (ArgumentException ex) { return(CommandResult <TOutput> .Fail( ex.Message, ex.GetDetailedExplanation() + @" Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output)))); } }
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)))); } }
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)))); } }
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; IEvaluateCommand command; if (!Cache.TryGetValue(argument.QueryName, out command)) { var queryType = DomainModel.Find(argument.QueryName); if (queryType == null) { return(CommandResult <TOutput> .Fail( "Couldn't find query type {0}.".With(argument.QueryName), @"Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output)))); } var returnMethods = (from m in queryType.GetMethods(BindingFlags.Static | BindingFlags.Public) where m.Name == "Return" let p = m.GetParameters() where p != null && p.Length == 1 && typeof(ICommand).IsAssignableFrom(p[0].ParameterType) select new { command = p[0].ParameterType, output = m.ReturnType, method = m }).ToList(); if (returnMethods.Count != 1) { return(CommandResult <TOutput> .Fail("Invalid query type {0}.".With(argument.QueryName), null)); } var acceptMethods = (from m in queryType.GetMethods(BindingFlags.Static | BindingFlags.Public) where m.Name == "Accept" let p = m.GetParameters() where p != null && p.Length < 2 && typeof(ICommand).IsAssignableFrom(m.ReturnType) select new { method = m, accept = p.Length == 1 ? p[0].ParameterType : null }).ToList(); if (acceptMethods.Count != 1) { return(CommandResult <TOutput> .Fail("Invalid query type {0}.".With(argument.QueryName), null)); } var commandType = acceptMethods[0].accept == null ? typeof(EvaluateCommand <,>).MakeGenericType(returnMethods[0].command, returnMethods[0].output) : typeof(EvaluateCommand <, ,>).MakeGenericType(returnMethods[0].command, acceptMethods[0].accept, returnMethods[0].output); command = Activator.CreateInstance(commandType, new object[] { acceptMethods[0].method, returnMethods[0].method }) as IEvaluateCommand; var newCache = new Dictionary <string, IEvaluateCommand>(Cache); newCache[argument.QueryName] = command; Cache = newCache; } try { var result = command.Evaluate(input, output, locator, argument.Data); return(CommandResult <TOutput> .Success(result, "Evaluated")); } catch (ArgumentException ex) { return(CommandResult <TOutput> .Fail( ex.Message, ex.GetDetailedExplanation() + @" Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output)))); } }
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)))); } }
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)))); } }
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)))); } }