diff --git a/EDILibrary/EDIHelper.cs b/EDILibrary/EDIHelper.cs index 8e35adb..96c999e 100644 --- a/EDILibrary/EDIHelper.cs +++ b/EDILibrary/EDIHelper.cs @@ -317,13 +317,19 @@ public static EDIFileInfo GetEdiFileInfo(string edi) unbParts[3].Split(specialChars.ElementDelimiter.ToCharArray()).Length > 1 ? unbParts[3].Split(specialChars.ElementDelimiter.ToCharArray())[1] : "500", ID = unbParts[3].Split(specialChars.ElementDelimiter.ToCharArray())[0] }; + var version = unhParts[2].Split(specialChars.ElementDelimiter.ToCharArray())[4]; + var format = Enum.Parse(unhParts[2].Split(specialChars.ElementDelimiter.ToCharArray())[0]); + if (format == EdifactFormat.UTILMD) + { + format = version.StartsWith("G") ? EdifactFormat.UTILMDG : version.StartsWith("S") ? EdifactFormat.UTILMDS : format; + } var file = new EDIFileInfo { Empfänger = receiver, Sender = sender, ID = unbParts[5].Split(specialChars.ElementDelimiter.ToCharArray())[0], - Format = Enum.Parse(unhParts[2].Split(specialChars.ElementDelimiter.ToCharArray())[0]), - Version = unhParts[2].Split(specialChars.ElementDelimiter.ToCharArray())[4], + Format = format, + Version = version, Freigabenummer = unhParts[2].Split(specialChars.ElementDelimiter.ToCharArray())[2], Nachrichtenversion = unhParts[2].Split(specialChars.ElementDelimiter.ToCharArray())[1] }; diff --git a/EDILibrary/EdifactFormatVersion.cs b/EDILibrary/EdifactFormatVersion.cs index 589a250..af798e2 100644 --- a/EDILibrary/EdifactFormatVersion.cs +++ b/EDILibrary/EdifactFormatVersion.cs @@ -81,6 +81,16 @@ public enum EdifactFormat /// UTILMD = 11, + /// + /// master data gas + /// + UTILMDG = 44, + + /// + /// master data electricity/ strom + /// + UTILMDS = 55, + /// /// Netznutzungszeiten-Nachricht /// @@ -175,7 +185,7 @@ public enum EdifactFormatVersion /// FV2304, /// - /// Format Version October 2023 (ako MaKo2023) + /// Format Version October 2023 (aka MaKo2023) /// FV2310 } @@ -406,6 +416,16 @@ public EdifactFormatVersion GetFormatVersion(EdifactFormat format, string versio "5.2c" => EdifactFormatVersion.FV2110, _ => GetCurrent() }, + EdifactFormat.UTILMDG => version switch + { + "G1.0a" => EdifactFormatVersion.FV2310, + _ => GetCurrent() + }, + EdifactFormat.UTILMDS => version switch + { + "S1.1" => EdifactFormatVersion.FV2310, + _ => GetCurrent() + }, EdifactFormat.MSCONS => version switch { "2.4a" => EdifactFormatVersion.FV2210, @@ -511,3 +531,4 @@ public EdifactFormatVersion GetFormatVersion(EdifactFormat format, string versio } } } + diff --git a/EDILibrary/TemplateHelper.cs b/EDILibrary/TemplateHelper.cs index baceb45..3929d29 100644 --- a/EDILibrary/TemplateHelper.cs +++ b/EDILibrary/TemplateHelper.cs @@ -19,7 +19,10 @@ protected static void ParseAPERAKString(string aperak, out string dataType, out list = null; length = 0; if (aperak == "N") + { return; + } + dataType = aperak.Substring(1, 2); //data type var maxLength = aperak.Length; var valueIndex = aperak.IndexOf('{'); @@ -52,49 +55,101 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) var newField = new JObject(); var name = elem.Attribute("name").Value; if (elem.Attribute("ahbName") != null) + { name = elem.Attribute("ahbName").Value; + } + newField.Add(elem.Attribute("name").Value, name); // newField.Add("key", name); var meta = new JObject(); if (elem.Attribute("meta.id") != null) + { meta.Add("id", elem.Attribute("meta.id").Value); + } + if (elem.Attribute("meta.help") != null) + { meta.Add("help", elem.Attribute("meta.help").Value); + } + if (elem.Attribute("meta.format") != null) + { meta.Add("format", elem.Attribute("meta.format").Value); + } + if (elem.Attribute("meta.type") != null) + { meta.Add("type", elem.Attribute("meta.type").Value); + } + if (elem.Attribute("meta.typeInfo") != null) + { meta.Add("typeInfo", elem.Attribute("meta.typeInfo").Value); + } + if (elem.Attribute("meta.orderId") != null) + { meta.Add("orderId", elem.Attribute("meta.orderId").Value); + } + if (elem.Attribute("meta.suppressCopy") != null) + { meta.Add("suppressCopy", elem.Attribute("meta.suppressCopy").Value); + } + if (elem.Attribute("meta.virtual") != null) + { meta.Add("virtual", elem.Attribute("meta.virtual").Value); + } + if (elem.Attribute("meta.virtualKey") != null) + { meta.Add("virtualKey", elem.Attribute("meta.virtualKey").Value); + } + if (elem.Attribute("meta.ignoreNull") != null) + { meta.Add("ignoreNull", elem.Attribute("meta.ignoreNull").Value); + } + if (elem.Attribute("meta.migMatch") != null) + { meta.Add("migMatch", elem.Attribute("meta.migMatch").Value); + } + if (elem.Attribute("meta.forceNameMatch") != null) + { meta.Add("forceNameMatch", elem.Attribute("meta.forceNameMatch").Value); + } + if (elem.Attribute("meta.ref") != null) + { meta.Add("ref", elem.Attribute("meta.ref").Value); + } + if (elem.Attribute("meta.objType") != null) + { meta.Add("objType", elem.Attribute("meta.objType").Value); + } + if (elem.Attribute("meta.sg") != null) + { meta.Add("sg", elem.Attribute("meta.sg").Value); + } //check for parent sg else if (cur.Attribute("ref") != null) + { meta.Add("sg", cur.Attribute("ref").Value); + } //add length, type? and list if (elem.Attribute("ref") != null) { var refKey = elem.Attribute("ref").Value; if (refKey.Contains("[")) //special case selection [blub=bla] + { refKey = refKey.Split('[').FirstOrDefault(); + } + if (refKey.Contains("(")) //special case multi-select (0,4) { var keyParts = refKey.Split('('); @@ -110,24 +165,35 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) { string[] elements; if (subElem.APERAK_Check_String.Contains("|")) + { elements = subElem.CONTRL_Check_String.Split('+'); + } else + { elements = subElem.APERAK_Check_String.Split('+'); + } if (elements.Length >= int.Parse(refKeys[1])) + { try { var key = elements[int.Parse(refKeys[1]) - 1].Split('*')[int.Parse(refKeys[2]) + 1]; if (key.Contains("|")) + { key = key.Split('|').First(); + } ParseAPERAKString(key, out var dataType, out var length, out var list); if (meta.Property("typeInfo") == null) + { meta.Add("typeInfo", dataType); + } meta.Add("length", length); if (list != null) + { meta.Add("list", JArray.FromObject(list)); + } } catch (IndexOutOfRangeException) { @@ -138,6 +204,7 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) // errors must not pass silently Console.Out.WriteLineAsync($"Error while processing: {e}"); } + } } } else if (refKey.StartsWith("SG")) @@ -147,9 +214,15 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) } if (meta.Properties().Any()) + { newField.Add("_meta", meta); + } + if (elem.Attribute("ahbName") != null) + { newField.Add("_ahbName", elem.Attribute("ahbName").Value); + } + if (elem.Attribute("groupBy") != null) { var groupName = elem.Attribute("groupBy").Value; @@ -158,8 +231,9 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) if (subGroup.Any()) { if (elem.Attribute("groupKey")?.Value != null) + { (subGroup.First() as JObject).Property("_meta").Value.Value().Add("key", name); - (subGroup.First() as JObject).Property("requires").Value.Value().Add(newField); + } (subGroup.First() as JObject).Property("requires").Value.Value().Add(newField); } else { @@ -174,9 +248,15 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) {"max", "1"} }; if (elem.Attribute("groupKey")?.Value != null) + { subMeta.Add("key", name); + } + if (elem.Attribute("groupCounterKey")?.Value != null) + { subMeta.Add("counterKey", name); + } + subObj.Add("_meta", subMeta); subObj.Add("requires", subArray); subArray.Add(newField); @@ -204,33 +284,67 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) var newChild = new JObject(); var name = elem.Attribute("name").Value; if (elem.Attribute("ahbName") != null) + { name = elem.Attribute("ahbName").Value; + } + newChild.Add("key", name); var meta = new JObject(); if (elem.Attribute("meta.id") != null) + { meta.Add("id", elem.Attribute("meta.id").Value); + } + if (elem.Attribute("meta.help") != null) + { meta.Add("help", elem.Attribute("meta.help").Value); + } + if (elem.Attribute("meta.format") != null) + { meta.Add("format", elem.Attribute("meta.format").Value); + } meta.Add("type", elem.Attribute("meta.type") != null ? elem.Attribute("meta.type").Value : "group"); if (elem.Attribute("meta.objType") != null) + { meta.Add("objType", elem.Attribute("meta.objType").Value); + } + if (elem.Attribute("meta.typeInfo") != null) + { meta.Add("typeInfo", elem.Attribute("meta.typeInfo").Value); + } + if (elem.Attribute("meta.suppressCopy") != null) + { meta.Add("suppressCopy", elem.Attribute("meta.suppressCopy").Value); + } + if (elem.Attribute("meta.virtual") != null) + { meta.Add("virtual", elem.Attribute("meta.virtual").Value); + } + if (elem.Attribute("meta.virtualKey") != null) + { meta.Add("virtualKey", elem.Attribute("meta.virtualKey").Value); + } + if (elem.Attribute("meta.ref") != null) + { meta.Add("ref", elem.Attribute("meta.ref").Value); + } + if (elem.Attribute("meta.ignore") != null) + { meta.Add("ignore", elem.Attribute("meta.ignore").Value); + } + if (elem.Attribute("meta.changeTrigger") != null) + { meta.Add("changeTrigger", elem.Attribute("meta.changeTrigger").Value); + } meta.Add("max", elem.Attribute("max") != null ? elem.Attribute("max").Value : "1"); //find key element in fields @@ -243,9 +357,13 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) if (keyField != null) { if (keyField.Attribute("ahbName") != null) + { meta.Add("groupKey", keyField.Attribute("ahbName")?.Value); + } else + { meta.Add("groupKey", keyField.Attribute("name")?.Value); + } } else if (elem.Attribute("groupKey") != null) { @@ -258,13 +376,23 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) } if (elem.Attribute("groupCounterKey") != null) + { meta.Add("counterKey", elem.Attribute("groupCounterKey")?.Value); + } + if (elem.Attribute("meta.sg") != null) + { meta.Add("sg", elem.Attribute("meta.sg").Value); + } else if (elem.Attribute("ref") != null) + { meta.Add("sg", elem.Attribute("ref")?.Value); + } + if (meta.Properties().Any()) + { newChild.Add("_meta", meta); + } var refKey = elem.Attribute("ref").Value; var refKeys = refKey.Split(':'); @@ -280,11 +408,15 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree) { newTree = treeUp.Parent.FindElement(refKeys.First()); if (newTree == null) + { treeUp = treeUp.Parent; + } } if (newTree != null) + { tree = newTree; + } } var requires = new JArray(); @@ -307,7 +439,10 @@ public string ConvertFilesToJSON(string inputFileName, string outputFileName, st var srcXml = XElement.Parse(File.ReadAllText(inputFileName)); TreeElement tree = null; if (!string.IsNullOrEmpty(treeFileName)) + { tree = new GenericEDILoader().LoadTree(File.ReadAllText(treeFileName)); + } + var tmp = new JArray(); Recurse(srcXml, tmp, tree); //retriev version information from inputFileName diff --git a/EDILibraryTests/EDIHelperTests.cs b/EDILibraryTests/EDIHelperTests.cs index 1b34e60..82f7558 100644 --- a/EDILibraryTests/EDIHelperTests.cs +++ b/EDILibraryTests/EDIHelperTests.cs @@ -70,6 +70,9 @@ public void TestNormalizeEdiHeader(string input, string expectedResult) [DataRow( "UNA:+.?'UNB+UNOC:3+123456789012345:500+123456789:500+210326:1553+WIM00000000901'UNH+WIM00000000901+UTILMD:D:11A:UN:5.2b'BGM+E03+WIM00000000901'DTM+137:202103261553:203'NAD+MS+123456789012345::293'CTA+IC+:Max Mustermann'COM+max@mustermann.de:EM'NAD+MR+123456789::293'IDE+24+WIMP0000000459'DTM+92:20210401:102'DTM+157:20210401:102'STS+7++ZE8'LOC+172+41234567896'RFF+Z13:11116'SEQ+Z01'CCI+Z30++Z06'", "{\"Format\":null}")] // wegen "UNA:+.?" ohne space vor "?" + [DataRow( + "UNA:+.? 'UNB+UNOC:3+123456789012345:500+123456789:500+210326:1553+WIM00000000901'UNH+WIM00000000901+UTILMD:D:11A:UN:S1.1'BGM+E03+WIM00000000901'DTM+137:202103261553:203'NAD+MS+123456789012345::293'CTA+IC+:Max Mustermann'COM+max@mustermann.de:EM'NAD+MR+123456789::293'IDE+24+WIMP0000000459'DTM+92:20210401:102'DTM+157:20210401:102'STS+7++ZE8'LOC+172+41234567896'RFF+Z13:11116'SEQ+Z01'CCI+Z30++Z06'", + "{\"Format\":\"UTILMDS\", \"Nachrichtenversion\":\"D\",\"Version\":\"S1.1\", \"Sender\":{\"CodeList\":\"500\",\"ID\":\"123456789012345\"}, \"Freigabenummer\":\"11A\", \"Empfänger\":{\"CodeList\":\"500\",\"ID\":\"123456789\"}, \"ID\":\"WIM00000000901\"}")] public void TestGetEdiFileInfo(string input, string expectedOutput) { diff --git a/EDILibraryTests/EdifactFormatVersionTests.cs b/EDILibraryTests/EdifactFormatVersionTests.cs index ffd60e1..db46d96 100644 --- a/EDILibraryTests/EdifactFormatVersionTests.cs +++ b/EDILibraryTests/EdifactFormatVersionTests.cs @@ -25,6 +25,8 @@ public class EdifactFormatVersionTests [DataRow("35002", EdifactFormat.REQOTE)] [DataRow("33001", EdifactFormat.REMADV)] [DataRow("11042", EdifactFormat.UTILMD)] + [DataRow("44001", EdifactFormat.UTILMDG)] + [DataRow("55001", EdifactFormat.UTILMDS)] public void TestPruefiToFormat(string pruefi, EdifactFormat expectedFormat) { var actualFormat = EdifactFormatHelper.FromPruefidentifikator(pruefi);