Files
BeWoPlaner/Data/ICF/ICFDAO.cs
2021-11-02 12:50:37 +01:00

153 lines
6.0 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
{
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";
}
//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 == "e" /*|| 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", "d", "e" };
//_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 IList<ValueListEntryDC> GetIcf()
{
if (_Root == null)
return null;
long id = 1;
var returnvalue = GetChildren(_Root, 0, ref id);
return returnvalue;
}
public static List<List<ValueListEntryDC>> GetWholeIcf()
{
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());
}
return returnvalueList;
}
//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)
{ // Hier Fehler bei 2. Kategorie
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": // Hier Fehler bei "s"
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
+ " (" + 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;
}
}
}