Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split repeated texts to separate table #26

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions KdyPojedeVlak.Web/Engine/DbStorage/DbModelContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
Expand All @@ -12,13 +13,12 @@
using KdyPojedeVlak.Web.Engine.SR70;
using KdyPojedeVlak.Web.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;

namespace KdyPojedeVlak.Web.Engine.DbStorage;

using static DbModelUtils;

public class DbModelContext : DbContext
public class DbModelContext(DbContextOptions<DbModelContext> options) : DbContext(options)
{
public DbSet<ImportedFile> ImportedFiles { get; set; }
public DbSet<TimetableYear> TimetableYears { get; set; }
Expand All @@ -29,11 +29,7 @@ public class DbModelContext : DbContext
public DbSet<CalendarDefinition> CalendarDefinitions { get; set; }
public DbSet<NeighboringPoints> NeighboringPointTuples { get; set; }
public DbSet<TrainCancellation> TrainCancellations { get; set; }

public DbModelContext(DbContextOptions<DbModelContext> options)
: base(options)
{
}
public DbSet<Text> Texts { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand Down Expand Up @@ -81,11 +77,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<TrainCancellation>()
.HasIndex(o => o.TimetableVariantId);

modelBuilder.Entity<Text>()
.HasIndex(o => o.Str).IsUnique();
}

public HashSet<RoutingPoint> GetNeighboringPoints(RoutingPoint point)
{
if (point == null) throw new ArgumentNullException(nameof(point));
ArgumentNullException.ThrowIfNull(point);

var pointId = point.Id;
var neighbors = NeighboringPointTuples
Expand Down Expand Up @@ -451,7 +450,7 @@ public class CentralPttNoteForVariant : PttNoteForVariant
public class NonCentralPttNoteForVariant : PttNoteForVariant
{
[Required]
public string Text { get; set; }
public Text Text { get; set; }

[Required]
public HeaderDisplay ShowInHeader { get; set; }
Expand All @@ -465,6 +464,8 @@ public class NonCentralPttNoteForVariant : PttNoteForVariant

public class NetworkSpecificParameterForPassage
{
public static readonly FrozenSet<NetworkSpecificParameterPassage> IndirectTypes = new[] { NetworkSpecificParameterPassage.CZPassengerPublicTransportOrderingCoName }.ToFrozenSet();

public int Id { get; set; }

[Required]
Expand All @@ -476,8 +477,15 @@ public class NetworkSpecificParameterForPassage
[Required]
public NetworkSpecificParameterPassage Type { get; set; }

[NotMapped]
[Required]
public string Value { get; set; }
public string Value => ValueDirect ?? ValueIndirect?.Str;

[Column("Value")]
public string ValueDirect { get; set; }

[ForeignKey("ValueRef")]
public Text ValueIndirect { get; set; }
}

public class TrainCancellation
Expand All @@ -504,6 +512,24 @@ public class TrainCancellation
public ImportedFile ImportedFrom { get; set; }
}

public class Text
{
public int Id { get; set; }

[Required]
public string Str { get; set; }

public static Text FindOrCreate(DbModelContext context, string str)
{
var existing = context.Texts.SingleOrDefault(t => t.Str == str);
if (existing != null) return existing;

var newText = new Text { Str = str };
context.Add(newText);
return newText;
}
}

public static class DbModelUtils
{
public static List<TEnum> ParseEnumList<TEnum>(string data)
Expand Down
30 changes: 20 additions & 10 deletions KdyPojedeVlak.Web/Engine/Djr/DjrSchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,23 @@ private static CZPTTCISMessageBase LoadXmlFile(Stream stream)
{
foreach (var param in networkSpecificParametersForPassage)
{
dbModelContext.Add(
new NetworkSpecificParameterForPassage
{
Passage = passage,
Type = Enum.Parse<NetworkSpecificParameterPassage>(param.Key),
Value = String.Join(';', param.Value)
});
var type = Enum.Parse<NetworkSpecificParameterPassage>(param.Key);
var paramValue = String.Join(';', param.Value);
var dbParam = new NetworkSpecificParameterForPassage
{
Passage = passage,
Type = type
};

if (NetworkSpecificParameterForPassage.IndirectTypes.Contains(type))
{
dbParam.ValueIndirect = Text.FindOrCreate(dbModelContext, paramValue);
}
else
{
dbParam.ValueDirect = paramValue;
}
dbModelContext.Add(dbParam);
}
}
}
Expand Down Expand Up @@ -519,7 +529,7 @@ private static CZPTTCISMessageBase LoadXmlFile(Stream stream)
}
if (networkSpecificParameters.TryGetValue(NetworkSpecificParameterGlobal.CZNonCentralPTTNote.ToString(), out var nonCentralPttNoteDefinitions))
{
dbModelContext.AddRange(nonCentralPttNoteDefinitions.Select(def => ParseNonCentralPttNoteDefinition(def, passages, pttNoteCalendars, trainTimetableVariant)));
dbModelContext.AddRange(nonCentralPttNoteDefinitions.Select(def => ParseNonCentralPttNoteDefinition(dbModelContext, def, passages, pttNoteCalendars, trainTimetableVariant)));
}
dbModelContext.SaveChanges();
}
Expand Down Expand Up @@ -697,7 +707,7 @@ private static CentralPttNoteForVariant ParseCentralPttNoteDefinition(string def
};
}

private static NonCentralPttNoteForVariant ParseNonCentralPttNoteDefinition(string definition, Dictionary<string, List<Passage>> passages, IDictionary<string, CalendarDefinition>? calendarDefinitions, TrainTimetableVariant trainTimetableVariant)
private static NonCentralPttNoteForVariant ParseNonCentralPttNoteDefinition(DbModelContext dbModelContext, string definition, Dictionary<string, List<Passage>> passages, IDictionary<string, CalendarDefinition>? calendarDefinitions, TrainTimetableVariant trainTimetableVariant)
{
var pieces = definition.Split('|');

Expand All @@ -717,7 +727,7 @@ private static NonCentralPttNoteForVariant ParseNonCentralPttNoteDefinition(stri
return new NonCentralPttNoteForVariant
{
TrainTimetableVariant = trainTimetableVariant,
Text = pieces[4],
Text = Text.FindOrCreate(dbModelContext, pieces[4]),
From = from,
To = to,
ShowInHeader = defShowInHeader[pieces[5]],
Expand Down
Loading
Loading