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

Source Code for Module Products.ZenRelations.RelationshipBase

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