From 9b590e58d84e6ba9f142e7228a342ccb25f93492 Mon Sep 17 00:00:00 2001 From: "Sergey V. Zhdanovskih" Date: Wed, 25 Dec 2024 23:34:56 +0300 Subject: [PATCH] Various improvements --- projects/GKCore/GDModel/GDMTree.cs | 2 +- projects/GKCore/GKCore/BaseContext.cs | 9 +- .../GKCore/Controllers/BaseController.cs | 8 +- .../GKCore/Controllers/TreeCheckController.cs | 6 + .../GKCore/Controllers/TreeMergeController.cs | 2 +- projects/GKCore/GKCore/GKUtils.cs | 112 ++++++++++-------- .../GKCore/GKCore/Interfaces/IBaseContext.cs | 5 + .../GKCore/GKCore/Lists/URefsListModel.cs | 1 - projects/GKCore/GKCore/Tools/TreeInspector.cs | 2 +- projects/GKTests/GKCore/BaseContextTests.cs | 2 +- projects/GKTests/GKCore/GKUtilsTests.cs | 5 + 11 files changed, 91 insertions(+), 63 deletions(-) diff --git a/projects/GKCore/GDModel/GDMTree.cs b/projects/GKCore/GDModel/GDMTree.cs index cae3c3f71..73bfa958f 100644 --- a/projects/GKCore/GDModel/GDMTree.cs +++ b/projects/GKCore/GDModel/GDMTree.cs @@ -386,7 +386,7 @@ public string NewXRef(GDMRecord gdmRec, bool removeOldXRef = false) public T GetPtrValue(GDMPointer ptr) where T : GDMRecord { - return (ptr == null || !ptr.IsPointer) ? default(T) : XRefIndex_Find(ptr.XRef) as T; + return (ptr == null || !ptr.IsPointer) ? default : XRefIndex_Find(ptr.XRef) as T; } public void SetPtrValue(GDMPointer ptr, GDMRecord record) diff --git a/projects/GKCore/GKCore/BaseContext.cs b/projects/GKCore/GKCore/BaseContext.cs index 23cb7709f..62595a899 100644 --- a/projects/GKCore/GKCore/BaseContext.cs +++ b/projects/GKCore/GKCore/BaseContext.cs @@ -331,8 +331,7 @@ public async Task DeleteRecord(GDMRecord record) } if (result) { - fTree.Header.TransmissionDateTime = DateTime.Now; - Modified = true; + SetModified(); if (fViewer != null) { fViewer.NotifyRecord(record, RecordAction.raDelete); @@ -1771,6 +1770,12 @@ public void DoRollback() #region Modify routines + public void SetModified() + { + fTree.Header.TransmissionDateTime = DateTime.Now; + Modified = true; + } + /// /// This method performs a basic locking of the records for their /// editors. diff --git a/projects/GKCore/GKCore/Controllers/BaseController.cs b/projects/GKCore/GKCore/Controllers/BaseController.cs index 6608dda52..11c5dfb38 100644 --- a/projects/GKCore/GKCore/Controllers/BaseController.cs +++ b/projects/GKCore/GKCore/Controllers/BaseController.cs @@ -1130,12 +1130,10 @@ public static void NotifyRecord(IBaseWindow baseWin, GDMRecord record, RecordAct { if (baseWin == null || record == null) return; - DateTime dtNow = DateTime.Now; - switch (action) { case RecordAction.raAdd: case RecordAction.raEdit: - record.ChangeDate.ChangeDateTime = dtNow; + record.ChangeDate.ChangeDateTime = DateTime.Now; break; case RecordAction.raDelete: @@ -1150,9 +1148,7 @@ public static void NotifyRecord(IBaseWindow baseWin, GDMRecord record, RecordAct } if (action != RecordAction.raJump) { - baseWin.Context.Tree.Header.TransmissionDateTime = dtNow; - baseWin.Context.Modified = true; - + baseWin.Context.SetModified(); AppHost.Instance.NotifyRecord(baseWin, record, action); } } diff --git a/projects/GKCore/GKCore/Controllers/TreeCheckController.cs b/projects/GKCore/GKCore/Controllers/TreeCheckController.cs index ee10fa978..bbca3553f 100644 --- a/projects/GKCore/GKCore/Controllers/TreeCheckController.cs +++ b/projects/GKCore/GKCore/Controllers/TreeCheckController.cs @@ -82,14 +82,20 @@ public void CheckBase() public async Task Repair() { try { + bool modified = false; int num = fView.ChecksList.Items.Count; for (int i = 0; i < num; i++) { IListItem item = fView.ChecksList.Items[i]; if (item.Checked) { var checkObj = item.Tag as TreeInspector.CheckObj; await TreeInspector.RepairProblem(fView, fBase, checkObj); + modified = true; } } + + if (modified) { + fBase.Context.SetModified(); + } } finally { fBase.RefreshLists(false); CheckBase(); diff --git a/projects/GKCore/GKCore/Controllers/TreeMergeController.cs b/projects/GKCore/GKCore/Controllers/TreeMergeController.cs index 9ff4f456e..c670714cc 100644 --- a/projects/GKCore/GKCore/Controllers/TreeMergeController.cs +++ b/projects/GKCore/GKCore/Controllers/TreeMergeController.cs @@ -46,7 +46,7 @@ public async void Merge() fView.UpdateBase.Text = fileName; TreeTools.MergeTreeFile(fBase.Context.Tree, fileName, fView.SyncLog, true); - fBase.Context.Modified = true; + fBase.Context.SetModified(); fBase.RefreshLists(false); } diff --git a/projects/GKCore/GKCore/GKUtils.cs b/projects/GKCore/GKCore/GKUtils.cs index 7bd293075..491587332 100644 --- a/projects/GKCore/GKCore/GKUtils.cs +++ b/projects/GKCore/GKCore/GKUtils.cs @@ -367,12 +367,15 @@ public static string GenRecordLink(GDMTree tree, GDMRecord record, bool signed) return result; } - public static Tuple GenRecordLinkTuple(GDMTree tree, GDMRecord record, bool signed) + private class LinksList : List> { } + + public static Tuple GenRecordLinkTuple(GDMTree tree, GDMRecord record, bool signed, string prefix = "", string suffix = "") { if (record != null) { string recName = GetRecordName(tree, record, signed); - string recLink = HyperLink(record.XRef, recName); - return new Tuple(recName, recLink); + string recLink = prefix + HyperLink(record.XRef, recName) + suffix; + // prefix always sortable + return new Tuple(prefix + recName, recLink); } else { return new Tuple(string.Empty, string.Empty); } @@ -1364,7 +1367,7 @@ public static int GetDaysFor(GDMDate dt, DateTime dtNow, out int years, out bool bdD -= 1; } - distance = DateHelper.DaysBetween(dtNow, new DateTime(bdY, bdM, bdD)); + distance = DateHelper.DaysBetween(dtNow.Date, new DateTime(bdY, bdM, bdD)); } } } catch (Exception ex) { @@ -2096,14 +2099,14 @@ public static void ShowDetailCause(GDMCustomEvent evt, StringList summary) } } - private static void ShowEvent(GDMTree tree, GDMRecord subj, StringList aToList, GDMRecord aRec, GDMCustomEvent evt) + private static void ShowEvent(GDMTree tree, GDMRecord subject, LinksList linksList, GDMRecord record, GDMCustomEvent evt) { - switch (subj.RecordType) { + switch (subject.RecordType) { case GDMRecordType.rtNote: if (evt.HasNotes) { for (int i = 0, num = evt.Notes.Count; i < num; i++) { - if (evt.Notes[i].XRef == subj.XRef) { - ShowLink(tree, subj, aToList, aRec, evt, null); + if (evt.Notes[i].XRef == subject.XRef) { + ShowLink(tree, subject, linksList, record, evt, null); } } } @@ -2112,8 +2115,8 @@ private static void ShowEvent(GDMTree tree, GDMRecord subj, StringList aToList, case GDMRecordType.rtMultimedia: if (evt.HasMultimediaLinks) { for (int i = 0, num = evt.MultimediaLinks.Count; i < num; i++) { - if (evt.MultimediaLinks[i].XRef == subj.XRef) { - ShowLink(tree, subj, aToList, aRec, evt, null); + if (evt.MultimediaLinks[i].XRef == subject.XRef) { + ShowLink(tree, subject, linksList, record, evt, null); } } } @@ -2122,8 +2125,9 @@ private static void ShowEvent(GDMTree tree, GDMRecord subj, StringList aToList, case GDMRecordType.rtSource: if (evt.HasSourceCitations) { for (int i = 0, num = evt.SourceCitations.Count; i < num; i++) { - if (evt.SourceCitations[i].XRef == subj.XRef) { - ShowLink(tree, subj, aToList, aRec, evt, evt.SourceCitations[i]); + var sourCit = evt.SourceCitations[i]; + if (sourCit.XRef == subject.XRef) { + ShowLink(tree, subject, linksList, record, evt, sourCit); } } } @@ -2131,27 +2135,23 @@ private static void ShowEvent(GDMTree tree, GDMRecord subj, StringList aToList, } } - private static void ShowLink(GDMTree tree, GDMRecord aSubject, StringList aToList, GDMRecord aRec, GDMTag aTag, GDMPointer aExt) + private static void ShowLink(GDMTree tree, GDMRecord aSubject, LinksList linksList, GDMRecord record, GDMTag aTag, GDMPointer aExt) { - string prefix; - if (aSubject is GDMSourceRecord && aExt != null) { - GDMSourceCitation cit = (aExt as GDMSourceCitation); - if (cit != null && !string.IsNullOrEmpty(cit.Page)) { - prefix = cit.Page + ": "; - } else { - prefix = ""; + string prefix = " "; + if (aSubject is GDMSourceRecord && aExt is GDMSourceCitation cit) { + if (!string.IsNullOrEmpty(cit.Page)) { + prefix += cit.Page + ": "; } - } else { - prefix = ""; } string suffix; - if (aTag is GDMCustomEvent) { - suffix = ", " + GetEventNameLd((GDMCustomEvent) aTag); + if (aTag is GDMCustomEvent evt) { + suffix = ", " + GetEventNameLd(evt); } else { suffix = ""; } - aToList.Add(" " + prefix + GenRecordLink(tree, aRec, true) + suffix); + + linksList.Add(GenRecordLinkTuple(tree, record, true, prefix, suffix)); } public static int FindLinkStr(StringList list, string link) @@ -2246,50 +2246,62 @@ private static void ShowPersonNamesakes(GDMTree tree, GDMIndividualRecord iRec, private static void ShowSubjectLinks(GDMTree tree, GDMRecord subject, StringList summary, bool sort = false) { try { - StringList linkList = new StringList(); + var subjectType = subject.RecordType; + var linksList = new LinksList(); for (int k = 0, num2 = tree.RecordsCount; k < num2; k++) { GDMRecord record = tree[k]; - if (subject is GDMNoteRecord && record.HasNotes) { - for (int i = 0, num = record.Notes.Count; i < num; i++) { - if (record.Notes[i].XRef == subject.XRef) { - ShowLink(tree, subject, linkList, record, null, null); + switch (subjectType) { + case GDMRecordType.rtNote: + if (record.HasNotes) { + for (int i = 0, num = record.Notes.Count; i < num; i++) { + if (record.Notes[i].XRef == subject.XRef) { + ShowLink(tree, subject, linksList, record, null, null); + } + } } - } - } else if (subject is GDMMultimediaRecord && record.HasMultimediaLinks) { - for (int i = 0, num = record.MultimediaLinks.Count; i < num; i++) { - if (record.MultimediaLinks[i].XRef == subject.XRef) { - ShowLink(tree, subject, linkList, record, null, null); + break; + + case GDMRecordType.rtMultimedia: + if (record.HasMultimediaLinks) { + for (int i = 0, num = record.MultimediaLinks.Count; i < num; i++) { + if (record.MultimediaLinks[i].XRef == subject.XRef) { + ShowLink(tree, subject, linksList, record, null, null); + } + } } - } - } else if (subject is GDMSourceRecord && record.HasSourceCitations) { - for (int i = 0, num = record.SourceCitations.Count; i < num; i++) { - var sourCit = record.SourceCitations[i]; - if (sourCit.XRef == subject.XRef) { - ShowLink(tree, subject, linkList, record, null, sourCit); + break; + + case GDMRecordType.rtSource: + if (record.HasSourceCitations) { + for (int i = 0, num = record.SourceCitations.Count; i < num; i++) { + var sourCit = record.SourceCitations[i]; + if (sourCit.XRef == subject.XRef) { + ShowLink(tree, subject, linksList, record, null, sourCit); + } + } } - } + break; } - var evsRec = record as GDMRecordWithEvents; - if (evsRec != null && evsRec.HasEvents) { + if (record is GDMRecordWithEvents evsRec && evsRec.HasEvents) { for (int i = 0, num = evsRec.Events.Count; i < num; i++) { - ShowEvent(tree, subject, linkList, evsRec, evsRec.Events[i]); + ShowEvent(tree, subject, linksList, evsRec, evsRec.Events[i]); } } } - if (linkList.Count > 0) { + if (linksList.Count > 0) { summary.Add(""); summary.Add(LangMan.LS(LSID.Links) + ":"); if (sort) { - linkList.Sort(); + linksList.Sort(); } - for (int j = 0, num3 = linkList.Count; j < num3; j++) { - summary.Add(linkList[j]); + for (int j = 0, num3 = linksList.Count; j < num3; j++) { + summary.Add(linksList[j].Item2); } } } catch (Exception ex) { @@ -2957,7 +2969,7 @@ public static void ShowRepositoryInfo(IBaseContext baseContext, GDMRepositoryRec summary.Add(""); summary.Add(LangMan.LS(LSID.RPSources) + ":"); - var sortedSources = new List>(); + var sortedSources = new LinksList(); GDMTree tree = baseContext.Tree; int num = tree.RecordsCount; for (int i = 0; i < num; i++) { diff --git a/projects/GKCore/GKCore/Interfaces/IBaseContext.cs b/projects/GKCore/GKCore/Interfaces/IBaseContext.cs index d2dac8aff..19c13593c 100644 --- a/projects/GKCore/GKCore/Interfaces/IBaseContext.cs +++ b/projects/GKCore/GKCore/Interfaces/IBaseContext.cs @@ -106,6 +106,11 @@ public interface IBaseContext : IDisposable void DoCommit(); void DoRollback(); + /// + /// Set the data modification flag and timestamp. + /// + void SetModified(); + void LockRecord(GDMRecord record); void UnlockRecord(GDMRecord record); bool IsAvailableRecord(GDMRecord record); diff --git a/projects/GKCore/GKCore/Lists/URefsListModel.cs b/projects/GKCore/GKCore/Lists/URefsListModel.cs index 9b1a95537..2e181e41a 100644 --- a/projects/GKCore/GKCore/Lists/URefsListModel.cs +++ b/projects/GKCore/GKCore/Lists/URefsListModel.cs @@ -108,7 +108,6 @@ public override async Task Modify(object sender, ModifyEventArgs eArgs) string confirmation = !string.IsNullOrEmpty(userRef.StringValue) ? userRef.StringValue : userRef.ReferenceType; if (await AppHost.StdDialogs.ShowQuestion(LangMan.LS(LSID.RemoveUserRefQuery, confirmation))) { result = fUndoman.DoOrdinaryOperation(OperationType.otIndividualURefRemove, iRec, userRef); - fBaseWin.Context.Modified = true; } break; } diff --git a/projects/GKCore/GKCore/Tools/TreeInspector.cs b/projects/GKCore/GKCore/Tools/TreeInspector.cs index 8e45c9a25..cf9ec900e 100644 --- a/projects/GKCore/GKCore/Tools/TreeInspector.cs +++ b/projects/GKCore/GKCore/Tools/TreeInspector.cs @@ -876,7 +876,7 @@ public static string DetectCycle(GDMTree tree, GDMIndividualRecord iRec) private static string CheckCycle(GDMTree tree, GDMIndividualRecord iRec) { var stack = new Stack(); - GDMIndividualRecord hasCycle = null; + GDMIndividualRecord hasCycle; var indiDCFlags = new GKVarCache(); if (!HasIndiFlag(indiDCFlags, iRec, DCFlag.dcfAncWalk)) { diff --git a/projects/GKTests/GKCore/BaseContextTests.cs b/projects/GKTests/GKCore/BaseContextTests.cs index 1e6b480e6..6d56e025a 100644 --- a/projects/GKTests/GKCore/BaseContextTests.cs +++ b/projects/GKTests/GKCore/BaseContextTests.cs @@ -35,7 +35,7 @@ namespace GKCore [TestFixture] public class BaseContextTests { - private BaseContext fContext; + private readonly BaseContext fContext; public BaseContextTests() { diff --git a/projects/GKTests/GKCore/GKUtilsTests.cs b/projects/GKTests/GKCore/GKUtilsTests.cs index 921eb7f50..3c1a33dc8 100644 --- a/projects/GKTests/GKCore/GKUtilsTests.cs +++ b/projects/GKTests/GKCore/GKUtilsTests.cs @@ -312,6 +312,11 @@ public void Test_GetDaysForBirth() Assert.AreEqual(1, GKUtils.GetDaysFor(gdmDate, DateTime.Parse("1990-12-19T00:00:00"), out years, out anniversary)); // 1 days left, and year *will be* 10 Assert.AreEqual(10, years); Assert.AreEqual(true, anniversary); + + gdmDate.ParseString("22 DEC 1990"); + Assert.AreEqual(1, GKUtils.GetDaysFor(gdmDate, DateTime.Parse("2024-12-21T21:00:00"), out years, out anniversary)); // 1 days left + Assert.AreEqual(34, years); + Assert.AreEqual(false, anniversary); } [Test]