From 2c89d2e0b2f9243fbbb8103169e4b65adc9bba28 Mon Sep 17 00:00:00 2001 From: Tim G L Lyons Date: Tue, 18 Oct 2011 16:38:18 +0000 Subject: [PATCH] Fix merge citations for repositories svn: r18343 --- src/Merge/mergecitation.py | 8 +++++- src/gen/lib/repo.py | 39 ++++++++++++++++++++++++++++++ src/plugins/tool/MergeCitations.py | 8 +++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/Merge/mergecitation.py b/src/Merge/mergecitation.py index 0f4d47981..77db47411 100644 --- a/src/Merge/mergecitation.py +++ b/src/Merge/mergecitation.py @@ -30,7 +30,7 @@ Provide merge capabilities for citations. # Gramps modules # #------------------------------------------------------------------------- -from gen.lib import (Person, Family, Event, Place, MediaObject) +from gen.lib import (Person, Family, Event, Place, MediaObject, Repository) from gen.db import DbTxn from gen.ggettext import sgettext as _ import const @@ -219,6 +219,12 @@ class MergeCitationQuery(object): assert(obj.has_citation_reference(old_handle)) obj.replace_citation_references(old_handle, new_handle) self.database.commit_media_object(obj, trans) + elif class_name == Repository.__name__: + repository = self.database.get_repository_from_handle(handle) + assert(repository.has_citation_reference(old_handle)) + repository.replace_citation_references(old_handle, + new_handle) + self.database.commit_repository(repository, trans) else: raise MergeError("Encounter an object of type %s that has " "a citation reference." % class_name) diff --git a/src/gen/lib/repo.py b/src/gen/lib/repo.py index f1c772fbf..ebadd23c3 100644 --- a/src/gen/lib/repo.py +++ b/src/gen/lib/repo.py @@ -139,6 +139,45 @@ class Repository(NoteBase, AddressBase, UrlBase, PrimaryObject): """ return self.get_referenced_note_handles() + def has_citation_reference(self, citation_handle) : + """ + Return True if any of the child objects has reference to this citation + handle. + + Note: for most objects, this is inherited from citationbase, which + checks both the object and the child objects. However, uniquely, + Repositories do not have citations for the primary object, but only for + child (secondary) objects. Hence, this function has to be implemented + directly in the primary object; it only checks the child objects. + + :param citation_handle: The citation handle to be checked. + :type citation_handle: str + :returns: Returns whether any of it's child objects has reference to + this citation handle. + :rtype: bool + """ + for item in self.get_citation_child_list(): + if item.has_citation_reference(citation_handle): + return True + + return False + + def replace_citation_references(self, old_handle, new_handle): + """ + Replace references to citation handles in the list in this object and + all child objects and merge equivalent entries. + + Note: the same comment about citationbase in has_citation_reference + applies here too. + + :param old_handle: The citation handle to be replaced. + :type old_handle: str + :param new_handle: The citation handle to replace the old one with. + :type new_handle: str + """ + for item in self.get_citation_child_list(): + item.replace_citation_references(old_handle, new_handle) + def has_source_reference(self, src_handle) : """ Return True if any of the child objects has reference to this source diff --git a/src/plugins/tool/MergeCitations.py b/src/plugins/tool/MergeCitations.py index 8f4d54f41..5d4e207a4 100644 --- a/src/plugins/tool/MergeCitations.py +++ b/src/plugins/tool/MergeCitations.py @@ -56,7 +56,8 @@ import ManagedWindow from gen.ggettext import sgettext as _ from glade import Glade from gen.db import DbTxn -from gen.lib import (Person, Family, Event, Place, MediaObject, Citation) +from gen.lib import (Person, Family, Event, Place, MediaObject, Citation, + Repository) from Errors import MergeError #------------------------------------------------------------------------- @@ -254,6 +255,11 @@ class MergeCitations(tool.BatchTool,ManagedWindow.ManagedWindow): assert(obj.has_citation_reference(old_handle)) obj.replace_citation_references(old_handle, new_handle) db.commit_media_object(obj, trans) + elif class_name == Repository.__name__: + repository = db.get_repository_from_handle(handle) + assert(repository.has_citation_reference(old_handle)) + repository.replace_citation_references(old_handle, new_handle) + db.commit_repository(repository, trans) else: raise MergeError("Encountered an object of type %s that has " "a citation reference." % class_name)