Example #1
0
        public async Task <FormatResult <TSchema> > FormatAsync(Stream output, EntityResult <TSchema> input)
        {
            using (var writer = new StreamWriter(output))
            {
                for (int i = 0;; i++)
                {
                    TSchema entity;
                    if (!input.TryGetEntity(i, out entity))
                    {
                        break;
                    }

                    HL7Segment segment = entity as HL7Segment;
                    if (segment == null)
                    {
                        throw new MacheteSchemaException($"The entity is not an HL7 segment: {TypeCache.GetShortName(entity.GetType())}");
                    }

                    if (i > 0)
                    {
                        await writer.WriteAsync(_settings.SegmentSeparator);
                    }

                    IEntityFormatter entityFormatter;
                    if (_schema.TryGetEntityFormatter(entity, out entityFormatter))
                    {
                        var context = new StringBuilderFormatContext();
                        context.AddSettings(_settings);

                        entityFormatter.Format(context, entity);

                        await writer.WriteAsync(context.ToString()).ConfigureAwait(false);
                    }
                    else
                    {
                        throw new MacheteSchemaException($"The entity type was not found: {TypeCache.GetShortName(entity.GetType())}");
                    }
                }
            }

            return(new HL7FormatResult <TSchema>());
        }
Example #2
0
        public async Task <FormatResult <TSchema> > FormatAsync(Stream output, EntityCollection <TSchema> input)
        {
            using (var writer = new StreamWriter(output))
            {
                for (var i = 0;; i++)
                {
                    if (!input.TryGetEntity(i, out TSchema entity))
                    {
                        break;
                    }

                    if (!(entity is HL7Segment))
                    {
                        throw new MacheteSchemaException($"The entity is not an HL7 segment: {TypeCache.GetShortName(entity.GetType())}");
                    }

                    if (i > 0)
                    {
                        await writer.WriteAsync(_settings.SegmentSeparator).ConfigureAwait(false);
                    }

                    if (!_schema.TryGetEntityFormatter(entity, out var entityFormatter))
                    {
                        throw new MacheteSchemaException($"An entity formatter for the entity was not found: {TypeCache.GetShortName(entity.GetType())}");
                    }

                    var context = new StringBuilderFormatContext();
                    context.AddSettings(_settings);

                    entityFormatter.Format(context, entity);

                    if (context.Position > 0)
                    {
                        await writer.WriteAsync(context.ToString()).ConfigureAwait(false);
                    }
                }
            }

            return(new HL7FormatResult <TSchema>());
        }