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

Source Code for Module 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 as published by 
  8  # 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   
 29  from Products.ZenRelations.Exceptions import * 
 30  from Products.ZenRelations.utils import importClass 
 31   
 32  from PrimaryPathObjectManager import PrimaryPathManager 
 33   
34 -class RelationshipBase(PrimaryPathManager):
35 """ 36 Abstract base class for all relationship classes. 37 """ 38 39 _operation = -1 # if a Relationship's are only deleted 40
41 - def __call__(self):
42 """Return the contents of this relation.""" 43 raise NotImplementedError
44 45
46 - def getId(self):
47 return self.id
48 49
50 - def hasobject(self, obj):
51 """Does this relationship relate to obj.""" 52 raise NotImplementedError
53 54
55 - def _add(self, obj):
56 """Add object to local side of relationship.""" 57 raise NotImplementedError
58 59
60 - def _remove(self,obj=None):
61 """ 62 Remove object from local side of relationship. 63 If obj=None remove all object in the relationship 64 """ 65 raise NotImplementedError
66 67
68 - def _remoteRemove(self, obj=None):
69 """Remove obj form the remote side of this relationship.""" 70 raise NotImplementedError
71 72
73 - def addRelation(self, obj):
74 """Form a bi-directional relation between self and obj.""" 75 if obj is None: raise ZenRelationsError("Can not add None to relation") 76 if not isinstance(obj, self.remoteClass()): 77 raise ZenSchemaError("%s restricted to class %s. %s is class %s" % 78 (self.id, self.remoteClass().__name__, 79 obj.id, obj.__class__.__name__)) 80 try: 81 self._add(obj) 82 rname = self.remoteName() 83 # make sure remote rel is on this obj 84 getattr(aq_base(obj), rname) 85 remoteRel = getattr(obj, self.remoteName()) 86 remoteRel._add(self.__primary_parent__) 87 except RelationshipExistsError: 88 log.debug("obj %s already exits on %s", obj.getPrimaryId(), 89 self.getPrimaryId())
90 91
92 - def removeRelation(self, obj=None):
93 """remove an object from a relationship""" 94 self._remoteRemove(obj) 95 self._remove(obj)
96 97
98 - def remoteType(self):
99 """Return the type of the remote end of our relationship.""" 100 schema = self.__primary_parent__.lookupSchema(self.id) 101 return schema.remoteType
102 103
104 - def remoteTypeName(self):
105 """Return the type of the remote end of our relationship.""" 106 schema = self.__primary_parent__.lookupSchema(self.id) 107 return schema.remoteType.__name__
108 109
110 - def remoteClass(self):
111 """Return the class at the remote end of our relationship.""" 112 classdef = getattr(aq_base(self), "_v_remoteClass", None) 113 if not classdef: 114 schema = self.__primary_parent__.lookupSchema(self.id) 115 classdef = importClass(schema.remoteClass) 116 self._v_remoteClass = classdef 117 return classdef
118 119
120 - def remoteName(self):
121 """Return the name at the remote end of our relationship.""" 122 schema = self.__primary_parent__.lookupSchema(self.id) 123 return schema.remoteName
124 125
126 - def getPrimaryParent(self):
127 """Return our parent object by our primary path""" 128 return self.__primary_parent__.primaryAq()
129 130
132 """ 133 Return the local class of this relationship. For all relationshps 134 this is the class of our __primary_parent___. 135 """ 136 return self.__primary_parent__.__class__
137 138
139 - def cb_isCopyable(self):
140 """Don't let relationships move off their managers""" 141 return 0
142 143
144 - def cb_isMoveable(self):
145 """Don't let relationships move off their managers""" 146 return 0
147 148
149 - def checkRelation(self, repair=False):
150 """Check to make sure that relationship bidirectionality is ok. 151 """ 152 return
153 154 155 InitializeClass(RelationshipBase) 156