Files
BeWoPlaner/Data/ICF/ICFDAO.cs
2023-09-11 13:38:00 +02:00

260 lines
9.9 KiB
C#

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<XElement> _RootList;
static ICFDAO()
{
try
{
var filename = GetFileName();
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var filepath = BS.Shared.Core.Utils.CreateSavePath(AppDomain.CurrentDomain.BaseDirectory, @"bin\ICF\" + filename);
//file = @"D:\Projects\beyondSoft\BeWoPlaner\BeWo\Data\ICF\icf.xml";
_File = XDocument.Load(filepath);
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<XElement>();
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 string GetFileName()
{
string filename = ConfigurationManager.AppSettings.Get("ICFFile");
if (String.IsNullOrEmpty(filename))
return "icf.xml";
string path = BS.Shared.Core.Utils.CreateSavePath(AppDomain.CurrentDomain.BaseDirectory, @"bin\ICF\" + filename);
if (File.Exists(path))
return filename;
return "icf.xml";
}
public static IList<ValueListEntryDC> GetIcf()
{
if (_Root == null)
return null;
long id = 1;
var returnvalue = GetChildren(_Root, 0, ref id);
return returnvalue;
}
public static List<ValueListEntryDC> GetWholeIcf()
{
//var testList = new List<List<ValueListEntryDC>>();
//testList.Add(new List<ValueListEntryDC>);
//testList.Add(new List<ValueListEntryDC>);
//testList.Add(GetIcf().ToList());
//testList.Add(new List<ValueListEntryDC>);
//return testList;
if (_RootList == null)
return null;
long id = 1;
List<List<ValueListEntryDC>> returnvalueList = new List<List<ValueListEntryDC>>();
foreach (var item in _RootList)
{
returnvalueList.Add(GetChildren(item, 0, ref id).ToList());
}
// Alles in eine Liste und übergeordnete Kapitel einfügen
List<ValueListEntryDC> listEntries = new List<ValueListEntryDC>();
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<ValueListEntryDC> AddKlassifikationen(List<ValueListEntryDC> 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<ValueListEntryDC> GetChildren(XElement xElement, long? parentId, ref long id)
{
var icfs = new List<ValueListEntryDC>();
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;
}
}
}