Files
BeWoPlaner/Data/ICF/ICFDAO.cs
2019-11-14 16:47:23 +01:00

125 lines
4.8 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 readonly XDocument _File;
private static readonly XElement _Root;
static ICFDAO()
{
try
{
string file = ConfigurationManager.AppSettings.Get("ICFFile");
if (String.IsNullOrEmpty(file) ||
!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\ICF\" + file)))
{
file = "icf.xml";
}
file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\ICF\" + file);
_File = XDocument.Load(file);
//_File = XDocument.Load(@"D:\Projects\BeWoPlaner\BeWo\Data\ICF\icf.xml");
Debug.WriteLine("Dokument geladen");
_Root = _File.Descendants("Class").Where(x => x.Attribute("code")?.Value == "d").First();
Debug.WriteLine("Root gefunden");
}
catch (Exception e)
{
}
}
public static IList<ValueListEntryDC> GetIcf()
{
if (_Root == null) return null;
long id = 1;
return GetChildren(_Root, 0, ref id);
}
//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" };
case "chapter":
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 = 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,
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;
}
}
}