From 4cd093092c568ab2e6706e0d41bae2697d968dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20St=C3=BCber?= Date: Thu, 23 Sep 2021 20:10:03 +0200 Subject: [PATCH 1/2] Refactoring --- src/CommandDefinitions.cs | 3 +- src/Converters/CsvConverterResolver.cs | 6 +-- src/Ecf.Edoosys.csproj | 7 ++-- src/Export/CsvExportManager.cs | 5 ++- src/Export/DbExportManager.cs | 26 +++++++++--- src/Utils/Converter.cs | 57 ++++++++++++++++++++++++++ src/Utils/IdFactory .cs | 34 +++++++++++++++ test/xunit/Ecf.Edoosys.XUnit.csproj | 2 +- 8 files changed, 124 insertions(+), 16 deletions(-) create mode 100644 src/Utils/Converter.cs create mode 100644 src/Utils/IdFactory .cs diff --git a/src/CommandDefinitions.cs b/src/CommandDefinitions.cs index 749da0a..17daedf 100644 --- a/src/CommandDefinitions.cs +++ b/src/CommandDefinitions.cs @@ -31,9 +31,8 @@ public static Command Export() { var command = new Command("export", "Exports data from a Edoosys export CSV to ECF files") { - new Option(new[] { "--config", "-c" }, "Path to existing JSON configuration file") + new Option(new[] { "--config", "-c" }, "Path to existing JSON configuration file") { - Argument = new Argument(), IsRequired = true }, }; diff --git a/src/Converters/CsvConverterResolver.cs b/src/Converters/CsvConverterResolver.cs index f8718ab..76d89f3 100644 --- a/src/Converters/CsvConverterResolver.cs +++ b/src/Converters/CsvConverterResolver.cs @@ -32,10 +32,8 @@ public class CsvConverterResolver : CsvDefaultConverterResolver protected override void RegisterDefaultConverters() { base.RegisterDefaultConverters(); - AddConverter(new CsvDateConverter()); - AddConverter(new CsvDateConverter()); - AddConverter(new CsvGenderConverter()); - AddConverter(new CsvGenderConverter()); + AddConverter(typeof(Date), new CsvDateConverter()); + AddConverter(typeof(EcfGender), new CsvGenderConverter()); } } } \ No newline at end of file diff --git a/src/Ecf.Edoosys.csproj b/src/Ecf.Edoosys.csproj index b96118c..f8d2cb1 100644 --- a/src/Ecf.Edoosys.csproj +++ b/src/Ecf.Edoosys.csproj @@ -13,10 +13,11 @@ - - + + - + + diff --git a/src/Export/CsvExportManager.cs b/src/Export/CsvExportManager.cs index 63a2078..9c9ed74 100644 --- a/src/Export/CsvExportManager.cs +++ b/src/Export/CsvExportManager.cs @@ -249,7 +249,8 @@ private async Task ExportStudentSubjects(CsvTableReader csvTableReader, Ecf await ecfTableWriter.WriteHeadersAsync( EcfHeaders.StudentId, EcfHeaders.SchoolClassId, - EcfHeaders.SubjectId); + EcfHeaders.SubjectId, + EcfHeaders.TeacherId); } while (await csvTableReader.ReadAsync() > 0) @@ -272,6 +273,7 @@ await ecfTableWriter.WriteHeadersAsync( var subValueParts = csvLineParser.Read(subValue.Trim()); if (subValueParts.Length == 2) { + var teacherCode = subValueParts[0]; var subjectCode = subValueParts[1]; if (!string.IsNullOrEmpty(subjectCode)) @@ -279,6 +281,7 @@ await ecfTableWriter.WriteHeadersAsync( ecfTableWriter.TrySetValue(EcfHeaders.StudentId, student.Id); ecfTableWriter.TrySetValue(EcfHeaders.SchoolClassId, schoolClass.Id); ecfTableWriter.TrySetValue(EcfHeaders.SubjectId, subjectCode); + ecfTableWriter.TrySetValue(EcfHeaders.TeacherId, teacherCode); await ecfTableWriter.WriteAsync(); diff --git a/src/Export/DbExportManager.cs b/src/Export/DbExportManager.cs index d460b69..6884e50 100644 --- a/src/Export/DbExportManager.cs +++ b/src/Export/DbExportManager.cs @@ -197,8 +197,8 @@ await ecfTableWriter.WriteHeadersAsync( ecfTableWriter.TrySetValue(EcfHeaders.Id, student.Id); ecfTableWriter.TrySetValue(EcfHeaders.LastName, student.Lastname); ecfTableWriter.TrySetValue(EcfHeaders.FirstName, student.Firstname); - ecfTableWriter.TrySetValue(EcfHeaders.Gender, student.Gender); - ecfTableWriter.TrySetValue(EcfHeaders.Birthdate, student.Birthdate); + ecfTableWriter.TrySetValue(EcfHeaders.Gender, Converter.GetGender(student.Gender)); + ecfTableWriter.TrySetValue(EcfHeaders.Birthdate, Converter.GetDate(student.Birthdate)); await ecfTableWriter.WriteAsync(); @@ -226,6 +226,7 @@ private async Task ExportStudentSchoolClassAttendances(EdoosysDbReader edoo else { await ecfTableWriter.WriteHeadersAsync( + EcfHeaders.Id, EcfHeaders.StudentId, EcfHeaders.SchoolClassId); } @@ -234,6 +235,7 @@ await ecfTableWriter.WriteHeadersAsync( { var schoolClassId = _config.EcfExport.NoSchoolClassGroups ? attendance.SchoolClassRootId : attendance.SchoolClassId; + ecfTableWriter.TrySetValue(EcfHeaders.Id, GenerateKey(attendance.StudentId, schoolClassId)); ecfTableWriter.TrySetValue(EcfHeaders.StudentId, attendance.StudentId); ecfTableWriter.TrySetValue(EcfHeaders.SchoolClassId, schoolClassId); await ecfTableWriter.WriteAsync(); @@ -263,6 +265,7 @@ private async Task ExportStudentSubjects(EdoosysDbReader edoosysDbReader, E else { await ecfTableWriter.WriteHeadersAsync( + EcfHeaders.Id, EcfHeaders.StudentId, EcfHeaders.SchoolClassId, EcfHeaders.SubjectId, @@ -272,7 +275,8 @@ await ecfTableWriter.WriteHeadersAsync( await foreach (var studentSubject in edoosysDbReader.StudentSubjectsAsync(_config.EcfExport.SchoolNo, _config.EcfExport.SchoolYearCode, activeStudentsOnly: true)) { var schoolClassId = _config.EcfExport.NoSchoolClassGroups ? studentSubject.SchoolClassRootId : studentSubject.SchoolClassId; - + + ecfTableWriter.TrySetValue(EcfHeaders.Id, GenerateKey(studentSubject.StudentId, studentSubject.SubjectId, schoolClassId, studentSubject.TeacherId)); ecfTableWriter.TrySetValue(EcfHeaders.StudentId, studentSubject.StudentId); ecfTableWriter.TrySetValue(EcfHeaders.SubjectId, studentSubject.SubjectId); ecfTableWriter.TrySetValue(EcfHeaders.SchoolClassId, schoolClassId); @@ -358,8 +362,8 @@ await ecfTableWriter.WriteHeadersAsync( ecfTableWriter.TrySetValue(EcfHeaders.Code, teacher.Code); ecfTableWriter.TrySetValue(EcfHeaders.LastName, teacher.Lastname); ecfTableWriter.TrySetValue(EcfHeaders.FirstName, teacher.Firstname); - ecfTableWriter.TrySetValue(EcfHeaders.Gender, teacher.Gender); - ecfTableWriter.TrySetValue(EcfHeaders.Birthdate, teacher.Birthdate); + ecfTableWriter.TrySetValue(EcfHeaders.Gender, Converter.GetGender(teacher.Gender)); + ecfTableWriter.TrySetValue(EcfHeaders.Birthdate, Converter.GetDate(teacher.Birthdate)); await ecfTableWriter.WriteAsync(); @@ -374,5 +378,17 @@ await ecfTableWriter.WriteHeadersAsync( throw new Exception("No school no and/or no school year for edoo.sys database defined"); } } + + private Guid GenerateKey(params string[] array) + { + var csvLineBuilder = new CsvLineBuilder(); + + foreach (var arrayItem in array) + { + csvLineBuilder.Append(arrayItem); + } + return IdFactory.CreateIdFromValue(csvLineBuilder.ToString()); + } + } } diff --git a/src/Utils/Converter.cs b/src/Utils/Converter.cs new file mode 100644 index 0000000..ffc242f --- /dev/null +++ b/src/Utils/Converter.cs @@ -0,0 +1,57 @@ +#region ENBREA - Copyright (C) 2021 STÜBER SYSTEMS GmbH +/* + * ENBREA + * + * Copyright (C) 2021 STÜBER SYSTEMS GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +#endregion + +using Enbrea.Ecf; +using Enbrea.Edoosys.Db; +using System; + +namespace Ecf.Edoosys +{ + /// + /// Data type converter + /// + public static class Converter + { + public static Date? GetDate(DateTime? value) + { + if (value != null) + { + return new Date((DateTime)value); + } + return null; + } + + public static EcfGender? GetGender(Gender? value) + { + if (value != null) + { + return (value) switch + { + Gender.Male => EcfGender.Male, + Gender.Female => EcfGender.Female, + Gender.Diverse => EcfGender.Diverse, + _ => null, + }; + } + return null; + } + } +} \ No newline at end of file diff --git a/src/Utils/IdFactory .cs b/src/Utils/IdFactory .cs new file mode 100644 index 0000000..136f9e3 --- /dev/null +++ b/src/Utils/IdFactory .cs @@ -0,0 +1,34 @@ +#region ENBREA - Copyright (C) 2021 STÜBER SYSTEMS GmbH +/* + * ENBREA + * + * Copyright (C) 2021 STÜBER SYSTEMS GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +#endregion + +using Enbrea.GuidFactory; +using System; + +namespace Ecf.Edoosys +{ + public static class IdFactory + { + public static Guid CreateIdFromValue(string value) + { + return GuidFactory.Create(GuidFactory.IsoOidNamespace, value); + } + } +} \ No newline at end of file diff --git a/test/xunit/Ecf.Edoosys.XUnit.csproj b/test/xunit/Ecf.Edoosys.XUnit.csproj index d4b7a2c..637e877 100644 --- a/test/xunit/Ecf.Edoosys.XUnit.csproj +++ b/test/xunit/Ecf.Edoosys.XUnit.csproj @@ -7,7 +7,7 @@ - + all From 8703c2740a9a84c77cb59b4488ec8dc7bdc21828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20St=C3=BCber?= Date: Thu, 23 Sep 2021 23:09:22 +0200 Subject: [PATCH 2/2] Updated dependencies --- src/Ecf.Edoosys.csproj | 4 ++-- src/Utils/IdFactory .cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Ecf.Edoosys.csproj b/src/Ecf.Edoosys.csproj index f8d2cb1..3c34ace 100644 --- a/src/Ecf.Edoosys.csproj +++ b/src/Ecf.Edoosys.csproj @@ -14,9 +14,9 @@ - + - + diff --git a/src/Utils/IdFactory .cs b/src/Utils/IdFactory .cs index 136f9e3..73d057f 100644 --- a/src/Utils/IdFactory .cs +++ b/src/Utils/IdFactory .cs @@ -28,7 +28,7 @@ public static class IdFactory { public static Guid CreateIdFromValue(string value) { - return GuidFactory.Create(GuidFactory.IsoOidNamespace, value); + return GuidGenerator.Create(GuidGenerator.IsoOidNamespace, value); } } } \ No newline at end of file