Beispiel #1
0
        public RoadSegmentRecordProjection(RecyclableMemoryStreamManager manager,
                                           Encoding encoding)
        {
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            When <Envelope <ImportedRoadSegment> >(async(context, envelope, token) =>
            {
                var geometry =
                    GeometryTranslator.FromGeometryMultiLineString(BackOffice.Core.GeometryTranslator.Translate(envelope.Message.Geometry));
                var polyLineMShapeContent         = new PolyLineMShapeContent(geometry);
                var statusTranslation             = RoadSegmentStatus.Parse(envelope.Message.Status).Translation;
                var morphologyTranslation         = RoadSegmentMorphology.Parse(envelope.Message.Morphology).Translation;
                var categoryTranslation           = RoadSegmentCategory.Parse(envelope.Message.Category).Translation;
                var geometryDrawMethodTranslation =
                    RoadSegmentGeometryDrawMethod.Parse(envelope.Message.GeometryDrawMethod).Translation;
                var accessRestrictionTranslation =
                    RoadSegmentAccessRestriction.Parse(envelope.Message.AccessRestriction).Translation;
                await context.RoadSegments.AddAsync(
                    new RoadSegmentRecord
                {
                    Id = envelope.Message.Id,
                    ShapeRecordContent       = polyLineMShapeContent.ToBytes(manager, encoding),
                    ShapeRecordContentLength = polyLineMShapeContent.ContentLength.ToInt32(),
                    BoundingBox = RoadSegmentBoundingBox.From(polyLineMShapeContent.Shape),
                    DbaseRecord = new RoadSegmentDbaseRecord
                    {
                        WS_OIDN   = { Value = envelope.Message.Id },
                        WS_UIDN   = { Value = $"{envelope.Message.Id}_{envelope.Message.Version}" },
                        WS_GIDN   = { Value = $"{envelope.Message.Id}_{envelope.Message.GeometryVersion}" },
                        B_WK_OIDN = { Value = envelope.Message.StartNodeId },
                        E_WK_OIDN = { Value = envelope.Message.EndNodeId },
                        STATUS    = { Value = statusTranslation.Identifier },
                        LBLSTATUS = { Value = statusTranslation.Name },
                        MORF      = { Value = morphologyTranslation.Identifier },
                        LBLMORF   = { Value = morphologyTranslation.Name },
                        WEGCAT    = { Value = categoryTranslation.Identifier },
                        LBLWEGCAT = { Value = categoryTranslation.Name },
                        LSTRNMID  = { Value = envelope.Message.LeftSide.StreetNameId },
                        LSTRNM    = { Value = envelope.Message.LeftSide.StreetName },
                        RSTRNMID  = { Value = envelope.Message.RightSide.StreetNameId },
                        RSTRNM    = { Value = envelope.Message.RightSide.StreetName },
                        BEHEER    = { Value = envelope.Message.MaintenanceAuthority.Code },
                        LBLBEHEER = { Value = envelope.Message.MaintenanceAuthority.Name },
                        METHODE   = { Value = geometryDrawMethodTranslation.Identifier },
                        LBLMETHOD = { Value = geometryDrawMethodTranslation.Name },
                        OPNDATUM  = { Value = envelope.Message.RecordingDate },
                        BEGINTIJD = { Value = envelope.Message.Origin.Since },
                        BEGINORG  = { Value = envelope.Message.Origin.OrganizationId },
                        LBLBGNORG = { Value = envelope.Message.Origin.Organization },
                        TGBEP     = { Value = accessRestrictionTranslation.Identifier },
                        LBLTGBEP  = { Value = accessRestrictionTranslation.Name }
                    }.ToBytes(manager, encoding)
                },
                    token);
            });

            When <Envelope <RoadNetworkChangesBasedOnArchiveAccepted> >(async(context, envelope, token) =>
            {
                foreach (var message in envelope.Message.Changes.Flatten())
                {
                    switch (message)
                    {
                    case RoadSegmentAdded segment:
                        var geometry =
                            GeometryTranslator.FromGeometryMultiLineString(BackOffice.Core.GeometryTranslator.Translate(segment.Geometry));
                        var polyLineMShapeContent         = new PolyLineMShapeContent(geometry);
                        var statusTranslation             = RoadSegmentStatus.Parse(segment.Status).Translation;
                        var morphologyTranslation         = RoadSegmentMorphology.Parse(segment.Morphology).Translation;
                        var categoryTranslation           = RoadSegmentCategory.Parse(segment.Category).Translation;
                        var geometryDrawMethodTranslation =
                            RoadSegmentGeometryDrawMethod.Parse(segment.GeometryDrawMethod).Translation;
                        var accessRestrictionTranslation =
                            RoadSegmentAccessRestriction.Parse(segment.AccessRestriction).Translation;
                        await context.RoadSegments.AddAsync(
                            new RoadSegmentRecord
                        {
                            Id = segment.Id,
                            ShapeRecordContent       = polyLineMShapeContent.ToBytes(manager, encoding),
                            ShapeRecordContentLength = polyLineMShapeContent.ContentLength.ToInt32(),
                            BoundingBox = RoadSegmentBoundingBox.From(polyLineMShapeContent.Shape),
                            DbaseRecord = new RoadSegmentDbaseRecord
                            {
                                WS_OIDN   = { Value = segment.Id },
                                WS_UIDN   = { Value = $"{segment.Id}_{segment.Version}" },
                                WS_GIDN   = { Value = $"{segment.Id}_{segment.GeometryVersion}" },
                                B_WK_OIDN = { Value = segment.StartNodeId },
                                E_WK_OIDN = { Value = segment.EndNodeId },
                                STATUS    = { Value = statusTranslation.Identifier },
                                LBLSTATUS = { Value = statusTranslation.Name },
                                MORF      = { Value = morphologyTranslation.Identifier },
                                LBLMORF   = { Value = morphologyTranslation.Name },
                                WEGCAT    = { Value = categoryTranslation.Identifier },
                                LBLWEGCAT = { Value = categoryTranslation.Name },
                                LSTRNMID  = { Value = segment.LeftSide.StreetNameId },
                                // TODO: Where does this come from?
                                LSTRNM   = { Value = null },
                                RSTRNMID = { Value = segment.RightSide.StreetNameId },
                                // TODO: Where does this come from?
                                RSTRNM    = { Value = null },
                                BEHEER    = { Value = segment.MaintenanceAuthority.Code },
                                LBLBEHEER = { Value = segment.MaintenanceAuthority.Name },
                                METHODE   = { Value = geometryDrawMethodTranslation.Identifier },
                                LBLMETHOD = { Value = geometryDrawMethodTranslation.Name },
                                OPNDATUM  = { Value = LocalDateTimeTranslator.TranslateFromWhen(envelope.Message.When) },
                                BEGINTIJD = { Value = LocalDateTimeTranslator.TranslateFromWhen(envelope.Message.When) },
                                BEGINORG  = { Value = envelope.Message.OrganizationId },
                                LBLBGNORG = { Value = envelope.Message.Organization },
                                TGBEP     = { Value = accessRestrictionTranslation.Identifier },
                                LBLTGBEP  = { Value = accessRestrictionTranslation.Name }
                            }.ToBytes(manager, encoding)
                        },
                            token);
                        break;
                    }
                }
            });
        }
        public RoadSegmentWidthAttributeRecordProjection(RecyclableMemoryStreamManager manager,
                                                         Encoding encoding)
        {
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            When <Envelope <ImportedRoadSegment> >((context, envelope, token) =>
            {
                if (envelope.Message.Widths.Length == 0)
                {
                    return(Task.CompletedTask);
                }

                var widths = envelope.Message
                             .Widths
                             .Select(width => new RoadSegmentWidthAttributeRecord
                {
                    Id            = width.AttributeId,
                    RoadSegmentId = envelope.Message.Id,
                    DbaseRecord   = new RoadSegmentWidthAttributeDbaseRecord
                    {
                        WB_OIDN   = { Value = width.AttributeId },
                        WS_OIDN   = { Value = envelope.Message.Id },
                        WS_GIDN   = { Value = $"{envelope.Message.Id}_{width.AsOfGeometryVersion}" },
                        BREEDTE   = { Value = width.Width },
                        VANPOS    = { Value = (double)width.FromPosition },
                        TOTPOS    = { Value = (double)width.ToPosition },
                        BEGINTIJD = { Value = width.Origin.Since },
                        BEGINORG  = { Value = width.Origin.OrganizationId },
                        LBLBGNORG = { Value = width.Origin.Organization }
                    }.ToBytes(manager, encoding)
                });

                return(context.RoadSegmentWidthAttributes.AddRangeAsync(widths, token));
            });

            When <Envelope <RoadNetworkChangesBasedOnArchiveAccepted> >(async(context, envelope, token) =>
            {
                foreach (var change in envelope.Message.Changes.Flatten())
                {
                    switch (change)
                    {
                    case RoadSegmentAdded segment:
                        if (segment.Widths.Length != 0)
                        {
                            var widths = segment
                                         .Widths
                                         .Select(width => new RoadSegmentWidthAttributeRecord
                            {
                                Id            = width.AttributeId,
                                RoadSegmentId = segment.Id,
                                DbaseRecord   = new RoadSegmentWidthAttributeDbaseRecord
                                {
                                    WB_OIDN   = { Value = width.AttributeId },
                                    WS_OIDN   = { Value = segment.Id },
                                    WS_GIDN   = { Value = $"{segment.Id}_{width.AsOfGeometryVersion}" },
                                    BREEDTE   = { Value = width.Width },
                                    VANPOS    = { Value = (double)width.FromPosition },
                                    TOTPOS    = { Value = (double)width.ToPosition },
                                    BEGINTIJD = { Value = LocalDateTimeTranslator.TranslateFromWhen(envelope.Message.When) },
                                    BEGINORG  = { Value = envelope.Message.OrganizationId },
                                    LBLBGNORG = { Value = envelope.Message.Organization }
                                }.ToBytes(manager, encoding)
                            });

                            await context.RoadSegmentWidthAttributes.AddRangeAsync(widths);
                        }
//                        case RoadSegmentModified segment:
//                            if (segment.Widths.Length == 0)
//                            {
//                                context.RoadSegmentWidthAttributes.RemoveRange(
//                                    context
//                                        .RoadSegmentWidthAttributes
//                                        .Local.Where(a => a.RoadSegmentId == segment.Id)
//                                        .Concat(await context
//                                            .RoadSegmentWidthAttributes
//                                            .Where(a => a.RoadSegmentId == segment.Id)
//                                            .ToArrayAsync(token)
//                                        ));
//                            }
//                            else
//                            {
//                                var currentSet = context
//                                    .RoadSegmentWidthAttributes
//                                    .Local.Where(a => a.RoadSegmentId == segment.Id)
//                                    .Concat(await context
//                                        .RoadSegmentWidthAttributes
//                                        .Where(a => a.RoadSegmentId == segment.Id)
//                                        .ToArrayAsync(token)
//                                    ).ToDictionary(a => a.Id);
//                                var nextSet = segment
//                                    .Widths
//                                    .Select(width => new RoadSegmentWidthAttributeRecord
//                                    {
//                                        Id = width.AttributeId,
//                                        RoadSegmentId = segment.Id,
//                                        DbaseRecord = new RoadSegmentWidthAttributeDbaseRecord
//                                        {
//                                            WB_OIDN = {Value = width.AttributeId},
//                                            WS_OIDN = {Value = segment.Id},
//                                            WS_GIDN = {Value = $"{segment.Id}_{width.AsOfGeometryVersion}"},
//                                            BREEDTE = {Value = width.Width},
//                                            VANPOS = {Value = (double) width.FromPosition},
//                                            TOTPOS = {Value = (double) width.ToPosition},
//                                            // TODO: This should come from the event
//                                            BEGINTIJD = {Value = null},
//                                            BEGINORG = {Value = null},
//                                            LBLBGNORG = {Value = null}
//                                        }.ToBytes(manager, encoding)
//                                    })
//                                    .ToDictionary(a => a.Id);
//                                context.RoadSegmentWidthAttributes.Synchronize(currentSet, nextSet, (current, next) =>
//                                    {
//                                        current.DbaseRecord = next.DbaseRecord;
//                                    });
//                            }
                        break;
                    }
                }
            });
        }
        public GradeSeparatedJunctionRecordProjection(RecyclableMemoryStreamManager manager, Encoding encoding)
        {
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            When <Envelope <ImportedGradeSeparatedJunction> >(async(context, envelope, token) =>
            {
                var translation    = GradeSeparatedJunctionType.Parse(envelope.Message.Type).Translation;
                var junctionRecord = new GradeSeparatedJunctionRecord
                {
                    Id          = envelope.Message.Id,
                    DbaseRecord = new GradeSeparatedJunctionDbaseRecord
                    {
                        OK_OIDN    = { Value = envelope.Message.Id },
                        TYPE       = { Value = translation.Identifier },
                        LBLTYPE    = { Value = translation.Name },
                        BO_WS_OIDN = { Value = envelope.Message.UpperRoadSegmentId },
                        ON_WS_OIDN = { Value = envelope.Message.LowerRoadSegmentId },
                        BEGINTIJD  = { Value = envelope.Message.Origin.Since },
                        BEGINORG   = { Value = envelope.Message.Origin.OrganizationId },
                        LBLBGNORG  = { Value = envelope.Message.Origin.Organization },
                    }.ToBytes(manager, encoding)
                };

                await context.AddAsync(junctionRecord, token);
            });

            When <Envelope <RoadNetworkChangesBasedOnArchiveAccepted> >(async(context, envelope, token) =>
            {
                foreach (var change in envelope.Message.Changes.Flatten())
                {
                    switch (change)
                    {
                    case GradeSeparatedJunctionAdded junction:
                        var translation    = GradeSeparatedJunctionType.Parse(junction.Type).Translation;
                        var junctionRecord = new GradeSeparatedJunctionRecord
                        {
                            Id          = junction.Id,
                            DbaseRecord = new GradeSeparatedJunctionDbaseRecord
                            {
                                OK_OIDN    = { Value = junction.Id },
                                TYPE       = { Value = translation.Identifier },
                                LBLTYPE    = { Value = translation.Name },
                                BO_WS_OIDN = { Value = junction.UpperRoadSegmentId },
                                ON_WS_OIDN = { Value = junction.LowerRoadSegmentId },
                                BEGINTIJD  =
                                {
                                    Value = LocalDateTimeTranslator.TranslateFromWhen(envelope.Message.When)
                                },
                                BEGINORG  = { Value = envelope.Message.OrganizationId },
                                LBLBGNORG = { Value = envelope.Message.Organization },
                            }.ToBytes(manager, encoding)
                        };

                        await context.AddAsync(junctionRecord, token);
                        break;
                    }
                }
            });
        }
        public RoadNodeRecordProjection(RecyclableMemoryStreamManager manager, Encoding encoding)
        {
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            When <Envelope <ImportedRoadNode> >(async(context, envelope, token) =>
            {
                var typeTranslation = RoadNodeType.Parse(envelope.Message.Type).Translation;
                var dbaseRecord     = new RoadNodeDbaseRecord
                {
                    WK_OIDN   = { Value = envelope.Message.Id },
                    WK_UIDN   = { Value = envelope.Message.Id + "_" + envelope.Message.Version },
                    TYPE      = { Value = typeTranslation.Identifier },
                    LBLTYPE   = { Value = typeTranslation.Name },
                    BEGINTIJD = { Value = envelope.Message.Origin.Since },
                    BEGINORG  = { Value = envelope.Message.Origin.OrganizationId },
                    LBLBGNORG = { Value = envelope.Message.Origin.Organization }
                };

                var point             = GeometryTranslator.FromGeometryPoint(BackOffice.Core.GeometryTranslator.Translate(envelope.Message.Geometry));
                var pointShapeContent = new PointShapeContent(point);

                await context.RoadNodes.AddAsync(new RoadNodeRecord
                {
                    Id = envelope.Message.Id,
                    ShapeRecordContent       = pointShapeContent.ToBytes(manager, encoding),
                    ShapeRecordContentLength = pointShapeContent.ContentLength.ToInt32(),
                    DbaseRecord = dbaseRecord.ToBytes(manager, encoding),
                    BoundingBox = RoadNodeBoundingBox.From(pointShapeContent.Shape)
                }, token);
            });

            When <Envelope <RoadNetworkChangesBasedOnArchiveAccepted> >(async(context, envelope, token) =>
            {
                foreach (var message in envelope.Message.Changes.Flatten())
                {
                    switch (message)
                    {
                    case RoadNodeAdded node:
                        var typeTranslation = RoadNodeType.Parse(node.Type).Translation;
                        var dbaseRecord     = new RoadNodeDbaseRecord
                        {
                            WK_OIDN   = { Value = node.Id },
                            WK_UIDN   = { Value = node.Id + "_0" },  // 1?
                            TYPE      = { Value = typeTranslation.Identifier },
                            LBLTYPE   = { Value = typeTranslation.Name },
                            BEGINTIJD = { Value = LocalDateTimeTranslator.TranslateFromWhen(envelope.Message.When) },
                            BEGINORG  = { Value = envelope.Message.OrganizationId },
                            LBLBGNORG = { Value = envelope.Message.Organization }
                        };

                        var point             = GeometryTranslator.FromGeometryPoint(BackOffice.Core.GeometryTranslator.Translate(node.Geometry));
                        var pointShapeContent = new PointShapeContent(point);

                        await context.RoadNodes.AddAsync(new RoadNodeRecord
                        {
                            Id = node.Id,
                            ShapeRecordContent       = pointShapeContent.ToBytes(manager, encoding),
                            ShapeRecordContentLength = pointShapeContent.ContentLength.ToInt32(),
                            DbaseRecord = dbaseRecord.ToBytes(manager, encoding),
                            BoundingBox = RoadNodeBoundingBox.From(pointShapeContent.Shape)
                        }, token);
                        break;
                    }
                }
            });
        }
        public RoadSegmentEuropeanRoadAttributeRecordProjection(RecyclableMemoryStreamManager manager,
                                                                Encoding encoding)
        {
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            When <Envelope <ImportedRoadSegment> >((context, envelope, token) =>
            {
                if (envelope.Message.PartOfEuropeanRoads.Length == 0)
                {
                    return(Task.CompletedTask);
                }

                var europeanRoadAttributes = envelope.Message
                                             .PartOfEuropeanRoads
                                             .Select(europeanRoad => new RoadSegmentEuropeanRoadAttributeRecord
                {
                    Id            = europeanRoad.AttributeId,
                    RoadSegmentId = envelope.Message.Id,
                    DbaseRecord   = new RoadSegmentEuropeanRoadAttributeDbaseRecord
                    {
                        EU_OIDN   = { Value = europeanRoad.AttributeId },
                        WS_OIDN   = { Value = envelope.Message.Id },
                        EUNUMMER  = { Value = europeanRoad.Number },
                        BEGINTIJD = { Value = europeanRoad.Origin.Since },
                        BEGINORG  = { Value = europeanRoad.Origin.OrganizationId },
                        LBLBGNORG = { Value = europeanRoad.Origin.Organization },
                    }.ToBytes(manager, encoding)
                });

                return(context.AddRangeAsync(europeanRoadAttributes, token));
            });

            When <Envelope <RoadNetworkChangesBasedOnArchiveAccepted> >(async(context, envelope, token) =>
            {
                foreach (var change in envelope.Message.Changes.Flatten())
                {
                    switch (change)
                    {
                    case RoadSegmentAddedToEuropeanRoad europeanRoad:
                        await context.RoadSegmentEuropeanRoadAttributes.AddAsync(new RoadSegmentEuropeanRoadAttributeRecord
                        {
                            Id            = europeanRoad.AttributeId,
                            RoadSegmentId = europeanRoad.SegmentId,
                            DbaseRecord   = new RoadSegmentEuropeanRoadAttributeDbaseRecord
                            {
                                EU_OIDN   = { Value = europeanRoad.AttributeId },
                                WS_OIDN   = { Value = europeanRoad.SegmentId },
                                EUNUMMER  = { Value = europeanRoad.Number },
                                BEGINTIJD = { Value = LocalDateTimeTranslator.TranslateFromWhen(envelope.Message.When) },
                                BEGINORG  = { Value = envelope.Message.OrganizationId },
                                LBLBGNORG = { Value = envelope.Message.Organization }
                            }.ToBytes(manager, encoding)
                        });
                        break;
                    }
                }
            });
        }