# Path: tmapi/constants.py
# AUTOMERGE_FEATURE_STRING = TMAPI_FEATURE_STRING_BASE + 'automerge'
# 
# XSD_ANY_URI = XSD + 'anyURI'
# 
# XSD_FLOAT = XSD + 'float'
# 
# XSD_INT = XSD + 'int'
# 
# XSD_LONG = XSD + 'long'
# 
# XSD_STRING = XSD + 'string'
# 
# Path: tmapi/exceptions.py
# class IdentityConstraintException (ModelConstraintException):
# 
#     """This exception is used to report identity constraint
#     violations. Assigning an item identifier, a subject identifier, or
#     a subject locator to different objects causes an
#     `IdentityConstraintException` to be raised."""
# 
#     def __init__ (self, reporter, existing, locator, message):
#         """Creates a new `IdentityConstraintException` with the
#         specified message.
# 
#         :param reporter: the construct to which the identity should
#           have been assigned
#         :type reporter: `Construct`
#         :param existing: the construct that has the same identity
#         :type existing: `Construct`
#         :param locator: the locator representing the identity
#         :type locator: `Locator`
#         :param message: the detail message
#         :type message: string
# 
#         """
#         super(IdentityConstraintException, self).__init__(reporter, message)
#         self._existing = existing
#         self._locator = locator
# 
#     def get_existing (self):
#         """Returns the `Construct` which already has the identity
#         represented by the locator get_locator().
# 
#         :rtype: `Construct`
# 
#         """
#         return self._existing
# 
#     def get_locator (self):
#         """Returns the `Locator` representing the identity that caused
#         the exception.
# 
#         :rtype: `Locator`
# 
#         """
#         return self._locator
# 
# class ModelConstraintException (TMAPIRuntimeException):
# 
#     """This exception is used to report Topic Maps - Data Model
#     constraint violations."""
# 
#     def __init__ (self, reporter, message):
#         """Creates a new ModelConstraintException with the specified
#         message.
# 
#         :param reporter: the construct which has thrown this exception
#         :type reporter: `Construct`
#         :param message: the detail message
#         :type message: string
# 
#         """
#         self._reporter = reporter
#         self._message = message
#     
#     def get_reporter (self):
#         """Return the `Construct` which has thrown the exception.
# 
#         :rtype: `Construct`
# 
#         """
#         return self._reporter
# 
# class TopicInUseException (ModelConstraintException):
# 
#     """Thrown when an attempt is made to remove a `Topic` which is
#     being used as a type, as a reifier, or as a role player in an
#     association, or in a scope."""
#     
#     def __init__ (self, topic, message):
#         """Creates a new `TopicInUseException` with the specified message.
# 
#         :param topic: the topic which is not removable
#         :type topic: `Topic`
#         :param message: the detail message
#         :type message: string
# 
#         """
#         super(TopicInUseException, self).__init__(topic, message)


from django.db import models
from tmapi.constants import AUTOMERGE_FEATURE_STRING, XSD_ANY_URI, XSD_FLOAT, XSD_INT, XSD_LONG, XSD_STRING
from tmapi.exceptions import IdentityConstraintException, \
    ModelConstraintException, TopicInUseException
from construct import Construct
from construct_fields import ConstructFields
from item_identifier import ItemIdentifier
from locator import Locator
from name import Name
from subject_identifier import SubjectIdentifier
from subject_locator import SubjectLocator
from occurrence import Occurrence
from merge_utils import handle_existing_construct, \
    move_role_characteristics, move_variants
