@ -1,17 +1,22 @@
using Gremlin_BlazorServer.Data.EntityClasses ;
using Gremlin_BlazorServer.Pages ;
namespace Gremlin_BlazorServer.Services ;
public class GenericImporter {
private readonly GenericController genericController = new ( ) ;
private readonly List < Account > newAccounts = new ( ) ;
private readonly List < Account > updatedAccounts = new ( ) ;
public async Task < bool > ImportCsvAsync < T > ( string fileContent ) where T : class , IMetadata {
Console . WriteLine ( "GENERIC IMPORTER: Importing accounts from csv..." ) ;
IList < string [ ] > splitLines = await Task . Run ( ( ) = > SplitLines ( fileContent ) ) ;
newAccounts . Clear ( ) ;
updatedAccounts . Clear ( ) ;
List < string [ ] > splitLines = await Task . Run ( ( ) = > SplitLines ( fileContent ) ) ;
if ( typeof ( T ) ! = typeof ( Account ) ) return false ;
Console . WriteLine ( $"Found {splitLines.Count} potential accounts in csv." ) ;
await ParseListToAccounts ( splitLines ) ;
int countNewAccounts = 0 ;
@ -30,22 +35,27 @@ public class GenericImporter {
return countNewAccounts > 0 | | updatedAccounts . Count > 0 ;
}
private static I List< string [ ] > SplitLines ( string fileContent ) {
private static List< string [ ] > SplitLines ( string fileContent ) {
IEnumerable < string > fileLines = fileContent . Split ( Environment . NewLine . ToCharArray ( ) ) ;
I List< string [ ] > fileList = ( from clipboardLine in fileLines where clipboardLine ! = "" select clipboardLine . Split ( '\t' ) ) . ToList ( ) ;
List< string [ ] > fileList = ( from clipboardLine in fileLines where clipboardLine ! = "" select clipboardLine . Split ( '\t' ) ) . ToList ( ) ;
return fileList ;
}
private async Task ParseListToAccounts ( IEnumerable < string [ ] > lineList ) // ID;Acct Name 1 and 2;Street;City;BP Role;Postal Code;Customer Type;Market Indicator;
private async Task ParseListToAccounts ( List < string [ ] > lineList ) // ID;Acct Name 1 and 2;Street;City;BP Role;Postal Code;Customer Type;Market Indicator;
{
int i = 0 ;
int maxI = lineList . Count ;
foreach ( string [ ] strings in lineList ) {
string [ ] line = strings . Select ( x = > x . Replace ( "\"" , string . Empty ) ) . ToArray ( ) ; // Remove all ""
// Console.WriteLine($"Current line: {line[0]}: {line[1]}...");
i + + ;
Accounts . ImportProgress = ( int ) ( ( float ) i / maxI * 100 ) ;
if ( line [ 0 ] . Contains ( "ID" ) ) continue ; //HACK: skip first row if header
if ( ! uint . TryParse ( line [ 0 ] , out uint sapAccountNumber ) ) continue ;
if ( ! uint . TryParse ( line [ 5 ] , out uint zip ) ) continue ;
if ( ! uint . TryParse ( line [ 0 ] , out uint sapAccountNumber ) ) continue ; //HACK: skip wrong SapAccountNumbers
if ( ! uint . TryParse ( line [ 5 ] , out uint zip ) ) continue ; //HACK: skip wrong ZIPs
string accountTypeCode = line [ 6 ] ;
if ( accountTypeCode is "" or null ) accountTypeCode = "FPC" ; // standard AccountType
@ -55,9 +65,9 @@ public class GenericImporter {
Account readAccount = new ( ) {
SapAccountNumber = sapAccountNumber ,
AccountName = line[ 1 ] ,
AccountName = System. Globalization . CultureInfo . CurrentCulture . TextInfo . ToTitleCase ( line[ 1 ] .ToLower ( ) ) , //line[1]
Street = line [ 2 ] ,
City = line[ 3 ] . To Upper( ) . First ( ) + line [ 3 ] [ 1. . ] . ToLower ( ) ,
City = System. Globalization . CultureInfo . CurrentCulture . TextInfo . ToTitleCase ( line[ 3 ] . To Lower( ) ) , //line[3].ToUpper().First() + line[3][1..].ToLower() ,
Zip = zip ,
PhoneNumber = line [ 8 ] ,
EMail = line [ 9 ] ,
@ -81,11 +91,11 @@ public class GenericImporter {
existingAccount . PhoneNumber = readAccount . PhoneNumber ;
existingAccount . Street = readAccount . Street ;
existingAccount . Zip = readAccount . Zip ;
Console . WriteLine ( $" Update in Account {existingAccount.SapAccountNumber}:{existingAccount.AccountName}") ;
Console . WriteLine ( $" Update in Account {existingAccount.SapAccountNumber}:{existingAccount.AccountName}") ;
updatedAccounts . Add ( existingAccount ) ;
}
else {
Console . WriteLine ( $"Account {readAccount. AccountName} ist neu!") ;
Console . WriteLine ( $"Account {readAccount. SapAccountNumber}:{readAccount. AccountName} ist neu!") ;
newAccounts . Add ( readAccount ) ;
}
}