LotSizingDataModel.Core 2.1.0
Core domain model, shared abstractions and XML-serializable entities.
Loading...
Searching...
No Matches
PhysicalFleetXmlMigration.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using System.Text.Json;
4using System.Xml.Linq;
5
7
8public static class PhysicalFleetXmlMigration
9{
10 private const string LegacyTarget = "lotsizing-transport-fleet";
11
12 /// <summary>Promotes optional legacy generator fleet data without writing to the source file.</summary>
13 public static void Migrate(XElement root)
14 {
15 foreach (var resource in root.Descendants().Where(element =>
16 element.Name.LocalName == "transportResource" && element.Parent?.Name.LocalName == "transportResources"))
17 {
18 var metadata = resource.Nodes().OfType<XProcessingInstruction>()
19 .Where(node => node.Target == LegacyTarget).ToArray();
20 var ns = resource.Name.Namespace;
21 if (metadata.Length + resource.Elements(ns + "physicalFleet").Take(2).Count() > 1)
22 throw new InvalidOperationException("Plusieurs définitions de la flotte physique sont présentes pour le même moyen.");
23 if (metadata.Length == 0)
24 continue;
25
26 XElement fleet;
27 try
28 {
29 using var document = JsonDocument.Parse(metadata[0].Data);
30 var data = document.RootElement;
31 fleet = new XElement(ns + "physicalFleet",
32 new XAttribute("vehicleCount", data.GetProperty("VehicleCount").GetInt32()),
33 new XAttribute("capacityPerVehicle", data.GetProperty("CapacityPerVehicle").GetDecimal()),
34 new XElement(ns + "initialAllocation", data.GetProperty("InitialAllocation").EnumerateArray().Select(row =>
35 new XElement(ns + "allocation", new XAttribute("count", row.GetProperty("Count").GetInt32()),
36 Reference(row.GetProperty("Warehouse"), ns + "warehouse")))),
37 new XElement(ns + "emptyMovements", data.GetProperty("EmptyMovements").EnumerateArray().Select(row =>
38 new XElement(ns + "movement", new XAttribute("duration", row.GetProperty("Duration").GetInt32()),
39 Reference(row.GetProperty("Origin"), ns + "origin"),
40 Reference(row.GetProperty("Destination"), ns + "destination")))));
41 }
42 catch (Exception error) when (error is JsonException or FormatException or InvalidOperationException
43 or System.Collections.Generic.KeyNotFoundException)
44 {
45 throw new InvalidOperationException("Les anciennes données de flotte sont illisibles.", error);
46 }
47 metadata[0].ReplaceWith(fleet);
48 }
49 }
50
51 private static XElement Reference(JsonElement value, XName name)
52 {
53 // Legacy generator SiteKind values are distinct from the official warehouse kind enum.
54 var kind = value.GetProperty("Kind").GetInt32() switch
55 {
56 2 => "plantWarehouse",
57 3 => "standaloneWarehouse",
58 _ => throw new InvalidOperationException("Type d’entrepôt inconnu dans l’ancienne flotte.")
59 };
60 return new XElement(name, new XAttribute("kind", kind), new XAttribute("id", value.GetProperty("Id").GetInt32()));
61 }
62}
static void Migrate(XElement root)
Promotes optional legacy generator fleet data without writing to the source file.