From 05a5912cc1f70426ea337fb5fc69a54ac64c6cc1 Mon Sep 17 00:00:00 2001 From: Alex Zaytsev Date: Tue, 7 Nov 2023 16:46:53 +1000 Subject: [PATCH] Merge spouses when merge family --- projects/GKCore/GDModel/GDMFamilyRecord.cs | 24 ++++++++++++++++------ projects/GKCore/GDModel/GDMTree.cs | 2 +- projects/GKCore/GKCore/Tools/TreeTools.cs | 22 ++++++++++---------- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/projects/GKCore/GDModel/GDMFamilyRecord.cs b/projects/GKCore/GDModel/GDMFamilyRecord.cs index 01290219f..6708c81a4 100644 --- a/projects/GKCore/GDModel/GDMFamilyRecord.cs +++ b/projects/GKCore/GDModel/GDMFamilyRecord.cs @@ -188,13 +188,12 @@ public override void MoveTo(GDMRecord targetRecord) base.MoveTo(targetRecord); - targetFamily.RemoveSpouse(fTree.GetPtrValue(targetFamily.Husband)); - targetFamily.Husband.XRef = fHusband.XRef; + MoveSpouse(fTree, targetFamily, fHusband, targetFamily.Husband); + MoveSpouse(fTree, targetFamily, fWife, targetFamily.Wife); - targetFamily.RemoveSpouse(fTree.GetPtrValue(targetFamily.Wife)); - targetFamily.Wife.XRef = fWife.XRef; - - targetFamily.Status = fStatus; + if (fStatus != GDMMarriageStatus.Unknown) { + targetFamily.Status = fStatus; + } while (fChildren.Count > 0) { var obj = fChildren.Extract(0); @@ -202,6 +201,19 @@ public override void MoveTo(GDMRecord targetRecord) } } + private void MoveSpouse(GDMTree tree, GDMFamilyRecord targetFamily, GDMPointer from, GDMPointer to) + { + var fromSpouse = tree.GetPtrValue(from); + var toSpouse = tree.GetPtrValue(to); + RemoveSpouse(fromSpouse); + if (toSpouse == null) { + targetFamily.AddSpouse(fromSpouse); + } else if (fromSpouse != null && !ReferenceEquals(fromSpouse, toSpouse)) { + fromSpouse.MoveTo(toSpouse); + tree.DeleteRecord(fromSpouse); + } + } + public override void ReplaceXRefs(GDMXRefReplacer map) { base.ReplaceXRefs(map); diff --git a/projects/GKCore/GDModel/GDMTree.cs b/projects/GKCore/GDModel/GDMTree.cs index 3b9b022ae..77e838914 100644 --- a/projects/GKCore/GDModel/GDMTree.cs +++ b/projects/GKCore/GDModel/GDMTree.cs @@ -289,7 +289,7 @@ public void SetXRef(string oldXRef, GDMRecord record, bool removeOldXRef) public List GetRecords() where T : GDMRecord { - List result = new List(); + List result = new List(fRecords.Count); for (int i = 0; i < fRecords.Count; i++) { T rec = fRecords[i] as T; diff --git a/projects/GKCore/GKCore/Tools/TreeTools.cs b/projects/GKCore/GKCore/Tools/TreeTools.cs index be3a2e473..5a9dfb900 100644 --- a/projects/GKCore/GKCore/Tools/TreeTools.cs +++ b/projects/GKCore/GKCore/Tools/TreeTools.cs @@ -380,22 +380,22 @@ public static void MergeRecord(IBaseWindow baseWin, GDMRecord targetRec, GDMReco using (var repMap = new GDMXRefReplacer()) { repMap.AddXRef(sourceRec, sourceRec.XRef, targetRec.XRef); - GDMTree tree = baseWin.Context.Tree; - int num = tree.RecordsCount; - for (int i = 0; i < num; i++) { - tree[i].ReplaceXRefs(repMap); - } - sourceRec.MoveTo(targetRec); - baseWin.Context.DeleteRecord(sourceRec); - if (targetRec.RecordType == GDMRecordType.rtIndividual && bookmark) { - ((GDMIndividualRecord)targetRec).Bookmark = true; + int num = baseWin.Context.Tree.RecordsCount; + for (int i = 0; i < num; i++) { + baseWin.Context.Tree[i].ReplaceXRefs(repMap); } - baseWin.NotifyRecord(targetRec, RecordAction.raEdit); - baseWin.RefreshLists(false); + baseWin.Context.Tree.DeleteRecord(sourceRec); + } + + if (targetRec.RecordType == GDMRecordType.rtIndividual && bookmark) { + ((GDMIndividualRecord)targetRec).Bookmark = true; } + + baseWin.NotifyRecord(targetRec, RecordAction.raEdit); + baseWin.RefreshLists(false); } #endregion