ToTitleCase

pull/1/head
Sascha 2023-07-05 16:20:46 +07:00
parent 47877dfa97
commit 5fb9315591
3 changed files with 32 additions and 36 deletions

@ -65,28 +65,6 @@
<NumericEdit TValue="uint" Value="Convert.ToUInt32(context.CellValue)" ValueChanged="v => context.CellValue = v"/>
</EditTemplate>
</DataGridColumn>
@* <DataGridColumn Field="@nameof(Account.AccountType)" Caption="AccountTypeCode" Filterable Sortable> *@
@* <DisplayTemplate> *@
@* @{ *@
@* AccountType? accountType = (context as Account)?.AccountType; *@
@* if (accountType is not null) @($"{accountType.AccountTypeCode}") *@
@* } *@
@* </DisplayTemplate> *@
@* </DataGridColumn> *@
@* <DataGridColumn Field="@nameof(Account.SubMarket)" Caption="SubMarketCode" Filterable Sortable> *@
@* <DisplayTemplate> *@
@* @{ *@
@* SubMarket? subMarket = (context as Account)?.SubMarket; *@
@* *@
@* if (subMarket != null) { *@
@* @($"{subMarket.SubMarketCode}") *@
@* } *@
@* } *@
@* </DisplayTemplate> *@
@* </DataGridColumn> *@
</DataGridColumns>
<ButtonRowTemplate>
@ -130,8 +108,14 @@
<FileEdit Filter=".csv" Changed="@OnImportAccounts"/>
</Field>
</Paragraph>
<Paragraph>
<Progress>
<ProgressBar Animated Value="@ImportProgress"/>
</Progress>
</Paragraph>
</Div>
</Authorized>
<NotAuthorized>
<Div Margin="Margin.Is3"
Border="Border.Dark.OnAll"

@ -12,6 +12,7 @@ namespace Gremlin_BlazorServer.Pages;
public partial class Accounts {
private readonly IList<Account> accounts = new List<Account>();
private Account? selectedAccount;
public static int ImportProgress { get; set; }
[CascadingParameter] private Task<AuthenticationState>? AuthenticationStateTask { get; set; }
@ -19,10 +20,11 @@ public partial class Accounts {
if (AuthenticationStateTask != null) {
ClaimsPrincipal user = (await AuthenticationStateTask).User;
if (user.Identity is { IsAuthenticated: true }) {
ImportProgress = 0;
await ApplicationLoadingIndicatorService.Show();
accounts.AddRange(await GenericController.GetAllAsync<Account>());
selectedAccount = accounts.First();
await OnSelectedAccountChanged(selectedAccount);
//selectedAccount = accounts.First();
//await OnSelectedAccountChanged(selectedAccount);
await ApplicationLoadingIndicatorService.Hide();
}

@ -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 IList<string[]> SplitLines(string fileContent) {
private static List<string[]> SplitLines(string fileContent) {
IEnumerable<string> fileLines = fileContent.Split(Environment.NewLine.ToCharArray());
IList<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].ToUpper().First() + line[3][1..].ToLower(),
City = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(line[3].ToLower()), //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);
}
}