170 lines
4.3 KiB
C#
170 lines
4.3 KiB
C#
using MoreLinq;
|
|
|
|
namespace Gremlin_BlazorServer.Services.GUClasses
|
|
{
|
|
internal class DataIdentificator
|
|
{
|
|
//private readonly string _source; //Qualifier-Liste
|
|
private readonly Dictionary<string, int>? mappingTable; //Mapping Spaltenzahl <-> Property/(übersetzte) Spaltenüberschrift
|
|
public List<DataIdType> DataTypes { get; set; } = new();
|
|
|
|
|
|
public DataIdentificator(Dictionary<string, int> mappingTable)
|
|
{
|
|
/*
|
|
mappingTable = mappingTable;
|
|
*/
|
|
//DataTypes populieren. Dazu: einlesen, nach DT qualifier in temp. Liste speichern, damit neuen DIDT erzeugen und zur Liste hinzufügen
|
|
Initialize();
|
|
}
|
|
|
|
private void Initialize()
|
|
{
|
|
string currentDataType;
|
|
DataTypes = new();
|
|
List<DataIdentifier> dataIdentifier = ReadDataIdentifierFromFile();
|
|
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]) //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)
|
|
{
|
|
foreach (DataIdType dataType in DataTypes) //may return multiple winners
|
|
{
|
|
if (dataType.AllQualifierMatched)
|
|
{
|
|
winners.Add(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 == true && 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");
|
|
}
|
|
}
|
|
|
|
if (mustMatchAllQualifer == true && winners.Contains("Account"))
|
|
{
|
|
if (winners.Contains("ProductLine"))
|
|
{
|
|
_ = winners.Remove("ProductLine");
|
|
}
|
|
|
|
if (winners.Contains("AccountType"))
|
|
{
|
|
_ = winners.Remove("AccountType");
|
|
}
|
|
|
|
if (winners.Contains("SubMarket"))
|
|
{
|
|
_ = winners.Remove("SubMarket");
|
|
}
|
|
}
|
|
|
|
if (mustMatchAllQualifer == false && 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");
|
|
}
|
|
}
|
|
|
|
return winners;
|
|
}
|
|
|
|
public static List<DataIdentifier> ReadDataIdentifierFromFile()
|
|
{
|
|
List<DataIdentifier> result = new(50);
|
|
string fileInput = FileService.ReadResource("DataIdentifier.txt");
|
|
string[] lines = fileInput.Split(Environment.NewLine);
|
|
foreach (string line in lines)
|
|
{
|
|
string[] fields = line.Split("|");
|
|
result.Add(new(fields[0], fields[1]));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
}
|
|
}
|