from signature import generate_association_signature, \
    generate_name_signature, generate_occurrence_signature

    class Meta:
        app_label = 'tmapi'

    def add_item_identifier (self, item_identifier):
        """Adds an item identifier to this topic.

        If adding the specified item identifier would make this topic
        represent the same subject as another topic and the feature
        "automerge" (http://tmapi.org/features/automerge/) is
        disabled, an `IdentityConstraintException` is thrown.

        :param item_identifier: the item identifier to be added
        :type item_identifier: `Locator`

        """
        if item_identifier is None:
            raise ModelConstraintException(
                self, 'The item identifier may not be None')
        address = item_identifier.to_external_form()
        try:
            ii = ItemIdentifier.objects.get(address=address,
                                            containing_topic_map=self.topic_map)
            construct = ii.get_construct()
            if construct == self:
                return
            if not isinstance(construct, Topic):
                raise IdentityConstraintException(
                    self, construct, item_identifier, 'This item identifier is already associated with another non-Topic construct')
            if self.topic_map.topic_map_system.get_feature(

# Path: tmapi/constants.py
# AUTOMERGE_FEATURE_STRING = TMAPI_FEATURE_STRING_BASE + 'automerge'
# 
# XSD_ANY_URI = XSD + 'anyURI'
# 
# XSD_FLOAT = XSD + 'float'
# 
# XSD_INT = XSD + 'int'
# 
# XSD_LONG = XSD + 'long'
# 
# XSD_STRING = XSD + 'string'
# 
# Path: tmapi/exceptions.py
# class IdentityConstraintException (ModelConstraintException):
# 
#     """This exception is used to report identity constraint
#     violations. Assigning an item identifier, a subject identifier, or
#     a subject locator to different objects causes an
#     `IdentityConstraintException` to be raised."""
# 
#     def __init__ (self, reporter, existing, locator, message):
#         """Creates a new `IdentityConstraintException` with the
#         specified message.
# 
#         :param reporter: the construct to which the identity should
#           have been assigned
#         :type reporter: `Construct`
#         :param existing: the construct that has the same identity
#         :type existing: `Construct`
#         :param locator: the locator representing the identity
#         :type locator: `Locator`
#         :param message: the detail message
#         :type message: string
# 
#         """
#         super(IdentityConstraintException, self).__init__(reporter, message)
#         self._existing = existing
#         self._locator = locator
# 
#     def get_existing (self):
#         """Returns the `Construct` which already has the identity
#         represented by the locator get_locator().
# 
#         :rtype: `Construct`
# 
#         """
#         return self._existing
# 
#     def get_locator (self):
#         """Returns the `Locator` representing the identity that caused
#         the exception.
# 
#         :rtype: `Locator`
# 
#         """
#         return self._locator
# 
# class ModelConstraintException (TMAPIRuntimeException):
# 
#     """This exception is used to report Topic Maps - Data Model
#     constraint violations."""
# 
#     def __init__ (self, reporter, message):
#         """Creates a new ModelConstraintException with the specified
#         message.
# 
#         :param reporter: the construct which has thrown this exception
#         :type reporter: `Construct`
#         :param message: the detail message
#         :type message: string
# 
#         """
#         self._reporter = reporter
#         self._message = message
#     
#     def get_reporter (self):
#         """Return the `Construct` which has thrown the exception.
# 
#         :rtype: `Construct`
# 
#         """
#         return self._reporter
# 
# class TopicInUseException (ModelConstraintException):
# 
#     """Thrown when an attempt is made to remove a `Topic` which is
#     being used as a type, as a reifier, or as a role player in an
#     association, or in a scope."""
#     
#     def __init__ (self, topic, message):
#         """Creates a new `TopicInUseException` with the specified message.
# 
#         :param topic: the topic which is not removable
#         :type topic: `Topic`
#         :param message: the detail message
#         :type message: string
# 
#         """
#         super(TopicInUseException, self).__init__(topic, message)


# Path: tmapi/models/topic.py
from django.db import models
from tmapi.constants import AUTOMERGE_FEATURE_STRING, XSD_ANY_URI, XSD_FLOAT, XSD_INT, XSD_LONG, XSD_STRING
from tmapi.exceptions import IdentityConstraintException, \
    ModelConstraintException, TopicInUseException
from construct import Construct
from construct_fields import ConstructFields
from item_identifier import ItemIdentifier
from locator import Locator
from name import Name
from subject_identifier import SubjectIdentifier
from subject_locator import SubjectLocator
from occurrence import Occurrence
from merge_utils import handle_existing_construct, \
    move_role_characteristics, move_variants
from signature import generate_association_signature, \
    generate_name_signature, generate_occurrence_signature


    class Meta:
        app_label = 'tmapi'

    def add_item_identifier (self, item_identifier):
        """Adds an item identifier to this topic.

        If adding the specified item identifier would make this topic
        represent the same subject as another topic and the feature
        "automerge" (http://tmapi.org/features/automerge/) is
        disabled, an `IdentityConstraintException` is thrown.

        :param item_identifier: the item identifier to be added
        :type item_identifier: `Locator`

        """
        if item_identifier is None:
            raise ModelConstraintException(
                self, 'The item identifier may not be None')
        address = item_identifier.to_external_form()
        try:
            ii = ItemIdentifier.objects.get(address=address,
                                            containing_topic_map=self.topic_map)
            construct = ii.get_construct()
            if construct == self:
                return
            if not isinstance(construct, Topic):
                raise IdentityConstraintException(
                    self, construct, item_identifier, 'This item identifier is already associated with another non-Topic construct')
            if self.topic_map.topic_map_system.get_feature(

                AUTOMERGE_FEATURE_STRING):