// Copyright (C) Stichting Deltares 2023. All rights reserved. // // This file is part of the application DAM - Live. // // DAM - Live is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Globalization; using System.Xml; using Deltares.Standard.Extensions; namespace Deltares.Dam.Data.IO; /// public static class BackwardCompatibility { /// Updates the specified dam project data for backward compatibility. /// The dam project data. /// Name of the project file. /// public static void Update(DamProjectData damProjectData, string projectFileName, int fileVersion) { var xmlDocument = new XmlDocument(); xmlDocument.Load(projectFileName); XmlNodeList locationNodes; locationNodes = xmlDocument.GetElementsByTagName("Subject"); var isDamLiveProject = false; if (locationNodes.Count == 0) { isDamLiveProject = true; locationNodes = xmlDocument.GetElementsByTagName("Location"); } foreach (XmlNode node in locationNodes) { bool isCorrectLocation = isDamLiveProject; if (!isCorrectLocation) { XmlAttribute attributeType = node.Attributes?["type"]; isCorrectLocation = (attributeType != null) && (attributeType.Value == "Deltares.Dam.Data.Location"); } if (isCorrectLocation) { XmlAttribute attributeName = node.Attributes?["Name"]; Location location = damProjectData.WaterBoard.Dikes[0].Locations.Find(item => item.Name.Equals(attributeName.Value)); // Older DamLive projects do not have scenarios defined, so add them if needed AddScenarioIfNoScenariosDefined(location); if (fileVersion <= 1) { UpdatePolderLevel(location, AttributeDoubleValueFromNode(node, "PolderLevel")); UpdateHeadPl2(location, AttributeDoubleValueFromNode(node, "HeadPL2")); UpdateZoneType(location, AttributeStringValueFromNode(node, "StabilityZoneType")); } if (fileVersion == 0) { UpdateHeadPl3(location, AttributeDoubleValueFromNode(node, "HeadPl3")); UpdateHeadPl4(location, AttributeDoubleValueFromNode(node, "HeadPl4")); } } } } private static void AddScenarioIfNoScenariosDefined(Location location) { if (location.Scenarios.Count == 0) { location.Scenarios.Add(new Scenario { RiverLevel = 0.0, RiverLevelLow = 0.0 }); } } private static double? AttributeDoubleValueFromNode(XmlNode node, string attributeName) { double? value = null; XmlAttribute attribute = node.Attributes?[attributeName]; if ((attribute != null) && !string.IsNullOrEmpty(attribute.Value)) { value = Double.Parse(attribute.Value, CultureInfo.InvariantCulture); } return value; } private static string AttributeStringValueFromNode(XmlNode node, string attributeName) { XmlAttribute attribute = node.Attributes?[attributeName]; return attribute?.Value; } private static void UpdatePolderLevel(Location location, double? value) { if (value != null) { foreach (Scenario scenario in location.Scenarios) { scenario.PolderLevel = value.Value; } } } private static void UpdateHeadPl2(Location location, double? value) { foreach (Scenario scenario in location.Scenarios) { scenario.HeadPl2 = value; } } private static void UpdateHeadPl3(Location location, double? value) { foreach (Scenario scenario in location.Scenarios) { scenario.HeadPl3 = value; } } private static void UpdateHeadPl4(Location location, double? value) { foreach (Scenario scenario in location.Scenarios) { scenario.HeadPl4 = value; } } private static void UpdateZoneType(Location location, string value) { if (!string.IsNullOrWhiteSpace(value)) { if (value.Equals("ZoneAreas")) { location.StabilityZoneType = MStabZonesType.NoZones; } else { location.StabilityZoneType = (MStabZonesType) Enum.Parse(typeof(MStabZonesType), value); } } } }