From 458c8c93c389d080b636c2cf8814655729255277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20St=C3=BCber?= Date: Tue, 9 Mar 2021 12:56:40 +0100 Subject: [PATCH] Bug fix: Keeping export from GPU and XML in sync --- src/Export/ExportManager.cs | 63 +++++++++++++++----------- src/Extensions/GpuAbsenceExtensions.cs | 58 ++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 32 deletions(-) diff --git a/src/Export/ExportManager.cs b/src/Export/ExportManager.cs index d7486c4..f8a3302 100644 --- a/src/Export/ExportManager.cs +++ b/src/Export/ExportManager.cs @@ -334,17 +334,20 @@ await ecfTableWriter.WriteHeadersAsync( { if (absence.Type == GpuAbsenceType.Room) { - ecfTableWriter.TrySetValue(EcfHeaders.Id, absence.Id); - ecfTableWriter.TrySetValue(EcfHeaders.RoomId, absence.GetUntisRoomId()); - ecfTableWriter.TrySetValue(EcfHeaders.StartTimepoint, absence.StartDate); - ecfTableWriter.TrySetValue(EcfHeaders.EndTimepoint, absence.EndDate); - ecfTableWriter.TrySetValue(EcfHeaders.ReasonId, absence.Reason); - ecfTableWriter.TrySetValue(EcfHeaders.Description, absence.Text); + if (absence.IsInsideTerm(_untisDocument.GeneralSettings) && absence.GetEcfRoomId(_untisDocument.Rooms) != null) + { + ecfTableWriter.TrySetValue(EcfHeaders.Id, absence.Id); + ecfTableWriter.TrySetValue(EcfHeaders.RoomId, absence.GetUntisRoomId()); + ecfTableWriter.TrySetValue(EcfHeaders.StartTimepoint, absence.StartDate); + ecfTableWriter.TrySetValue(EcfHeaders.EndTimepoint, absence.EndDate); + ecfTableWriter.TrySetValue(EcfHeaders.ReasonId, absence.Reason); + ecfTableWriter.TrySetValue(EcfHeaders.Description, absence.Text); - await ecfTableWriter.WriteAsync(); + await ecfTableWriter.WriteAsync(); - _untisAbsencesCache.Add(absence.Id); - ecfRecordCounter++; + _untisAbsencesCache.Add(absence.Id); + ecfRecordCounter++; + } } } @@ -458,17 +461,20 @@ await ecfTableWriter.WriteHeadersAsync( { if (absence.Type == GpuAbsenceType.Class) { - ecfTableWriter.TrySetValue(EcfHeaders.Id, absence.Id); - ecfTableWriter.TrySetValue(EcfHeaders.SchoolClassId, absence.GetUntisSchoolClassId()); - ecfTableWriter.TrySetValue(EcfHeaders.StartTimepoint, absence.StartDate); - ecfTableWriter.TrySetValue(EcfHeaders.EndTimepoint, absence.EndDate); - ecfTableWriter.TrySetValue(EcfHeaders.ReasonId, absence.Reason); - ecfTableWriter.TrySetValue(EcfHeaders.Description, absence.Text); + if (absence.IsInsideTerm(_untisDocument.GeneralSettings) && absence.GetEcfSchoolClassId(_untisDocument.Classes) != null) + { + ecfTableWriter.TrySetValue(EcfHeaders.Id, absence.Id); + ecfTableWriter.TrySetValue(EcfHeaders.SchoolClassId, absence.GetUntisSchoolClassId()); + ecfTableWriter.TrySetValue(EcfHeaders.StartTimepoint, absence.StartDate); + ecfTableWriter.TrySetValue(EcfHeaders.EndTimepoint, absence.EndDate); + ecfTableWriter.TrySetValue(EcfHeaders.ReasonId, absence.Reason); + ecfTableWriter.TrySetValue(EcfHeaders.Description, absence.Text); - await ecfTableWriter.WriteAsync(); + await ecfTableWriter.WriteAsync(); - _untisAbsencesCache.Add(absence.Id); - ecfRecordCounter++; + _untisAbsencesCache.Add(absence.Id); + ecfRecordCounter++; + } } } @@ -681,17 +687,20 @@ await ecfTableWriter.WriteHeadersAsync( { if (absence.Type == GpuAbsenceType.Teacher) { - ecfTableWriter.TrySetValue(EcfHeaders.Id, absence.Id); - ecfTableWriter.TrySetValue(EcfHeaders.TeacherId, absence.GetUntisTeacherId()); - ecfTableWriter.TrySetValue(EcfHeaders.StartTimepoint, absence.StartDate); - ecfTableWriter.TrySetValue(EcfHeaders.EndTimepoint, absence.EndDate); - ecfTableWriter.TrySetValue(EcfHeaders.ReasonId, absence.Reason); - ecfTableWriter.TrySetValue(EcfHeaders.Description, absence.Text); + if (absence.IsInsideTerm(_untisDocument.GeneralSettings) && absence.GetEcfTeacherId(_untisDocument.Teachers) != null) + { + ecfTableWriter.TrySetValue(EcfHeaders.Id, absence.Id); + ecfTableWriter.TrySetValue(EcfHeaders.TeacherId, absence.GetUntisTeacherId()); + ecfTableWriter.TrySetValue(EcfHeaders.StartTimepoint, absence.StartDate); + ecfTableWriter.TrySetValue(EcfHeaders.EndTimepoint, absence.EndDate); + ecfTableWriter.TrySetValue(EcfHeaders.ReasonId, absence.Reason); + ecfTableWriter.TrySetValue(EcfHeaders.Description, absence.Text); - await ecfTableWriter.WriteAsync(); + await ecfTableWriter.WriteAsync(); - _untisAbsencesCache.Add(absence.Id); - ecfRecordCounter++; + _untisAbsencesCache.Add(absence.Id); + ecfRecordCounter++; + } } } diff --git a/src/Extensions/GpuAbsenceExtensions.cs b/src/Extensions/GpuAbsenceExtensions.cs index 7851476..75680ba 100644 --- a/src/Extensions/GpuAbsenceExtensions.cs +++ b/src/Extensions/GpuAbsenceExtensions.cs @@ -20,6 +20,8 @@ #endregion using Enbrea.Untis.Gpu; +using Enbrea.Untis.Xml; +using System.Collections.Generic; namespace Ecf.Untis { @@ -28,6 +30,45 @@ namespace Ecf.Untis /// public static class GpuAbsenceExtensions { + public static string GetEcfRoomId(this GpuAbsence absence, List rooms) + { + var room = rooms.Find(x => x.Id == absence.GetUntisRoomId()); + if (room != null) + { + return room.Id; + } + else + { + return default; + } + } + + public static string GetEcfSchoolClassId(this GpuAbsence absence, List schoolClasses) + { + var schoolClass = schoolClasses.Find(x => x.Id == absence.GetUntisSchoolClassId()); + if (schoolClass != null) + { + return schoolClass.Id; + } + else + { + return default; + } + } + + public static string GetEcfTeacherId(this GpuAbsence absence, List teachers) + { + var teacher = teachers.Find(x => x.Id == absence.GetUntisTeacherId()); + if (teacher != null) + { + return teacher.Id; + } + else + { + return default; + } + } + public static string GetUntisRoomId(this GpuAbsence absence) { if (string.IsNullOrEmpty(absence.ShortName)) @@ -40,19 +81,19 @@ public static string GetUntisRoomId(this GpuAbsence absence) } } - public static string GetUntisTeacherId(this GpuAbsence absence) + public static string GetUntisSchoolClassId(this GpuAbsence absence) { if (string.IsNullOrEmpty(absence.ShortName)) { return null; } - else + else { - return "TR_" + absence.ShortName; + return "CL_" + absence.ShortName; } } - public static string GetUntisSchoolClassId(this GpuAbsence absence) + public static string GetUntisTeacherId(this GpuAbsence absence) { if (string.IsNullOrEmpty(absence.ShortName)) { @@ -60,8 +101,15 @@ public static string GetUntisSchoolClassId(this GpuAbsence absence) } else { - return "CL_" + absence.ShortName; + return "TR_" + absence.ShortName; } } + + public static bool IsInsideTerm(this GpuAbsence absence, UntisGeneralSettings generalSettings) + { + return + (absence.StartDate >= generalSettings.TermBeginDate && absence.StartDate <= generalSettings.TermEndDate) || + (absence.EndDate >= generalSettings.TermBeginDate && absence.EndDate <= generalSettings.TermEndDate); + } } } \ No newline at end of file