using BS.Shared.DataContracts; using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace BeWo.Data.ICF { public static class ICFDAO { private static XDocument _File; private static readonly XElement _Root; private static readonly List _RootList; static ICFDAO() { try { string file = ConfigurationManager.AppSettings.Get("ICFFile"); if (String.IsNullOrEmpty(file) || !File.Exists(BS.Shared.Core.Utils.CreateSavePath(AppDomain.CurrentDomain.BaseDirectory, @"bin\ICF\" + file))) { file = "icf.xml"; } var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; file = BS.Shared.Core.Utils.CreateSavePath(AppDomain.CurrentDomain.BaseDirectory, @"bin\ICF\" + file); //file = @"D:\Projects\beyondSoft\BeWoPlaner\BeWo\Data\ICF\icf.xml"; _File = XDocument.Load(file); Debug.WriteLine("Dokument geladen"); _Root = _File.Descendants("Class").Where(x => x.Attribute("code")?.Value == "d" /*|| x.Attribute("code")?.Value == "s" || x.Attribute("code")?.Value == "d" || x.Attribute("code")?.Value == "e"*/).First(); // Entscheidet, welche Kapitel rein sollen string[] codes = new string[] { "b", "s", "d", "e" }; //string[] codes = new string[] { "b", "d" }; _RootList = new List(); foreach (var c in codes) { _RootList.Add(_File.Descendants("Class").Where(x => x.Attribute("code")?.Value == c).First()); } } catch (Exception e) { //throw Utils.CreateBeWoFaultException(e); } } public static IList GetIcf() { if (_Root == null) return null; long id = 1; var returnvalue = GetChildren(_Root, 0, ref id); return returnvalue; } public static List GetWholeIcf() { //var testList = new List>(); //testList.Add(new List); //testList.Add(new List); //testList.Add(GetIcf().ToList()); //testList.Add(new List); //return testList; if (_RootList == null) return null; long id = 1; List> returnvalueList = new List>(); foreach (var item in _RootList) { returnvalueList.Add(GetChildren(item, 0, ref id).ToList()); } // Alles in eine Liste und übergeordnete Kapitel einfügen List listEntries = new List(); var counter = 0; foreach (var list in returnvalueList) { if (listEntries.Count == 0) // Im ersten Durchlauf ICF und 4 Klassifikationen einsetzen { listEntries.Add(list[0]); listEntries = AddKlassifikationen(listEntries); } if (listEntries.Count > 0) { foreach (var item in list) { if (item != null) { int? parentOid = null; switch (counter) { case 0: parentOid = -2; break; case 1: parentOid = -3; break; case 2: parentOid = -4; break; case 3: parentOid = -5; break; } if (item.DisplayName != "ICF") { if (item.ParentOid == -999) item.ParentOid = (int)parentOid; listEntries.Add(item); } } } counter++; } } return listEntries; } public static List AddKlassifikationen(List listEntries) { listEntries.Add(new ValueListEntryDC() { TypeDescription = "Klassifikation der Körperfunktionen (Komponente b)", ParentOid = -999, ValueListEntryOid = -2 }); listEntries.Add(new ValueListEntryDC() { TypeDescription = "Klassifikation der Körperstrukturen (Komponente s)", ParentOid = -999, ValueListEntryOid = -3 }); listEntries.Add(new ValueListEntryDC() { TypeDescription = "Klassifikation der Aktivitäten und Partizipationen (Komponente d)", ParentOid = -999, ValueListEntryOid = -4 }); listEntries.Add(new ValueListEntryDC() { TypeDescription = "Klassifikation der Umweltfaktoren (Komponente e)", ParentOid = -999, ValueListEntryOid = -5 }); return listEntries; } //var chapters = root.Descendants("SubClass"); public static IList GetChildren(XElement xElement, long? parentId, ref long id) { var icfs = new List(); var actualEntry = GenerateValueListEntryDC(xElement, parentId, ref id); icfs.Add(actualEntry); var xElementSubClasses = xElement.Descendants("SubClass"); var xElementSubClassesCodes = xElementSubClasses.Select(x => x.Attribute("code").Value); var allClasses = _File.Descendants("Class"); var subClasses = allClasses.Where(x => xElementSubClassesCodes.Contains(x.Attribute("code").Value)); if (subClasses.Any()) { foreach (var subClass in subClasses) { icfs.AddRange(GetChildren(subClass, actualEntry.ValueListEntryOid, ref id)); } } return icfs; } // ValueListEntryOid = x.Oid, // ParentOid = x.ParentOID, // TypeDescription = x.Bezeichnung, // Abbreviation = x.Code, // Notice = x.Beschreibung public static ValueListEntryDC GenerateValueListEntryDC(XElement xElement, long? parentId, ref long id) { id++; switch (xElement.Attribute("kind").Value) { case "component": return new ValueListEntryDC() { ParentOid = -1, TypeDescription = "ICF", ValueListEntryOid = -999 }; case "chapter": // Hier Fehler bei "s" // Notice ist das Problem var noticeString = ""; var n = xElement.Descendants("Rubric").Where(x => x.Attribute("kind").Value == "coding-hint").ToList();//.First().Descendants("Label").First().Value; if (n.Count > 0) noticeString = n.First().Descendants("Label").First().Value; return new ValueListEntryDC() { ValueListEntryOid = id, ParentOid = parentId, TypeDescription = "Kapitel " + xElement.Attribute("code").Value[1] + ": " + xElement.Descendants("Rubric").Where(x => x.Attribute("kind").Value == "preferred").First().Descendants("Label").First().Value, Abbreviation = null, Notice = noticeString //Notice = xElement.Descendants("Rubric").Where(x => x.Attribute("kind").Value == "coding-hint").First().Descendants("Label").First().Value }; case "block": return new ValueListEntryDC() { ValueListEntryOid = id, ParentOid = parentId, TypeDescription = xElement.Descendants("Rubric").Where(x => x.Attribute("kind").Value == "preferred").First().Descendants("Label").First().Value + " (" + xElement.Attribute("code").Value + ")", Abbreviation = null, Notice = null }; case "fourdigit": case "fivedigit": return new ValueListEntryDC() { ValueListEntryOid = id, ParentOid = parentId, TypeDescription = xElement.Descendants("Rubric").Where(x => x.Attribute("kind").Value == "preferred").FirstOrDefault()?.Descendants("Label").First().Value, Abbreviation = xElement.Attribute("code").Value, Notice = xElement.Descendants("Rubric").Where(x => x.Attribute("kind").Value == "coding-hint").FirstOrDefault()?.Descendants("Label").First().Value }; } return null; } } }