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