Beispiel #1
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);
            }
            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))));
            }
        }
Beispiel #2
0
        public Stream Execute <TInput>(ISerialization <TInput> input, Argument <TInput> argument)
        {
            var file = Path.Combine(DocumentFolder, argument.File);

            if (!File.Exists(file))
            {
                throw new FileNotFoundException("Can't locate file: {0}. Check if correct file is specified.".With(argument.File));
            }

            var cms = ChunkedMemoryStream.Create();

            using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                fs.CopyTo(cms);
            }
            cms.Position = 0;
            var ext = Path.GetExtension(argument.File);

            using (var document = TemplaterFactory.Open(cms, ext))
            {
                if (argument.GetSources != null)
                {
                    foreach (var source in argument.GetSources)
                    {
                        var found = GetDomain.GetData(source);
                        document.Process(found);
                    }
                }
                if (argument.SearchSources != null)
                {
                    foreach (var source in argument.SearchSources)
                    {
                        var found = SearchDomain.FindData <TInput>(input, source);
                        document.Process(found);
                    }
                }
                var specification =
                    (from a in (argument.SearchSources ?? new SearchDomainObject.Argument <TInput> [0])
                     where a.Specification != null
                     select a.Specification)
                    .FirstOrDefault();
                if (specification != null)
                {
                    dynamic filter = input.Deserialize <TInput, dynamic>(specification);
                    document.Process(filter);
                }
            }
            cms.Position = 0;
            return(argument.ToPdf ? PdfConverter.Convert(cms, ext, true) : cms);
        }