Package Products :: Package ZenRelations :: Module RelationshipBase |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenRelations.RelationshipBase

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  __doc__="""RelationshipBase
     12
     13  RelationshipBase is the base class for RelationshipManager
     14  and ToManyRelationship.
     15
     16  $Id: RelationshipBase.py,v 1.26 2003/10/03 16:16:01 edahl Exp $"""
     17
     18  __version__ = "$Revision: 1.26 $"[11:-2]
     19
     20  import logging
     21  log = logging.getLogger("zen.Relations")
     22
     23  from Globals import InitializeClass
     24  from Acquisition import aq_base
     25  from zope import interface
     26
     27  from Products.ZenRelations.Exceptions import *
     28  from Products.ZenRelations.utils import importClass
     29
     30  from PrimaryPathObjectManager import PrimaryPathManager
     31
     32  from zope.event import notify
     33  from OFS.event import ObjectWillBeAddedEvent
     34  from OFS.event import ObjectWillBeRemovedEvent
     35  from zope.container.contained import dispatchToSublocations
     36  from zope.container.contained import ObjectAddedEvent
     37  from zope.container.contained import ObjectRemovedEvent
     38  from zope.container.contained import ContainerModifiedEvent
     39
     40 -class IRelationship(interface.Interface):
     41      """
     42      Marker interface.
     43      """
     44
     45 -class RelationshipBase(PrimaryPathManager):
     46      """
     47      Abstract base class for all relationship classes.
     48      """
     49      interface.implements(IRelationship)
     50
     51      _operation = -1 # if a Relationship's are only deleted
     52
     53 -    def __call__(self):
     54          """Return the contents of this relation."""
     55          raise NotImplementedError
     56
     57
     58 -    def getId(self):
     59          return self.id
     60
     61
     62 -    def hasobject(self, obj):
     63          """Does this relationship relate to obj."""
     64          raise NotImplementedError
     65
     66
     67 -    def _add(self, obj):
     68          """Add object to local side of relationship."""
     69          raise NotImplementedError
     70
     71
     72 -    def _remove(self,obj=None, suppress_events=False):
     73          """
     74          Remove object from local side of relationship.
     75          If obj=None remove all object in the relationship
     76          """
     77          raise NotImplementedError
     78
     79
     80 -    def _remoteRemove(self, obj=None):
     81          """Remove obj form the remote side of this relationship."""
     82          raise NotImplementedError
     83
     84
     85 -    def addRelation(self, obj):
     86          """Form a bi-directional relation between self and obj."""
     87          if obj is None: raise ZenRelationsError("Can not add None to relation")
     88          if not isinstance(obj, self.remoteClass()):
     89              raise ZenSchemaError("%s restricted to class %s. %s is class %s" %
     90                  (self.id, self.remoteClass().__name__,
     91                   obj.id, obj.__class__.__name__))
     92          try:
     93              self._add(obj)
     94              rname = self.remoteName()
     95              # make sure remote rel is on this obj
     96              getattr(aq_base(obj), rname)
     97              remoteRel = getattr(obj, self.remoteName())
     98              remoteRel._add(self.__primary_parent__)
     99          except RelationshipExistsError:
    100              log.debug("obj %s already exists on %s", obj.getPrimaryId(),
    101                          self.getPrimaryId())
    102
    103
    104 -    def removeRelation(self, obj=None, suppress_events=False):
    105          """remove an object from a relationship"""
    106          self._remoteRemove(obj)
    107          self._remove(obj, suppress_events=suppress_events)
    108
    109
    110 -    def remoteType(self):
    111          """Return the type of the remote end of our relationship."""
    112          schema = self.__primary_parent__.lookupSchema(self.id)
    113          return schema.remoteType
    114
    115
    116 -    def remoteTypeName(self):
    117          """Return the type of the remote end of our relationship."""
    118          schema = self.__primary_parent__.lookupSchema(self.id)
    119          return schema.remoteType.__name__
    120
    121
    122 -    def remoteClass(self):
    123          """Return the class at the remote end of our relationship."""
    124          classdef = getattr(aq_base(self), "_v_remoteClass", None)
    125          if not classdef:
    126              schema = self.__primary_parent__.lookupSchema(self.id)
    127              classdef = importClass(schema.remoteClass)
    128              self._v_remoteClass = classdef
    129          return classdef
    130
    131
    132 -    def remoteName(self):
    133          """Return the name at the remote end of our relationship."""
    134          schema = self.__primary_parent__.lookupSchema(self.id)
    135          return schema.remoteName
    136
    137
    138 -    def getPrimaryParent(self):
    139          """Return our parent object by our primary path"""
    140          return self.__primary_parent__.primaryAq()
    141
    142
    143 -    def getRelationshipManagerClass(self):
    144          """
    145          Return the local class of this relationship. For all relationshps
    146          this is the class of our __primary_parent___.
    147          """
    148          return self.__primary_parent__.__class__
    149
    150
    151 -    def cb_isCopyable(self):
    152          """Don't let relationships move off their managers"""
    153          return 0
    154
    155
    156 -    def cb_isMoveable(self):
    157          """Don't let relationships move off their managers"""
    158          return 0
    159
    160
    161 -    def checkRelation(self, repair=False):
    162          """Check to make sure that relationship bidirectionality is ok.
    163          """
    164          return
    165
    166
    167  InitializeClass(RelationshipBase)
    168