public Task When_importing_road_nodes() { var random = new Random(); var data = _fixture .CreateMany <ImportedRoadSegment>(random.Next(1, 10)) .Select(segment => { segment.Surfaces = _fixture .CreateMany <ImportedRoadSegmentSurfaceAttributes>(random.Next(1, 10)) .ToArray(); var expected = segment .Surfaces .Select(surface => new RoadSegmentSurfaceAttributeRecord { Id = surface.AttributeId, RoadSegmentId = segment.Id, DbaseRecord = new RoadSegmentSurfaceAttributeDbaseRecord { WV_OIDN = { Value = surface.AttributeId }, WS_OIDN = { Value = segment.Id }, WS_GIDN = { Value = segment.Id + "_" + surface.AsOfGeometryVersion }, TYPE = { Value = RoadSegmentSurfaceType.Parse(surface.Type).Translation.Identifier }, LBLTYPE = { Value = RoadSegmentSurfaceType.Parse(surface.Type).Translation.Name }, VANPOS = { Value = (double)surface.FromPosition }, TOTPOS = { Value = (double)surface.ToPosition }, BEGINTIJD = { Value = surface.Origin.Since }, BEGINORG = { Value = surface.Origin.OrganizationId }, LBLBGNORG = { Value = surface.Origin.Organization } }.ToBytes(_services.MemoryStreamManager, Encoding.UTF8) }); return(new { importedRoadSegment = segment, expected }); }).ToList(); return(new RoadRegistry.Product.Projections.RoadSegmentSurfaceAttributeRecordProjection(_services.MemoryStreamManager, Encoding.UTF8) .Scenario() .Given(data.Select(d => d.importedRoadSegment)) .Expect(data .SelectMany(d => d.expected) .Cast <object>() .ToArray() )); }
public RoadSegmentSurfaceAttributeRecordProjection(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.Surfaces.Length == 0) { return(Task.CompletedTask); } var surfaces = envelope.Message .Surfaces .Select(surface => { var typeTranslation = RoadSegmentSurfaceType.Parse(surface.Type).Translation; return(new RoadSegmentSurfaceAttributeRecord { Id = surface.AttributeId, RoadSegmentId = envelope.Message.Id, DbaseRecord = new RoadSegmentSurfaceAttributeDbaseRecord { WV_OIDN = { Value = surface.AttributeId }, WS_OIDN = { Value = envelope.Message.Id }, WS_GIDN = { Value = $"{envelope.Message.Id}_{surface.AsOfGeometryVersion}" }, TYPE = { Value = typeTranslation.Identifier }, LBLTYPE = { Value = typeTranslation.Name }, VANPOS = { Value = (double)surface.FromPosition }, TOTPOS = { Value = (double)surface.ToPosition }, BEGINTIJD = { Value = surface.Origin.Since }, BEGINORG = { Value = surface.Origin.OrganizationId }, LBLBGNORG = { Value = surface.Origin.Organization }, }.ToBytes(manager, encoding) }); }); return(context.RoadSegmentSurfaceAttributes.AddRangeAsync(surfaces, token)); }); When <Envelope <RoadNetworkChangesBasedOnArchiveAccepted> >(async(context, envelope, token) => { foreach (var change in envelope.Message.Changes.Flatten()) { switch (change) { case RoadSegmentAdded segment: if (segment.Surfaces.Length != 0) { var surfaces = segment .Surfaces .Select(surface => { var typeTranslation = RoadSegmentSurfaceType.Parse(surface.Type).Translation; return(new RoadSegmentSurfaceAttributeRecord { Id = surface.AttributeId, RoadSegmentId = segment.Id, DbaseRecord = new RoadSegmentSurfaceAttributeDbaseRecord { WV_OIDN = { Value = surface.AttributeId }, WS_OIDN = { Value = segment.Id }, WS_GIDN = { Value = $"{segment.Id}_{surface.AsOfGeometryVersion}" }, TYPE = { Value = typeTranslation.Identifier }, LBLTYPE = { Value = typeTranslation.Name }, VANPOS = { Value = (double)surface.FromPosition }, TOTPOS = { Value = (double)surface.ToPosition }, BEGINTIJD = { Value = LocalDateTimeTranslator.TranslateFromWhen(envelope.Message.When) }, BEGINORG = { Value = envelope.Message.OrganizationId }, LBLBGNORG = { Value = envelope.Message.Organization } }.ToBytes(manager, encoding) }); }); await context.RoadSegmentSurfaceAttributes.AddRangeAsync(surfaces); } // case RoadSegmentModified segment: // if (segment.Surfaces.Length == 0) // { // context.RoadSegmentSurfaceAttributes.RemoveRange( // context // .RoadSegmentSurfaceAttributes // .Local.Where(a => a.RoadSegmentId == segment.Id) // .Concat(await context // .RoadSegmentSurfaceAttributes // .Where(a => a.RoadSegmentId == segment.Id) // .ToArrayAsync(token) // )); // } // else // { // var currentSet = context // .RoadSegmentSurfaceAttributes // .Local.Where(a => a.RoadSegmentId == segment.Id) // .Concat(await context // .RoadSegmentSurfaceAttributes // .Where(a => a.RoadSegmentId == segment.Id) // .ToArrayAsync(token) // ).ToDictionary(a => a.Id); // var nextSet = segment // .Surfaces // .Select(surface => // { // var typeTranslation = RoadSegmentSurfaceType.Parse(surface.Type).Translation; // return new RoadSegmentSurfaceAttributeRecord // { // Id = surface.AttributeId, // RoadSegmentId = segment.Id, // DbaseRecord = new RoadSegmentSurfaceAttributeDbaseRecord // { // WV_OIDN = {Value = surface.AttributeId}, // WS_OIDN = {Value = segment.Id}, // WS_GIDN = {Value = $"{segment.Id}_{surface.AsOfGeometryVersion}"}, // TYPE = {Value = typeTranslation.Identifier}, // LBLTYPE = {Value = typeTranslation.Name}, // VANPOS = {Value = (double) surface.FromPosition}, // TOTPOS = {Value = (double) surface.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.RoadSegmentSurfaceAttributes.Synchronize(currentSet, nextSet, (current, next) => // { // current.DbaseRecord = next.DbaseRecord; // }); // } break; } } }); }
private AddRoadSegment Translate(Messages.AddRoadSegment command, IRequestedChangeIdentityTranslator translator) { var permanent = _nextRoadSegmentId(); var temporary = new RoadSegmentId(command.TemporaryId); var startNodeId = new RoadNodeId(command.StartNodeId); RoadNodeId?temporaryStartNodeId; if (translator.TryTranslateToPermanent(startNodeId, out var permanentStartNodeId)) { temporaryStartNodeId = startNodeId; startNodeId = permanentStartNodeId; } else { temporaryStartNodeId = null; } var endNodeId = new RoadNodeId(command.EndNodeId); RoadNodeId?temporaryEndNodeId; if (translator.TryTranslateToPermanent(endNodeId, out var permanentEndNodeId)) { temporaryEndNodeId = endNodeId; endNodeId = permanentEndNodeId; } else { temporaryEndNodeId = null; } var geometry = GeometryTranslator.Translate(command.Geometry); var maintainer = new OrganizationId(command.MaintenanceAuthority); var geometryDrawMethod = RoadSegmentGeometryDrawMethod.Parse(command.GeometryDrawMethod); var morphology = RoadSegmentMorphology.Parse(command.Morphology); var status = RoadSegmentStatus.Parse(command.Status); var category = RoadSegmentCategory.Parse(command.Category); var accessRestriction = RoadSegmentAccessRestriction.Parse(command.AccessRestriction); var leftSideStreetNameId = command.LeftSideStreetNameId.HasValue ? new CrabStreetnameId(command.LeftSideStreetNameId.Value) : new CrabStreetnameId?(); var rightSideStreetNameId = command.RightSideStreetNameId.HasValue ? new CrabStreetnameId(command.RightSideStreetNameId.Value) : new CrabStreetnameId?(); var nextLaneAttributeId = _nextRoadSegmentLaneAttributeId(permanent); var laneAttributes = Array.ConvertAll( command.Lanes, item => new RoadSegmentLaneAttribute( nextLaneAttributeId(), new AttributeId(item.AttributeId), new RoadSegmentLaneCount(item.Count), RoadSegmentLaneDirection.Parse(item.Direction), new RoadSegmentPosition(item.FromPosition), new RoadSegmentPosition(item.ToPosition), new GeometryVersion(0) ) ); var nextWidthAttributeId = _nextRoadSegmentWidthAttributeId(permanent); var widthAttributes = Array.ConvertAll( command.Widths, item => new RoadSegmentWidthAttribute( nextWidthAttributeId(), new AttributeId(item.AttributeId), new RoadSegmentWidth(item.Width), new RoadSegmentPosition(item.FromPosition), new RoadSegmentPosition(item.ToPosition), new GeometryVersion(0) ) ); var nextSurfaceAttributeId = _nextRoadSegmentSurfaceAttributeId(permanent); var surfaceAttributes = Array.ConvertAll( command.Surfaces, item => new RoadSegmentSurfaceAttribute( nextSurfaceAttributeId(), new AttributeId(item.AttributeId), RoadSegmentSurfaceType.Parse(item.Type), new RoadSegmentPosition(item.FromPosition), new RoadSegmentPosition(item.ToPosition), new GeometryVersion(0) ) ); return(new AddRoadSegment ( permanent, temporary, startNodeId, temporaryStartNodeId, endNodeId, temporaryEndNodeId, geometry, maintainer, geometryDrawMethod, morphology, status, category, accessRestriction, leftSideStreetNameId, rightSideStreetNameId, laneAttributes, widthAttributes, surfaceAttributes )); }