116 lines
4.0 KiB
C#
116 lines
4.0 KiB
C#
using Gremlin_BlazorServer.Services;
|
|
|
|
namespace Gremlin_BlazorServer.Utilities.GUClasses
|
|
{
|
|
internal class DataIdentificator
|
|
{
|
|
//private readonly string _source; //Qualifier-Liste
|
|
private readonly Dictionary<string, int> mappingTable; //Mapping Spaltenzahl <-> Property/(übersetzte) Spaltenüberschrift
|
|
private List<DataIdType> DataTypes { get; set; } = new();
|
|
|
|
public DataIdentificator(Dictionary<string, int> mT)
|
|
{
|
|
mappingTable = mT;
|
|
//DataTypes populieren. Dazu: einlesen, nach DT qualifier in temp. Liste speichern, damit neuen DIDT erzeugen und zur Liste hinzufügen
|
|
Initialize();
|
|
}
|
|
|
|
private void Initialize()
|
|
{
|
|
DataTypes = new();
|
|
List<DataIdentifier> dataIdentifier = ReadDataIdentifierFromFile();
|
|
string currentDataType = dataIdentifier[0].DataType;
|
|
List<string> identifierOfCurrentType = new();
|
|
foreach (DataIdentifier qualifier in dataIdentifier)
|
|
{
|
|
if (qualifier.DataType == currentDataType)
|
|
{
|
|
identifierOfCurrentType.Add(qualifier.Identifier);
|
|
if (qualifier != dataIdentifier[^1]) continue; //letztes Element der Liste
|
|
DataIdType typeToBeAdded = new(currentDataType, identifierOfCurrentType);
|
|
DataTypes.Add(typeToBeAdded);
|
|
//_currentDataType = qualifier.DataType;
|
|
}
|
|
else
|
|
{
|
|
DataIdType typeToBeAdded = new(currentDataType, identifierOfCurrentType);
|
|
DataTypes.Add(typeToBeAdded);
|
|
currentDataType = qualifier.DataType;
|
|
identifierOfCurrentType.Clear();
|
|
identifierOfCurrentType.Add(qualifier.Identifier);
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<string> Identify(bool mustMatchAllQualifer = true)
|
|
{
|
|
List<string> winners = new();
|
|
foreach (DataIdType datatype in DataTypes)
|
|
{
|
|
foreach (string qualifier in datatype.Qualifiers)
|
|
{
|
|
if (mappingTable.ContainsKey(qualifier)) datatype.AddFoundQualfier(qualifier);
|
|
}
|
|
}
|
|
if (mustMatchAllQualifer)
|
|
winners.AddRange(from dataType in DataTypes
|
|
where dataType.AllQualifierMatched
|
|
select dataType.DataType);
|
|
else //return the winner by highest score:
|
|
winners.Add(DataTypes.Aggregate((x, y) => x.Score > y.Score ? x : y).DataType.ToString());
|
|
//Plausibilty checkpoint:
|
|
//1. Remove "PL", "AccountType", "Submarket" from Winner-List, if detected datatype is "LSAG" or "Account".
|
|
//2. LSAG = Account + Contact --> when LSAG is a winner, remove "LSAG" and add "Account" and "Contact" to use their dedicated import methods.
|
|
//3. Remove any other winner, when winner is one of "PL", "AccountType", "Submarket" AND fields[].count = 2.
|
|
if (mustMatchAllQualifer && winners.Contains("LSAG"))
|
|
{
|
|
if (winners.Contains("ProductLine")) winners.Remove("ProductLine");
|
|
if (winners.Contains("AccountType")) winners.Remove("AccountType");
|
|
if (winners.Contains("SubMarket")) winners.Remove("SubMarket");
|
|
if (winners.Contains("Account")) winners.Remove("Account");
|
|
if (winners.Contains("Contact")) winners.Remove("Contact");
|
|
}
|
|
switch (mustMatchAllQualifer)
|
|
{
|
|
case true when winners.Contains("Account"):
|
|
{
|
|
if (winners.Contains("ProductLine")) winners.Remove("ProductLine");
|
|
if (winners.Contains("AccountType")) winners.Remove("AccountType");
|
|
if (winners.Contains("SubMarket")) winners.Remove("SubMarket");
|
|
break;
|
|
}
|
|
case false when mappingTable.Count == 2:
|
|
{
|
|
if (winners.Contains("ProductLine"))
|
|
{
|
|
winners.Clear();
|
|
winners.Add("ProductLine");
|
|
}
|
|
if (winners.Contains("AccountType"))
|
|
{
|
|
winners.Clear();
|
|
winners.Add("AccountType");
|
|
}
|
|
if (winners.Contains("SubMarket"))
|
|
{
|
|
winners.Clear();
|
|
winners.Add("SubMarket");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return winners;
|
|
}
|
|
|
|
private static List<DataIdentifier> ReadDataIdentifierFromFile()
|
|
{
|
|
List<DataIdentifier> result = new(50);
|
|
string fileInput = FileService.ReadResource("DataIdentifier.txt");
|
|
string[] lines = fileInput.Split(Environment.NewLine);
|
|
result.AddRange(lines.Select(line => line.Split("|")).Select(fields => new DataIdentifier(fields[0], fields[1])));
|
|
return result;
|
|
}
|
|
|
|
}
|
|
}
|